Skip to content

Commit

Permalink
Fix bug #14479 by setting explicitly the current requester as the aut…
Browse files Browse the repository at this point in the history
…hor and the current date as the creation date when saving the comment in database
  • Loading branch information
mmoqui committed Oct 25, 2024
1 parent 4b3871c commit 5b81297
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import org.apache.commons.lang3.StringUtils;
import org.silverpeas.core.ResourceReference;
import org.silverpeas.core.admin.user.model.User;
import org.silverpeas.core.annotation.Bean;
import org.silverpeas.kernel.annotation.Technical;
import org.silverpeas.core.comment.model.Comment;
Expand Down Expand Up @@ -53,6 +54,8 @@
import java.util.Set;
import java.util.stream.Collectors;

import static org.silverpeas.core.util.ArgumentAssertion.assertDefined;
import static org.silverpeas.core.util.ArgumentAssertion.assertNotNull;
import static org.silverpeas.core.util.DateUtil.*;

/**
Expand Down Expand Up @@ -86,7 +89,9 @@ protected JDBCCommentRequester() {
/**
* Saves the specified comment with the specified connection onto a data source. Once saved, the
* comment passed as argument isn't updated with the persistence information and hence the
* returned comment instance should be used for further handling.
* returned comment instance should be used for further handling. Whatever the author and
* creation date setting, it isn't taken into account: the current requester and the current
* date are taken into account as author and creation date.
* @param con the connection to a data source.
* @param cmt the comment to save.
* @return the comment that is saved into the data source with its unique identifier.
Expand All @@ -96,18 +101,17 @@ public Comment saveComment(Connection con, Comment cmt) throws SQLException {
String insertQuery = "INSERT INTO sb_comment_comment (commentId , commentOwnerId, " +
"commentCreationDate, commentModificationDate, commentComment, resourceType, resourceId, " +
"instanceId) VALUES ( ?, ?, ?, ?, ?, ?, ?, ? )";
String errorMessage = "The comment should be created by an authenticated user";
int newId = DBUtil.getNextId(COMMENT_TABLE, COMMENT_ID);
User author = User.getCurrentRequester();
assertNotNull(author, errorMessage);
assertDefined(author.getId(), errorMessage);
String now = date2SQLDate(new Date());
try (PreparedStatement prepStmt = con.prepareStatement(insertQuery)) {
prepStmt.setInt(1, newId);
prepStmt.setInt(2, Integer.parseInt(cmt.getCreatorId()));
prepStmt.setString(3, date2SQLDate(cmt.getCreationDate()));
String updateDate;
if (cmt.getLastUpdateDate() != null) {
updateDate = date2SQLDate(cmt.getLastUpdateDate());
} else {
updateDate = null;
}
prepStmt.setString(4, updateDate);
prepStmt.setInt(2, Integer.parseInt(author.getId()));
prepStmt.setString(3, now);
prepStmt.setString(4, now);
prepStmt.setString(5, cmt.getMessage());
prepStmt.setString(6, cmt.getResourceType());
prepStmt.setString(7, cmt.getResourceReference().getLocalId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,16 @@
public class Comment implements SilverpeasContent {

private static final long serialVersionUID = 3738544756345055840L;
private static SettingBundle settingBundle = getSettingBundle("org.silverpeas.util.comment.Comment");
private static final SettingBundle settingBundle =
getSettingBundle("org.silverpeas.util.comment.Comment");
public static final String CONTRIBUTION_TYPE = "Comment";
private CommentId id;
private String resourceType;
private ResourceReference resource;
private final CommentId id;
private final String resourceType;
private final ResourceReference resource;
private String message;
private Date creationDate;
private final Date creationDate;
private Date updateDate;
private String authorId;
private final String authorId;

/**
* Constructs a comment about the given resource and written by the specified author at the given
Expand Down

0 comments on commit 5b81297

Please sign in to comment.