1 | /* |
---|
2 | * To change this template, choose Tools | Templates |
---|
3 | * and open the template in the editor. |
---|
4 | */ |
---|
5 | package clarin.cmdi.componentregistry.rest; |
---|
6 | |
---|
7 | import clarin.cmdi.componentregistry.ComponentRegistry; |
---|
8 | import clarin.cmdi.componentregistry.ComponentRegistryException; |
---|
9 | import clarin.cmdi.componentregistry.MDMarshaller; |
---|
10 | import clarin.cmdi.componentregistry.components.AttributeListType.Attribute; |
---|
11 | import clarin.cmdi.componentregistry.components.CMDComponentSpec; |
---|
12 | import clarin.cmdi.componentregistry.model.AbstractDescription; |
---|
13 | import clarin.cmdi.componentregistry.model.Comment; |
---|
14 | import clarin.cmdi.componentregistry.model.ComponentDescription; |
---|
15 | import clarin.cmdi.componentregistry.model.ProfileDescription; |
---|
16 | import java.io.InputStream; |
---|
17 | import java.util.ArrayList; |
---|
18 | import java.util.Arrays; |
---|
19 | import java.util.Collection; |
---|
20 | import java.util.Collections; |
---|
21 | import java.util.List; |
---|
22 | import javax.xml.bind.JAXBException; |
---|
23 | |
---|
24 | /** |
---|
25 | * |
---|
26 | * @author jeafer |
---|
27 | */ |
---|
28 | class ComValidator implements Validator { |
---|
29 | |
---|
30 | static final String MISMATCH_ERROR = "Cannot register component as a profile or vica versa."; |
---|
31 | static final String COMMENT_NOT_REGISTERED_ERROR = "referenced comment is not registered or does not have a correct commentId: "; |
---|
32 | static final String PARSE_ERROR = "Error in validation input file. Error is: "; |
---|
33 | static final String COMMENT_NOT_PUBLICLY_REGISTERED_ERROR = "referenced comment cannot be found in the published comments: "; |
---|
34 | static final String COMPONENT_REGISTRY_EXCEPTION_ERROR = "An exception occurred while accessing the component registry: "; |
---|
35 | static final String ILLEGAL_ATTRIBUTE_NAME_ERROR = "Illegal attribute name: "; |
---|
36 | static final Collection<String> ILLEGAL_ATTRIBUTE_NAMES = Collections.unmodifiableCollection(Arrays.asList("ref", "CommentId")); |
---|
37 | private List<String> errorMessages = new ArrayList<String>(); |
---|
38 | private Comment spec = null; |
---|
39 | private final InputStream input; |
---|
40 | private final Comment comment; |
---|
41 | private final ComponentRegistry registry; |
---|
42 | private final ComponentRegistry userRegistry; |
---|
43 | private final AbstractDescription descriptionId; |
---|
44 | |
---|
45 | /** |
---|
46 | * |
---|
47 | * @param input In order to validate the input is consumed. So use @see getCMDComponentSpec to get the parsed CMDComponentSpec. |
---|
48 | * @param desc |
---|
49 | * @param registry (registry you currently used) |
---|
50 | * @param userRegistry can be null, We get user registry as well so we can give nice error messages if needed. Can be the same as @param registry |
---|
51 | */ |
---|
52 | public ComValidator(InputStream input, Comment comment, ComponentRegistry registry, ComponentRegistry userRegistry, AbstractDescription descriptionId) { |
---|
53 | this.input = input; |
---|
54 | this.comment = comment; |
---|
55 | this.registry = registry; |
---|
56 | this.userRegistry = userRegistry; |
---|
57 | this.descriptionId = descriptionId; |
---|
58 | } |
---|
59 | |
---|
60 | @Override |
---|
61 | public List<String> getErrorMessages() { |
---|
62 | return errorMessages; |
---|
63 | } |
---|
64 | |
---|
65 | @Override |
---|
66 | public boolean validate() { |
---|
67 | try { |
---|
68 | spec = MDMarshaller.unmarshal(Comment.class, input, null); |
---|
69 | } catch (JAXBException e) { |
---|
70 | errorMessages.add(PARSE_ERROR + e); |
---|
71 | } |
---|
72 | if (errorMessages.isEmpty()) { |
---|
73 | try { |
---|
74 | validateComment(spec.getComments()); |
---|
75 | } catch (ComponentRegistryException e) { |
---|
76 | errorMessages.add(COMPONENT_REGISTRY_EXCEPTION_ERROR + e); |
---|
77 | } |
---|
78 | } |
---|
79 | return errorMessages.isEmpty(); |
---|
80 | } |
---|
81 | |
---|
82 | private void validateComment(List<Comment> comment) throws ComponentRegistryException { |
---|
83 | for (Comment commentType : comment) { |
---|
84 | validateDescribedComments(commentType); |
---|
85 | validateComment(commentType.getComments());//Recursion |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | private void validateDescribedComments(Comment comment) throws ComponentRegistryException { |
---|
90 | checkPublicComments(comment); |
---|
91 | checkLegalAttributes(comment); |
---|
92 | } |
---|
93 | |
---|
94 | private void checkPublicComments(Comment comment) throws ComponentRegistryException { |
---|
95 | if (isDefinedInSeparateFile(comment)) { |
---|
96 | String id = comment.getId(); |
---|
97 | String profileId = comment.getProfileDescriptionId(); |
---|
98 | String componentId = comment.getComponentDescriptionId(); |
---|
99 | Comment registeredComment = null; |
---|
100 | if (descriptionId.isProfile()) { |
---|
101 | if (descriptionId != null) { |
---|
102 | registeredComment = registry.getSpecifiedCommentInProfile(profileId); |
---|
103 | if (registeredComment == null) { |
---|
104 | String error = comment.getId(); |
---|
105 | errorMessages.add(COMMENT_NOT_PUBLICLY_REGISTERED_ERROR + error); |
---|
106 | } |
---|
107 | } |
---|
108 | } else { //User registry, can link to components from public registry and the user's registry |
---|
109 | if (descriptionId != null) { |
---|
110 | registeredComment = registry.getSpecifiedCommentInComponent(componentId); |
---|
111 | if (registeredComment == null) { |
---|
112 | errorMessages.add(COMMENT_NOT_REGISTERED_ERROR + comment.getId()); |
---|
113 | } |
---|
114 | |
---|
115 | } |
---|
116 | } |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | private boolean isDefinedInSeparateFile(Comment comment) { |
---|
121 | return comment.getComment() == null; |
---|
122 | } |
---|
123 | |
---|
124 | private void checkLegalAttributes(Comment comment) { |
---|
125 | if (comment.getComment() == null) { |
---|
126 | errorMessages.add(ILLEGAL_ATTRIBUTE_NAME_ERROR + "comment has to be filled in"); |
---|
127 | } |
---|
128 | if (descriptionId.isProfile()) { |
---|
129 | if (comment.getProfileDescriptionId() == null) { |
---|
130 | errorMessages.add(ILLEGAL_ATTRIBUTE_NAME_ERROR + "profileId could not be found"); |
---|
131 | } |
---|
132 | } else { |
---|
133 | if (comment.getComponentDescriptionId() == null) { |
---|
134 | errorMessages.add(ILLEGAL_ATTRIBUTE_NAME_ERROR + "componentId could not be found"); |
---|
135 | } |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | public Comment getCommentSpec() { |
---|
140 | return spec; |
---|
141 | } |
---|
142 | } |
---|