added the ability to check if a like is alredy present

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portal/social-networking-library@117281 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Massimiliano Assante 2015-07-16 14:27:33 +00:00
parent d921f5b282
commit d8948a8580
1 changed files with 53 additions and 19 deletions

View File

@ -520,6 +520,34 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
} }
return toReturn; return toReturn;
} }
/**
* helper method that return whether the user
* @param userid user identifier
* @param feedid the feed identifier
* @return true if the feed id liked already
*/
private boolean isFeedLiked(String userid, String feedid) {
OperationResult<Rows<String, String>> result = null;
try {
result = conn.getKeyspace().prepareQuery(cf_UserLikedFeeds)
.getKeySlice(userid)
.execute();
} catch (ConnectionException e) {
e.printStackTrace();
}
// Iterate rows and their columns looking for the feeid id
for (Row<String, String> row : result.getResult()) {
for (Column<String> column : row.getColumns()) {
if (column.getStringValue().compareTo(feedid)==0)
return true;
}
}
return false;
}
/** /**
* helper method that retrieve all the feed Ids belonging to an application * helper method that retrieve all the feed Ids belonging to an application
* @param appid application identifier * @param appid application identifier
@ -1309,25 +1337,31 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
e.printStackTrace(); e.printStackTrace();
return false; return false;
} }
// Inserting data if (isFeedLiked(like.getUserid(), feedId)) {
MutationBatch m = conn.getKeyspace().prepareMutationBatch(); _log.info("User " + like.getUserid() + " already liked Feed " + feedId);
//an entry in the feed CF return true;
m.withRow(cf_Likes, like.getKey().toString()) }
.putColumn("Timestamp", like.getTime().getTime()+"", null) else {
.putColumn("Userid", like.getUserid(), null) // Inserting data
.putColumn("Feedid",like.getFeedid(), null) MutationBatch m = conn.getKeyspace().prepareMutationBatch();
.putColumn("FullName",like.getFullName(), null) //an entry in the feed CF
.putColumn("ThumbnailURL", like.getThumbnailURL(), null); m.withRow(cf_Likes, like.getKey().toString())
//and an entry in the UserLikesCF .putColumn("Timestamp", like.getTime().getTime()+"", null)
m.withRow(cf_UserLikedFeeds, like.getUserid()).putColumn(like.getKey(), like.getFeedid(), null); .putColumn("Userid", like.getUserid(), null)
.putColumn("Feedid",like.getFeedid(), null)
.putColumn("FullName",like.getFullName(), null)
.putColumn("ThumbnailURL", like.getThumbnailURL(), null);
//and an entry in the UserLikesCF
m.withRow(cf_UserLikedFeeds, like.getUserid()).putColumn(like.getKey(), like.getFeedid(), null);
try { try {
m.execute(); m.execute();
} catch (ConnectionException e) { } catch (ConnectionException e) {
e.printStackTrace(); e.printStackTrace();
return false; return false;
} }
return updateFeedLikesCount(toLike, true); return updateFeedLikesCount(toLike, true);
}
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
@ -1721,7 +1755,7 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore {
if (column.getStringValue().compareTo(status[i].toString())==0) if (column.getStringValue().compareTo(status[i].toString())==0)
invitesIds.add(column.getName()); invitesIds.add(column.getName());
} }
} }
} }
else { else {