1 16 package org.outerj.daisy.repository.serverimpl.comment; 17 18 import org.outerj.daisy.repository.commonimpl.comment.CommentStrategy; 19 import org.outerj.daisy.repository.commonimpl.comment.CommentImpl; 20 import org.outerj.daisy.repository.commonimpl.AuthenticatedUser; 21 import org.outerj.daisy.repository.comment.CommentVisibility; 22 import org.outerj.daisy.repository.comment.Comment; 23 import org.outerj.daisy.repository.RepositoryException; 24 import org.outerj.daisy.repository.AccessException; 25 import org.outerj.daisy.repository.serverimpl.LocalRepositoryManager; 26 import org.outerj.daisy.repository.serverimpl.AbstractLocalStrategy; 27 import org.outerj.daisy.repository.acl.AclResultInfo; 28 import org.outerj.daisy.repository.acl.AclPermission; 29 import org.outerj.daisy.jdbcutil.JdbcHelper; 30 import org.apache.xmlbeans.XmlObject; 31 import org.outerx.daisy.x10.CommentCreatedDocument; 32 import org.outerx.daisy.x10.CommentDeletedDocument; 33 34 import java.sql.*; 35 import java.util.Date ; 36 import java.util.ArrayList ; 37 38 public class LocalCommentStrategy extends AbstractLocalStrategy implements CommentStrategy { 39 public LocalCommentStrategy(LocalRepositoryManager.Context context, AuthenticatedUser systemUser, JdbcHelper jdbcHelper) { 40 super(context, systemUser, jdbcHelper); 41 } 42 43 public CommentImpl storeComment(long documentId, long branchId, long languageId, CommentVisibility visibility, String text, AuthenticatedUser user) throws RepositoryException { 44 45 AclResultInfo aclInfo = context.getCommonRepository().getAccessManager().getAclInfoOnLive(systemUser, 49 user.getId(), user.getActiveRoleIds(), documentId, branchId, languageId); 50 if (!aclInfo.isAllowed(AclPermission.READ_LIVE)) 51 throw new AccessException("User " + user.getId() + " is not allowed to add comments to document ID " + documentId + ", branch " + getBranchLabel(branchId) + ", language " + getLanguageLabel(languageId)); 52 if (visibility == CommentVisibility.EDITORS && !aclInfo.isAllowed(AclPermission.WRITE)) 53 throw new RepositoryException("Comments with editors-only visibility can only be created by users with write rights to the document variant."); 54 55 Connection conn = null; 56 PreparedStatement stmt = null; 57 try { 58 conn = context.getDataSource().getConnection(); 59 jdbcHelper.startTransaction(conn); 60 61 long commentId = context.getNextCommentId(); 62 Date createdOn = new Date (); 63 64 stmt = conn.prepareStatement("insert into comments (id, doc_id, branch_id, lang_id, created_by, created_on, visibility, comment_text) values (?,?,?,?,?,?,?,?)"); 65 stmt.setLong(1, commentId); 66 stmt.setLong(2, documentId); 67 stmt.setLong(3, branchId); 68 stmt.setLong(4, languageId); 69 stmt.setLong(5, user.getId()); 70 stmt.setTimestamp(6, new Timestamp(createdOn.getTime())); 71 stmt.setString(7, visibility.getCode()); 72 stmt.setString(8, text); 73 stmt.execute(); 74 stmt.close(); 75 76 CommentImpl newComment = new CommentImpl(documentId, branchId, languageId, commentId, text, visibility, createdOn, user.getId()); 77 78 XmlObject eventDescription = createCommentCreatedEvent(newComment); 80 eventHelper.createEvent(eventDescription, "CommentCreated", conn); 81 82 conn.commit(); 83 84 return newComment; 85 } catch (Throwable e) { 86 jdbcHelper.rollback(conn); 87 throw new RepositoryException("Problem storing comment.", e); 88 } finally { 89 jdbcHelper.closeStatement(stmt); 90 jdbcHelper.closeConnection(conn); 91 } 92 } 93 94 public CommentCreatedDocument createCommentCreatedEvent(Comment comment) { 95 CommentCreatedDocument commentCreatedDocument = CommentCreatedDocument.Factory.newInstance(); 96 CommentCreatedDocument.CommentCreated commentCreated = commentCreatedDocument.addNewCommentCreated(); 97 commentCreated.addNewNewComment().setComment(comment.getXml().getComment()); 98 return commentCreatedDocument; 99 } 100 101 public void deleteComment(long documentId, long branchId, long languageId, long id, AuthenticatedUser user) throws RepositoryException { 102 103 AclResultInfo aclInfo = context.getCommonRepository().getAccessManager().getAclInfoOnLive(systemUser, 104 user.getId(), user.getActiveRoleIds(), documentId, branchId, languageId); 105 boolean writeAccess = aclInfo.isAllowed(AclPermission.WRITE); 106 107 Connection conn = null; 108 PreparedStatement stmt = null; 109 try { 110 conn = context.getDataSource().getConnection(); 111 jdbcHelper.startTransaction(conn); 112 113 stmt = conn.prepareStatement("select visibility, comment_text, created_by, created_on from comments where doc_id = ? and branch_id = ? and lang_id = ? and id = ? " + jdbcHelper.getSharedLockClause()); 114 stmt.setLong(1, documentId); 115 stmt.setLong(2, branchId); 116 stmt.setLong(3, languageId); 117 stmt.setLong(4, id); 118 ResultSet rs = stmt.executeQuery(); 119 120 CommentImpl comment; 121 if (rs.next()) { 122 comment = new CommentImpl(documentId, branchId, languageId, id, rs.getString("comment_text"), CommentVisibility.getByCode(rs.getString("visibility")), rs.getDate("created_on"), rs.getLong("created_by")); 123 } else { 124 throw new RepositoryException("The document " + documentId + ", branch " + getBranchLabel(branchId) + ", language " + getLanguageLabel(languageId) + " doesn't have a comment with ID " + id + "."); 125 } 126 stmt.close(); 127 128 boolean canDelete = true; 129 if (!user.isInAdministratorRole()) { 130 if (!writeAccess && comment.getVisibility() != CommentVisibility.PRIVATE) 131 canDelete = false; 132 if (comment.getVisibility() == CommentVisibility.PRIVATE && comment.getCreatedBy() != user.getId()) 133 canDelete = false; 134 } 135 136 if (!canDelete) 137 throw new RepositoryException("You are not allowed to remove comment " + id + " of document ID " + documentId + ", branch " + getBranchLabel(branchId) + ", language " + getLanguageLabel(languageId)); 138 139 stmt = conn.prepareStatement("delete from comments where id = ? and doc_id = ? and branch_id = ? and lang_id = ?"); 140 stmt.setLong(1, id); 141 stmt.setLong(2, documentId); 142 stmt.setLong(3, branchId); 143 stmt.setLong(4, languageId); 144 stmt.execute(); 145 stmt.close(); 146 147 XmlObject eventDescription = createCommentDeletedEvent(comment, user.getId()); 149 eventHelper.createEvent(eventDescription, "CommentDeleted", conn); 150 151 conn.commit(); 152 } catch (Throwable e) { 153 jdbcHelper.rollback(conn); 154 throw new RepositoryException("Problem deleting comment.", e); 155 } finally { 156 jdbcHelper.closeStatement(stmt); 157 jdbcHelper.closeConnection(conn); 158 } 159 } 160 161 public CommentDeletedDocument createCommentDeletedEvent(Comment comment, long deleterId) { 162 CommentDeletedDocument commentDeletedDocument = CommentDeletedDocument.Factory.newInstance(); 163 CommentDeletedDocument.CommentDeleted commentDeleted = commentDeletedDocument.addNewCommentDeleted(); 164 commentDeleted.addNewDeletedComment().setComment(comment.getXml().getComment()); 165 commentDeleted.setDeletedTime(getCalendar(new Date ())); 166 commentDeleted.setDeleterId(deleterId); 167 return commentDeletedDocument; 168 } 169 170 public Comment[] loadComments(long documentId, long branchId, long languageId, AuthenticatedUser user) throws RepositoryException { 171 AclResultInfo aclInfo = context.getCommonRepository().getAccessManager().getAclInfoOnLive(systemUser, 173 user.getId(), user.getActiveRoleIds(), documentId, branchId, languageId); 174 if (!aclInfo.isAllowed(AclPermission.READ_LIVE)) 175 throw new AccessException("User " + user.getId() + " cannot read the comments of document ID " + documentId + ", branch " + getBranchLabel(branchId) + ", language " + getLanguageLabel(languageId)); 176 177 Connection conn = null; 178 PreparedStatement stmt = null; 179 ResultSet rs = null; 180 try { 181 conn = context.getDataSource().getConnection(); 182 StringBuffer query = new StringBuffer (SELECT_COMMENTS); 183 query.append(" where doc_id = ? and branch_id = ? and lang_id = ? "); 184 query.append(" and (visibility = '").append(CommentVisibility.PUBLIC.getCode()).append("'"); 185 query.append(" or (visibility = '").append(CommentVisibility.PRIVATE.getCode()).append("' and created_by = ?)"); 186 if (aclInfo.isAllowed(AclPermission.WRITE)) 187 query.append(" or visibility = '").append(CommentVisibility.EDITORS.getCode()).append("'"); 188 query.append(") order by created_on"); 189 stmt = conn.prepareStatement(query.toString()); 190 stmt.setLong(1, documentId); 191 stmt.setLong(2, branchId); 192 stmt.setLong(3, languageId); 193 stmt.setLong(4, user.getId()); 194 rs = stmt.executeQuery(); 195 return getCommentsFromResultSet(rs); 196 } catch (Throwable e) { 197 throw new RepositoryException("Problem loading comments.", e); 198 } finally { 199 jdbcHelper.closeStatement(stmt); 200 jdbcHelper.closeConnection(conn); 201 } 202 } 203 204 public Comment[] loadComments(CommentVisibility visibility, AuthenticatedUser user) throws RepositoryException { 205 Connection conn = null; 206 PreparedStatement stmt = null; 207 ResultSet rs = null; 208 try { 209 conn = context.getDataSource().getConnection(); 210 StringBuffer query = new StringBuffer (SELECT_COMMENTS); 211 query.append(" where created_by = ?"); 212 if (visibility != null) 213 query.append(" and visibility = '").append(visibility.getCode()).append("'"); 214 query.append(" order by created_on desc"); 215 stmt = conn.prepareStatement(query.toString()); 216 stmt.setLong(1, user.getId()); 217 rs = stmt.executeQuery(); 218 return getCommentsFromResultSet(rs); 219 } catch (Throwable e) { 220 throw new RepositoryException("Problem loading comments.", e); 221 } finally { 222 jdbcHelper.closeStatement(stmt); 223 jdbcHelper.closeConnection(conn); 224 } 225 } 226 227 public Comment[] loadComments(AuthenticatedUser user) throws RepositoryException { 228 return loadComments(null, user); 229 } 230 231 private static final String SELECT_COMMENTS = "select doc_id, branch_id, lang_id, id, comment_text, visibility, created_on, created_by from comments"; 232 233 private Comment[] getCommentsFromResultSet(ResultSet rs) throws SQLException { 234 ArrayList comments = new ArrayList (); 235 while (rs.next()) { 236 CommentImpl comment = new CommentImpl(rs.getLong(1), rs.getLong(2), rs.getLong(3), rs.getLong(4), rs.getString(5), CommentVisibility.getByCode(rs.getString(6)), rs.getTimestamp(7), rs.getLong(8)); 237 comments.add(comment); 238 } 239 return (Comment[])comments.toArray(new Comment[comments.size()]); 240 } 241 } 242 | Popular Tags |