Changeset 1633
- Timestamp:
- 11/30/11 13:18:31 (12 years ago)
- Location:
- ComponentRegistry/branches/jeaferversion/ComponentRegistry/src
- Files:
-
- 2 added
- 14 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/ComponentRegistry.java
r1631 r1633 8 8 import clarin.cmdi.componentregistry.components.CMDComponentSpec; 9 9 import clarin.cmdi.componentregistry.model.AbstractDescription; 10 import clarin.cmdi.componentregistry.model.Comment Mapping.Comment;10 import clarin.cmdi.componentregistry.model.Comment; 11 11 import clarin.cmdi.componentregistry.model.ComponentDescription; 12 12 import clarin.cmdi.componentregistry.model.ProfileDescription; … … 154 154 Comment getSpecifiedCommentInComponent(String commentId) throws ComponentRegistryException; 155 155 156 public void deleteComment(String commentId, Principal principal)throws IOException, ComponentRegistryException, UserUnauthorizedException, 157 DeleteFailedException;; 158 156 159 157 160 -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/ComponentRegistryImplBase.java
r1631 r1633 20 20 import clarin.cmdi.componentregistry.components.CMDComponentSpec.Header; 21 21 import clarin.cmdi.componentregistry.model.AbstractDescription; 22 import clarin.cmdi.componentregistry.model.CommentMapping.Comment;23 22 import clarin.cmdi.componentregistry.model.ComponentDescription; 24 23 import clarin.cmdi.componentregistry.model.ProfileDescription; -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/CommentsDao.java
r1631 r1633 5 5 package clarin.cmdi.componentregistry.impl.database; 6 6 7 import clarin.cmdi.componentregistry.model.Comment Mapping.Comment;7 import clarin.cmdi.componentregistry.model.Comment; 8 8 9 9 import java.util.Date; … … 17 17 import org.slf4j.Logger; 18 18 import org.slf4j.LoggerFactory; 19 import org.springframework.beans.factory.annotation.Autowired; 19 20 import org.springframework.dao.DataAccessException; 20 21 import org.springframework.jdbc.core.simple.ParameterizedRowMapper; 22 import org.springframework.jdbc.core.simple.ParameterizedSingleColumnRowMapper; 21 23 import org.springframework.jdbc.core.simple.SimpleJdbcInsert; 24 import org.springframework.transaction.PlatformTransactionManager; 25 import org.springframework.transaction.TransactionDefinition; 26 import org.springframework.transaction.TransactionStatus; 22 27 23 28 /** … … 26 31 */ 27 32 public class CommentsDao extends ComponentRegistryDao<Comment> { 33 34 @Autowired 35 private PlatformTransactionManager txManager; 36 @Autowired 37 private TransactionDefinition txDefinition; 28 38 private final static Logger LOG = LoggerFactory.getLogger(CommentsDao.class); 29 private final static String SELECT_BASE = "SELECT " + COLUMN_ID + ", comments, comment_date, user_id FROM " + TABLE_COMMENTS; 30 31 // @Autowired 32 // private PlatformTransactionManager txManager; 33 // @Autowired 34 // private TransactionDefinition txDefinition; 35 // public List<Comment> getAllComments() throws DataAccessException { 36 // return getList(SELECT_BASE); 37 // } 38 protected String getTableName() { 39 private final static String SELECT_BASE = "SELECT " + COLUMN_ID + ", comments, comment_date, user_id, " 40 + "component_description_id, profile_description_id FROM " + TABLE_COMMENTS; 41 42 protected String getTableName() { 39 43 return TABLE_COMMENTS; 40 44 } 41 45 42 46 public List<Comment> getAllComments() throws DataAccessException { 43 return getList(SELECT_BASE); 44 } 45 46 47 48 47 return getList(SELECT_BASE); 48 } 49 49 50 /** 50 51 * … … 67 68 68 69 public Comment getSpecifiedCommentFromProfile(String commentId) throws DataAccessException { 69 String select = SELECT_BASE + " WHERE "+ COLUMN_ID + " = " + commentId;70 return getFirstOrNull( select, commentId);70 //String select = SELECT_BASE + " WHERE " + COLUMN_ID + " = ?"; 71 return getFirstOrNull(SELECT_BASE + " WHERE " + COLUMN_ID + " = ?", Integer.parseInt(commentId)); 71 72 } 72 73 … … 77 78 */ 78 79 public Comment getSpecifiedCommentFromComponent(String commentId) throws DataAccessException { 79 String select = SELECT_BASE + " WHERE "+ COLUMN_ID + " = " + commentId;80 return getFirstOrNull( select, commentId);80 //String select = SELECT_BASE + " WHERE " + COLUMN_ID + " = ?"; 81 return getFirstOrNull(SELECT_BASE + " WHERE " + COLUMN_ID + " = ?", Integer.parseInt(commentId)); 81 82 } 82 83 83 84 public List<Comment> getCommentsFromComponent(String componentId) throws DataAccessException { 84 return getList(SELECT_BASE + " WHERE component_description_id =" + componentId); 85 } 86 87 88 public Comment getByComment(String aComment) throws DataAccessException { 89 return getFirstOrNull(SELECT_BASE + " WHERE comments = ?", aComment); 85 return getList(SELECT_BASE + " WHERE component_description_id = ?", componentId); 86 } 87 88 public Comment getByComment(String aComment) throws DataAccessException { 89 return getFirstOrNull(SELECT_BASE + " WHERE comments = ?", aComment); 90 90 } 91 91 … … 96 96 * @return Principal name of description's owner, if any. Otherwise, null. 97 97 */ 98 //public String getOwnerPrincipalName(Number id) {99 //StringBuilder select = new StringBuilder("SELECT principal_name FROM " + TABLE_REGISTRY_USER);100 //select.append(" JOIN ").append(getTableName());101 //select.append(" ON user_id = " + TABLE_REGISTRY_USER + ".id ");102 //select.append(" WHERE ").append(getTableName()).append(".id = ?");103 //List<String> owner = getSimpleJdbcTemplate().query(select.toString(), new ParameterizedSingleColumnRowMapper<String>(), id);104 //if (owner.isEmpty()) {105 //return null;106 //} else {107 //return owner.get(0);108 //}109 //}98 public String getOwnerPrincipalName(Number id) { 99 StringBuilder select = new StringBuilder("SELECT principal_name FROM " + TABLE_REGISTRY_USER); 100 select.append(" JOIN ").append(getTableName()); 101 select.append(" ON user_id = " + TABLE_REGISTRY_USER + ".id "); 102 select.append(" WHERE ").append(getTableName()).append(".id = ?"); 103 List<String> owner = getSimpleJdbcTemplate().query(select.toString(), new ParameterizedSingleColumnRowMapper<String>(), id); 104 if (owner.isEmpty()) { 105 return null; 106 } else { 107 return owner.get(0); 108 } 109 } 110 110 /** 111 111 * … … 121 121 params.put("comments", comment.getComment()); 122 122 params.put("comment_date", extractTimestamp(comment)); 123 params.put("component_description ", comment.getComponentDescriptionId());124 params.put("profile_description ", comment.getProfileDescriptionId());123 params.put("component_description_id", comment.getComponentDescriptionId()); 124 params.put("profile_description_id", comment.getProfileDescriptionId()); 125 125 params.put("user_id", comment.getUserId()); 126 126 return insert.executeAndReturnKey(params); … … 139 139 comment.setId(rs.getString(COLUMN_ID)); 140 140 comment.setComment(rs.getString("comments")); 141 comment.setComponentDescriptionId(rs.getString("component_description_id")); 142 comment.setProfileDescriptionId(rs.getString("profile_description_id")); 143 comment.setUserId(rs.getString("user_id")); 141 144 comment.setCommentDate(commentDate == null ? null : Comment.createNewDate(commentDate.getTime())); 142 145 return comment; … … 163 166 // throw new UnsupportedOperationException("Not yet implemented"); 164 167 // } 165 // void setDeleted(Comment desc, boolean isDeleted) throws DataAccessException { 166 // TransactionStatus transaction = getTransaction(); 167 // Number dbId = getDbId(desc.getId()); 168 // StringBuilder update = new StringBuilder("UPDATE ").append(getTableName()); 169 // update.append(" SET is_deleted = ").append(Boolean.toString(isDeleted)).append(" WHERE " + COLUMN_ID + " = ?"); 170 // getSimpleJdbcTemplate().update(update.toString(), dbId); 171 // txManager.commit(transaction); 172 // } 173 // 174 public Number getDbId(Number cmdId) { 175 StringBuilder query = new StringBuilder("SELECT " + COLUMN_ID + " FROM ").append(getTableName()); 176 query.append(" WHERE ").append(getDbId(cmdId)).append(" = ?"); 177 return getSimpleJdbcTemplate().queryForInt(query.toString(), cmdId); 178 } 179 180 private Timestamp extractTimestamp(Comment comment) { 181 if (comment.getCommentDate() != null) { 182 try { 183 Date date = Comment.getDate(comment.getCommentDate()); 184 return new Timestamp(date.getTime()); 185 } catch (ParseException ex) { 186 LOG.warn("Could not convert registration date " + comment.getCommentDate() + " to date", ex); 187 } catch (IllegalArgumentException ex) { 188 LOG.warn("Could not convert registration date " + comment.getCommentDate() + " to timestamp", ex); 189 } 190 } 191 return null; 192 } 193 // 194 // private TransactionStatus getTransaction() { 195 // return txManager.getTransaction(txDefinition); 196 // } 168 169 public void deleteComment(Comment com, boolean isDeleted) throws DataAccessException { 170 TransactionStatus transaction = getTransaction(); 171 Number dbId = getDbId(Integer.parseInt(com.getId())); 172 StringBuilder delete = new StringBuilder("DELETE ").append(getTableName()); 173 delete.append(" WHERE " + COLUMN_ID + " = ?"); 174 getSimpleJdbcTemplate().update(delete.toString(), dbId); 175 txManager.commit(transaction); 176 } 177 178 public Number getDbId(Number cmdId) { 179 StringBuilder query = new StringBuilder("SELECT " + COLUMN_ID + " FROM ").append(getTableName()); 180 query.append(" WHERE ").append(getDbId(cmdId)).append(" = ?"); 181 return getSimpleJdbcTemplate().queryForInt(query.toString(), cmdId); 182 } 183 184 private Timestamp extractTimestamp(Comment comment) { 185 if (comment.getCommentDate() != null) { 186 try { 187 Date date = Comment.getDate(comment.getCommentDate()); 188 return new Timestamp(date.getTime()); 189 } catch (ParseException ex) { 190 LOG.warn("Could not convert registration date " + comment.getCommentDate() + " to date", ex); 191 } catch (IllegalArgumentException ex) { 192 LOG.warn("Could not convert registration date " + comment.getCommentDate() + " to timestamp", ex); 193 } 194 } 195 return null; 196 } 197 198 private TransactionStatus getTransaction() { 199 return txManager.getTransaction(txDefinition); 200 } 197 201 } -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/ComponentRegistryDbImpl.java
r1631 r1633 3 3 import java.io.ByteArrayInputStream; 4 4 import java.io.ByteArrayOutputStream; 5 import java.io.IOException; 5 6 import java.io.InputStream; 6 7 import java.io.OutputStream; … … 29 30 import clarin.cmdi.componentregistry.impl.ComponentRegistryImplBase; 30 31 import clarin.cmdi.componentregistry.model.AbstractDescription; 31 import clarin.cmdi.componentregistry.model.Comment Mapping.Comment;32 import clarin.cmdi.componentregistry.model.Comment; 32 33 import clarin.cmdi.componentregistry.model.ComponentDescription; 33 34 import clarin.cmdi.componentregistry.model.ProfileDescription; … … 50 51 @Autowired 51 52 private ComponentDescriptionDao componentDescriptionDao; 52 @Autowired 53 @Autowired 53 54 private UserDao userDao; 54 55 @Autowired … … 132 133 return commentsDao.getCommentsFromProfile(profileId); 133 134 } catch (DataAccessException ex) { 135 throw new ComponentRegistryException("Database access error while trying to get list of comments from profile", ex); 136 } 137 } 138 139 @Override 140 public Comment getSpecifiedCommentInProfile(String commentId) throws ComponentRegistryException { 141 try { 142 return commentsDao.getSpecifiedCommentFromProfile(commentId); 143 } catch (DataAccessException ex) { 134 144 throw new ComponentRegistryException("Database access error while trying to get comment from profile", ex); 135 145 } … … 137 147 138 148 @Override 139 public Comment getSpecifiedCommentInProfile(String commentId) throws ComponentRegistryException { 140 try { 141 return commentsDao.getSpecifiedCommentFromProfile(commentId); 149 public List<Comment> getCommentsInComponent(String componentId) throws ComponentRegistryException { 150 try { 151 return commentsDao.getCommentsFromComponent(componentId); 152 } catch (DataAccessException ex) { 153 throw new ComponentRegistryException("Database access error while trying to get list of comments from component", ex); 154 } 155 } 156 157 @Override 158 public Comment getSpecifiedCommentInComponent(String commentId) throws ComponentRegistryException { 159 try { 160 return commentsDao.getSpecifiedCommentFromComponent(commentId); 142 161 } catch (DataAccessException ex) { 143 162 throw new ComponentRegistryException("Database access error while trying to get comment from component", ex); 144 163 } 145 164 } 146 147 @Override148 public List<Comment> getCommentsInComponent(String componentId) throws ComponentRegistryException {149 try {150 return commentsDao.getCommentsFromComponent(componentId);151 } catch (DataAccessException ex) {152 throw new ComponentRegistryException("Database access error while trying to get comment from component", ex);153 }154 }155 156 @Override157 public Comment getSpecifiedCommentInComponent(String commentId) throws ComponentRegistryException {158 try {159 return commentsDao.getSpecifiedCommentFromComponent(commentId);160 } catch (DataAccessException ex) {161 throw new ComponentRegistryException("Database access error while trying to get comment from component", ex);162 }163 }164 165 // @Override166 // public List<Comment> getComments() throws ComponentRegistryException {167 // throw new UnsupportedOperationException("Not supported yet.");168 // }169 165 170 166 @Override … … 212 208 } 213 209 } 214 215 //// @Override216 //// public CMDComponentSpec getMDComment(String id) throws ComponentRegistryException {217 ////// if (inWorkspace(commentsDao, id)) {218 ////// CMDComponentSpec result = commentsCache.get(id);219 ////// if (result == null && !commentsCache.containsKey(id)) {220 ////// result = getUncachedMDComment(id);221 ////// commentsCache.put(id, result);222 ////// }223 ////// return result;224 ////// } else {225 ////// // May exist, but not in this workspace226 //// return null;227 ////// }228 //// }229 //230 //// public CMDComponentSpec getUncachedMDComment(String id) throws ComponentRegistryException {231 //// try {232 //// return getUncachedMDComment(id, commentsDao);233 //// } catch (DataAccessException ex) {234 //// throw new ComponentRegistryException("Database access error while trying to get component", ex);235 // }236 // }237 210 238 211 @Override … … 383 356 } 384 357 } 385 386 // public void deleteMDComment(String commentId, Principal principal) throws UserUnauthorizedException, DeleteFailedException, ComponentRegistryException {387 // Comment desc = getCommentsInComponent(commentId);388 // if(desc != null) {389 // try {390 // checkAuthorisationComment(desc, principal);391 // checkCommentAge(desc, principal);392 // commentsDao.setDeleted(desc, true);393 // invalidateCommentCache(desc);394 // } catch (DataAccessException ex) {395 // throw new DeleteFailedException("Database access error while trying to delete profile", ex);396 // }397 // }398 // }399 358 400 359 @Override … … 450 409 } 451 410 } 452 453 // private void invalidateCommentCache(Comment comment) {454 // commentsCache.remove(comment.getId());455 // }456 411 457 412 private AbstractDescriptionDao<?> getDaoForDescription(AbstractDescription description) { … … 459 414 } 460 415 461 // private CommentsDao getDaoForCommentDescription(Comment comment) {462 // return commentsDao;463 // }464 416 465 417 /** … … 488 440 } 489 441 490 491 // private Number getIdForCommentDescription(Comment comment) throws IllegalArgumentException { 492 // Number dbId = null; 493 // CommentsDao dao = getDaoForCommentDescription(comment); 494 // try { 495 // dbId = dao.getDbId(comment.getId()); 496 // } catch (DataAccessException ex) { 497 // LOG.error("Error getting dbId for comment with id " + comment.getId(), ex); 498 // } 499 // if (dbId == null) { 500 // throw new IllegalArgumentException("Could not get database Id for description"); 501 // } else { 502 // return dbId; 503 // } 504 // } 442 private Number getIdForCommentDescription(Comment comment) throws IllegalArgumentException { 443 Number dbId = null; 444 try { 445 dbId = commentsDao.getDbId(Integer.parseInt(comment.getId())); 446 } catch (DataAccessException ex) { 447 LOG.error("Error getting dbId for comment with id " + comment.getId(), ex); 448 } 449 if (dbId == null) { 450 throw new IllegalArgumentException("Could not get database Id for description"); 451 } else { 452 return dbId; 453 } 454 } 505 455 506 456 private String componentSpecToString(CMDComponentSpec spec) throws UnsupportedEncodingException, JAXBException { … … 527 477 } 528 478 529 // private CMDComponentSpec getUncachedMDComment(String id, CommentsDao dao) {530 // String xml = dao.getContent(false, id);531 // if (xml != null) {532 // try {533 // InputStream is = new ByteArrayInputStream(xml.getBytes("UTF-8"));534 // return MDMarshaller.unmarshal(CMDComponentSpec.class, is, null);535 //536 // } catch (JAXBException ex) {537 // LOG.error(null, ex);538 // } catch (UnsupportedEncodingException ex) {539 // LOG.error(null, ex);540 // }541 // }542 // return null;543 // }544 545 479 private void checkAuthorisation(AbstractDescription desc, Principal principal) throws UserUnauthorizedException { 546 480 if (!isOwnerOfDescription(desc, principal.getName()) && !configuration.isAdminUser(principal)) { … … 550 484 } 551 485 } 552 553 // private void checkAuthorisationComment(Comment desc, Principal principal)throws UserUnauthorizedException {554 //if (!isOwnerOfComment(desc, principal.getName()) && !configuration.isAdminUser(principal)) {555 //throw new UserUnauthorizedException("Unauthorized operation user '" + principal.getName()556 //+ "' is not the creator (nor an administrator) of the " + (desc.getId()) + "(" + desc557 //+ ").");558 //}559 //}486 487 private void checkAuthorisationComment(Comment desc, Principal principal) throws UserUnauthorizedException { 488 if (!isOwnerOfComment(desc, principal.getName()) && !configuration.isAdminUser(principal)) { 489 throw new UserUnauthorizedException("Unauthorized operation user '" + principal.getName() 490 + "' is not the creator (nor an administrator) of the " + (desc.getId()) + "(" + desc 491 + ")."); 492 } 493 } 560 494 561 495 private boolean isOwnerOfDescription(AbstractDescription desc, String principalName) { … … 564 498 && principalName.equals(owner); 565 499 } 500 501 private boolean isOwnerOfComment(Comment desc, String principalName) { 502 String owner = commentsDao.getOwnerPrincipalName(getIdForCommentDescription(desc)); 503 return owner != null // If owner is null, no one can be owner 504 && principalName.equals(owner); 505 } 566 506 567 // private boolean isOwnerOfComment(Comment desc, String principalName) {568 // String owner = getDaoForCommentDescription(desc).getOwnerPrincipalName(getIdForCommentDescription(desc));569 // return owner != null // If owner is null, no one can be owner570 // && principalName.equals(owner);571 // }572 573 507 private void checkAge(AbstractDescription desc, Principal principal) throws DeleteFailedException { 574 508 if (isPublic() && !configuration.isAdminUser(principal)) { … … 588 522 } 589 523 } 590 591 //private void checkCommentAge(Comment desc, Principal principal) throws DeleteFailedException {592 //if (isPublic() && !configuration.isAdminUser(principal)) {593 //try {594 //Date regDate = Comment.getDate(desc.getCommentDate());595 //Calendar calendar = Calendar.getInstance();596 //calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);597 //if (regDate.before(calendar.getTime())) { // More then month old598 //throw new DeleteFailedException(599 //"The "600 //+ (desc.getId())601 //+ " is more then a month old and cannot be deleted anymore.");602 //}603 //} catch (ParseException e) {604 //LOG.error("Cannot parse date of " + desc + " Error:" + e);605 //}606 //}607 //}524 525 private void checkCommentAge(Comment desc, Principal principal) throws DeleteFailedException { 526 if (isPublic() && !configuration.isAdminUser(principal)) { 527 try { 528 Date regDate = Comment.getDate(desc.getCommentDate()); 529 Calendar calendar = Calendar.getInstance(); 530 calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); 531 if (regDate.before(calendar.getTime())) { // More then month old 532 throw new DeleteFailedException( 533 "The " 534 + (desc.getId()) 535 + " is more then a month old and cannot be deleted anymore."); 536 } 537 } catch (ParseException e) { 538 LOG.error("Cannot parse date of " + desc + " Error:" + e); 539 } 540 } 541 } 608 542 609 543 private boolean inWorkspace(AbstractDescriptionDao<?> dao, String cmdId) { … … 622 556 // } 623 557 // } 624 625 558 @Override 626 559 public String getName() { … … 641 574 return componentDescriptionDao.getDeletedDescriptions(getUserId()); 642 575 } 643 } 576 577 @Override 578 public void deleteComment(String commentId, Principal principal) throws IOException, 579 ComponentRegistryException, UserUnauthorizedException, DeleteFailedException { 580 581 Comment com = commentsDao.getById(commentId); 582 if (com!= null) { 583 try { 584 checkAuthorisationComment(com, principal); 585 checkCommentAge(com, principal); 586 commentsDao.deleteComment(com, true); 587 } catch (DataAccessException ex) { 588 throw new DeleteFailedException("Database access error while trying to delete component", ex); 589 } 590 } 591 } 592 } -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/model/Comment.java
r1631 r1633 5 5 package clarin.cmdi.componentregistry.model; 6 6 7 import clarin.cmdi.componentregistry.ComponentRegistry; 8 import clarin.cmdi.componentregistry.IdSequence; 7 9 import java.text.ParseException; 10 import java.util.ArrayList; 8 11 import java.util.Date; 12 import java.util.List; 9 13 import javax.xml.bind.annotation.XmlAccessType; 10 14 import javax.xml.bind.annotation.XmlAccessorType; 15 import javax.xml.bind.annotation.XmlElement; 16 import javax.xml.bind.annotation.XmlRootElement; 11 17 import org.apache.commons.lang.time.DateFormatUtils; 12 18 import org.apache.commons.lang.time.DateUtils; … … 16 22 * @author jeafer 17 23 */ 18 public class CommentMapping { 19 20 @XmlAccessorType(XmlAccessType.FIELD) 21 public static class Comment {22 24 @XmlRootElement(name = "comment") 25 @XmlAccessorType(XmlAccessType.FIELD) 26 public class Comment { 27 @XmlElement(name = "Comment_Type") 28 protected List<Comment> commentType; 23 29 private String comments; 24 30 private String commentDate; … … 28 34 private String id; 29 35 36 37 public List<Comment> getComments() { 38 if (commentType == null) { 39 commentType = new ArrayList<Comment>(); 40 } 41 return this.commentType; 42 } 43 30 44 public void setComment(String comment) { 31 45 this.comments = comment; … … 47 61 this.id = commentId; 48 62 } 49 50 63 51 64 public String getId() { … … 69 82 } 70 83 71 public void setUserI D(String userId) {84 public void setUserId(String userId) { 72 85 this.userId = userId; 73 86 } 74 75 87 76 88 public String getUserId() { … … 81 93 return createNewDate(new Date().getTime()); 82 94 } 83 84 85 return DateUtils.parseDate(registrationDate, new String[] { DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern()});86 }95 96 public static Date getDate(String registrationDate) throws ParseException { 97 return DateUtils.parseDate(registrationDate, new String[]{DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern()}); 98 } 87 99 88 100 public static String createNewDate(long time) { 89 101 return DateFormatUtils.formatUTC(time, DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern()); 90 102 } 103 104 public static Comment createANewComment() { 105 String id = ComponentRegistry.REGISTRY_ID + "p_" + IdSequence.get(); 106 Comment com = new Comment(); 107 com.setId(id); 108 com.setCommentDate(createNewDate()); 109 return com; 91 110 } 92 }111 } -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/model/RegisterResponse.java
r426 r1633 33 33 @XmlElement 34 34 private AbstractDescription description; 35 @XmlElement 36 private Comment comment; 35 37 36 38 … … 58 60 this.description = description; 59 61 } 62 63 public void setComment(Comment comment) { 64 this.comment = comment; 65 } 60 66 61 67 public AbstractDescription getDescription() { -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/ComponentRegistryRestService.java
r1631 r1633 41 41 import clarin.cmdi.componentregistry.components.CMDComponentSpec; 42 42 import clarin.cmdi.componentregistry.model.AbstractDescription; 43 import clarin.cmdi.componentregistry.model.Comment Mapping.Comment;43 import clarin.cmdi.componentregistry.model.Comment; 44 44 import clarin.cmdi.componentregistry.model.ComponentDescription; 45 45 import clarin.cmdi.componentregistry.model.ProfileDescription; … … 237 237 238 238 @GET 239 @Path("/profile /{profileId}/comments")240 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 241 public List<Comment> get RegisteredCommentsFromProfile(@PathParam("profileId") String profileId, @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) throws ComponentRegistryException {239 @Path("/profiles/{profileId}/comments") 240 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 241 public List<Comment> getCommentsFromProfile(@PathParam("profileId") String profileId, @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) throws ComponentRegistryException { 242 242 long start = System.currentTimeMillis(); 243 243 List<Comment> comments = getRegistry(userspace).getCommentsInProfile(profileId); … … 248 248 249 249 @GET 250 @Path("/component /{componentId}/comments")251 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 252 public List<Comment> get RegisteredCommentsFromComponent(@PathParam("componentId") String componentId, @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) throws ComponentRegistryException {250 @Path("/components/{componentId}/comments") 251 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 252 public List<Comment> getCommentsFromComponent(@PathParam("componentId") String componentId, @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) throws ComponentRegistryException { 253 253 long start = System.currentTimeMillis(); 254 254 List<Comment> comments = getRegistry(userspace).getCommentsInComponent(componentId); … … 259 259 260 260 @GET 261 @Path("/profiles/{profileId}/comments/{comment sId}")261 @Path("/profiles/{profileId}/comments/{commentId}") 262 262 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 263 263 public Comment getSpecifiedCommentFromProfile(@PathParam("commentId") String commentId, @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) throws ComponentRegistryException { … … 268 268 269 269 @GET 270 @Path("/component /{componentId}/comments/{commentsId}")271 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 272 public Comment get RegisteredCommentFromComponent(@PathParam("commentId") String commentId, @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) throws ComponentRegistryException {270 @Path("/components/{componentId}/comments/{commentId}") 271 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 272 public Comment getSpecifiedCommentFromComponent(@PathParam("commentId") String commentId, @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) throws ComponentRegistryException { 273 273 LOG.info(" Comments of component with id" + commentId + " are requested."); 274 274 return getRegistry(userspace).getSpecifiedCommentInComponent(commentId); … … 294 294 } 295 295 296 //@POST297 //@Path("/profiles/{profileId}/comments/{commentId}")298 // public Response manipulateRegisteredCommentFromProfile(@PathParam("profileId") String commentId, @FormParam("method") String method,299 //@QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) {300 //if ("delete".equalsIgnoreCase(method)) {301 // return deleteRegisteredProfile(commentId, userspace);302 //} else {303 //return Response.ok().build();304 //}305 //}306 // 307 //@POST308 //@Path("/components/{componentId}/comments/{commentId}")309 // public Response manipulateRegisteredCommentFromComponent(@PathParam("profileId") String commentId, @FormParam("method") String method,310 //@QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) {311 //if ("delete".equalsIgnoreCase(method)) {312 // return deleteRegisteredProfile(commentId, userspace);313 //} else {314 //return Response.ok().build();315 //}316 //}296 @POST 297 @Path("/profiles/{profileId}/comments/{commentId}") 298 public Response manipulateCommentFromProfile(@PathParam("profileId") String commentId, @FormParam("method") String method, 299 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) { 300 if ("delete".equalsIgnoreCase(method)) { 301 return deleteCommentFromProfile(commentId, userspace); 302 } else { 303 return Response.ok().build(); 304 } 305 } 306 307 @POST 308 @Path("/components/{componentId}/comments/{commentId}") 309 public Response manipulateCommentFromComponent(@PathParam("profileId") String commentId, @FormParam("method") String method, 310 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) { 311 if ("delete".equalsIgnoreCase(method)) { 312 return deleteCommentFromComponent(commentId, userspace); 313 } else { 314 return Response.ok().build(); 315 } 316 } 317 317 318 318 @POST … … 486 486 } 487 487 488 //@DELETE489 //@Path("/profiles/{profileId}/comments/{commentId}")490 // public Response deleteRegisteredCommentFromProfile(@PathParam("commentId") String commentId,491 //@QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) {492 //Principal principal = checkAndGetUserPrincipal();493 //LOG.info("Comment with id: " + commentId + " set for deletion.");494 //try {495 // getRegistry(userspace).deleteMDComment(commentId, principal);496 //} catch (DeleteFailedException e) {497 //LOG.info("Comment with id: " + commentId + " deletion failed: " + e.getMessage());498 //return Response.serverError().status(Status.FORBIDDEN).entity("" + e.getMessage()).build();499 //} catch (ComponentRegistryException e) {500 //LOG.info("Could not retrieve component", e);501 //return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build();502 //} catch (IOException e) {503 //LOG.info("Comment with id: " + commentId + " deletion failed.", e);504 //return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build();505 //} catch (UserUnauthorizedException e) {506 //LOG.info("Comment with id: " + commentId + " deletion failed: " + e.getMessage());507 //return Response.serverError().status(Status.UNAUTHORIZED).entity("" + e.getMessage()).build();508 //}509 //LOG.info("Profile with id: " + commentId + " deleted.");510 //return Response.ok().build();511 //}512 // 513 //@DELETE514 //@Path("/components/{componentId}/comments/{commentId}")515 // public Response deleteRegisteredCommentFromComponent(@PathParam("commentId") String commentId,516 //@QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) {517 //Principal principal = checkAndGetUserPrincipal();518 //LOG.info("Comment with id: " + commentId + " set for deletion.");519 //try {520 // getRegistry(userspace).deleteMDComment(commentId, principal);521 //} catch (DeleteFailedException e) {522 //LOG.info("Comment with id: " + commentId + " deletion failed: " + e.getMessage());523 //return Response.serverError().status(Status.FORBIDDEN).entity("" + e.getMessage()).build();524 //} catch (ComponentRegistryException e) {525 //LOG.info("Could not retrieve component", e);526 //return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build();527 //} catch (IOException e) {528 //LOG.info("Comment with id: " + commentId + " deletion failed.", e);529 //return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build();530 //} catch (UserUnauthorizedException e) {531 //LOG.info("Comment with id: " + commentId + " deletion failed: " + e.getMessage());532 //return Response.serverError().status(Status.UNAUTHORIZED).entity("" + e.getMessage()).build();533 //}534 //LOG.info("Profile with id: " + commentId + " deleted.");535 //return Response.ok().build();536 //}488 @DELETE 489 @Path("/profiles/{profileId}/comments/{commentId}") 490 public Response deleteCommentFromProfile(@PathParam("commentId") String commentId, 491 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) { 492 Principal principal = checkAndGetUserPrincipal(); 493 LOG.info("Comment with id: " + commentId + " set for deletion."); 494 try { 495 getRegistry(userspace).deleteComment(commentId, principal); 496 } catch (DeleteFailedException e) { 497 LOG.info("Comment with id: " + commentId + " deletion failed: " + e.getMessage()); 498 return Response.serverError().status(Status.FORBIDDEN).entity("" + e.getMessage()).build(); 499 } catch (ComponentRegistryException e) { 500 LOG.info("Could not retrieve component", e); 501 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 502 } catch (IOException e) { 503 LOG.info("Comment with id: " + commentId + " deletion failed.", e); 504 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 505 } catch (UserUnauthorizedException e) { 506 LOG.info("Comment with id: " + commentId + " deletion failed: " + e.getMessage()); 507 return Response.serverError().status(Status.UNAUTHORIZED).entity("" + e.getMessage()).build(); 508 } 509 LOG.info("Profile with id: " + commentId + " deleted."); 510 return Response.ok().build(); 511 } 512 513 @DELETE 514 @Path("/components/{componentId}/comments/{commentId}") 515 public Response deleteCommentFromComponent(@PathParam("commentId") String commentId, 516 @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) { 517 Principal principal = checkAndGetUserPrincipal(); 518 LOG.info("Comment with id: " + commentId + " set for deletion."); 519 try { 520 getRegistry(userspace).deleteComment(commentId, principal); 521 } catch (DeleteFailedException e) { 522 LOG.info("Comment with id: " + commentId + " deletion failed: " + e.getMessage()); 523 return Response.serverError().status(Status.FORBIDDEN).entity("" + e.getMessage()).build(); 524 } catch (ComponentRegistryException e) { 525 LOG.info("Could not retrieve component", e); 526 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 527 } catch (IOException e) { 528 LOG.info("Comment with id: " + commentId + " deletion failed.", e); 529 return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build(); 530 } catch (UserUnauthorizedException e) { 531 LOG.info("Comment with id: " + commentId + " deletion failed: " + e.getMessage()); 532 return Response.serverError().status(Status.UNAUTHORIZED).entity("" + e.getMessage()).build(); 533 } 534 LOG.info("Profile with id: " + commentId + " deleted."); 535 return Response.ok().build(); 536 } 537 537 538 538 @GET … … 640 640 LOG.info("Trying to register Component: " + desc); 641 641 return register(input, desc, userCredentials, userspace, new NewAction()); 642 } 643 644 @POST 645 @Path("/components/{componentId}/comments") 646 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 647 @Consumes("multipart/form-data") 648 public Response registerCommentInComponent(@FormDataParam(DATA_FORM_FIELD) InputStream input, @FormDataParam(NAME_FORM_FIELD) String comment, 649 @FormDataParam("componentId") AbstractDescription componentId, @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) { 650 Principal principal = checkAndGetUserPrincipal(); 651 UserCredentials userCredentials = getUserCredentials(principal); 652 Comment com = createNewComment(); 653 com.setComponentDescriptionId("componentId"); 654 com.setUserId(userCredentials.getPrincipalName()); // Hash used to be created here, now Id is constructed by impl 655 com.setComment(comment); 656 LOG.info("Trying to register Comment: " + com); 657 return registerComment(input, com, userCredentials, userspace, componentId, new NewAction()); 642 658 } 643 659 … … 691 707 } 692 708 709 private Response registerComment(InputStream input, Comment com, UserCredentials userCredentials, boolean userspace, 710 AbstractDescription descriptionId, 711 RegisterAction action) { 712 try { 713 ComponentRegistry registry = getRegistry(userspace, userCredentials); 714 CommentValidator commentValidator = new CommentValidator(com); 715 ComValidator validator = new ComValidator(input, com, registry, getRegistry(true), descriptionId 716 ); 717 RegisterResponse response = new RegisterResponse(); 718 response.setIsInUserSpace(userspace); 719 validate(response, validator); 720 if (response.getErrors().isEmpty()) { 721 Comment spec = validator.getCommentSpec(); 722 int returnCode = action.executeComment(com, spec, response, registry); 723 if (returnCode == 0) { 724 response.setRegistered(true); 725 response.setComment(com); 726 } else { 727 response.setRegistered(false); 728 response.addError("Unable to register at this moment. Internal server error."); 729 } 730 } else { 731 LOG.info("Registration failed with validation errors:" + Arrays.toString(response.getErrors().toArray())); 732 response.setRegistered(false); 733 } 734 return Response.ok(response).build(); 735 } finally { 736 try { 737 input.close();//either we read the input or there was an exception, we need to close it. 738 } catch (IOException e) { 739 LOG.error("Error when closing inputstream: ", e); 740 } 741 } 742 } 743 693 744 private ComponentDescription createNewComponentDescription() { 694 745 ComponentDescription desc = ComponentDescription.createNewDescription(); … … 701 752 desc.setHref(createXlink(desc.getId())); 702 753 return desc; 754 } 755 756 private Comment createNewComment() { 757 Comment com = Comment.createANewComment(); 758 return com; 703 759 } 704 760 -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/DescriptionValidator.java
r127 r1633 16 16 this.desc = desc; 17 17 } 18 18 19 19 public List<String> getErrorMessages() { 20 20 return errorMessages; … … 27 27 return errorMessages.isEmpty(); 28 28 } 29 29 30 30 private boolean isOk(String... fields) { 31 31 boolean isOk = true; -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/NewAction.java
r1087 r1633 4 4 import clarin.cmdi.componentregistry.components.CMDComponentSpec; 5 5 import clarin.cmdi.componentregistry.model.AbstractDescription; 6 import clarin.cmdi.componentregistry.model.Comment; 6 7 import clarin.cmdi.componentregistry.model.RegisterResponse; 7 8 … … 13 14 } 14 15 16 @Override 17 public int executeComment(Comment com, Comment spec, RegisterResponse response, ComponentRegistry registry) { 18 throw new UnsupportedOperationException("Not supported yet."); 19 } 20 15 21 } -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/PublishAction.java
r1087 r1633 1 1 package clarin.cmdi.componentregistry.rest; 2 2 3 import clarin.cmdi.componentregistry.model.Comment; 3 4 import java.security.Principal; 4 5 … … 22 23 } 23 24 25 @Override 26 public int executeComment(Comment com, Comment spec, RegisterResponse response, ComponentRegistry registry) { 27 throw new UnsupportedOperationException("Not supported yet."); 28 } 29 24 30 } -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/RegisterAction.java
r1087 r1633 4 4 import clarin.cmdi.componentregistry.components.CMDComponentSpec; 5 5 import clarin.cmdi.componentregistry.model.AbstractDescription; 6 import clarin.cmdi.componentregistry.model.Comment; 6 7 import clarin.cmdi.componentregistry.model.RegisterResponse; 7 8 … … 10 11 int execute(AbstractDescription desc, CMDComponentSpec spec, RegisterResponse response, ComponentRegistry registry); 11 12 13 int executeComment(Comment com, Comment spec, RegisterResponse response, ComponentRegistry registry); 14 12 15 } -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/UpdateAction.java
r1364 r1633 1 1 package clarin.cmdi.componentregistry.rest; 2 2 3 import clarin.cmdi.componentregistry.model.Comment; 3 4 import java.security.Principal; 4 5 … … 21 22 } 22 23 24 @Override 25 public int executeComment(Comment com, Comment spec, RegisterResponse response, ComponentRegistry registry) { 26 throw new UnsupportedOperationException("Not supported yet."); 27 } 28 23 29 } -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/sql/create.sql
r1631 r1633 137 137 138 138 -- 139 -- Name: fki_comments_profile ; Type: INDEX; Schema: public; Owner: -; Tablespace:140 -- 141 142 CREATE INDEX fki_comments_profile ON comments USING btree (profile_description_id);143 144 -- 145 -- Name: fki_comments_component ; Type: INDEX; Schema: public; Owner: -; Tablespace:146 -- 147 148 CREATE INDEX fki_comments_component ON comments USING btree (component_description_id);139 -- Name: fki_comments_profile_id; Type: INDEX; Schema: public; Owner: -; Tablespace: 140 -- 141 142 CREATE INDEX fki_comments_profile_id ON comments USING btree (profile_description_id); 143 144 -- 145 -- Name: fki_comments_component_id; Type: INDEX; Schema: public; Owner: -; Tablespace: 146 -- 147 148 CREATE INDEX fki_comments_component_id ON comments USING btree (component_description_id); 149 149 150 150 … … 178 178 179 179 -- 180 -- Name: comments_profile ; Type: FK CONSTRAINT; Schema: public; Owner: -180 -- Name: comments_profile_id; Type: FK CONSTRAINT; Schema: public; Owner: - 181 181 -- 182 182 183 183 ALTER TABLE ONLY comments 184 ADD CONSTRAINT comments_profile FOREIGN KEY (profile_description_id) REFERENCES profile_description(profile_id);184 ADD CONSTRAINT comments_profile_id FOREIGN KEY (profile_description_id) REFERENCES profile_description(profile_id); 185 185 186 186 … … 194 194 195 195 -- 196 -- Name: comments_component ; Type: FK CONSTRAINT; Schema: public; Owner: -196 -- Name: comments_component_id; Type: FK CONSTRAINT; Schema: public; Owner: - 197 197 -- 198 198 199 199 ALTER TABLE ONLY comments 200 ADD CONSTRAINT comments_component FOREIGN KEY (component_description_id) REFERENCES component_description(component_id);200 ADD CONSTRAINT comments_component_id FOREIGN KEY (component_description_id) REFERENCES component_description(component_id); 201 201 202 202 -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/impl/database/CommentsDaoTest.java
r1631 r1633 1 1 package clarin.cmdi.componentregistry.impl.database; 2 2 3 import clarin.cmdi.componentregistry.model.Comment Mapping;4 import clarin.cmdi.componentregistry.model.Comment Mapping.Comment;3 import clarin.cmdi.componentregistry.model.Comment; 4 import clarin.cmdi.componentregistry.model.Comment; 5 5 import java.util.List; 6 6 import org.junit.runner.RunWith; … … 73 73 @Test 74 74 public void testGetCommentsFromProfile() { 75 75 List<Comment> descriptions = commentsDao.getCommentsFromProfile(TEST_COMMENT_PROFILE_ID); 76 assertNotNull(descriptions); 77 } 78 79 @Test 80 public void testGetCommentFromComponent(){ 81 List<Comment> descriptions = commentsDao.getCommentsFromComponent(TEST_COMMENT_COMPONENT_ID); 82 assertNotNull(descriptions); 76 83 } 77 84 … … 95 102 testComment.setCommentDate(TEST_COMMENT_DATE); 96 103 testComment.setId(TEST_COMMENT_ID); 104 //testComment.setComponentDescriptionId(TEST_COMMENT_COMPONENT_ID); 105 testComment.setProfileDescriptionId(TEST_COMMENT_PROFILE_ID); 106 testComment.setUserId(TEST_COMMENT_USER_ID); 97 107 return testComment; 98 108 } 109 110 public void testSetDelete(){ 111 Comment testComment = createTestComment(); 112 int count = commentsDao.getAllComments().size(); 113 114 commentsDao.insertComment(testComment); 115 assertEquals(count + 1, commentsDao.getAllComments().size()); 116 117 commentsDao.deleteComment(testComment, true); 118 assertEquals(count, commentsDao.getAllComments().size()); 119 120 assertNull(commentsDao.getByComment("NOT_EXISTING_COMMENT_NAME")); 121 } 122 99 123 public final static String TEST_COMMENT_ID = "1"; 124 public final static String TEST_COMMENT_PROFILE_ID = "clarin.eu:cr1:p_1297242111880"; 125 public final static String TEST_COMMENT_COMPONENT_ID = "clarin.eu:cr1:c_1290431694600"; 100 126 public final static String TEST_COMMENT_NAME = "test"; 127 public final static String TEST_COMMENT_USER_ID = "8"; 101 128 public final static String TEST_COMMENT_DATE = Comment.createNewDate(); 102 129 } -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/impl/database/ComponentRegistryDbImplTest.java
r1603 r1633 348 348 return description; 349 349 } 350 350 351 351 private ComponentRegistry getComponentRegistryForUser(Number userId) { 352 352 ComponentRegistryDbImpl componentRegistry = componentRegistryBeanFactory.getNewComponentRegistry();
Note: See TracChangeset
for help on using the changeset viewer.