From 61723dcc11f12562c8253392264ec880d62d1707 Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim Date: Mon, 27 Nov 2023 15:45:26 +0100 Subject: [PATCH] DB Write bug fixed --- .../server/DBCassandraAstyanaxImpl.java | 300 ++++++++++-------- .../databook/shared/PostWithAttachment.java | 31 ++ 2 files changed, 204 insertions(+), 127 deletions(-) create mode 100644 src/main/java/org/gcube/portal/databook/shared/PostWithAttachment.java 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 e95a7a7..08d5bf0 100644 --- a/src/main/java/org/gcube/portal/databook/server/DBCassandraAstyanaxImpl.java +++ b/src/main/java/org/gcube/portal/databook/server/DBCassandraAstyanaxImpl.java @@ -6,6 +6,7 @@ import java.util.stream.Collectors; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; +import javax.print.attribute.standard.MediaSize; import com.datastax.oss.driver.api.core.CqlSession; import com.datastax.oss.driver.api.core.cql.*; @@ -403,6 +404,7 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { .value(PREFERENCE, QueryBuilder.bindMarker()) .build()); } + private static PreparedStatement createNewCommentEntry(CqlSession session){ return session.prepare( QueryBuilder.insertInto(COMMENTS) @@ -414,6 +416,7 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { .value(POST_ID, QueryBuilder.bindMarker()) .value(TIMESTAMP, QueryBuilder.bindMarker()) .value(IS_EDIT, QueryBuilder.bindMarker()) + .value(LAST_EDIT_TIME, QueryBuilder.bindMarker()) .build()); } private static PreparedStatement createNewLikeEntry(CqlSession session){ @@ -566,6 +569,26 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { return feed; } private List insertIntoPosts(CqlSession session, Post post){ + PreparedStatement prepStmt1 = session.prepare(QueryBuilder.insertInto(POSTS).value(POST_ID, QueryBuilder.bindMarker()).value(LINK_HOST, QueryBuilder.bindMarker()).build()); + PreparedStatement prepStmt2 = session.prepare(QueryBuilder.insertInto(POSTS).value(POST_ID, QueryBuilder.bindMarker()).value(DESCRIPTION, QueryBuilder.bindMarker()).build()); + PreparedStatement prepStmt3 = session.prepare(QueryBuilder.insertInto(POSTS).value(POST_ID, QueryBuilder.bindMarker()).value(EMAIL, QueryBuilder.bindMarker()).build()); + PreparedStatement prepStmt4 = session.prepare(QueryBuilder.insertInto(POSTS).value(POST_ID, QueryBuilder.bindMarker()).value(LIKES_NO, QueryBuilder.bindMarker()).build()); + PreparedStatement prepStmt5 = session.prepare(QueryBuilder.insertInto(POSTS).value(POST_ID, QueryBuilder.bindMarker()).value(THUMBNAIL_URL, QueryBuilder.bindMarker()).build()); + PreparedStatement prepStmt6 = session.prepare(QueryBuilder.insertInto(POSTS).value(POST_ID, QueryBuilder.bindMarker()).value(LINK_DESCRIPTION, QueryBuilder.bindMarker()).build()); + PreparedStatement prepStmt7 = session.prepare(QueryBuilder.insertInto(POSTS).value(POST_ID, QueryBuilder.bindMarker()).value(TIMESTAMP, QueryBuilder.bindMarker()).build()); + PreparedStatement prepStmt8 = session.prepare(QueryBuilder.insertInto(POSTS).value(POST_ID, QueryBuilder.bindMarker()).value(URI, QueryBuilder.bindMarker()).build()); + PreparedStatement prepStmt9 = session.prepare(QueryBuilder.insertInto(POSTS).value(POST_ID, QueryBuilder.bindMarker()).value(IS_APPLICATION_POST, QueryBuilder.bindMarker()).build()); + PreparedStatement prepStmt10 = session.prepare(QueryBuilder.insertInto(POSTS).value(POST_ID, QueryBuilder.bindMarker()).value(ENTITY_ID, QueryBuilder.bindMarker()).build()); + PreparedStatement prepStmt11 = session.prepare(QueryBuilder.insertInto(POSTS).value(POST_ID, QueryBuilder.bindMarker()).value(PRIVACY, QueryBuilder.bindMarker()).build()); + PreparedStatement prepStmt12 = session.prepare(QueryBuilder.insertInto(POSTS).value(POST_ID, QueryBuilder.bindMarker()).value(TYPE, QueryBuilder.bindMarker()).build()); + PreparedStatement prepStmt13 = session.prepare(QueryBuilder.insertInto(POSTS).value(POST_ID, QueryBuilder.bindMarker()).value(URI_THUMBNAIL, QueryBuilder.bindMarker()).build()); + PreparedStatement prepStmt14 = session.prepare(QueryBuilder.insertInto(POSTS).value(POST_ID, QueryBuilder.bindMarker()).value(VRE_ID, QueryBuilder.bindMarker()).build()); + PreparedStatement prepStmt15 = session.prepare(QueryBuilder.insertInto(POSTS).value(POST_ID, QueryBuilder.bindMarker()).value(MULTI_FILE_UPLOAD, QueryBuilder.bindMarker()).build()); + PreparedStatement prepStmt16 = session.prepare(QueryBuilder.insertInto(POSTS).value(POST_ID, QueryBuilder.bindMarker()).value(FULL_NAME, QueryBuilder.bindMarker()).build()); + PreparedStatement prepStmt17 = session.prepare(QueryBuilder.insertInto(POSTS).value(POST_ID, QueryBuilder.bindMarker()).value(COMMENTS_NO, QueryBuilder.bindMarker()).build()); + PreparedStatement prepStmt18 = session.prepare(QueryBuilder.insertInto(POSTS).value(POST_ID, QueryBuilder.bindMarker()).value(LINK_TITLE, QueryBuilder.bindMarker()).build()); + +/* PreparedStatement prepStmt1 = session.prepare("INSERT INTO posts("+POST_ID+","+ LINK_HOST+") values(?, ?)"); PreparedStatement prepStmt2 = session.prepare("INSERT INTO posts("+POST_ID+","+ DESCRIPTION+") values(?, ?)"); PreparedStatement prepStmt3 = session.prepare("INSERT INTO posts("+POST_ID+","+ EMAIL+") values(?, ?)"); @@ -584,7 +607,7 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { PreparedStatement prepStmt16 = session.prepare("INSERT INTO posts("+POST_ID+","+ FULL_NAME+") values(?, ?)"); PreparedStatement prepStmt17 = session.prepare("INSERT INTO posts("+POST_ID+","+ COMMENTS_NO+") values(?, ?)"); PreparedStatement prepStmt18 = session.prepare("INSERT INTO posts("+POST_ID+","+ LINK_TITLE+") values(?, ?)"); - +*/ List boundStatements = new ArrayList<>(); if(post.getLinkHost()!=null){ @@ -639,10 +662,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { @Override public boolean saveUserFeed(Feed post) { CqlSession session = conn.getKeyspaceSession(); - BatchStatement writeBatch = getBatch(); List boundStatements = insertIntoPosts(session, feed2post(post)); - boundStatements.forEach(stmt -> writeBatch.add(stmt)); + + //boundStatements.forEach(stmt -> writeBatch.add(stmt)); //an entry in posts /*PreparedStatement stmt1 = createPostEntry(session); @@ -668,15 +691,14 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { */ //an entry in the user Timeline - PreparedStatement stmt2 = createUserTimelineEntry(session); - writeBatch.add(stmt2.bind(post.getEntityId(), post.getTime().toInstant(), UUID.fromString(post.getKey()))); - + BoundStatement stmt2 = createUserTimelineEntry(session).bind(post.getEntityId(), post.getTime().toInstant(), UUID.fromString(post.getKey())); + boundStatements.add(stmt2); //an entry in the VRES Timeline iff vreid field is not empty if (post.getVreid() != null && post.getVreid().compareTo("") != 0){ - PreparedStatement stmt3 = createVreTimelineEntry(session); - writeBatch.add(stmt3.bind(post.getVreid(), post.getTime().toInstant(), UUID.fromString(post.getKey()))); - + BoundStatement stmt3 = createVreTimelineEntry(session).bind(post.getVreid(), post.getTime().toInstant(), UUID.fromString(post.getKey())); + boundStatements.add(stmt3); } + BatchStatement writeBatch = getBatch().addAll(boundStatements); Boolean result = session.execute(writeBatch).wasApplied(); if (result){ @@ -692,9 +714,8 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { @Override public boolean saveUserPost(Post post) { CqlSession session = conn.getKeyspaceSession(); - BatchStatement writeBatch = getBatch(); List boundStatements = insertIntoPosts(session, post); - boundStatements.forEach(stmt -> writeBatch.add(stmt)); + //boundStatements.forEach(stmt -> writeBatch.add(stmt)); //an entry in posts /*PreparedStatement stmt1 = createPostEntry(session); writeBatch.add(stmt1.bind(UUID.fromString(post.getKey()), @@ -718,15 +739,15 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { post.getLinkTitle()));*/ //an entry in the user Timeline - PreparedStatement stmt2 = createUserTimelineEntry(session); - writeBatch.add(stmt2.bind(post.getEntityId(), post.getTime().toInstant(), UUID.fromString(post.getKey()))); - + BoundStatement stmt2 = createUserTimelineEntry(session).bind(post.getEntityId(), post.getTime().toInstant(), UUID.fromString(post.getKey())); + boundStatements.add(stmt2); //an entry in the VRES Timeline iff vreid field is not empty if (post.getVreid() != null && post.getVreid().compareTo("") != 0){ - PreparedStatement stmt3 = createVreTimelineEntry(session); - writeBatch.add(stmt3.bind(post.getVreid(), post.getTime().toInstant(), UUID.fromString(post.getKey()))); - + BoundStatement stmt3 = createVreTimelineEntry(session).bind(post.getVreid(), post.getTime().toInstant(), UUID.fromString(post.getKey())); + boundStatements.add(stmt3); } + BatchStatement writeBatch = getBatch().addAll(boundStatements); + Boolean result = session.execute(writeBatch).wasApplied(); if (result){ _log.info("Wrote user post with id " + post.getKey()); @@ -764,11 +785,12 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { post.setMultiFileUpload(true); boolean savePostResult = saveUserPost(post); if (savePostResult) { + _log.info("Post has been saved"); String postkey = post.getKey(); for (Attachment attachment : attachments) { boolean attachSaveResult = saveAttachmentEntry(postkey, attachment); if (!attachSaveResult) - _log.warn("Some of the attachments failed to be saved: " + attachment.getName()); + _log.info("Some of the attachments failed to be saved: " + attachment.getName()); } return true; } @@ -782,10 +804,9 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { @Deprecated public boolean saveAppFeed(Feed post) { CqlSession session = conn.getKeyspaceSession(); - BatchStatement writeBatch = getBatch(); List boundStatements = insertIntoPosts(session, feed2post(post)); - boundStatements.forEach(stmt -> writeBatch.add(stmt)); + //boundStatements.forEach(stmt -> writeBatch.add(stmt)); //an entry to posts /*PreparedStatement stmt1 = createPostEntry(session); writeBatch.add(stmt1.bind(UUID.fromString(post.getKey()), @@ -810,15 +831,14 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { //an entry in the Applications Timeline - PreparedStatement stmt2 = createAppTimelineEntry(session); - writeBatch.add(stmt2.bind(post.getEntityId(), post.getTime().toInstant(), UUID.fromString(post.getKey()))); - + BoundStatement stmt2 = createAppTimelineEntry(session).bind(post.getEntityId(), post.getTime().toInstant(), UUID.fromString(post.getKey())); + boundStatements.add(stmt2); //an entry in the VRES Timeline iff vreid field is not empty if (post.getVreid() != null && post.getVreid().compareTo("") != 0){ - PreparedStatement stmt3 = createVreTimelineEntry(session); - writeBatch.add(stmt3.bind(post.getVreid(), post.getTime().toInstant(), UUID.fromString(post.getKey()))); - + BoundStatement stmt3 = createVreTimelineEntry(session).bind(post.getVreid(), post.getTime().toInstant(), UUID.fromString(post.getKey())); + boundStatements.add(stmt3); } + BatchStatement writeBatch = getBatch().addAll(boundStatements); boolean result = session.execute(writeBatch).wasApplied(); if (result) @@ -832,10 +852,9 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { @Override public boolean saveAppPost(Post post) { CqlSession session = conn.getKeyspaceSession(); - BatchStatement writeBatch = getBatch(); List boundStatements = insertIntoPosts(session, post); - boundStatements.forEach(stmt -> writeBatch.add(stmt)); + //boundStatements.forEach(stmt -> writeBatch.add(stmt)); //an entry to posts /*PreparedStatement stmt1 = createPostEntry(session); writeBatch.add(stmt1.bind(UUID.fromString(post.getKey()), @@ -860,15 +879,14 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { //an entry in the Applications Timeline - PreparedStatement stmt2 = createAppTimelineEntry(session); - writeBatch.add(stmt2.bind(post.getEntityId(), post.getTime().toInstant(), UUID.fromString(post.getKey()))); - + BoundStatement stmt2 = createAppTimelineEntry(session).bind(post.getEntityId(), post.getTime().toInstant(), UUID.fromString(post.getKey())); + boundStatements.add(stmt2); //an entry in the VRES Timeline iff vreid field is not empty if (post.getVreid() != null && post.getVreid().compareTo("") != 0){ - PreparedStatement stmt3 = createVreTimelineEntry(session); - writeBatch.add(stmt3.bind(post.getVreid(), post.getTime().toInstant(), UUID.fromString(post.getKey()))); - + BoundStatement stmt3 = createVreTimelineEntry(session).bind(post.getVreid(), post.getTime().toInstant(), UUID.fromString(post.getKey())); + boundStatements.add(stmt3); } + BatchStatement writeBatch = getBatch().addAll(boundStatements); boolean result = session.execute(writeBatch).wasApplied(); if (result) @@ -931,10 +949,8 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { return false; } CqlSession session = conn.getKeyspaceSession(); - BatchStatement writeBatch = getBatch(); - - PreparedStatement stmt3 = createVreTimelineEntry(session); - writeBatch.add(stmt3.bind(vreid, toCheck.getTime().toInstant(), UUID.fromString(toCheck.getKey()))); + BatchStatement writeBatch = getBatch().add( + createVreTimelineEntry(session).bind(vreid, toCheck.getTime().toInstant(), UUID.fromString(toCheck.getKey()))); try{ boolean res = session.execute(writeBatch).wasApplied(); @@ -960,10 +976,7 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { return false; } CqlSession session = conn.getKeyspaceSession(); - BatchStatement writeBatch = getBatch(); - - PreparedStatement stmt3 = createVreTimelineEntry(session); - writeBatch.add(stmt3.bind(vreid, toCheck.getTime().toInstant(), UUID.fromString(toCheck.getKey()))); + BatchStatement writeBatch = getBatch().add(createVreTimelineEntry(session).bind(vreid, toCheck.getTime().toInstant(), UUID.fromString(toCheck.getKey()))); try{ boolean res = session.execute(writeBatch).wasApplied(); @@ -1116,10 +1129,7 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { public boolean deleteFeed(String feedId) throws FeedIDNotFoundException, PrivacyLevelTypeNotFoundException, FeedTypeNotFoundException, ColumnNameNotFoundException { Feed toDelete = readFeed(feedId); CqlSession session = conn.getKeyspaceSession(); - BatchStatement writeBatch = getBatch(); - - PreparedStatement stmt1 = updatePostEntry(session, TYPE); - writeBatch.add(stmt1.bind(FeedType.DISABLED.toString(), UUID.fromString(toDelete.getKey()))); + BatchStatement writeBatch = getBatch().add(updatePostEntry(session, TYPE).bind(FeedType.DISABLED.toString(), UUID.fromString(toDelete.getKey()))); try { session.execute(writeBatch); @@ -1139,10 +1149,7 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { public boolean deletePost(String postid) throws FeedIDNotFoundException, PrivacyLevelTypeNotFoundException, FeedTypeNotFoundException, ColumnNameNotFoundException { Post toDelete = readPost(postid); CqlSession session = conn.getKeyspaceSession(); - BatchStatement writeBatch = getBatch(); - - PreparedStatement stmt1 = updatePostEntry(session, TYPE); - writeBatch.add(stmt1.bind(PostType.DISABLED.toString(), UUID.fromString(toDelete.getKey()))); + BatchStatement writeBatch = getBatch().add(updatePostEntry(session, TYPE).bind(PostType.DISABLED.toString(), UUID.fromString(toDelete.getKey()))); try { session.execute(writeBatch); @@ -1785,6 +1792,18 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { * */ private List insertIntoNotifications(CqlSession session, Notification notification){ + PreparedStatement prepStmt1 = session.prepare(QueryBuilder.insertInto(NOTIFICATIONS).value(NOT_ID, QueryBuilder.bindMarker()).value(TYPE, QueryBuilder.bindMarker()).build()); + PreparedStatement prepStmt2 = session.prepare(QueryBuilder.insertInto(NOTIFICATIONS).value(NOT_ID, QueryBuilder.bindMarker()).value(USER_ID, QueryBuilder.bindMarker()).build()); + PreparedStatement prepStmt3 = session.prepare(QueryBuilder.insertInto(NOTIFICATIONS).value(NOT_ID, QueryBuilder.bindMarker()).value(SUBJECT_ID, QueryBuilder.bindMarker()).build()); + PreparedStatement prepStmt4 = session.prepare(QueryBuilder.insertInto(NOTIFICATIONS).value(NOT_ID, QueryBuilder.bindMarker()).value(TIMESTAMP, QueryBuilder.bindMarker()).build()); + PreparedStatement prepStmt5 = session.prepare(QueryBuilder.insertInto(NOTIFICATIONS).value(NOT_ID, QueryBuilder.bindMarker()).value(DESCRIPTION, QueryBuilder.bindMarker()).build()); + PreparedStatement prepStmt6 = session.prepare(QueryBuilder.insertInto(NOTIFICATIONS).value(NOT_ID, QueryBuilder.bindMarker()).value(URI, QueryBuilder.bindMarker()).build()); + PreparedStatement prepStmt7 = session.prepare(QueryBuilder.insertInto(NOTIFICATIONS).value(NOT_ID, QueryBuilder.bindMarker()).value(SENDER_ID, QueryBuilder.bindMarker()).build()); + PreparedStatement prepStmt8 = session.prepare(QueryBuilder.insertInto(NOTIFICATIONS).value(NOT_ID, QueryBuilder.bindMarker()).value(SENDER_FULL_NAME, QueryBuilder.bindMarker()).build()); + PreparedStatement prepStmt9 = session.prepare(QueryBuilder.insertInto(NOTIFICATIONS).value(NOT_ID, QueryBuilder.bindMarker()).value(SENDER_THUMBNAIL_URL, QueryBuilder.bindMarker()).build()); + PreparedStatement prepStmt10 = session.prepare(QueryBuilder.insertInto(NOTIFICATIONS).value(NOT_ID, QueryBuilder.bindMarker()).value(IS_READ, QueryBuilder.bindMarker()).build()); + + /* PreparedStatement prepStmt1 = session.prepare("INSERT INTO notifications("+NOT_ID+","+ TYPE+") values(?, ?)"); PreparedStatement prepStmt2 = session.prepare("INSERT INTO notifications("+NOT_ID+","+ USER_ID+") values(?, ?)"); PreparedStatement prepStmt3 = session.prepare("INSERT INTO notifications("+NOT_ID+","+ SUBJECT_ID+") values(?, ?)"); @@ -1795,7 +1814,7 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { PreparedStatement prepStmt8 = session.prepare("INSERT INTO notifications("+NOT_ID+","+ SENDER_FULL_NAME+") values(?, ?)"); PreparedStatement prepStmt9 = session.prepare("INSERT INTO notifications("+NOT_ID+","+ SENDER_THUMBNAIL_URL+") values(?, ?)"); PreparedStatement prepStmt10 = session.prepare("INSERT INTO notifications("+NOT_ID+","+ IS_READ+") values(?, ?)"); - +*/ List boundStatements = new ArrayList<>(); if(notification.getType()!=null){ @@ -1836,10 +1855,9 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { @Override public boolean saveNotification(Notification n) { CqlSession session = conn.getKeyspaceSession(); - BatchStatement writeBatch = getBatch(); List boundStatements = insertIntoNotifications(session, n); - boundStatements.forEach(stmt -> writeBatch.add(stmt)); + //boundStatements.forEach(stmt -> writeBatch.add(stmt)); /* //notification entry @@ -1858,12 +1876,13 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { */ //an entry in the user Notifications Timeline - PreparedStatement stmt2 = createUserNotificationsEntry(session); - writeBatch.add(stmt2.bind(n.getUserid(), n.getTime().toInstant(), UUID.fromString(n.getKey()))); + BoundStatement stmt2 = createUserNotificationsEntry(session).bind(n.getUserid(), n.getTime().toInstant(), UUID.fromString(n.getKey())); + boundStatements.add(stmt2); // save key in the unread notifications column family too - PreparedStatement stmt3 = createUnreadNotificationEntry(session); - writeBatch.add(stmt3.bind(n.getUserid(), n.getTime().toInstant(), UUID.fromString(n.getKey()))); + BoundStatement stmt3 = createUnreadNotificationEntry(session).bind(n.getUserid(), n.getTime().toInstant(), UUID.fromString(n.getKey())); + boundStatements.add(stmt3); + BatchStatement writeBatch = getBatch().addAll(boundStatements); try{ boolean res = session.execute(writeBatch).wasApplied(); @@ -1901,13 +1920,13 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { throw new NotificationIDNotFoundException("The specified notification to set Read with id: " + notificationidToSet + " does not exist"); CqlSession session = conn.getKeyspaceSession(); - BatchStatement writeBatch = getBatch(); + BatchStatement writeBatch = getBatch() //update the entry in notifications - writeBatch.add(updateNotificationEntry(session,IS_READ).bind(true,UUID.fromString(notificationidToSet))); + .add(updateNotificationEntry(session,IS_READ).bind(true,UUID.fromString(notificationidToSet))) // delete the notification's key from the unread notifications column family - writeBatch.add(deleteUnreadNotEntry(session).bind(toSet.getUserid(), toSet.getTime().toInstant())); + .add(deleteUnreadNotEntry(session).bind(toSet.getUserid(), toSet.getTime().toInstant())); // execute the operations try { @@ -2181,7 +2200,7 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { @Override public boolean setUserNotificationPreferences(String userid, Map enabledChannels) { CqlSession session = conn.getKeyspaceSession(); - BatchStatement writeBatch = getBatch(); + List boundStatements = new ArrayList<>(); for (NotificationType nType : enabledChannels.keySet()) { String valueToInsert = ""; @@ -2195,9 +2214,11 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { valueToInsert = ""; _log.trace("No Channels selected for " + nType + " by " + userid); } - writeBatch.add(createNotificationPreferenceEntry(session).bind(userid, nType.toString(), valueToInsert)); + + boundStatements.add(createNotificationPreferenceEntry(session).bind(userid, nType.toString(), valueToInsert)); } + BatchStatement writeBatch = getBatch().addAll(boundStatements); boolean overAllresult = session.execute(writeBatch).wasApplied(); if (overAllresult) _log.trace("Set Notification Map for " + userid + " OK"); @@ -2275,13 +2296,21 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { * */ private List insertIntoComments(CqlSession session, Comment comment){ - - PreparedStatement prepStmt1 = session.prepare("INSERT INTO comments("+COMMENT_ID+","+ USER_ID+") values(?, ?)"); - PreparedStatement prepStmt2 = session.prepare("INSERT INTO comments("+COMMENT_ID+","+ FULL_NAME+") values(?, ?)"); - PreparedStatement prepStmt3 = session.prepare("INSERT INTO comments("+COMMENT_ID+","+ THUMBNAIL_URL+") values(?, ?)"); - PreparedStatement prepStmt4 = session.prepare("INSERT INTO comments("+COMMENT_ID+","+ COMMENT+") values(?, ?)"); - PreparedStatement prepStmt5 = session.prepare("INSERT INTO comments("+COMMENT_ID+","+ POST_ID+") values(?, ?)"); - PreparedStatement prepStmt6 = session.prepare("INSERT INTO comments("+COMMENT_ID+","+ TIMESTAMP+") values(?, ?)"); + PreparedStatement prepStmt1 = session.prepare(QueryBuilder.insertInto(COMMENTS).value(COMMENT_ID, QueryBuilder.bindMarker()).value(USER_ID, QueryBuilder.bindMarker()).build()); + // "INSERT INTO comments("+COMMENT_ID+","+ USER_ID+") values(?, ?)"); + PreparedStatement prepStmt2 = session.prepare(QueryBuilder.insertInto(COMMENTS).value(COMMENT_ID, QueryBuilder.bindMarker()).value(FULL_NAME, QueryBuilder.bindMarker()).build()); + //"INSERT INTO comments("+COMMENT_ID+","+ FULL_NAME+") values(?, ?)"); + PreparedStatement prepStmt3 = session.prepare(QueryBuilder.insertInto(COMMENTS).value(COMMENT_ID, QueryBuilder.bindMarker()).value(THUMBNAIL_URL, QueryBuilder.bindMarker()).build()); + //"INSERT INTO comments("+COMMENT_ID+","+ THUMBNAIL_URL+") values(?, ?)"); + PreparedStatement prepStmt4 = session.prepare(QueryBuilder.insertInto(COMMENTS).value(COMMENT_ID, QueryBuilder.bindMarker()).value(COMMENT, QueryBuilder.bindMarker()).build()); + //"INSERT INTO comments("+COMMENT_ID+","+ COMMENT+") values(?, ?)"); + PreparedStatement prepStmt5 = session.prepare(QueryBuilder.insertInto(COMMENTS).value(COMMENT_ID, QueryBuilder.bindMarker()).value(POST_ID, QueryBuilder.bindMarker()).build()); + //"INSERT INTO comments("+COMMENT_ID+","+ POST_ID+") values(?, ?)"); + PreparedStatement prepStmt6 = session.prepare(QueryBuilder.insertInto(COMMENTS).value(COMMENT_ID, QueryBuilder.bindMarker()).value(TIMESTAMP, QueryBuilder.bindMarker()).build()); + //"INSERT INTO comments("+COMMENT_ID+","+ TIMESTAMP+") values(?, ?)"); + PreparedStatement prepStmt7 = session.prepare(QueryBuilder.insertInto(COMMENTS).value(COMMENT_ID, QueryBuilder.bindMarker()).value(IS_EDIT, QueryBuilder.bindMarker()).build()); + //"INSERT INTO comments("+COMMENT_ID+","+ TIMESTAMP+") values(?, ?)"); + PreparedStatement prepStmt8 = session.prepare(QueryBuilder.insertInto(COMMENTS).value(COMMENT_ID, QueryBuilder.bindMarker()).value(LAST_EDIT_TIME, QueryBuilder.bindMarker()).build()); List boundStatements = new ArrayList<>(); @@ -2304,6 +2333,11 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { }if(comment.getTime()!=null){ boundStatements.add(prepStmt6.bind(UUID.fromString(comment.getKey()),comment.getTime().toInstant())); } + boundStatements.add(prepStmt7.bind(UUID.fromString(comment.getKey()),comment.isEdit())); + + if(comment.getLastEditTime()!=null){ + boundStatements.add(prepStmt8.bind(UUID.fromString(comment.getKey()),comment.getLastEditTime().toInstant())); + } return boundStatements; } @@ -2328,25 +2362,21 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { e.printStackTrace(); return false; } + _log.info("Writing comment : {}", comment.toString()); CqlSession session = conn.getKeyspaceSession(); - BatchStatement writeBatch = getBatch(); List boundStatements = insertIntoComments(session, comment); - boundStatements.forEach(stmt->writeBatch.add(stmt)); -/* - writeBatch.add(createNewCommentEntry(session).bind( - UUID.fromString(comment.getKey()), - comment.getUserid(), - comment.getFullName(), - comment.getThumbnailURL(), - comment.getText(), - UUID.fromString(comment.getFeedid()), - comment.getTime().toInstant(), - null - ));*/ + //boundStatements.forEach(stmt->writeBatch.add(stmt)); + + BatchStatement writeBatch = getBatch().addAll(boundStatements); try { - session.execute(writeBatch); + ResultSet res = session.execute(writeBatch); + for (ExecutionInfo ex: res.getExecutionInfos()){ + _log.info("Writing comment result errors: {}", ex.getErrors()); + _log.info("Writing comment result payload: {}", ex.getIncomingPayload()); + } + _log.info("Writing comment result executed?: {}", res.wasApplied()); } catch (Exception e) { e.printStackTrace(); @@ -2498,11 +2528,9 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { @Override public boolean editComment(Comment comment2Edit) throws PrivacyLevelTypeNotFoundException, FeedTypeNotFoundException, ColumnNameNotFoundException, CommentIDNotFoundException, FeedIDNotFoundException { CqlSession session = conn.getKeyspaceSession(); - BatchStatement writeBatch = getBatch(); - - writeBatch.add(updateCommentEntry(session, COMMENT).bind(comment2Edit.getText(), UUID.fromString(comment2Edit.getKey()))) - .add(updateCommentEntry(session, IS_EDIT).bind(comment2Edit.isEdit(), UUID.fromString(comment2Edit.getKey()))) - .add(updateCommentEntry(session, LAST_EDIT_TIME).bind(comment2Edit.getTime().toInstant(), UUID.fromString(comment2Edit.getKey()))); + BatchStatement writeBatch = getBatch().add(updateCommentEntry(session, COMMENT).bind(comment2Edit.getText(), UUID.fromString(comment2Edit.getKey()))) + .add(updateCommentEntry(session, IS_EDIT).bind(true, UUID.fromString(comment2Edit.getKey()))) + .add(updateCommentEntry(session, LAST_EDIT_TIME).bind(new Date().toInstant(), UUID.fromString(comment2Edit.getKey()))); try { boolean res = session.execute(writeBatch).wasApplied(); @@ -2546,12 +2574,24 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { private List insertIntoLikes(CqlSession session, Like like){ - PreparedStatement prepStmt1 = session.prepare("INSERT INTO likes("+LIKE_ID+","+ USER_ID+") values(?, ?)"); + PreparedStatement prepStmt1 = session.prepare(QueryBuilder.insertInto(LIKES).value(LIKE_ID, QueryBuilder.bindMarker()).value(USER_ID, QueryBuilder.bindMarker()).build()); + // "INSERT INTO comments("+COMMENT_ID+","+ USER_ID+") values(?, ?)"); + PreparedStatement prepStmt2 = session.prepare(QueryBuilder.insertInto(LIKES).value(LIKE_ID, QueryBuilder.bindMarker()).value(FULL_NAME, QueryBuilder.bindMarker()).build()); + //"INSERT INTO comments("+COMMENT_ID+","+ FULL_NAME+") values(?, ?)"); + PreparedStatement prepStmt3 = session.prepare(QueryBuilder.insertInto(LIKES).value(LIKE_ID, QueryBuilder.bindMarker()).value(THUMBNAIL_URL, QueryBuilder.bindMarker()).build()); + //"INSERT INTO comments("+COMMENT_ID+","+ THUMBNAIL_URL+") values(?, ?)"); + PreparedStatement prepStmt4 = session.prepare(QueryBuilder.insertInto(LIKES).value(LIKE_ID, QueryBuilder.bindMarker()).value(POST_ID, QueryBuilder.bindMarker()).build()); + //"INSERT INTO comments("+COMMENT_ID+","+ POST_ID+") values(?, ?)"); + PreparedStatement prepStmt5 = session.prepare(QueryBuilder.insertInto(LIKES).value(LIKE_ID, QueryBuilder.bindMarker()).value(TIMESTAMP, QueryBuilder.bindMarker()).build()); + //"INSERT INTO comments("+COMMENT_ID+","+ TIMESTAMP+") values(?, ?)"); + + + /*PreparedStatement prepStmt1 = session.prepare("INSERT INTO likes("+LIKE_ID+","+ USER_ID+") values(?, ?)"); PreparedStatement prepStmt2 = session.prepare("INSERT INTO likes("+LIKE_ID+","+ FULL_NAME+") values(?, ?)"); PreparedStatement prepStmt3 = session.prepare("INSERT INTO likes("+LIKE_ID+","+ THUMBNAIL_URL+") values(?, ?)"); PreparedStatement prepStmt4 = session.prepare("INSERT INTO likes("+LIKE_ID+","+ POST_ID+") values(?, ?)"); PreparedStatement prepStmt5 = session.prepare("INSERT INTO likes("+LIKE_ID+","+ TIMESTAMP+") values(?, ?)"); - + */ List boundStatements = new ArrayList<>(); if(like.getUserid()!=null){ @@ -2604,19 +2644,10 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { // Inserting data //an entry in the feed CF //and an entry in the UserLikesCF - BatchStatement writeBatch = getBatch(). - //List boundStatements = insertIntoLikes(session,like); + List boundStatements = insertIntoLikes(session,like); //boundStatements.forEach(stmt->writeBatch.add(stmt)); + BatchStatement writeBatch = getBatch().addAll(boundStatements); - add(createNewLikeEntry(session).bind(UUID.fromString(like.getKey()), - like.getUserid(), - like.getFullName(), - like.getThumbnailURL(), - UUID.fromString(like.getFeedid()), - like.getTime().toInstant())) - .add(createNewUserLikesEntry(session).bind(like.getUserid(), - UUID.fromString(like.getKey()), - UUID.fromString(like.getFeedid()))); try { session.execute(writeBatch); @@ -3189,14 +3220,22 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { */ private List insertIntoInvites(CqlSession session, Invite invite){ - PreparedStatement prepStmt1 = session.prepare("INSERT INTO invites("+INVITE_ID+","+ SENDER_USER_ID+") values(?, ?)"); + PreparedStatement prepStmt1 = session.prepare(QueryBuilder.insertInto(INVITES).value(INVITE_ID, QueryBuilder.bindMarker()).value(SENDER_USER_ID, QueryBuilder.bindMarker()).build()); + PreparedStatement prepStmt2 = session.prepare(QueryBuilder.insertInto(INVITES).value(INVITE_ID, QueryBuilder.bindMarker()).value(VRE_ID, QueryBuilder.bindMarker()).build()); + PreparedStatement prepStmt3 = session.prepare(QueryBuilder.insertInto(INVITES).value(INVITE_ID, QueryBuilder.bindMarker()).value(EMAIL, QueryBuilder.bindMarker()).build()); + PreparedStatement prepStmt4 = session.prepare(QueryBuilder.insertInto(INVITES).value(INVITE_ID, QueryBuilder.bindMarker()).value(CONTROL_CODE, QueryBuilder.bindMarker()).build()); + PreparedStatement prepStmt5 = session.prepare(QueryBuilder.insertInto(INVITES).value(INVITE_ID, QueryBuilder.bindMarker()).value(STATUS, QueryBuilder.bindMarker()).build()); + PreparedStatement prepStmt6 = session.prepare(QueryBuilder.insertInto(INVITES).value(INVITE_ID, QueryBuilder.bindMarker()).value(TIMESTAMP, QueryBuilder.bindMarker()).build()); + PreparedStatement prepStmt7 = session.prepare(QueryBuilder.insertInto(INVITES).value(INVITE_ID, QueryBuilder.bindMarker()).value(SENDER_FULL_NAME, QueryBuilder.bindMarker()).build()); + + /*PreparedStatement prepStmt1 = session.prepare("INSERT INTO invites("+INVITE_ID+","+ SENDER_USER_ID+") values(?, ?)"); PreparedStatement prepStmt2 = session.prepare("INSERT INTO invites("+INVITE_ID+","+ VRE_ID+") values(?, ?)"); PreparedStatement prepStmt3 = session.prepare("INSERT INTO invites("+INVITE_ID+","+ EMAIL+") values(?, ?)"); PreparedStatement prepStmt4 = session.prepare("INSERT INTO invites("+INVITE_ID+","+ CONTROL_CODE+") values(?, ?)"); PreparedStatement prepStmt5 = session.prepare("INSERT INTO invites("+INVITE_ID+","+ STATUS+") values(?, ?)"); PreparedStatement prepStmt6 = session.prepare("INSERT INTO invites("+INVITE_ID+","+ TIMESTAMP+") values(?, ?)"); PreparedStatement prepStmt7 = session.prepare("INSERT INTO invites("+INVITE_ID+","+ SENDER_FULL_NAME+") values(?, ?)"); - +*/ List boundStatements = new ArrayList<>(); if(invite.getSenderUserId()!=null){ @@ -3233,12 +3272,14 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { * @param invite * @return the partial mutation batch instance */ - private BatchStatement initSaveInvite(Invite invite, CqlSession session) { - BatchStatement writeBatch = getBatch(); + private List initSaveInvite(Invite invite, CqlSession session) { + List boundStatements = insertIntoInvites(session, invite); + if (invite == null) throw new NullArgumentException("Invite instance is null"); - insertIntoInvites(session,invite).forEach(stmt->writeBatch.add(stmt)); + //insertIntoInvites(session,invite).forEach(stmt->writeBatch.add(stmt)); + //BatchStatement writeBatch = getBatch().addAll(boundStatements); // Inserting data /*writeBatch.add(createNewInviteEntry(session).bind( UUID.fromString(invite.getKey()), @@ -3252,7 +3293,7 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { ));*/ - return writeBatch; + return boundStatements; } /** @@ -3300,12 +3341,13 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { return InviteOperationResult.ALREADY_INVITED; _log.debug("Invite not found, proceed to save it ..."); CqlSession session = conn.getKeyspaceSession(); - BatchStatement m = initSaveInvite(invite, session); + List boundStatements = initSaveInvite(invite, session); //an entry in the VRE Invites - m.add(createNewVreInviteEntry(session).bind(invite.getVreid(), UUID.fromString(invite.getKey()), InviteStatus.PENDING.toString())); + boundStatements.add(createNewVreInviteEntry(session).bind(invite.getVreid(), UUID.fromString(invite.getKey()), InviteStatus.PENDING.toString())); //an entry in the EMAIL Invites - m.add(createNewEmailInviteEntry(session).bind(email, invite.getVreid(), UUID.fromString(invite.getKey()))); - boolean result = session.execute(m).wasApplied(); + boundStatements.add(createNewEmailInviteEntry(session).bind(email, invite.getVreid(), UUID.fromString(invite.getKey()))); + BatchStatement writeBatch = getBatch().addAll(boundStatements); + boolean result = session.execute(writeBatch).wasApplied(); return result ? InviteOperationResult.SUCCESS : InviteOperationResult.FAILED; } @@ -3362,10 +3404,7 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { throw new InviteIDNotFoundException("The specified invite to set with id: " + inviteid + " does not exist"); CqlSession session = conn.getKeyspaceSession(); - BatchStatement writeBatch = getBatch(); - //update in the Invites Static CF - //updated in the VREInvites Dynamic CF - writeBatch.add(updateInviteEntry(session, STATUS).bind(status.toString(), UUID.fromString(inviteid))) + BatchStatement writeBatch = getBatch().add(updateInviteEntry(session, STATUS).bind(status.toString(), UUID.fromString(inviteid))) .add(updateVreInviteEntry(session, STATUS).bind(status.toString(), vreid, UUID.fromString(inviteid))); try { session.execute(writeBatch); @@ -3466,13 +3505,20 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { private List insertIntoAttachments(CqlSession session, Attachment attachment, String feedId){ - PreparedStatement prepStmt1 = session.prepare("INSERT INTO attachments("+ATTACH_ID+","+ POST_ID+") values(?, ?)"); + PreparedStatement prepStmt1 = session.prepare(QueryBuilder.insertInto(ATTACHMENTS).value(ATTACH_ID, QueryBuilder.bindMarker()).value(POST_ID, QueryBuilder.bindMarker()).build()); + PreparedStatement prepStmt2 = session.prepare(QueryBuilder.insertInto(ATTACHMENTS).value(ATTACH_ID, QueryBuilder.bindMarker()).value(URI, QueryBuilder.bindMarker()).build()); + PreparedStatement prepStmt3 = session.prepare(QueryBuilder.insertInto(ATTACHMENTS).value(ATTACH_ID, QueryBuilder.bindMarker()).value(NAME, QueryBuilder.bindMarker()).build()); + PreparedStatement prepStmt4 = session.prepare(QueryBuilder.insertInto(ATTACHMENTS).value(ATTACH_ID, QueryBuilder.bindMarker()).value(DESCRIPTION, QueryBuilder.bindMarker()).build()); + PreparedStatement prepStmt5 = session.prepare(QueryBuilder.insertInto(ATTACHMENTS).value(ATTACH_ID, QueryBuilder.bindMarker()).value(URI_THUMBNAIL, QueryBuilder.bindMarker()).build()); + PreparedStatement prepStmt6 = session.prepare(QueryBuilder.insertInto(ATTACHMENTS).value(ATTACH_ID, QueryBuilder.bindMarker()).value(MIME_TYPE, QueryBuilder.bindMarker()).build()); + + /*PreparedStatement prepStmt1 = session.prepare("INSERT INTO attachments("+ATTACH_ID+","+ POST_ID+") values(?, ?)"); PreparedStatement prepStmt2 = session.prepare("INSERT INTO attachments("+ATTACH_ID+","+ URI+") values(?, ?)"); PreparedStatement prepStmt3 = session.prepare("INSERT INTO attachments("+ATTACH_ID+","+ NAME+") values(?, ?)"); PreparedStatement prepStmt4 = session.prepare("INSERT INTO attachments("+ATTACH_ID+","+ DESCRIPTION+") values(?, ?)"); PreparedStatement prepStmt5 = session.prepare("INSERT INTO attachments("+ATTACH_ID+","+ URI_THUMBNAIL+") values(?, ?)"); PreparedStatement prepStmt6 = session.prepare("INSERT INTO attachments("+ATTACH_ID+","+ MIME_TYPE+") values(?, ?)"); - +*/ List boundStatements = new ArrayList<>(); boundStatements.add(prepStmt1.bind(UUID.fromString(attachment.getId()),UUID.fromString(feedId))); @@ -3510,9 +3556,11 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { //an entry in the Attachment CF try { List boundStatements = insertIntoAttachments(session, toSave, feedId); - BatchStatement writeBatch = getBatch(); - boundStatements.forEach(stmt->writeBatch.add(stmt)); - session.execute(writeBatch); + BatchStatement writeBatch = getBatch().addAll(boundStatements); + //boundStatements.forEach(stmt->writeBatch.add(stmt)); + ResultSet res = session.execute(writeBatch); + _log.info(res.getExecutionInfos().toString()); + _log.info(""+res.wasApplied()); /* session.execute(createNewaAttachEntry(session).bind( UUID.fromString(toSave.getId()), @@ -3883,21 +3931,19 @@ public final class DBCassandraAstyanaxImpl implements DatabookStore { } _log.debug("Updating counter for " + hashtag + " to " + newCount); CqlSession session = conn.getKeyspaceSession(); - PreparedStatement stmt; - BatchStatement writeBatch = getBatch(); + BoundStatement stmt; + if (existRecordbyCompId(session, HASHTAG, VRE_ID, hashtag, vreid, HASHTAGS_COUNTER)){ stmt = session.prepare(QueryBuilder.update(HASHTAGS_COUNTER) .setColumn(COUNT, QueryBuilder.bindMarker()) .whereColumn(HASHTAG).isEqualTo(QueryBuilder.bindMarker()) .whereColumn(VRE_ID).isEqualTo(QueryBuilder.bindMarker()) - .build()); - writeBatch.add(stmt.bind((long)newCount, hashtag, vreid)); + .build()).bind((long)newCount, hashtag, vreid); } else{ - stmt = createNewUHashtagCounterEntry(session); - writeBatch.add(stmt.bind(vreid, hashtag, (long)newCount)); + stmt = createNewUHashtagCounterEntry(session).bind(vreid, hashtag, (long)newCount); } - + BatchStatement writeBatch = getBatch().add(stmt); try { session.execute(writeBatch); diff --git a/src/main/java/org/gcube/portal/databook/shared/PostWithAttachment.java b/src/main/java/org/gcube/portal/databook/shared/PostWithAttachment.java new file mode 100644 index 0000000..948dcc3 --- /dev/null +++ b/src/main/java/org/gcube/portal/databook/shared/PostWithAttachment.java @@ -0,0 +1,31 @@ +package org.gcube.portal.databook.shared; + +import java.io.Serializable; +import java.util.List; + +public class PostWithAttachment implements Serializable { + private Post post; + private List attachments; + + public PostWithAttachment(Post post, List attachments){ + super(); + this.post = post; + this.attachments = attachments; + } + + public void setAttachments(List attachments) { + this.attachments = attachments; + } + public void setPost(Post post) { + this.post = post; + } + + + public List getAttachments() { + return attachments; + } + + public Post getPost() { + return post; + } +}