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