From 551c3ec92376aa863ec8be18d194393b38eeb6ee Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim Date: Fri, 12 Apr 2024 11:17:28 +0200 Subject: [PATCH] added null checks --- .../server/DBCassandraAstyanaxImpl.java | 150 +++++++++++------- 1 file changed, 93 insertions(+), 57 deletions(-) diff --git a/src/main/java/org/gcube/portal/databook/server/DBCassandraAstyanaxImpl.java b/src/main/java/org/gcube/portal/databook/server/DBCassandraAstyanaxImpl.java index bf9b8ad..bf627d2 100644 --- a/src/main/java/org/gcube/portal/databook/server/DBCassandraAstyanaxImpl.java +++ b/src/main/java/org/gcube/portal/databook/server/DBCassandraAstyanaxImpl.java @@ -276,12 +276,15 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { if (timeInMillis > now.getTime()) throw new IllegalArgumentException("the timeInMillis must be before today"); List posts = getRecentPostsByUserAndDate(userid, timeInMillis); - _log.info("Length of posts is " + posts.size()); - List feeds = new ArrayList<>(); - for(Post post: posts){ - _log.info(post.toString()); - feeds.add(post2feed(post)); + if(posts!=null){ + _log.info("Length of posts is " + posts.size()); + List feeds = new ArrayList<>(); + for(Post post: posts){ + _log.info(post.toString()); + feeds.add(post2feed(post)); + } } + return feeds; } /** @@ -318,8 +321,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { public List getAllFeedsByUser(String userid) throws PrivacyLevelTypeNotFoundException, FeedTypeNotFoundException, ColumnNameNotFoundException, FeedIDNotFoundException { List posts = getAllPostsByUser(userid); List feeds = new ArrayList<>(); - for(Post post: posts){ - feeds.add(post2feed(post)); + if(posts!=null){ + for(Post post: posts){ + feeds.add(post2feed(post)); + } } return feeds; } @@ -337,9 +342,12 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { public List getAllFeedsByApp(String appid) throws PrivacyLevelTypeNotFoundException, FeedTypeNotFoundException, ColumnNameNotFoundException, FeedIDNotFoundException { List posts = getAllPostsByApp(appid); List feeds = new ArrayList<>(); - for(Post post: posts){ - feeds.add(post2feed(post)); + if(posts!=null){ + for(Post post: posts){ + feeds.add(post2feed(post)); + } } + return feeds; } /** @@ -359,8 +367,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { long timeInMillis) throws Exception { List posts = getRecentCommentedPostsByUserAndDate(userid, timeInMillis); List feeds = new ArrayList<>(); - for(Post post: posts){ - feeds.add(post2feed(post)); + if(posts!=null){ + for(Post post: posts){ + feeds.add(post2feed(post)); + } } return feeds; } @@ -381,8 +391,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { public List getAllPortalPrivacyLevelFeeds() throws FeedTypeNotFoundException, ColumnNameNotFoundException, PrivacyLevelTypeNotFoundException { List posts = getAllPortalPrivacyLevelPosts(); List feeds = new ArrayList<>(); - for(Post post: posts){ - feeds.add(post2feed(post)); + if (posts!=null){ + for(Post post: posts){ + feeds.add(post2feed(post)); + } } return feeds; } @@ -398,8 +410,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { public List getRecentFeedsByUser(String userid, int quantity) throws PrivacyLevelTypeNotFoundException, FeedTypeNotFoundException, ColumnNameNotFoundException, FeedIDNotFoundException { List posts = getRecentPostsByUser(userid, quantity); List feeds = new ArrayList<>(); - for(Post post: posts){ - feeds.add(post2feed(post)); + if(posts!=null){ + for(Post post: posts){ + feeds.add(post2feed(post)); + } } return feeds; } @@ -415,8 +429,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { public List getAllFeedsByVRE(String vreid) throws PrivacyLevelTypeNotFoundException, FeedTypeNotFoundException, ColumnNameNotFoundException, FeedIDNotFoundException { List posts = getAllPostsByVRE(vreid); List feeds = new ArrayList<>(); - for(Post post: posts){ - feeds.add(post2feed(post)); + if(posts!=null){ + for(Post post: posts){ + feeds.add(post2feed(post)); + } } return feeds; } @@ -435,10 +451,12 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { public List getRecentFeedsByVRE(String vreid, int quantity) throws PrivacyLevelTypeNotFoundException, FeedTypeNotFoundException, ColumnNameNotFoundException, FeedIDNotFoundException { _log.info("\n\n in getRecentFeedsByVRE"); List posts = getRecentPostsByVRE(vreid, quantity); - _log.info("length of vre posts = " + posts.size()); List feeds = new ArrayList<>(); - for(Post post: posts){ - feeds.add(post2feed(post)); + if(posts!=null){ + _log.info("length of vre posts = " + posts.size()); + for(Post post: posts){ + feeds.add(post2feed(post)); + } } _log.info("length of vre feeds = " + feeds.size()); return feeds; @@ -455,12 +473,18 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { public RangeFeeds getRecentFeedsByVREAndRange(String vreid, int from, int quantity) throws IllegalArgumentException, PrivacyLevelTypeNotFoundException, FeedTypeNotFoundException, ColumnNameNotFoundException, FeedIDNotFoundException { RangePosts rangePosts = getRecentPostsByVREAndRange(vreid, from, quantity); List posts = rangePosts.getPosts(); - ArrayList feeds = new ArrayList<>(); - for(Post post: posts){ - feeds.add(post2feed(post)); + if(posts!=null){ + ArrayList feeds = new ArrayList<>(); + for(Post post: posts){ + feeds.add(post2feed(post)); + } + RangeFeeds rangeFeeds = new RangeFeeds(rangePosts.getLastReturnedPostTimelineIndex(), feeds); + return rangeFeeds; } - RangeFeeds rangeFeeds = new RangeFeeds(rangePosts.getLastReturnedPostTimelineIndex(), feeds); - return rangeFeeds; + else{ + return new RangeFeeds(); + } + } /** * {@inheritDoc} @@ -559,10 +583,12 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { */ @Override public boolean setUserNotificationPreferences(String userid, Map enabledChannels) { - for(NotificationType notificationType: enabledChannels.keySet()){ - _log.info("Type: " + notificationType.toString()); - for(NotificationChannelType channelType: enabledChannels.get(notificationType)){ - _log.info(channelType.toString()); + if(enabledChannels!=null){ + for(NotificationType notificationType: enabledChannels.keySet()){ + _log.info("Type: " + notificationType.toString()); + for(NotificationChannelType channelType: enabledChannels.get(notificationType)){ + _log.info(channelType.toString()); + } } } return libClient.setUserNotificationPreferencesLib(userid, enabledChannels); @@ -674,22 +700,26 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { ArrayList toReturn = new ArrayList<>(); List likedPostIDs = getAllLikedPostIdsByUser(userid); - //check if quantity is greater than user feeds - limit = (limit > likedPostIDs.size()) ? likedPostIDs.size() : limit; + if(likedPostIDs!=null){ + //check if quantity is greater than user feeds + limit = (limit > likedPostIDs.size()) ? likedPostIDs.size() : limit; - //need them in reverse order - for (int i = likedPostIDs.size()-1; i >= (likedPostIDs.size()-limit); i--) { - Feed toAdd = readFeed(likedPostIDs.get(i)); - if (toAdd.getType() == FeedType.TWEET || toAdd.getType() == FeedType.SHARE || toAdd.getType() == FeedType.PUBLISH) { - toReturn.add(toAdd); - _log.trace("Read recent post: " + likedPostIDs.get(i)); - } else { - _log.trace("Read and skipped post: " + likedPostIDs.get(i) + " (Removed post)"); - limit += 1; //increase the quantity in case of removed feed - //check if quantity is greater than user feeds - limit = (limit > likedPostIDs.size()) ? likedPostIDs.size() : limit; + //need them in reverse order + for (int i = likedPostIDs.size()-1; i >= (likedPostIDs.size()-limit); i--) { + Feed toAdd = readFeed(likedPostIDs.get(i)); + if (toAdd.getType() == FeedType.TWEET || toAdd.getType() == FeedType.SHARE || toAdd.getType() == FeedType.PUBLISH) { + toReturn.add(toAdd); + _log.trace("Read recent post: " + likedPostIDs.get(i)); + } else { + _log.trace("Read and skipped post: " + likedPostIDs.get(i) + " (Removed post)"); + limit += 1; //increase the quantity in case of removed feed + //check if quantity is greater than user feeds + limit = (limit > likedPostIDs.size()) ? likedPostIDs.size() : limit; + } } + } + return toReturn; } /** @@ -700,22 +730,25 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { ArrayList toReturn = new ArrayList(); List likedPostIDs = getAllLikedPostIdsByUser(userid); - //check if quantity is greater than user feeds - limit = (limit > likedPostIDs.size()) ? likedPostIDs.size() : limit; + if(likedPostIDs!=null){ + //check if quantity is greater than user feeds + limit = (limit > likedPostIDs.size()) ? likedPostIDs.size() : limit; - //need them in reverse order - for (int i = likedPostIDs.size()-1; i >= (likedPostIDs.size()-limit); i--) { - Post toAdd = readPost(likedPostIDs.get(i)); - if (toAdd.getType() == PostType.TWEET || toAdd.getType() == PostType.SHARE || toAdd.getType() == PostType.PUBLISH) { - toReturn.add(toAdd); - _log.trace("Read recent post: " + likedPostIDs.get(i)); - } else { - _log.trace("Read and skipped post: " + likedPostIDs.get(i) + " (Removed post)"); - limit += 1; //increase the quantity in case of removed feed - //check if quantity is greater than user feeds - limit = (limit > likedPostIDs.size()) ? likedPostIDs.size() : limit; + //need them in reverse order + for (int i = likedPostIDs.size()-1; i >= (likedPostIDs.size()-limit); i--) { + Post toAdd = readPost(likedPostIDs.get(i)); + if (toAdd.getType() == PostType.TWEET || toAdd.getType() == PostType.SHARE || toAdd.getType() == PostType.PUBLISH) { + toReturn.add(toAdd); + _log.trace("Read recent post: " + likedPostIDs.get(i)); + } else { + _log.trace("Read and skipped post: " + likedPostIDs.get(i) + " (Removed post)"); + limit += 1; //increase the quantity in case of removed feed + //check if quantity is greater than user feeds + limit = (limit > likedPostIDs.size()) ? likedPostIDs.size() : limit; + } } } + return toReturn; } @@ -890,9 +923,12 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { public List getVREFeedsByHashtag(String vreid, String hashtag) throws PrivacyLevelTypeNotFoundException, FeedTypeNotFoundException, FeedIDNotFoundException, ColumnNameNotFoundException { Listposts = getVREPostsByHashtag(vreid,hashtag); Listfeeds = new ArrayList<>(); - for(Post post: posts){ - feeds.add(post2feed(post)); + if(posts!=null){ + for(Post post: posts){ + feeds.add(post2feed(post)); + } } + return feeds; } /**