source: ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/CommentValidator.java @ 1640

Last change on this file since 1640 was 1640, checked in by jeafer, 12 years ago

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

File size: 3.4 KB
Line 
1package clarin.cmdi.componentregistry.rest;
2
3import clarin.cmdi.componentregistry.ComponentRegistryException;
4import clarin.cmdi.componentregistry.MDMarshaller;
5import clarin.cmdi.componentregistry.model.AbstractDescription;
6import clarin.cmdi.componentregistry.model.Comment;
7import java.io.InputStream;
8import java.util.ArrayList;
9import java.util.Arrays;
10import java.util.Collection;
11import java.util.Collections;
12import java.util.List;
13import javax.xml.bind.JAXBException;
14
15/**
16 * Implementation of Validator
17 * @author jean-charles FerriÚres <jean-charles.ferrieres@mpi.nl>
18 */
19class CommentValidator implements Validator {
20
21    static final String COMMENT_NOT_REGISTERED_ERROR = "referenced comment is not registered or does not have a correct commentId: ";
22    static final String PARSE_ERROR = "Error in validation input file. Error is: ";
23    static final String COMMENT_NOT_PUBLICLY_REGISTERED_ERROR = "referenced comment cannot be found in the published comments: ";
24    static final String COMPONENT_REGISTRY_EXCEPTION_ERROR = "An exception occurred while accessing the component registry: ";
25    static final String ILLEGAL_ATTRIBUTE_NAME_ERROR = "Illegal attribute name: ";
26    static final Collection<String> ILLEGAL_ATTRIBUTE_NAMES = Collections.unmodifiableCollection(Arrays.asList("ref", "CommentId"));
27    private List<String> errorMessages = new ArrayList<String>();
28    private Comment com = null;
29    private final InputStream input;
30    private final AbstractDescription description;
31
32    /*
33     * @param input In order to validate the input is consumed. So use @see getCommentSpec to get the parsed CommentSpec.
34     * @param description use to validate the comment with the appropriate description (profile or a component)
35     */
36    public CommentValidator(InputStream input, AbstractDescription description) {
37        this.input = input;
38        this.description = description;
39    }
40
41    @Override
42    public List<String> getErrorMessages() {
43        return errorMessages;
44    }
45
46    @Override
47    public boolean validate() {
48        try {
49            com = MDMarshaller.unmarshal(Comment.class, input, null);
50        } catch (JAXBException e) {
51            errorMessages.add(PARSE_ERROR + e);
52        }
53        if (errorMessages.isEmpty()) {
54            try {
55                validateComment(com);
56            } catch (ComponentRegistryException e) {
57                errorMessages.add(COMPONENT_REGISTRY_EXCEPTION_ERROR + e);
58            }
59        }
60        return errorMessages.isEmpty();
61    }
62
63    /*
64     * Method that check wether a comment is valid (good attributes)
65     * @param comment, the comment to be validated
66     */
67    private void validateComment(Comment comment) throws ComponentRegistryException {
68        if (comment.getComment() == null | comment.getComment().isEmpty()) {
69            errorMessages.add(ILLEGAL_ATTRIBUTE_NAME_ERROR + "comment has to be filled in");
70        }
71        if (description.isProfile()) {
72            if (comment.getProfileDescriptionId() == null | comment.getProfileDescriptionId().isEmpty()) {
73                errorMessages.add(ILLEGAL_ATTRIBUTE_NAME_ERROR + "profileId could not be found");
74            }
75        } else {
76            if (comment.getComponentDescriptionId() == null | comment.getComponentDescriptionId().isEmpty()) {
77                errorMessages.add(ILLEGAL_ATTRIBUTE_NAME_ERROR + "componentId could not be found");
78            }
79        }
80    }
81
82    public Comment getCommentSpec() {
83        return com;
84    }
85}
Note: See TracBrowser for help on using the repository browser.