Changeset 1640


Ignore:
Timestamp:
12/08/11 12:53:53 (12 years ago)
Author:
jeafer
Message:

Jean-Charles branch ComponentRegistry commit5 (functionnal),
Changes regarding comment on the ComponentRegistry.

Modification of classes CommentsDao?
Response class has been split up. A general class ComponentRegistryResponse?
a abstractDescription response: RegisterResponse?
a comment response : CommentResponse?
Improvement of validation process for comments
Improvement of ComponentRegistryRestService?
New classes test CommentResponseTest? and CommentValidatorTest?

Documentation added to most classes
Clean up code in other classes

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  
    6969     */
    7070    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);
    7272    }
    7373
     
    7979     */
    8080    public Comment getSpecifiedCommentFromProfile(String commentId) throws DataAccessException {
    81         //String select = SELECT_BASE + " WHERE " + COLUMN_ID + " = ?";
    8281        return getFirstOrNull(SELECT_BASE + " WHERE " + COLUMN_ID + " = ?", Integer.parseInt(commentId));
    8382    }
     
    9089     */
    9190    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);
    9392    }
    9493
     
    134133     * @throws DataAccessException
    135134     */
    136     public Number insertComment(Comment comment, String content, Number userId) throws DataAccessException {
     135    public Number insertComment(Comment comment, Number userId) throws DataAccessException {
    137136        TransactionStatus transaction = getTransaction();
    138137        try {
    139             SimpleJdbcInsert insert = new SimpleJdbcInsert(getDataSource()).withTableName(TABLE_COMMENTS).usingGeneratedKeyColumns(
    140                     COLUMN_ID);
    141138            SimpleJdbcInsert insertComment = new SimpleJdbcInsert(getDataSource()).withTableName(getTableName()).usingGeneratedKeyColumns(COLUMN_ID);
    142139            Map<String, Object> params = new HashMap<String, Object>();
     
    241238        return getFirstOrNull(SELECT_BASE + " WHERE comments = ?", aComment);
    242239    }
     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    }
    243248}
  • ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/ComponentRegistryDbImpl.java

    r1639 r1640  
    233233    public int registerComment(Comment comment, String userId) {
    234234        try {
    235             String xml = commmentSpecToString(comment);
    236235            // Convert principal name to user record id
    237236            Number uid = convertUserIdInComment(comment, userId);
    238             //Number uid = Integer.parseInt(userId.toString());
    239             commentsDao.insertComment(comment, xml, uid);
     237            commentsDao.insertComment(comment, uid);
    240238            return 0;
    241239        } catch (DataAccessException ex) {
    242240            LOG.error("Database error while registering component", ex);
    243241            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;
    250242        }
    251243    }
     
    492484    }
    493485
    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 
    501486    private CMDComponentSpec getUncachedMDComponent(String id, AbstractDescriptionDao dao) {
    502487        String xml = dao.getContent(false, id);
  • ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/model/RegisterResponse.java

    r1635 r1640  
    11package clarin.cmdi.componentregistry.model;
    2 
    3 import java.util.ArrayList;
    4 import java.util.List;
    52
    63import javax.xml.bind.annotation.XmlAccessType;
     
    85import javax.xml.bind.annotation.XmlAttribute;
    96import javax.xml.bind.annotation.XmlElement;
    10 import javax.xml.bind.annotation.XmlElementWrapper;
    11 import javax.xml.bind.annotation.XmlElements;
    127import javax.xml.bind.annotation.XmlRootElement;
    138
     
    1510@XmlAccessorType(XmlAccessType.FIELD)
    1611@XmlRootElement(name = "registerResponse")
    17 public class RegisterResponse {
    18 
    19     @XmlAttribute(required = true)
    20     private Boolean isInUserSpace;
     12public class RegisterResponse extends ComponentRegistryResponse {
    2113
    2214    @XmlAttribute(required = true)
    2315    private Boolean isProfile;
    2416
    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 
    3317    @XmlElement
    3418    private AbstractDescription description;
    35     @XmlElement
    36     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     }
    5819
    5920    public void setDescription(AbstractDescription description) {
    6021        this.description = description;
    61     }
    62    
    63     public void setComment(Comment comment) {
    64         this.comment = comment;
    65     }
    66    
    67     public Comment getComment() {
    68         return comment;
    6922    }
    7023
     
    8033        this.isProfile = isProfile;
    8134    }
    82 
    83     public void setIsInUserSpace(Boolean isInUserSpace) {
    84         this.isInUserSpace = isInUserSpace;
    85     }
    86 
    87     public Boolean isInUserSpace() {
    88         return isInUserSpace;
    89     }
    9035   
    9136}
  • ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/CommentValidator.java

    r1639 r1640  
    11package clarin.cmdi.componentregistry.rest;
    22
    3 import clarin.cmdi.componentregistry.ComponentRegistry;
    43import clarin.cmdi.componentregistry.ComponentRegistryException;
    54import clarin.cmdi.componentregistry.MDMarshaller;
     
    2019class CommentValidator implements Validator {
    2120
    22     static final String MISMATCH_ERROR = "Cannot register component as a profile or vica versa.";
    2321    static final String COMMENT_NOT_REGISTERED_ERROR = "referenced comment is not registered or does not have a correct commentId: ";
    2422    static final String PARSE_ERROR = "Error in validation input file. Error is: ";
     
    3634     * @param description use to validate the comment with the appropriate description (profile or a component)
    3735     */
    38     public CommentValidator(InputStream input, ComponentRegistry registry, ComponentRegistry userRegistry, AbstractDescription description) {
     36    public CommentValidator(InputStream input, AbstractDescription description) {
    3937        this.input = input;
    4038        this.description = description;
     
    6866     */
    6967    private void validateComment(Comment comment) throws ComponentRegistryException {
    70         if (comment.getComment() == null) {
     68        if (comment.getComment() == null | comment.getComment().isEmpty()) {
    7169            errorMessages.add(ILLEGAL_ATTRIBUTE_NAME_ERROR + "comment has to be filled in");
    7270        }
    7371        if (description.isProfile()) {
    74             if (comment.getProfileDescriptionId() == null) {
     72            if (comment.getProfileDescriptionId() == null | comment.getProfileDescriptionId().isEmpty()) {
    7573                errorMessages.add(ILLEGAL_ATTRIBUTE_NAME_ERROR + "profileId could not be found");
    7674            }
    7775        } else {
    78             if (comment.getComponentDescriptionId() == null) {
     76            if (comment.getComponentDescriptionId() == null | comment.getComponentDescriptionId().isEmpty()) {
    7977                errorMessages.add(ILLEGAL_ATTRIBUTE_NAME_ERROR + "componentId could not be found");
    8078            }
  • ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/ComponentRegistryRestService.java

    r1639 r1640  
    4444import clarin.cmdi.componentregistry.model.ComponentDescription;
    4545import clarin.cmdi.componentregistry.model.ProfileDescription;
     46import clarin.cmdi.componentregistry.model.CommentResponse;
    4647import clarin.cmdi.componentregistry.model.RegisterResponse;
    4748
     
    654655        Comment com = createNewComment();
    655656        com.setComponentDescriptionId("componentId");
    656         //com.setUserId(userCredentials.getPrincipalName()); // Hash used to be created here, now Id is constructed by impl
    657         //com.setComment(comment);
    658657        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);
    660659    }
    661660
     
    670669        ComponentRegistry registry = getRegistry(userspace, userCredentials);
    671670        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);
    676671        LOG.info("Trying to register Comment: ");
    677         return registerComment(input, registry, userspace, description, principal, new NewAction());
     672        return registerComment(input, registry, userspace, description, principal);
    678673    }
    679674
     
    728723
    729724    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();
    735729            response.setIsInUserSpace(userspace);
    736             validate(response, validator);
     730            validateComment(response, validator);
    737731            if (response.getErrors().isEmpty()) {
    738732                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());
    741735                if (returnCode == 0) {
    742736                    response.setRegistered(true);
     
    791785        }
    792786    }
     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    }
    793797
    794798    /**
  • ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/NewAction.java

    r1639 r1640  
    44import clarin.cmdi.componentregistry.components.CMDComponentSpec;
    55import clarin.cmdi.componentregistry.model.AbstractDescription;
    6 import clarin.cmdi.componentregistry.model.Comment;
    76import clarin.cmdi.componentregistry.model.RegisterResponse;
    87
     
    1312        return registry.register(desc, spec);
    1413    }
    15 
    16     @Override
    17     public int executeComment(Comment com, RegisterResponse response, ComponentRegistry registry, String userId) {
    18         return registry.registerComment(com, userId);
    19     }
    20 
    2114}
  • ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/PublishAction.java

    r1639 r1640  
    11package clarin.cmdi.componentregistry.rest;
    22
    3 import clarin.cmdi.componentregistry.model.Comment;
    43import java.security.Principal;
    54
     
    2221        return registry.publish(desc, spec, principal);
    2322    }
    24 
    25     @Override
    26     public int executeComment(Comment com, RegisterResponse response, ComponentRegistry registry, String userId) {
    27         throw new UnsupportedOperationException("Not supported yet.");
    28     }
    29 
    3023}
  • ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/RegisterAction.java

    r1639 r1640  
    44import clarin.cmdi.componentregistry.components.CMDComponentSpec;
    55import clarin.cmdi.componentregistry.model.AbstractDescription;
    6 import clarin.cmdi.componentregistry.model.Comment;
    76import clarin.cmdi.componentregistry.model.RegisterResponse;
    87
     
    1110    int execute(AbstractDescription desc, CMDComponentSpec spec, RegisterResponse response, ComponentRegistry registry);
    1211
    13     int executeComment(Comment com, RegisterResponse response, ComponentRegistry registry, String userId);
    14 
    1512}
  • ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/UpdateAction.java

    r1639 r1640  
    11package clarin.cmdi.componentregistry.rest;
    22
    3 import clarin.cmdi.componentregistry.model.Comment;
    43import java.security.Principal;
    54
     
    2221    }
    2322
    24     @Override
    25     public int executeComment(Comment com, RegisterResponse response, ComponentRegistry registry, String userId) {
    26         throw new UnsupportedOperationException("Not supported yet.");
    27     }
    28 
    2923}
  • ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/impl/database/CommentsDaoTest.java

    r1639 r1640  
    4444        Comment comment = createTestComment();
    4545
    46         String testComment = comment.getComment();
    4746        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));
    4948        assertNotNull(newId);
    5049     
     
    8180    public void testGetComment() {
    8281        Comment comment = createTestComment();
    83         String testComment = comment.getComment();
    84         commentsDao.insertComment(comment, testComment, 8);
     82        commentsDao.insertComment(comment, 8);
    8583
    8684        assertNotNull(commentsDao.getByComment(TEST_COMMENT_NAME));
     
    10199    public void testSetDelete(){
    102100        Comment comment = createTestComment();
    103         String testComment = comment.getComment();
    104101        int count = commentsDao.getAllComments().size();
    105102       
    106         commentsDao.insertComment(comment, testComment, 8);
     103        commentsDao.insertComment(comment, 8);
    107104        assertEquals(count + 1, commentsDao.getAllComments().size());
    108105
  • ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/model/RegisterResponseTest.java

    r1602 r1640  
    2626        String expected = "";
    2727        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";
    2929        expected += "    <errors>\n";
    3030        expected += "        <error>Error 1</error>\n";
     
    5151        String expected = "";
    5252        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";
    5454        expected += "    <errors/>\n";
    5555        expected += "    <description xsi:type=\"profileDescription\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n";
     
    8282        String expected = "";
    8383        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";
    8585        expected += "    <errors/>\n";
    8686        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  
    3232import clarin.cmdi.componentregistry.model.ComponentDescription;
    3333import clarin.cmdi.componentregistry.model.ProfileDescription;
     34import clarin.cmdi.componentregistry.model.CommentResponse;
    3435import clarin.cmdi.componentregistry.model.RegisterResponse;
    3536
     
    152153                COMMENT_LIST_GENERICTYPE);
    153154        assertEquals(3, response.size());
    154         assertEquals("comment2", response.get(0).getComment());
     155        assertEquals("COMMENT1", response.get(0).getComment());
    155156        assertEquals("comment1", response.get(1).getComment());
    156         assertEquals("COMMENT1", response.get(2).getComment());
     157        assertEquals("comment2", response.get(2).getComment());
    157158    }
    158159
     
    167168                COMMENT_LIST_GENERICTYPE);
    168169        assertEquals(3, response.size());
    169         assertEquals("comment3", response.get(0).getComment());
     170        assertEquals("COMMENT2", response.get(0).getComment());
    170171        assertEquals("comment4", response.get(1).getComment());
    171         assertEquals("COMMENT2", response.get(2).getComment());
     172        assertEquals("comment3", response.get(2).getComment());
    172173    }
    173174
     
    852853                MediaType.APPLICATION_OCTET_STREAM_TYPE);
    853854        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);
    856857        assertTrue(response.isRegistered());
    857858        assertFalse(response.isInUserSpace());
  • ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/MDValidatorTest.java

    r1603 r1640  
    140140        profileContent += "</CMD_ComponentSpec>\n";
    141141
     142        // Ids not registered will return two errors. One for each id
    142143        ProfileDescription desc = ProfileDescription.createNewDescription();
    143144        MDValidator validator = new MDValidator(new ByteArrayInputStream(profileContent.getBytes()), desc, publicRegistry, null, publicRegistry);
     
    147148        assertTrue(validator.getErrorMessages().get(1).startsWith(MDValidator.COMPONENT_NOT_PUBLICLY_REGISTERED_ERROR));
    148149
     150        // id1 will be added and therefore only id2 is not registered
    149151        RegistryTestHelper.addComponent(publicRegistry, id1);
    150152        validator = new MDValidator(new ByteArrayInputStream(profileContent.getBytes()), desc, publicRegistry, null, publicRegistry);
     
    153155        assertTrue(validator.getErrorMessages().get(0).startsWith(MDValidator.COMPONENT_NOT_PUBLICLY_REGISTERED_ERROR));
    154156
     157        // id2 is added, no more errors shoud be return
    155158        RegistryTestHelper.addComponent(publicRegistry, id2);
    156159        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  
    1515import clarin.cmdi.componentregistry.MDMarshaller;
    1616import clarin.cmdi.componentregistry.components.CMDComponentSpec;
    17 import clarin.cmdi.componentregistry.impl.database.UserDao;
    1817import clarin.cmdi.componentregistry.model.Comment;
    1918import clarin.cmdi.componentregistry.model.ComponentDescription;
    2019import clarin.cmdi.componentregistry.model.ProfileDescription;
    21 import clarin.cmdi.componentregistry.model.UserMapping.User;
    22 import java.security.Principal;
    2320
    2421/**
Note: See TracChangeset for help on using the changeset viewer.