Changeset 1639
- Timestamp:
- 12/07/11 10:24:43 (12 years ago)
- Location:
- ComponentRegistry/branches/jeaferversion/ComponentRegistry/src
- Files:
-
- 1 deleted
- 14 edited
Legend:
- Unmodified
- Added
- Removed
-
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/ComponentRegistry.java
r1635 r1639 32 32 */ 33 33 List<ProfileDescription> getProfileDescriptions() throws ComponentRegistryException; 34 35 36 //List<Comment> getComments() throws ComponentRegistryException;37 34 38 35 /** … … 48 45 49 46 CMDComponentSpec getMDComponent(String id) throws ComponentRegistryException; 50 //CMDComponentSpec getMDComment(String id) throws ComponentRegistryException;51 47 52 48 /** … … 56 52 int register(AbstractDescription desc, CMDComponentSpec spec); 57 53 58 59 int registerComment(Comment comment, Comment spec); 54 /** 55 * 56 * @param comment 57 * @return -1 if comment could not be registered 58 */ 59 int registerComment(Comment comment, String userId); 60 60 /** 61 61 * … … 105 105 DeleteFailedException; 106 106 107 108 //void deleteMDComment(String commentId, Principal principal)throws IOException, UserUnauthorizedException, ComponentRegistryException, DeleteFailedException;109 107 /** 110 108 * … … 147 145 148 146 147 /** 148 * 149 * @param profileId 150 * @return List of comments fro a specific profile 151 * @throws ComponentRegistryException 152 */ 149 153 List<Comment> getCommentsInProfile(String profileId) throws ComponentRegistryException; 150 154 151 152 155 /** 156 * 157 * @param commentId 158 * @return a specific comment from a profile 159 * @throws ComponentRegistryException 160 */ 153 161 Comment getSpecifiedCommentInProfile(String commentId) throws ComponentRegistryException; 154 162 163 /** 164 * 165 * @param componentId 166 * @return List of comments from a specific component 167 * @throws ComponentRegistryException 168 */ 155 169 List<Comment> getCommentsInComponent(String componentId) throws ComponentRegistryException; 170 171 /** 172 * 173 * @param commentId 174 * @return a specific comment from a component 175 * @throws ComponentRegistryException 176 */ 156 177 Comment getSpecifiedCommentInComponent(String commentId) throws ComponentRegistryException; 157 178 179 /** 180 * 181 * @param commentId 182 * @param principal 183 * @throws IOException 184 * @throws ComponentRegistryException 185 * @throws UserUnauthorizedException 186 * @throws DeleteFailedException 187 */ 158 188 public void deleteComment(String commentId, Principal principal)throws IOException, ComponentRegistryException, UserUnauthorizedException, 159 189 DeleteFailedException;; -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/ComponentRegistryImplBase.java
r1635 r1639 56 56 return result; 57 57 } 58 59 // public List<Comment> getUsageInComments(String componentId) throws ComponentRegistryException {60 // List<Comment> result = new ArrayList<Comment>();61 // for (Comment comment : getComments()) {62 // CMDComponentSpec myComment = getMDComment(comment.getId());63 // if (myComment != null && findComponentId(componentId, myComment.getCMDComponent())) {64 // result.add(comment);65 // System.out.println("get unsage in comments : " + myComment + "\n");66 // }67 // }68 //69 // return result;70 // }71 58 72 59 /** -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/CommentsDao.java
r1635 r1639 1 /*2 * To change this template, choose Tools | Templates3 * and open the template in the editor.4 */5 1 package clarin.cmdi.componentregistry.impl.database; 6 2 … … 12 8 import java.sql.Timestamp; 13 9 import java.text.ParseException; 14 import java.util.Collections;15 10 import java.util.HashMap; 16 11 import java.util.List; … … 29 24 /** 30 25 * 31 * @author jea fer26 * @author jean-charles FerriÚres <jean-charles.ferrieres@mpi.nl> 32 27 */ 33 28 public class CommentsDao extends ComponentRegistryDao<Comment> { … … 45 40 } 46 41 42 protected String getCMDIdColumn() { 43 return COLUMN_ID; 44 } 45 46 /** 47 * Get the list of all the comments available in the database 48 * The distinction between profile or component is not treated in this method 49 * @return list of Comments 50 */ 47 51 public List<Comment> getAllComments() throws DataAccessException { 48 52 return getList(SELECT_BASE); … … 50 54 51 55 /** 52 * 56 * Look for a specified Comment through its id 53 57 * @param id Database record id (key) 54 58 * @return Comment, if it exists … … 60 64 61 65 /** 62 * @param id Database record id (key)63 * Method to retrieve comments from profiles64 * @return Comments, comments_date, user_id and id66 * Retrieve the comments related to a certain profile 67 * @param id Database record id (fkey) 68 * @return list of Comments 65 69 */ 66 70 public List<Comment> getCommentsFromProfile(String profileId) throws DataAccessException { … … 68 72 } 69 73 74 /** 75 * Retrieve a specific comment related to a profile 76 * @param id Database record id (key) 77 * @return Comment 78 * @throws DataAccessException 79 */ 70 80 public Comment getSpecifiedCommentFromProfile(String commentId) throws DataAccessException { 71 81 //String select = SELECT_BASE + " WHERE " + COLUMN_ID + " = ?"; … … 74 84 75 85 /** 76 * @param id Database record id (key) 77 * Method to retrieve comments from components 78 * @return Comments, comments_date, user_id and id 79 */ 80 public Comment getSpecifiedCommentFromComponent(String commentId) throws DataAccessException { 81 //String select = SELECT_BASE + " WHERE " + COLUMN_ID + " = ?"; 82 return getFirstOrNull(SELECT_BASE + " WHERE " + COLUMN_ID + " = ?", Integer.parseInt(commentId)); 83 } 84 86 * Retrieve the comments related to a certain component 87 * @param id Database record id (fkey) 88 * @return list of Comments 89 * @throws DataAccessException 90 */ 85 91 public List<Comment> getCommentsFromComponent(String componentId) throws DataAccessException { 86 92 return getList(SELECT_BASE + " WHERE component_description_id = ?", componentId); 87 93 } 88 94 89 public Comment getByComment(String aComment) throws DataAccessException { 90 return getFirstOrNull(SELECT_BASE + " WHERE comments = ?", aComment); 95 /** 96 * Retrieve a specific comment related to a component 97 * @param id Database record id (key) 98 * @return Comment 99 * @throws DataAccessException 100 */ 101 public Comment getSpecifiedCommentFromComponent(String commentId) throws DataAccessException { 102 return getFirstOrNull(SELECT_BASE + " WHERE " + COLUMN_ID + " = ?", Integer.parseInt(commentId)); 91 103 } 92 104 … … 105 117 if (owner.isEmpty()) { 106 118 return null; 107 } else { 119 } 120 { 108 121 return owner.get(0); 109 122 } … … 113 126 * 114 127 * @param comment 128 * the comment to be inserted 129 * @param content 130 * 131 * @param userId 132 * the id of the user 115 133 * @return Record id of the inserted comment 116 134 * @throws DataAccessException … … 121 139 SimpleJdbcInsert insert = new SimpleJdbcInsert(getDataSource()).withTableName(TABLE_COMMENTS).usingGeneratedKeyColumns( 122 140 COLUMN_ID); 123 //Number contentId = insert.executeAndReturnKey(Collections.singletonMap("content", (Object) content));124 125 141 SimpleJdbcInsert insertComment = new SimpleJdbcInsert(getDataSource()).withTableName(getTableName()).usingGeneratedKeyColumns(COLUMN_ID); 126 142 Map<String, Object> params = new HashMap<String, Object>(); 127 putInsertComment(params, comment, content,userId);143 putInsertComment(params, comment, userId); 128 144 129 145 Number id = insertComment.executeAndReturnKey(params); … … 136 152 } 137 153 138 protected void putInsertComment(Map<String, Object> params, Comment comment, String contentId, Number userId) throws DataAccessException { 139 // SimpleJdbcInsert insert = new SimpleJdbcInsert(getDataSource()).withTableName(TABLE_COMMENTS).usingGeneratedKeyColumns( 140 // COLUMN_ID); 154 /** 155 * 156 * @param params 157 * @param comment 158 * @param contentId 159 * @param userId 160 * @throws DataAccessException 161 */ 162 protected void putInsertComment(Map<String, Object> params, Comment comment, Number userId) throws DataAccessException { 141 163 params.put("comments", comment.getComment()); 142 164 params.put("comment_date", extractTimestamp(comment)); 143 165 params.put("component_description_id", comment.getComponentDescriptionId()); 144 166 params.put("profile_description_id", comment.getProfileDescriptionId()); 145 params.put("user_id", comment.getUserId());167 params.put("user_id", userId); 146 168 } 147 169 … … 165 187 } 166 188 }; 167 // protected String getTableName() { 168 // throw new UnsupportedOperationException("Not supported yet."); 169 // } 170 // 171 // 172 // protected String getCMDIdColumn() { 173 // throw new UnsupportedOperationException("Not supported yet."); 174 // } 175 // 176 // String getContent(boolean b, String id) { 177 // throw new UnsupportedOperationException("Not yet implemented"); 178 // } 179 // 180 // boolean isPublic(String cmdId) { 181 // throw new UnsupportedOperationException("Not yet implemented"); 182 // } 183 // 184 // boolean isInUserSpace(String cmdId, Number userId) { 185 // throw new UnsupportedOperationException("Not yet implemented"); 186 // } 187 188 public void deleteComment(Comment com, boolean isDeleted) throws DataAccessException { 189 190 /** 191 * Method that will delete a comment from the database based on its id 192 * @param com 193 * comment to be deleted 194 * @throws DataAccessException 195 */ 196 public void deleteComment(Comment com) throws DataAccessException { 189 197 TransactionStatus transaction = getTransaction(); 190 Number dbId = getDbId(Integer.parseInt(com.getId()));191 StringBuilder delete = new StringBuilder("DELETE ").append(getTableName());198 Number dbId = Integer.parseInt(com.getId()); 199 StringBuilder delete = new StringBuilder("DELETE FROM ").append(getTableName()); 192 200 delete.append(" WHERE " + COLUMN_ID + " = ?"); 193 201 getSimpleJdbcTemplate().update(delete.toString(), dbId); … … 195 203 } 196 204 197 public Number getDbId(Number cmdId) { 205 /** 206 * Retrieve comment Id From a comment 207 * @param cmdId 208 * @return query for database access to id 209 */ 210 public Number getDbId(String cmdId) { 198 211 StringBuilder query = new StringBuilder("SELECT " + COLUMN_ID + " FROM ").append(getTableName()); 199 query.append(" WHERE ").append( getDbId(cmdId)).append(" = ?");212 query.append(" WHERE ").append(COLUMN_ID).append(" = ?"); 200 213 return getSimpleJdbcTemplate().queryForInt(query.toString(), cmdId); 201 214 } 202 // public Comment getDbId(Number cmId) {203 // return getFirstOrNull(getSelectStatement("WHERE id=?"), cmId);204 // }205 //206 // private StringBuilder getSelectStatement(String... where) throws DataAccessException {207 // StringBuilder select = new StringBuilder("SELECT ").append(getCommentColumnList());208 // select.append(" FROM ").append(getTableName());209 // if (where.length > 0) {210 // select.append(" ");211 // for (String str : where) {212 // select.append(" ").append(str);213 // }214 // }215 // return select;216 // }217 //218 // protected StringBuilder getCommentColumnList() {219 //220 // StringBuilder sb = new StringBuilder();221 // sb.append(getOrderByColumn());222 // sb.append(",comment,comment_date,user_id,");223 // return sb;224 // }225 //226 // private String getOrderByColumn() {227 // return "name";228 // }229 215 230 216 private Timestamp extractTimestamp(Comment comment) { … … 245 231 return txManager.getTransaction(txDefinition); 246 232 } 233 234 /** 235 * Method use for tests that will select comments based on the content 236 * @param aComment 237 * @return list of Comments 238 * @throws DataAccessException 239 */ 240 public Comment getByComment(String aComment) throws DataAccessException { 241 return getFirstOrNull(SELECT_BASE + " WHERE comments = ?", aComment); 242 } 247 243 } -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/ComponentRegistryDbImpl.java
r1635 r1639 61 61 @Autowired 62 62 private CommentsDao commentsDao; 63 //private CMDComponentSpecCache commentsCache;64 63 65 64 /** … … 230 229 } 231 230 } 232 233 @Override 234 public int registerComment(Comment comment, Comment spec){235 try { 236 String xml = commmentSpecToString( spec);231 232 @Override 233 public int registerComment(Comment comment, String userId) { 234 try { 235 String xml = commmentSpecToString(comment); 237 236 // Convert principal name to user record id 238 Number uid = convertUserIdInComment(comment); 237 Number uid = convertUserIdInComment(comment, userId); 238 //Number uid = Integer.parseInt(userId.toString()); 239 239 commentsDao.insertComment(comment, xml, uid); 240 240 return 0; … … 276 276 return uid; 277 277 } 278 279 private Number convertUserIdInComment(Comment comment) throws DataAccessException { 278 279 /** 280 * Calling service sets user id to principle. Our task is to convert this to 281 * an id for later reference. If none is set and this is a user's workspace, 282 * set from that user's id. 283 * 284 * @param comment 285 * Comment containing principle name as userId 286 * @return Id (from database) 287 * @throws DataAccessException 288 */ 289 private Number convertUserIdInComment(Comment comment, String uId) throws DataAccessException { 280 290 Number uid = null; 281 291 if (comment.getUserId() != null) { 282 User user = userDao.getByPrincipalName( comment.getUserId());292 User user = userDao.getByPrincipalName(uId); 283 293 if (user != null) { 284 294 uid = user.getId(); … … 449 459 return description.isProfile() ? profileDescriptionDao : componentDescriptionDao; 450 460 } 451 452 461 453 462 /** … … 476 485 } 477 486 478 private Number getIdForCommentDescription(Comment comment) throws IllegalArgumentException {479 Number dbId = null;480 try {481 dbId = commentsDao.getDbId(Integer.parseInt(comment.getId()));482 } catch (DataAccessException ex) {483 LOG.error("Error getting dbId for comment with id " + comment.getId(), ex);484 }485 if (dbId == null) {486 throw new IllegalArgumentException("Could not get database Id for description");487 } else {488 return dbId;489 }490 }491 492 487 private String componentSpecToString(CMDComponentSpec spec) throws UnsupportedEncodingException, JAXBException { 493 488 ByteArrayOutputStream os = new ByteArrayOutputStream(); … … 496 491 return xml; 497 492 } 498 499 493 494 private String commmentSpecToString(Comment spec) throws UnsupportedEncodingException, JAXBException { 500 495 ByteArrayOutputStream os = new ByteArrayOutputStream(); 501 496 MDMarshaller.marshal(spec, os); … … 542 537 } 543 538 544 private boolean isOwnerOfComment(Comment desc, String principalName) {545 String owner = commentsDao.getOwnerPrincipalName( getIdForCommentDescription(desc));539 private boolean isOwnerOfComment(Comment com, String principalName) { 540 String owner = commentsDao.getOwnerPrincipalName(Integer.parseInt(com.getId())); 546 541 return owner != null // If owner is null, no one can be owner 547 542 && principalName.equals(owner); 548 543 } 549 544 550 545 private void checkAge(AbstractDescription desc, Principal principal) throws DeleteFailedException { 551 546 if (isPublic() && !configuration.isAdminUser(principal)) { … … 594 589 595 590 @Override 596 public void deleteComment(String commentId, Principal principal) throws IOException, 597 ComponentRegistryException, UserUnauthorizedException, DeleteFailedException { 598 599 Comment com = commentsDao.getById(commentId); 600 if (com!= null) { 591 public void deleteComment(String commentId, Principal principal) throws IOException, 592 ComponentRegistryException, UserUnauthorizedException, DeleteFailedException { 593 Comment com = commentsDao.getById(commentId); 594 if (com != null) { 601 595 try { 602 603 commentsDao.deleteComment(com, true);604 605 606 607 608 609 596 checkAuthorisationComment(com, principal); 597 commentsDao.deleteComment(com); 598 } catch (DataAccessException ex) { 599 throw new DeleteFailedException("Database access error while trying to delete component", ex); 600 } 601 } 602 } 603 } -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/model/Comment.java
r1635 r1639 1 /*2 * To change this template, choose Tools | Templates3 * and open the template in the editor.4 */5 1 package clarin.cmdi.componentregistry.model; 6 2 … … 8 4 import clarin.cmdi.componentregistry.IdSequence; 9 5 import java.text.ParseException; 10 import java.util.ArrayList;11 6 import java.util.Date; 12 import java.util.List;13 7 import javax.xml.bind.annotation.XmlAccessType; 14 8 import javax.xml.bind.annotation.XmlAccessorType; 15 import javax.xml.bind.annotation.XmlElement;16 9 import javax.xml.bind.annotation.XmlRootElement; 17 10 import org.apache.commons.lang.time.DateFormatUtils; … … 20 13 /** 21 14 * 22 * @author jea fer15 * @author jean-charles FerriÚres <jean-charles.ferrieres@mpi.nl> 23 16 */ 24 17 @XmlRootElement(name = "comment") 25 18 @XmlAccessorType(XmlAccessType.FIELD) 26 19 public class Comment { 27 @XmlElement(name = "Comment_Type")28 protected List<Comment> commentType;29 private String comments;30 private String commentDate;31 private String componentDescriptionId;32 private String profileDescriptionId;33 private String userId;34 private String id;35 20 36 37 public List<Comment> getComments() { 38 if (commentType == null) { 39 commentType = new ArrayList<Comment>(); 40 } 41 return this.commentType; 21 private String comments; 22 private String commentDate; 23 private String componentDescriptionId; 24 private String profileDescriptionId; 25 private String userId; 26 private String id; 27 28 public void setComment(String comment) { 29 this.comments = comment; 42 30 } 43 44 public void setComment(String comment) {45 this.comments = comment;46 }47 31 48 49 50 32 public String getComment() { 33 return comments; 34 } 51 35 52 53 54 36 public void setCommentDate(String commentDate) { 37 this.commentDate = commentDate; 38 } 55 39 56 57 58 40 public String getCommentDate() { 41 return commentDate; 42 } 59 43 60 61 62 44 public void setId(String commentId) { 45 this.id = commentId; 46 } 63 47 64 65 66 48 public String getId() { 49 return id; 50 } 67 51 68 69 70 52 public String getComponentDescriptionId() { 53 return componentDescriptionId; 54 } 71 55 72 73 74 56 public void setComponentDescriptionId(String ComponentDescriptionId) { 57 this.componentDescriptionId = ComponentDescriptionId; 58 } 75 59 76 77 78 60 public void setProfileDescriptionId(String profileDescriptionId) { 61 this.profileDescriptionId = profileDescriptionId; 62 } 79 63 80 81 82 64 public String getProfileDescriptionId() { 65 return profileDescriptionId; 66 } 83 67 84 85 86 68 public void setUserId(String userId) { 69 this.userId = userId; 70 } 87 71 88 89 90 72 public String getUserId() { 73 return userId; 74 } 91 75 92 public static String createNewDate(){93 return createNewDate(new Date().getTime());94 76 public static Date getDate(String registrationDate) throws ParseException { 77 return DateUtils.parseDate(registrationDate, new String[]{DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern()}); 78 } 95 79 96 public static Date getDate(String registrationDate) throws ParseException { 97 return DateUtils.parseDate(registrationDate, new String[]{DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern()}); 98 } 80 /* 81 * Helper method to set the Date 82 */ 83 public static String createNewDate() { 84 return createNewDate(new Date().getTime()); 85 } 86 87 /* 88 * Helper method to set the Date in the same format 89 * @param time, long that contains the time to be set 90 */ 99 91 100 public static String createNewDate(long time) { 101 return DateFormatUtils.formatUTC(time, DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern()); 102 } 103 104 public static Comment createANewComment() { 105 String descriptionId = ComponentRegistry.REGISTRY_ID+ "p_" + IdSequence.get(); 106 Comment com = new Comment(); 107 com.setComment("Actual"); 108 com.setProfileDescriptionId(descriptionId); 109 com.setCommentDate(createNewDate()); 110 return com; 92 public static String createNewDate(long time) { 93 return DateFormatUtils.formatUTC(time, DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern()); 111 94 } 95 96 public static Comment createANewComment() { 97 String Id = ComponentRegistry.REGISTRY_ID + IdSequence.get(); 98 Comment com = new Comment(); 99 com.setId(Id); 100 com.setCommentDate(createNewDate()); 101 return com; 112 102 } 103 } -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/CommentValidator.java
r1633 r1639 1 /*2 * To change this template, choose Tools | Templates3 * and open the template in the editor.4 */5 1 package clarin.cmdi.componentregistry.rest; 6 2 3 import clarin.cmdi.componentregistry.ComponentRegistry; 4 import clarin.cmdi.componentregistry.ComponentRegistryException; 5 import clarin.cmdi.componentregistry.MDMarshaller; 6 import clarin.cmdi.componentregistry.model.AbstractDescription; 7 7 import clarin.cmdi.componentregistry.model.Comment; 8 import java.io.InputStream; 8 9 import java.util.ArrayList; 10 import java.util.Arrays; 11 import java.util.Collection; 12 import java.util.Collections; 9 13 import java.util.List; 10 import org.apache.commons.lang.StringUtils;14 import javax.xml.bind.JAXBException; 11 15 12 16 /** 13 * 14 * @author jea fer17 * Implementation of Validator 18 * @author jean-charles FerriÚres <jean-charles.ferrieres@mpi.nl> 15 19 */ 16 class CommentValidator implements Validator{ 17 private final Comment com; 20 class CommentValidator implements Validator { 21 22 static final String MISMATCH_ERROR = "Cannot register component as a profile or vica versa."; 23 static final String COMMENT_NOT_REGISTERED_ERROR = "referenced comment is not registered or does not have a correct commentId: "; 24 static final String PARSE_ERROR = "Error in validation input file. Error is: "; 25 static final String COMMENT_NOT_PUBLICLY_REGISTERED_ERROR = "referenced comment cannot be found in the published comments: "; 26 static final String COMPONENT_REGISTRY_EXCEPTION_ERROR = "An exception occurred while accessing the component registry: "; 27 static final String ILLEGAL_ATTRIBUTE_NAME_ERROR = "Illegal attribute name: "; 28 static final Collection<String> ILLEGAL_ATTRIBUTE_NAMES = Collections.unmodifiableCollection(Arrays.asList("ref", "CommentId")); 18 29 private List<String> errorMessages = new ArrayList<String>(); 30 private Comment com = null; 31 private final InputStream input; 32 private final AbstractDescription description; 19 33 20 public CommentValidator(Comment com) { 21 this.com = com; 34 /* 35 * @param input In order to validate the input is consumed. So use @see getCommentSpec to get the parsed CommentSpec. 36 * @param description use to validate the comment with the appropriate description (profile or a component) 37 */ 38 public CommentValidator(InputStream input, ComponentRegistry registry, ComponentRegistry userRegistry, AbstractDescription description) { 39 this.input = input; 40 this.description = description; 22 41 } 23 42 24 43 @Override 25 44 public List<String> getErrorMessages() { … … 29 48 @Override 30 49 public boolean validate() { 31 if (!isOk(com.getUserId(), com.getComment(), com.getCommentDate())) { 32 errorMessages.add("Fields are not filled in correctly"); 50 try { 51 com = MDMarshaller.unmarshal(Comment.class, input, null); 52 } catch (JAXBException e) { 53 errorMessages.add(PARSE_ERROR + e); 54 } 55 if (errorMessages.isEmpty()) { 56 try { 57 validateComment(com); 58 } catch (ComponentRegistryException e) { 59 errorMessages.add(COMPONENT_REGISTRY_EXCEPTION_ERROR + e); 60 } 33 61 } 34 62 return errorMessages.isEmpty(); 35 63 } 36 37 private boolean isOk(String... fields) { 38 boolean isOk = true; 39 for (String field : fields) { 40 isOk &= StringUtils.isNotBlank(field); 64 65 /* 66 * Method that check wether a comment is valid (good attributes) 67 * @param comment, the comment to be validated 68 */ 69 private void validateComment(Comment comment) throws ComponentRegistryException { 70 if (comment.getComment() == null) { 71 errorMessages.add(ILLEGAL_ATTRIBUTE_NAME_ERROR + "comment has to be filled in"); 41 72 } 42 return isOk; 73 if (description.isProfile()) { 74 if (comment.getProfileDescriptionId() == null) { 75 errorMessages.add(ILLEGAL_ATTRIBUTE_NAME_ERROR + "profileId could not be found"); 76 } 77 } else { 78 if (comment.getComponentDescriptionId() == null) { 79 errorMessages.add(ILLEGAL_ATTRIBUTE_NAME_ERROR + "componentId could not be found"); 80 } 81 } 43 82 } 44 83 84 public Comment getCommentSpec() { 85 return com; 86 } 45 87 } -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/ComponentRegistryRestService.java
r1635 r1639 507 507 return Response.serverError().status(Status.UNAUTHORIZED).entity("" + e.getMessage()).build(); 508 508 } 509 LOG.info(" Profilewith id: " + commentId + " deleted.");509 LOG.info("Comment with id: " + commentId + " deleted."); 510 510 return Response.ok().build(); 511 511 } … … 532 532 return Response.serverError().status(Status.UNAUTHORIZED).entity("" + e.getMessage()).build(); 533 533 } 534 LOG.info(" Profilewith id: " + commentId + " deleted.");534 LOG.info("Comment with id: " + commentId + " deleted."); 535 535 return Response.ok().build(); 536 536 } … … 647 647 @Consumes("multipart/form-data") 648 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){649 @PathParam("componentId") String componentId, @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) throws ComponentRegistryException { 650 650 Principal principal = checkAndGetUserPrincipal(); 651 651 UserCredentials userCredentials = getUserCredentials(principal); 652 ComponentRegistry registry = getRegistry(userspace, userCredentials); 653 AbstractDescription description = registry.getComponentDescription(componentId); 652 654 Comment com = createNewComment(); 653 655 com.setComponentDescriptionId("componentId"); 654 com.setUserId(userCredentials.getPrincipalName()); // Hash used to be created here, now Id is constructed by impl655 com.setComment(comment);656 //com.setUserId(userCredentials.getPrincipalName()); // Hash used to be created here, now Id is constructed by impl 657 //com.setComment(comment); 656 658 LOG.info("Trying to register Comment: " + com); 657 return registerComment(input, com, userCredentials, userspace, componentId, new NewAction());658 } 659 660 659 return registerComment(input, registry, userspace, description, principal, new NewAction()); 660 } 661 662 @POST 661 663 @Path("/profiles/{profileId}/comments") 662 664 @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 663 665 @Consumes("multipart/form-data") 664 public Response registerCommentInProfile(@FormDataParam(DATA_FORM_FIELD) InputStream input, @FormDataParam(NAME_FORM_FIELD) String comment,665 @ FormDataParam("profileId") AbstractDescription profileId, @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace){666 public Response registerCommentInProfile(@FormDataParam(DATA_FORM_FIELD) InputStream input, 667 @PathParam("profileId") String profileId, @QueryParam(USERSPACE_PARAM) @DefaultValue("false") boolean userspace) throws ComponentRegistryException { 666 668 Principal principal = checkAndGetUserPrincipal(); 667 669 UserCredentials userCredentials = getUserCredentials(principal); 668 Comment com = createNewComment(); 669 com.setComponentDescriptionId("profileId"); 670 com.setUserId(userCredentials.getPrincipalName()); // Hash used to be created here, now Id is constructed by impl 671 com.setComment(comment); 672 LOG.info("Trying to register Comment: " + com); 673 return registerComment(input, com, userCredentials, userspace, profileId, new NewAction()); 670 ComponentRegistry registry = getRegistry(userspace, userCredentials); 671 AbstractDescription description = registry.getProfileDescription(profileId); 672 //Comment com = createNewComment(); 673 //com.setProfileDescriptionId("profileId"); 674 //com.setUserId(userCredentials.getPrincipalName()); // Hash used to be created here, now Id is constructed by impl 675 //com.setComment(comment); 676 LOG.info("Trying to register Comment: "); 677 return registerComment(input, registry, userspace, description, principal, new NewAction()); 674 678 } 675 679 … … 723 727 } 724 728 725 private Response registerComment(InputStream input, Com ment com, UserCredentials userCredentials, boolean userspace,726 AbstractDescription description Id,729 private Response registerComment(InputStream input, ComponentRegistry registry, boolean userspace, 730 AbstractDescription description, Principal principal, 727 731 RegisterAction action) { 728 732 try { 729 ComponentRegistry registry = getRegistry(userspace, userCredentials); 730 CommentValidator commentValidator = new CommentValidator(com); 731 ComValidator validator = new ComValidator(input, com, registry, getRegistry(true), descriptionId 732 ); 733 CommentValidator validator = new CommentValidator(input, registry, getRegistry(true), description); 733 734 RegisterResponse response = new RegisterResponse(); 734 735 response.setIsInUserSpace(userspace); 735 validate(response, commentValidator,validator);736 validate(response, validator); 736 737 if (response.getErrors().isEmpty()) { 737 Comment spec = validator.getCommentSpec(); 738 int returnCode = action.executeComment(com, spec, response, registry); 738 Comment com = validator.getCommentSpec(); 739 int returnCode = action.executeComment(com, response, registry, principal.getName()); 740 // registry.registerComment(com, principal.getName()); 739 741 if (returnCode == 0) { 740 742 response.setRegistered(true); -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/NewAction.java
r1635 r1639 15 15 16 16 @Override 17 public int executeComment(Comment com, Comment spec, RegisterResponse response, ComponentRegistry registry) {18 return registry.registerComment(com, spec);17 public int executeComment(Comment com, RegisterResponse response, ComponentRegistry registry, String userId) { 18 return registry.registerComment(com, userId); 19 19 } 20 20 -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/PublishAction.java
r1633 r1639 24 24 25 25 @Override 26 public int executeComment(Comment com, Comment spec, RegisterResponse response, ComponentRegistry registry) {26 public int executeComment(Comment com, RegisterResponse response, ComponentRegistry registry, String userId) { 27 27 throw new UnsupportedOperationException("Not supported yet."); 28 28 } -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/RegisterAction.java
r1633 r1639 11 11 int execute(AbstractDescription desc, CMDComponentSpec spec, RegisterResponse response, ComponentRegistry registry); 12 12 13 int executeComment(Comment com, Comment spec, RegisterResponse response, ComponentRegistry registry);13 int executeComment(Comment com, RegisterResponse response, ComponentRegistry registry, String userId); 14 14 15 15 } -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/UpdateAction.java
r1633 r1639 23 23 24 24 @Override 25 public int executeComment(Comment com, Comment spec, RegisterResponse response, ComponentRegistry registry) {25 public int executeComment(Comment com, RegisterResponse response, ComponentRegistry registry, String userId) { 26 26 throw new UnsupportedOperationException("Not supported yet."); 27 27 } -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/impl/database/CommentsDaoTest.java
r1635 r1639 1 1 package clarin.cmdi.componentregistry.impl.database; 2 2 3 import clarin.cmdi.componentregistry.model.Comment;4 3 import clarin.cmdi.componentregistry.model.Comment; 5 4 import java.util.List; … … 18 17 /** 19 18 * 20 * @author jea fer19 * @author jean-charles FerriÚres <jean-charles.ferrieres@mpi.nl> 21 20 */ 22 21 @RunWith(SpringJUnit4ClassRunner.class) … … 44 43 public void testInsertComment() { 45 44 Comment comment = createTestComment(); 46 47 48 49 50 //testComment.setId(TEST_COMMENT_ID);51 52 //comment.setUserId("1");53 //testComment.setComponentDescriptionId("A component1");54 //testComment.setProfileDescriptionId("A profile1");55 45 56 46 String testComment = comment.getComment(); … … 117 107 assertEquals(count + 1, commentsDao.getAllComments().size()); 118 108 119 commentsDao.deleteComment(comment , true);109 commentsDao.deleteComment(comment); 120 110 assertEquals(count, commentsDao.getAllComments().size()); 121 111 -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/ComponentRegistryRestServiceTest.java
r1635 r1639 76 76 RegistryTestHelper.addComponent(getTestRegistry(), "component2"); 77 77 RegistryTestHelper.addComponent(getTestRegistry(), "component1"); 78 RegistryTestHelper.addComment(getTestRegistry(), "comment2"); 79 RegistryTestHelper.addComment(getTestRegistry(), "comment1"); 80 78 RegistryTestHelper.addComment(getTestRegistry(), "comment2", "profile1", "JUnit@test.com"); 79 RegistryTestHelper.addComment(getTestRegistry(), "comment1", "profile1", "JUnit@test.com"); 80 RegistryTestHelper.addComment(getTestRegistry(), "comment3", "component1", "JUnit@test.com"); 81 RegistryTestHelper.addComment(getTestRegistry(), "comment4", "component1", "JUnit@test.com"); 82 81 83 } 82 84 … … 143 145 public void testGetRegisteredCommentsInProfile() throws Exception { 144 146 fillUp(); 145 RegistryTestHelper.addComment(getTestRegistry(), "COMMENT1" );146 List<Comment> response = getResource().path("/registry/profiles/ clarin.eu:cr1:profile1/comments").accept(MediaType.APPLICATION_XML).147 RegistryTestHelper.addComment(getTestRegistry(), "COMMENT1", "profile1", "JUnit@test.com"); 148 List<Comment> response = getResource().path("/registry/profiles/profile1/comments/").accept(MediaType.APPLICATION_XML). 147 149 get(COMMENT_LIST_GENERICTYPE); 148 assertEquals( 0, response.size());149 response = getResource().path("/registry/ components/clarin.eu:cr1:profile1/comments").accept(MediaType.APPLICATION_JSON).get(150 assertEquals(3, response.size()); 151 response = getResource().path("/registry/profiles/profile1/comments").accept(MediaType.APPLICATION_JSON).get( 150 152 COMMENT_LIST_GENERICTYPE); 151 153 assertEquals(3, response.size()); 152 assertEquals("comment 1", response.get(0).getComment());153 assertEquals("comment 2", response.get(1).getComment());154 assertEquals("comment2", response.get(0).getComment()); 155 assertEquals("comment1", response.get(1).getComment()); 154 156 assertEquals("COMMENT1", response.get(2).getComment()); 157 } 158 159 @Test 160 public void testGetRegisteredCommentsInComponent() throws Exception { 161 fillUp(); 162 RegistryTestHelper.addComment(getTestRegistry(), "COMMENT2", "component1", "JUnit@test.com"); 163 List<Comment> response = getResource().path("/registry/components/component1/comments/").accept(MediaType.APPLICATION_XML). 164 get(COMMENT_LIST_GENERICTYPE); 165 assertEquals(3, response.size()); 166 response = getResource().path("/registry/components/component1/comments").accept(MediaType.APPLICATION_JSON).get( 167 COMMENT_LIST_GENERICTYPE); 168 assertEquals(3, response.size()); 169 assertEquals("comment3", response.get(0).getComment()); 170 assertEquals("comment4", response.get(1).getComment()); 171 assertEquals("COMMENT2", response.get(2).getComment()); 172 } 173 174 @Test 175 public void testGetSpecifiedCommentInComponent() throws Exception { 176 fillUp(); 177 Comment comment = getResource().path("/registry/components/component1/comments/2").accept(MediaType.APPLICATION_JSON).get(Comment.class); 178 assertNotNull(comment); 179 assertEquals("comment3", comment.getComment()); 180 assertEquals("2", comment.getId()); 181 comment = getResource().path("/registry/components/component1/comments/3").accept(MediaType.APPLICATION_JSON).get(Comment.class); 182 assertNotNull(comment); 183 assertEquals("comment4", comment.getComment()); 184 assertEquals("3", comment.getId()); 185 assertEquals("component1", comment.getComponentDescriptionId()); 186 } 187 188 @Test 189 public void testGetSpecifiedCommentInProfile() throws Exception { 190 fillUp(); 191 Comment comment = getResource().path("/registry/profiles/profile1/comments/0").accept(MediaType.APPLICATION_JSON).get(Comment.class); 192 assertNotNull(comment); 193 assertEquals("comment2", comment.getComment()); 194 assertEquals("0", comment.getId()); 195 comment = getResource().path("/registry/profiles/profile1/comments/1").accept(MediaType.APPLICATION_JSON).get(Comment.class); 196 assertNotNull(comment); 197 assertEquals("comment1", comment.getComment()); 198 assertEquals("1", comment.getId()); 199 assertEquals("profile1", comment.getProfileDescriptionId()); 200 } 201 202 @Test 203 public void testDeleteCommentFromComponent() throws Exception { 204 fillUp(); 205 List<Comment> comments = getResource().path("/registry/components/component1/comments").get(COMMENT_LIST_GENERICTYPE); 206 assertEquals(2, comments.size()); 207 Comment aComment = getResource().path("/registry/components/component1/comments/2").get(Comment.class); 208 assertNotNull(aComment); 209 ClientResponse response = getAuthenticatedResource("/registry/components/component1/comments/2").delete(ClientResponse.class); 210 assertEquals(200, response.getStatus()); 211 212 comments = getResource().path("/registry/components/component1/comments/").get(COMMENT_LIST_GENERICTYPE); 213 assertEquals(1, comments.size()); 214 215 response = getAuthenticatedResource("/registry/components/component1/comments/3").delete(ClientResponse.class); 216 assertEquals(200, response.getStatus()); 217 218 comments = getResource().path("/registry/components/component1/comments").get(COMMENT_LIST_GENERICTYPE); 219 assertEquals(0, comments.size()); 220 } 221 222 @Test 223 public void testDeleteCommentFromProfile() throws Exception { 224 fillUp(); 225 List<Comment> comments = getResource().path("/registry/profiles/profile1/comments").get(COMMENT_LIST_GENERICTYPE); 226 assertEquals(2, comments.size()); 227 Comment aComment = getResource().path("/registry/profiles/profile1/comments/0").get(Comment.class); 228 assertNotNull(aComment); 229 ClientResponse response = getAuthenticatedResource("/registry/profiles/profile1/comments/0").delete(ClientResponse.class); 230 assertEquals(200, response.getStatus()); 231 232 comments = getResource().path("/registry/profiles/profile1/comments/").get(COMMENT_LIST_GENERICTYPE); 233 assertEquals(1, comments.size()); 234 235 response = getAuthenticatedResource("/registry/profiles/profile1/comments/1").delete(ClientResponse.class); 236 assertEquals(200, response.getStatus()); 237 238 comments = getResource().path("/registry/profiles/profile1/comments").get(COMMENT_LIST_GENERICTYPE); 239 assertEquals(0, comments.size()); 155 240 } 156 241 … … 769 854 RegisterResponse response = getAuthenticatedResource("/registry/profiles/clarin.eu:cr1:profile1/comments").type(MediaType.MULTIPART_FORM_DATA).post( 770 855 RegisterResponse.class, form); 856 assertTrue(response.isRegistered()); 771 857 assertFalse(response.isInUserSpace()); 772 Comment comment = (Comment)response.getComment();858 Comment comment = response.getComment(); 773 859 assertNotNull(comment); 774 assertEquals(" CommentTest1", comment.getComment());860 assertEquals("Actual", comment.getComment()); 775 861 assertEquals(expectedUserId("JUnit@test.com"), comment.getUserId()); 776 assertTrue(comment.getId().startsWith(ComponentRegistry.REGISTRY_ID + "p_"));777 862 assertNotNull(comment.getCommentDate()); 778 String url = getResource().getUriBuilder().build().toString();779 assertEquals(url + "registry/profiles/{clarin.eu:cr1:p_1297242111880}/comments/" + comment.getId(), 1);780 863 } 781 864 -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/RegistryTestHelper.java
r1635 r1639 15 15 import clarin.cmdi.componentregistry.MDMarshaller; 16 16 import clarin.cmdi.componentregistry.components.CMDComponentSpec; 17 import clarin.cmdi.componentregistry.impl.database.UserDao; 17 18 import clarin.cmdi.componentregistry.model.Comment; 18 19 import clarin.cmdi.componentregistry.model.ComponentDescription; 19 20 import clarin.cmdi.componentregistry.model.ProfileDescription; 21 import clarin.cmdi.componentregistry.model.UserMapping.User; 22 import java.security.Principal; 20 23 21 24 /** … … 181 184 } 182 185 183 public static Comment addComment(ComponentRegistry testRegistry, String id) throws ParseException, JAXBException { 184 return addComment(testRegistry, id, RegistryTestHelper.getCommentTestContent()); 185 } 186 187 // public static Comment addComment(ComponentRegistry testRegistry, String id, String content) throws ParseException, 188 // JAXBException { 189 // return addComment(testRegistry, id, new ByteArrayInputStream(content.getBytes())); 190 // } 191 192 private static Comment addComment(ComponentRegistry testRegistry, String id, InputStream content) throws ParseException, 193 JAXBException { 194 Comment com = Comment.createANewComment(); 195 com.setUserId(DummyPrincipal.DUMMY_CREDENTIALS.getPrincipalName()); 196 com.setId(id); 197 com.setComment("Test Comment"); 186 public static Comment addComment(ComponentRegistry testRegistry, String id, String descriptionId, String principal) throws ParseException, JAXBException { 187 return addComment(testRegistry, RegistryTestHelper.getTestCommentContent(id, descriptionId), principal); 188 } 189 190 private static Comment addComment(ComponentRegistry testRegistry, InputStream content, String principal) throws ParseException, 191 JAXBException { 198 192 Comment spec = MDMarshaller.unmarshal(Comment.class, content, null); 199 testRegistry.registerComment( com, spec);200 return com;201 } 202 203 public static String getCommentTestContentString(String commentName ) {193 testRegistry.registerComment(spec, principal); 194 return spec; 195 } 196 197 public static String getCommentTestContentString(String commentName, String profileId) { 204 198 String comContent = ""; 205 199 comContent += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"; 206 200 comContent += "<comment xmlns:ns2=\"http://www.w3.org/1999/xlink\">\n"; 207 comContent += " <comments>" + commentName +"</comments>\n";208 comContent += " <commentDate> myDate</commentDate>\n";209 comContent += " <profileDescriptionId> profile1</profileDescriptionId>\n";210 comContent += " <userId> 8</userId>\n";201 comContent += " <comments>" + commentName + "</comments>\n"; 202 comContent += " <commentDate>" + Comment.createNewDate() + "</commentDate>\n"; 203 comContent += " <profileDescriptionId>" + profileId + "</profileDescriptionId>\n"; 204 comContent += " <userId>0</userId>\n"; 211 205 comContent += " <id>1</id>\n"; 212 206 comContent += "</comment>\n"; … … 214 208 } 215 209 216 public static InputStream getTestCommentContent(String content) { 217 return new ByteArrayInputStream(getCommentTestContentString(content).getBytes()); 218 } 219 220 public static String getCommentTestContent(String commentName) { 221 return getCommentTestContentString("Actual"); 210 public static String getCommentTestContentStringForComponent(String commentName, String componentId) { 211 String comContent = ""; 212 comContent += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"; 213 comContent += "<comment xmlns:ns2=\"http://www.w3.org/1999/xlink\">\n"; 214 comContent += " <comments>" + commentName + "</comments>\n"; 215 comContent += " <commentDate>" + Comment.createNewDate() + "</commentDate>\n"; 216 comContent += " <componentDescriptionId>" + componentId + "</componentDescriptionId>"; 217 comContent += " <userId>0</userId>\n"; 218 comContent += " <id>1</id>\n"; 219 comContent += "</comment>\n"; 220 return comContent; 221 } 222 223 public static InputStream getTestCommentContent(String content, String descriptionId) { 224 if (descriptionId.contains("profile")) { 225 return new ByteArrayInputStream(getCommentTestContentString(content, descriptionId).getBytes()); 226 } else { 227 return new ByteArrayInputStream(getCommentTestContentStringForComponent(content, descriptionId).getBytes()); 228 } 229 } 230 231 public static String getCommentTestContent(String commentId, String descriptionId) { 232 return getCommentTestContentString(commentId, descriptionId); 222 233 } 223 234 224 235 public static InputStream getCommentTestContent() { 225 return getTestCommentContent("Actual" );236 return getTestCommentContent("Actual", "profile1"); 226 237 } 227 238
Note: See TracChangeset
for help on using the changeset viewer.