added null checks

This commit is contained in:
Ahmed Salah Tawfik Ibrahim 2024-04-12 11:17:28 +02:00
parent 66387bb586
commit 551c3ec923
1 changed files with 93 additions and 57 deletions

View File

@ -276,12 +276,15 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
if (timeInMillis > now.getTime()) if (timeInMillis > now.getTime())
throw new IllegalArgumentException("the timeInMillis must be before today"); throw new IllegalArgumentException("the timeInMillis must be before today");
List<Post> posts = getRecentPostsByUserAndDate(userid, timeInMillis); List<Post> posts = getRecentPostsByUserAndDate(userid, timeInMillis);
_log.info("Length of posts is " + posts.size()); if(posts!=null){
List<Feed> feeds = new ArrayList<>(); _log.info("Length of posts is " + posts.size());
for(Post post: posts){ List<Feed> feeds = new ArrayList<>();
_log.info(post.toString()); for(Post post: posts){
feeds.add(post2feed(post)); _log.info(post.toString());
feeds.add(post2feed(post));
}
} }
return feeds; return feeds;
} }
/** /**
@ -318,8 +321,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
public List<Feed> getAllFeedsByUser(String userid) throws PrivacyLevelTypeNotFoundException, FeedTypeNotFoundException, ColumnNameNotFoundException, FeedIDNotFoundException { public List<Feed> getAllFeedsByUser(String userid) throws PrivacyLevelTypeNotFoundException, FeedTypeNotFoundException, ColumnNameNotFoundException, FeedIDNotFoundException {
List<Post> posts = getAllPostsByUser(userid); List<Post> posts = getAllPostsByUser(userid);
List<Feed> feeds = new ArrayList<>(); List<Feed> feeds = new ArrayList<>();
for(Post post: posts){ if(posts!=null){
feeds.add(post2feed(post)); for(Post post: posts){
feeds.add(post2feed(post));
}
} }
return feeds; return feeds;
} }
@ -337,9 +342,12 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
public List<Feed> getAllFeedsByApp(String appid) throws PrivacyLevelTypeNotFoundException, FeedTypeNotFoundException, ColumnNameNotFoundException, FeedIDNotFoundException { public List<Feed> getAllFeedsByApp(String appid) throws PrivacyLevelTypeNotFoundException, FeedTypeNotFoundException, ColumnNameNotFoundException, FeedIDNotFoundException {
List<Post> posts = getAllPostsByApp(appid); List<Post> posts = getAllPostsByApp(appid);
List<Feed> feeds = new ArrayList<>(); List<Feed> feeds = new ArrayList<>();
for(Post post: posts){ if(posts!=null){
feeds.add(post2feed(post)); for(Post post: posts){
feeds.add(post2feed(post));
}
} }
return feeds; return feeds;
} }
/** /**
@ -359,8 +367,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
long timeInMillis) throws Exception { long timeInMillis) throws Exception {
List<Post> posts = getRecentCommentedPostsByUserAndDate(userid, timeInMillis); List<Post> posts = getRecentCommentedPostsByUserAndDate(userid, timeInMillis);
List<Feed> feeds = new ArrayList<>(); List<Feed> feeds = new ArrayList<>();
for(Post post: posts){ if(posts!=null){
feeds.add(post2feed(post)); for(Post post: posts){
feeds.add(post2feed(post));
}
} }
return feeds; return feeds;
} }
@ -381,8 +391,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
public List<Feed> getAllPortalPrivacyLevelFeeds() throws FeedTypeNotFoundException, ColumnNameNotFoundException, PrivacyLevelTypeNotFoundException { public List<Feed> getAllPortalPrivacyLevelFeeds() throws FeedTypeNotFoundException, ColumnNameNotFoundException, PrivacyLevelTypeNotFoundException {
List<Post> posts = getAllPortalPrivacyLevelPosts(); List<Post> posts = getAllPortalPrivacyLevelPosts();
List<Feed> feeds = new ArrayList<>(); List<Feed> feeds = new ArrayList<>();
for(Post post: posts){ if (posts!=null){
feeds.add(post2feed(post)); for(Post post: posts){
feeds.add(post2feed(post));
}
} }
return feeds; return feeds;
} }
@ -398,8 +410,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
public List<Feed> getRecentFeedsByUser(String userid, int quantity) throws PrivacyLevelTypeNotFoundException, FeedTypeNotFoundException, ColumnNameNotFoundException, FeedIDNotFoundException { public List<Feed> getRecentFeedsByUser(String userid, int quantity) throws PrivacyLevelTypeNotFoundException, FeedTypeNotFoundException, ColumnNameNotFoundException, FeedIDNotFoundException {
List<Post> posts = getRecentPostsByUser(userid, quantity); List<Post> posts = getRecentPostsByUser(userid, quantity);
List<Feed> feeds = new ArrayList<>(); List<Feed> feeds = new ArrayList<>();
for(Post post: posts){ if(posts!=null){
feeds.add(post2feed(post)); for(Post post: posts){
feeds.add(post2feed(post));
}
} }
return feeds; return feeds;
} }
@ -415,8 +429,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
public List<Feed> getAllFeedsByVRE(String vreid) throws PrivacyLevelTypeNotFoundException, FeedTypeNotFoundException, ColumnNameNotFoundException, FeedIDNotFoundException { public List<Feed> getAllFeedsByVRE(String vreid) throws PrivacyLevelTypeNotFoundException, FeedTypeNotFoundException, ColumnNameNotFoundException, FeedIDNotFoundException {
List<Post> posts = getAllPostsByVRE(vreid); List<Post> posts = getAllPostsByVRE(vreid);
List<Feed> feeds = new ArrayList<>(); List<Feed> feeds = new ArrayList<>();
for(Post post: posts){ if(posts!=null){
feeds.add(post2feed(post)); for(Post post: posts){
feeds.add(post2feed(post));
}
} }
return feeds; return feeds;
} }
@ -435,10 +451,12 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
public List<Feed> getRecentFeedsByVRE(String vreid, int quantity) throws PrivacyLevelTypeNotFoundException, FeedTypeNotFoundException, ColumnNameNotFoundException, FeedIDNotFoundException { public List<Feed> getRecentFeedsByVRE(String vreid, int quantity) throws PrivacyLevelTypeNotFoundException, FeedTypeNotFoundException, ColumnNameNotFoundException, FeedIDNotFoundException {
_log.info("\n\n in getRecentFeedsByVRE"); _log.info("\n\n in getRecentFeedsByVRE");
List<Post> posts = getRecentPostsByVRE(vreid, quantity); List<Post> posts = getRecentPostsByVRE(vreid, quantity);
_log.info("length of vre posts = " + posts.size());
List<Feed> feeds = new ArrayList<>(); List<Feed> feeds = new ArrayList<>();
for(Post post: posts){ if(posts!=null){
feeds.add(post2feed(post)); _log.info("length of vre posts = " + posts.size());
for(Post post: posts){
feeds.add(post2feed(post));
}
} }
_log.info("length of vre feeds = " + feeds.size()); _log.info("length of vre feeds = " + feeds.size());
return feeds; 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 { public RangeFeeds getRecentFeedsByVREAndRange(String vreid, int from, int quantity) throws IllegalArgumentException, PrivacyLevelTypeNotFoundException, FeedTypeNotFoundException, ColumnNameNotFoundException, FeedIDNotFoundException {
RangePosts rangePosts = getRecentPostsByVREAndRange(vreid, from, quantity); RangePosts rangePosts = getRecentPostsByVREAndRange(vreid, from, quantity);
List<Post> posts = rangePosts.getPosts(); List<Post> posts = rangePosts.getPosts();
ArrayList<Feed> feeds = new ArrayList<>(); if(posts!=null){
for(Post post: posts){ ArrayList<Feed> feeds = new ArrayList<>();
feeds.add(post2feed(post)); for(Post post: posts){
feeds.add(post2feed(post));
}
RangeFeeds rangeFeeds = new RangeFeeds(rangePosts.getLastReturnedPostTimelineIndex(), feeds);
return rangeFeeds;
} }
RangeFeeds rangeFeeds = new RangeFeeds(rangePosts.getLastReturnedPostTimelineIndex(), feeds); else{
return rangeFeeds; return new RangeFeeds();
}
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
@ -559,10 +583,12 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
*/ */
@Override @Override
public boolean setUserNotificationPreferences(String userid, Map<NotificationType, NotificationChannelType[]> enabledChannels) { public boolean setUserNotificationPreferences(String userid, Map<NotificationType, NotificationChannelType[]> enabledChannels) {
for(NotificationType notificationType: enabledChannels.keySet()){ if(enabledChannels!=null){
_log.info("Type: " + notificationType.toString()); for(NotificationType notificationType: enabledChannels.keySet()){
for(NotificationChannelType channelType: enabledChannels.get(notificationType)){ _log.info("Type: " + notificationType.toString());
_log.info(channelType.toString()); for(NotificationChannelType channelType: enabledChannels.get(notificationType)){
_log.info(channelType.toString());
}
} }
} }
return libClient.setUserNotificationPreferencesLib(userid, enabledChannels); return libClient.setUserNotificationPreferencesLib(userid, enabledChannels);
@ -674,22 +700,26 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
ArrayList<Feed> toReturn = new ArrayList<>(); ArrayList<Feed> toReturn = new ArrayList<>();
List<String> likedPostIDs = getAllLikedPostIdsByUser(userid); List<String> likedPostIDs = getAllLikedPostIdsByUser(userid);
//check if quantity is greater than user feeds if(likedPostIDs!=null){
limit = (limit > likedPostIDs.size()) ? likedPostIDs.size() : limit; //check if quantity is greater than user feeds
limit = (limit > likedPostIDs.size()) ? likedPostIDs.size() : limit;
//need them in reverse order //need them in reverse order
for (int i = likedPostIDs.size()-1; i >= (likedPostIDs.size()-limit); i--) { for (int i = likedPostIDs.size()-1; i >= (likedPostIDs.size()-limit); i--) {
Feed toAdd = readFeed(likedPostIDs.get(i)); Feed toAdd = readFeed(likedPostIDs.get(i));
if (toAdd.getType() == FeedType.TWEET || toAdd.getType() == FeedType.SHARE || toAdd.getType() == FeedType.PUBLISH) { if (toAdd.getType() == FeedType.TWEET || toAdd.getType() == FeedType.SHARE || toAdd.getType() == FeedType.PUBLISH) {
toReturn.add(toAdd); toReturn.add(toAdd);
_log.trace("Read recent post: " + likedPostIDs.get(i)); _log.trace("Read recent post: " + likedPostIDs.get(i));
} else { } else {
_log.trace("Read and skipped post: " + likedPostIDs.get(i) + " (Removed post)"); _log.trace("Read and skipped post: " + likedPostIDs.get(i) + " (Removed post)");
limit += 1; //increase the quantity in case of removed feed limit += 1; //increase the quantity in case of removed feed
//check if quantity is greater than user feeds //check if quantity is greater than user feeds
limit = (limit > likedPostIDs.size()) ? likedPostIDs.size() : limit; limit = (limit > likedPostIDs.size()) ? likedPostIDs.size() : limit;
}
} }
} }
return toReturn; return toReturn;
} }
/** /**
@ -700,22 +730,25 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
ArrayList<Post> toReturn = new ArrayList<Post>(); ArrayList<Post> toReturn = new ArrayList<Post>();
List<String> likedPostIDs = getAllLikedPostIdsByUser(userid); List<String> likedPostIDs = getAllLikedPostIdsByUser(userid);
//check if quantity is greater than user feeds if(likedPostIDs!=null){
limit = (limit > likedPostIDs.size()) ? likedPostIDs.size() : limit; //check if quantity is greater than user feeds
limit = (limit > likedPostIDs.size()) ? likedPostIDs.size() : limit;
//need them in reverse order //need them in reverse order
for (int i = likedPostIDs.size()-1; i >= (likedPostIDs.size()-limit); i--) { for (int i = likedPostIDs.size()-1; i >= (likedPostIDs.size()-limit); i--) {
Post toAdd = readPost(likedPostIDs.get(i)); Post toAdd = readPost(likedPostIDs.get(i));
if (toAdd.getType() == PostType.TWEET || toAdd.getType() == PostType.SHARE || toAdd.getType() == PostType.PUBLISH) { if (toAdd.getType() == PostType.TWEET || toAdd.getType() == PostType.SHARE || toAdd.getType() == PostType.PUBLISH) {
toReturn.add(toAdd); toReturn.add(toAdd);
_log.trace("Read recent post: " + likedPostIDs.get(i)); _log.trace("Read recent post: " + likedPostIDs.get(i));
} else { } else {
_log.trace("Read and skipped post: " + likedPostIDs.get(i) + " (Removed post)"); _log.trace("Read and skipped post: " + likedPostIDs.get(i) + " (Removed post)");
limit += 1; //increase the quantity in case of removed feed limit += 1; //increase the quantity in case of removed feed
//check if quantity is greater than user feeds //check if quantity is greater than user feeds
limit = (limit > likedPostIDs.size()) ? likedPostIDs.size() : limit; limit = (limit > likedPostIDs.size()) ? likedPostIDs.size() : limit;
}
} }
} }
return toReturn; return toReturn;
} }
@ -890,9 +923,12 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
public List<Feed> getVREFeedsByHashtag(String vreid, String hashtag) throws PrivacyLevelTypeNotFoundException, FeedTypeNotFoundException, FeedIDNotFoundException, ColumnNameNotFoundException { public List<Feed> getVREFeedsByHashtag(String vreid, String hashtag) throws PrivacyLevelTypeNotFoundException, FeedTypeNotFoundException, FeedIDNotFoundException, ColumnNameNotFoundException {
List<Post>posts = getVREPostsByHashtag(vreid,hashtag); List<Post>posts = getVREPostsByHashtag(vreid,hashtag);
List<Feed>feeds = new ArrayList<>(); List<Feed>feeds = new ArrayList<>();
for(Post post: posts){ if(posts!=null){
feeds.add(post2feed(post)); for(Post post: posts){
feeds.add(post2feed(post));
}
} }
return feeds; return feeds;
} }
/** /**