1 | package clarin.cmdi.componentregistry.rest; |
---|
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 | import clarin.cmdi.componentregistry.model.Comment; |
---|
8 | import java.io.InputStream; |
---|
9 | import java.util.ArrayList; |
---|
10 | import java.util.Arrays; |
---|
11 | import java.util.Collection; |
---|
12 | import java.util.Collections; |
---|
13 | import java.util.List; |
---|
14 | import javax.xml.bind.JAXBException; |
---|
15 | |
---|
16 | /** |
---|
17 | * Implementation of Validator |
---|
18 | * @author jean-charles FerriÚres <jean-charles.ferrieres@mpi.nl> |
---|
19 | */ |
---|
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")); |
---|
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 | } |
---|