Changeset 1640
- Timestamp:
- 12/08/11 12:53:53 (12 years ago)
- Location:
- ComponentRegistry/branches/jeaferversion/ComponentRegistry/src
- Files:
-
- 4 added
- 14 edited
Legend:
- Unmodified
- Added
- Removed
-
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/CommentsDao.java
r1639 r1640 69 69 */ 70 70 public List<Comment> getCommentsFromProfile(String profileId) throws DataAccessException { 71 return getList( SELECT_BASE + " WHERE profile_description_id = ?", profileId);71 return getList((SELECT_BASE + " WHERE profile_description_id = ?").concat(getOrderByDate()), profileId); 72 72 } 73 73 … … 79 79 */ 80 80 public Comment getSpecifiedCommentFromProfile(String commentId) throws DataAccessException { 81 //String select = SELECT_BASE + " WHERE " + COLUMN_ID + " = ?";82 81 return getFirstOrNull(SELECT_BASE + " WHERE " + COLUMN_ID + " = ?", Integer.parseInt(commentId)); 83 82 } … … 90 89 */ 91 90 public List<Comment> getCommentsFromComponent(String componentId) throws DataAccessException { 92 return getList( SELECT_BASE + " WHERE component_description_id = ?", componentId);91 return getList((SELECT_BASE + " WHERE component_description_id = ?").concat(getOrderByDate()), componentId); 93 92 } 94 93 … … 134 133 * @throws DataAccessException 135 134 */ 136 public Number insertComment(Comment comment, String content,Number userId) throws DataAccessException {135 public Number insertComment(Comment comment, Number userId) throws DataAccessException { 137 136 TransactionStatus transaction = getTransaction(); 138 137 try { 139 SimpleJdbcInsert insert = new SimpleJdbcInsert(getDataSource()).withTableName(TABLE_COMMENTS).usingGeneratedKeyColumns(140 COLUMN_ID);141 138 SimpleJdbcInsert insertComment = new SimpleJdbcInsert(getDataSource()).withTableName(getTableName()).usingGeneratedKeyColumns(COLUMN_ID); 142 139 Map<String, Object> params = new HashMap<String, Object>(); … … 241 238 return getFirstOrNull(SELECT_BASE + " WHERE comments = ?", aComment); 242 239 } 240 241 /** 242 * Sort the returned comments per date from the most recent to the oldest 243 * @return 244 */ 245 private String getOrderByDate() { 246 return " order by comment_date asc "; 247 } 243 248 } -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/ComponentRegistryDbImpl.java
r1639 r1640 233 233 public int registerComment(Comment comment, String userId) { 234 234 try { 235 String xml = commmentSpecToString(comment);236 235 // Convert principal name to user record id 237 236 Number uid = convertUserIdInComment(comment, userId); 238 //Number uid = Integer.parseInt(userId.toString()); 239 commentsDao.insertComment(comment, xml, uid); 237 commentsDao.insertComment(comment, uid); 240 238 return 0; 241 239 } catch (DataAccessException ex) { 242 240 LOG.error("Database error while registering component", ex); 243 241 return -1; 244 } catch (JAXBException ex) {245 LOG.error("Error while registering component", ex);246 return -2;247 } catch (UnsupportedEncodingException ex) {248 LOG.error("Error while registering component", ex);249 return -3;250 242 } 251 243 } … … 492 484 } 493 485 494 private String commmentSpecToString(Comment spec) throws UnsupportedEncodingException, JAXBException {495 ByteArrayOutputStream os = new ByteArrayOutputStream();496 MDMarshaller.marshal(spec, os);497 String xml = os.toString("UTF-8");498 return xml;499 }500 501 486 private CMDComponentSpec getUncachedMDComponent(String id, AbstractDescriptionDao dao) { 502 487 String xml = dao.getContent(false, id); -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/model/RegisterResponse.java
r1635 r1640 1 1 package clarin.cmdi.componentregistry.model; 2 3 import java.util.ArrayList;4 import java.util.List;5 2 6 3 import javax.xml.bind.annotation.XmlAccessType; … … 8 5 import javax.xml.bind.annotation.XmlAttribute; 9 6 import javax.xml.bind.annotation.XmlElement; 10 import javax.xml.bind.annotation.XmlElementWrapper;11 import javax.xml.bind.annotation.XmlElements;12 7 import javax.xml.bind.annotation.XmlRootElement; 13 8 … … 15 10 @XmlAccessorType(XmlAccessType.FIELD) 16 11 @XmlRootElement(name = "registerResponse") 17 public class RegisterResponse { 18 19 @XmlAttribute(required = true) 20 private Boolean isInUserSpace; 12 public class RegisterResponse extends ComponentRegistryResponse { 21 13 22 14 @XmlAttribute(required = true) 23 15 private Boolean isProfile; 24 16 25 @XmlAttribute(required = true)26 private Boolean registered;27 28 29 @XmlElementWrapper(name = "errors", required = false)30 @XmlElements(@XmlElement(name = "error", type = String.class))31 private List<String> errors = new ArrayList<String>();32 33 17 @XmlElement 34 18 private AbstractDescription description; 35 @XmlElement36 private Comment comment;37 38 39 public void setRegistered(boolean registered) {40 this.registered = registered;41 }42 43 public boolean isRegistered() {44 return registered;45 }46 47 public void setErrors(List<String> errors) {48 this.errors = errors;49 }50 51 public List<String> getErrors() {52 return errors;53 }54 55 public void addError(String error) {56 getErrors().add(error);57 }58 19 59 20 public void setDescription(AbstractDescription description) { 60 21 this.description = description; 61 }62 63 public void setComment(Comment comment) {64 this.comment = comment;65 }66 67 public Comment getComment() {68 return comment;69 22 } 70 23 … … 80 33 this.isProfile = isProfile; 81 34 } 82 83 public void setIsInUserSpace(Boolean isInUserSpace) {84 this.isInUserSpace = isInUserSpace;85 }86 87 public Boolean isInUserSpace() {88 return isInUserSpace;89 }90 35 91 36 } -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/CommentValidator.java
r1639 r1640 1 1 package clarin.cmdi.componentregistry.rest; 2 2 3 import clarin.cmdi.componentregistry.ComponentRegistry;4 3 import clarin.cmdi.componentregistry.ComponentRegistryException; 5 4 import clarin.cmdi.componentregistry.MDMarshaller; … … 20 19 class CommentValidator implements Validator { 21 20 22 static final String MISMATCH_ERROR = "Cannot register component as a profile or vica versa.";23 21 static final String COMMENT_NOT_REGISTERED_ERROR = "referenced comment is not registered or does not have a correct commentId: "; 24 22 static final String PARSE_ERROR = "Error in validation input file. Error is: "; … … 36 34 * @param description use to validate the comment with the appropriate description (profile or a component) 37 35 */ 38 public CommentValidator(InputStream input, ComponentRegistry registry, ComponentRegistry userRegistry,AbstractDescription description) {36 public CommentValidator(InputStream input, AbstractDescription description) { 39 37 this.input = input; 40 38 this.description = description; … … 68 66 */ 69 67 private void validateComment(Comment comment) throws ComponentRegistryException { 70 if (comment.getComment() == null ) {68 if (comment.getComment() == null | comment.getComment().isEmpty()) { 71 69 errorMessages.add(ILLEGAL_ATTRIBUTE_NAME_ERROR + "comment has to be filled in"); 72 70 } 73 71 if (description.isProfile()) { 74 if (comment.getProfileDescriptionId() == null ) {72 if (comment.getProfileDescriptionId() == null | comment.getProfileDescriptionId().isEmpty()) { 75 73 errorMessages.add(ILLEGAL_ATTRIBUTE_NAME_ERROR + "profileId could not be found"); 76 74 } 77 75 } else { 78 if (comment.getComponentDescriptionId() == null ) {76 if (comment.getComponentDescriptionId() == null | comment.getComponentDescriptionId().isEmpty()) { 79 77 errorMessages.add(ILLEGAL_ATTRIBUTE_NAME_ERROR + "componentId could not be found"); 80 78 } -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/ComponentRegistryRestService.java
r1639 r1640 44 44 import clarin.cmdi.componentregistry.model.ComponentDescription; 45 45 import clarin.cmdi.componentregistry.model.ProfileDescription; 46 import clarin.cmdi.componentregistry.model.CommentResponse; 46 47 import clarin.cmdi.componentregistry.model.RegisterResponse; 47 48 … … 654 655 Comment com = createNewComment(); 655 656 com.setComponentDescriptionId("componentId"); 656 //com.setUserId(userCredentials.getPrincipalName()); // Hash used to be created here, now Id is constructed by impl657 //com.setComment(comment);658 657 LOG.info("Trying to register Comment: " + com); 659 return registerComment(input, registry, userspace, description, principal , new NewAction());658 return registerComment(input, registry, userspace, description, principal); 660 659 } 661 660 … … 670 669 ComponentRegistry registry = getRegistry(userspace, userCredentials); 671 670 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 impl675 //com.setComment(comment);676 671 LOG.info("Trying to register Comment: "); 677 return registerComment(input, registry, userspace, description, principal , new NewAction());672 return registerComment(input, registry, userspace, description, principal); 678 673 } 679 674 … … 728 723 729 724 private Response registerComment(InputStream input, ComponentRegistry registry, boolean userspace, 730 AbstractDescription description, Principal principal, 731 RegisterAction action) { 732 try { 733 CommentValidator validator = new CommentValidator(input, registry, getRegistry(true), description); 734 RegisterResponse response = new RegisterResponse(); 725 AbstractDescription description, Principal principal) { 726 try { 727 CommentValidator validator = new CommentValidator(input, description); 728 CommentResponse response = new CommentResponse(); 735 729 response.setIsInUserSpace(userspace); 736 validate (response, validator);730 validateComment(response, validator); 737 731 if (response.getErrors().isEmpty()) { 738 732 Comment com = validator.getCommentSpec(); 739 int returnCode = action.executeComment(com, response, registry, principal.getName());740 //registry.registerComment(com, principal.getName());733 //int returnCode = action.executeComment(com, response, registry, principal.getName()); 734 int returnCode = registry.registerComment(com, principal.getName()); 741 735 if (returnCode == 0) { 742 736 response.setRegistered(true); … … 791 785 } 792 786 } 787 788 private void validateComment(CommentResponse response, Validator... validators) { 789 for (Validator validator : validators) { 790 if (!validator.validate()) { 791 for (String error : validator.getErrorMessages()) { 792 response.addError(error); 793 } 794 } 795 } 796 } 793 797 794 798 /** -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/NewAction.java
r1639 r1640 4 4 import clarin.cmdi.componentregistry.components.CMDComponentSpec; 5 5 import clarin.cmdi.componentregistry.model.AbstractDescription; 6 import clarin.cmdi.componentregistry.model.Comment;7 6 import clarin.cmdi.componentregistry.model.RegisterResponse; 8 7 … … 13 12 return registry.register(desc, spec); 14 13 } 15 16 @Override17 public int executeComment(Comment com, RegisterResponse response, ComponentRegistry registry, String userId) {18 return registry.registerComment(com, userId);19 }20 21 14 } -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/PublishAction.java
r1639 r1640 1 1 package clarin.cmdi.componentregistry.rest; 2 2 3 import clarin.cmdi.componentregistry.model.Comment;4 3 import java.security.Principal; 5 4 … … 22 21 return registry.publish(desc, spec, principal); 23 22 } 24 25 @Override26 public int executeComment(Comment com, RegisterResponse response, ComponentRegistry registry, String userId) {27 throw new UnsupportedOperationException("Not supported yet.");28 }29 30 23 } -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/RegisterAction.java
r1639 r1640 4 4 import clarin.cmdi.componentregistry.components.CMDComponentSpec; 5 5 import clarin.cmdi.componentregistry.model.AbstractDescription; 6 import clarin.cmdi.componentregistry.model.Comment;7 6 import clarin.cmdi.componentregistry.model.RegisterResponse; 8 7 … … 11 10 int execute(AbstractDescription desc, CMDComponentSpec spec, RegisterResponse response, ComponentRegistry registry); 12 11 13 int executeComment(Comment com, RegisterResponse response, ComponentRegistry registry, String userId);14 15 12 } -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/UpdateAction.java
r1639 r1640 1 1 package clarin.cmdi.componentregistry.rest; 2 2 3 import clarin.cmdi.componentregistry.model.Comment;4 3 import java.security.Principal; 5 4 … … 22 21 } 23 22 24 @Override25 public int executeComment(Comment com, RegisterResponse response, ComponentRegistry registry, String userId) {26 throw new UnsupportedOperationException("Not supported yet.");27 }28 29 23 } -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/impl/database/CommentsDaoTest.java
r1639 r1640 44 44 Comment comment = createTestComment(); 45 45 46 String testComment = comment.getComment();47 46 assertEquals(0, commentsDao.getAllComments().size()); 48 Number newId = commentsDao.insertComment(comment, testComment,Integer.parseInt(TEST_COMMENT_USER_ID));47 Number newId = commentsDao.insertComment(comment, Integer.parseInt(TEST_COMMENT_USER_ID)); 49 48 assertNotNull(newId); 50 49 … … 81 80 public void testGetComment() { 82 81 Comment comment = createTestComment(); 83 String testComment = comment.getComment(); 84 commentsDao.insertComment(comment, testComment, 8); 82 commentsDao.insertComment(comment, 8); 85 83 86 84 assertNotNull(commentsDao.getByComment(TEST_COMMENT_NAME)); … … 101 99 public void testSetDelete(){ 102 100 Comment comment = createTestComment(); 103 String testComment = comment.getComment();104 101 int count = commentsDao.getAllComments().size(); 105 102 106 commentsDao.insertComment(comment, testComment,8);103 commentsDao.insertComment(comment, 8); 107 104 assertEquals(count + 1, commentsDao.getAllComments().size()); 108 105 -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/model/RegisterResponseTest.java
r1602 r1640 26 26 String expected = ""; 27 27 expected += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"; 28 expected += "<registerResponse registered=\"false\" isProfile=\"true\" isInUserSpace=\"true\" xmlns:ns2=\"http://www.w3.org/1999/xlink\">\n";28 expected += "<registerResponse isProfile=\"true\" isInUserSpace=\"true\" registered=\"false\" xmlns:ns2=\"http://www.w3.org/1999/xlink\">\n"; 29 29 expected += " <errors>\n"; 30 30 expected += " <error>Error 1</error>\n"; … … 51 51 String expected = ""; 52 52 expected += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"; 53 expected += "<registerResponse registered=\"true\" isProfile=\"true\" isInUserSpace=\"false\" xmlns:ns2=\"http://www.w3.org/1999/xlink\">\n";53 expected += "<registerResponse isProfile=\"true\" isInUserSpace=\"false\" registered=\"true\" xmlns:ns2=\"http://www.w3.org/1999/xlink\">\n"; 54 54 expected += " <errors/>\n"; 55 55 expected += " <description xsi:type=\"profileDescription\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n"; … … 82 82 String expected = ""; 83 83 expected += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"; 84 expected += "<registerResponse registered=\"true\" isProfile=\"false\" isInUserSpace=\"true\" xmlns:ns2=\"http://www.w3.org/1999/xlink\">\n";84 expected += "<registerResponse isProfile=\"false\" isInUserSpace=\"true\" registered=\"true\" xmlns:ns2=\"http://www.w3.org/1999/xlink\">\n"; 85 85 expected += " <errors/>\n"; 86 86 expected += " <description xsi:type=\"componentDescription\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n"; -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/ComponentRegistryRestServiceTest.java
r1639 r1640 32 32 import clarin.cmdi.componentregistry.model.ComponentDescription; 33 33 import clarin.cmdi.componentregistry.model.ProfileDescription; 34 import clarin.cmdi.componentregistry.model.CommentResponse; 34 35 import clarin.cmdi.componentregistry.model.RegisterResponse; 35 36 … … 152 153 COMMENT_LIST_GENERICTYPE); 153 154 assertEquals(3, response.size()); 154 assertEquals(" comment2", response.get(0).getComment());155 assertEquals("COMMENT1", response.get(0).getComment()); 155 156 assertEquals("comment1", response.get(1).getComment()); 156 assertEquals(" COMMENT1", response.get(2).getComment());157 assertEquals("comment2", response.get(2).getComment()); 157 158 } 158 159 … … 167 168 COMMENT_LIST_GENERICTYPE); 168 169 assertEquals(3, response.size()); 169 assertEquals(" comment3", response.get(0).getComment());170 assertEquals("COMMENT2", response.get(0).getComment()); 170 171 assertEquals("comment4", response.get(1).getComment()); 171 assertEquals(" COMMENT2", response.get(2).getComment());172 assertEquals("comment3", response.get(2).getComment()); 172 173 } 173 174 … … 852 853 MediaType.APPLICATION_OCTET_STREAM_TYPE); 853 854 fillUp(); 854 RegisterResponse response = getAuthenticatedResource("/registry/profiles/clarin.eu:cr1:profile1/comments").type(MediaType.MULTIPART_FORM_DATA).post(855 RegisterResponse.class, form);855 CommentResponse response = getAuthenticatedResource("/registry/profiles/clarin.eu:cr1:profile1/comments").type(MediaType.MULTIPART_FORM_DATA).post( 856 CommentResponse.class, form); 856 857 assertTrue(response.isRegistered()); 857 858 assertFalse(response.isInUserSpace()); -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/MDValidatorTest.java
r1603 r1640 140 140 profileContent += "</CMD_ComponentSpec>\n"; 141 141 142 // Ids not registered will return two errors. One for each id 142 143 ProfileDescription desc = ProfileDescription.createNewDescription(); 143 144 MDValidator validator = new MDValidator(new ByteArrayInputStream(profileContent.getBytes()), desc, publicRegistry, null, publicRegistry); … … 147 148 assertTrue(validator.getErrorMessages().get(1).startsWith(MDValidator.COMPONENT_NOT_PUBLICLY_REGISTERED_ERROR)); 148 149 150 // id1 will be added and therefore only id2 is not registered 149 151 RegistryTestHelper.addComponent(publicRegistry, id1); 150 152 validator = new MDValidator(new ByteArrayInputStream(profileContent.getBytes()), desc, publicRegistry, null, publicRegistry); … … 153 155 assertTrue(validator.getErrorMessages().get(0).startsWith(MDValidator.COMPONENT_NOT_PUBLICLY_REGISTERED_ERROR)); 154 156 157 // id2 is added, no more errors shoud be return 155 158 RegistryTestHelper.addComponent(publicRegistry, id2); 156 159 validator = new MDValidator(new ByteArrayInputStream(profileContent.getBytes()), desc, publicRegistry, null, publicRegistry); -
ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/RegistryTestHelper.java
r1639 r1640 15 15 import clarin.cmdi.componentregistry.MDMarshaller; 16 16 import clarin.cmdi.componentregistry.components.CMDComponentSpec; 17 import clarin.cmdi.componentregistry.impl.database.UserDao;18 17 import clarin.cmdi.componentregistry.model.Comment; 19 18 import clarin.cmdi.componentregistry.model.ComponentDescription; 20 19 import clarin.cmdi.componentregistry.model.ProfileDescription; 21 import clarin.cmdi.componentregistry.model.UserMapping.User;22 import java.security.Principal;23 20 24 21 /**
Note: See TracChangeset
for help on using the changeset viewer.