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

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

Jean-Charles branch ComponentRegistry commit4,
Changes regarding comment on the ComponentRegistry.

Modification of classes Comment, CommentsDao?
Improvement of validation process for comments
Improvement of ComponentRegistryRestService?
Functionnal implementation of class test (RegisterHelper? and RestServiceTest?)

File size: 3.5 KB
Line 
1package clarin.cmdi.componentregistry.rest;
2
3import clarin.cmdi.componentregistry.ComponentRegistry;
4import clarin.cmdi.componentregistry.ComponentRegistryException;
5import clarin.cmdi.componentregistry.MDMarshaller;
6import clarin.cmdi.componentregistry.model.AbstractDescription;
7import clarin.cmdi.componentregistry.model.Comment;
8import java.io.InputStream;
9import java.util.ArrayList;
10import java.util.Arrays;
11import java.util.Collection;
12import java.util.Collections;
13import java.util.List;
14import javax.xml.bind.JAXBException;
15
16/**
17 * Implementation of Validator
18 * @author jean-charles FerriÚres <jean-charles.ferrieres@mpi.nl>
19 */
20class 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"));
29    private List<String> errorMessages = new ArrayList<String>();
30    private Comment com = null;
31    private final InputStream input;
32    private final AbstractDescription description;
33
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;
41    }
42
43    @Override
44    public List<String> getErrorMessages() {
45        return errorMessages;
46    }
47
48    @Override
49    public boolean validate() {
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            }
61        }
62        return errorMessages.isEmpty();
63    }
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");
72        }
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        }
82    }
83
84    public Comment getCommentSpec() {
85        return com;
86    }
87}
Note: See TracBrowser for help on using the repository browser.