]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
e8401602b9320c01cec18cb98d38a1ca36dafce8
[tmp/jakarta-migration.git] /
1 /**
2  *  This document is a part of the source code and related artifacts
3  *  for CollectionSpace, an open source collections management system
4  *  for museums and related institutions:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California, Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
18  *  Unless required by applicable law or agreed to in writing, software
19  *  distributed under the License is distributed on an "AS IS" BASIS,
20  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  *  See the License for the specific language governing permissions and
22  *  limitations under the License.
23  */
24
25 package org.collectionspace.services.place.nuxeo;
26
27 import java.util.List;
28 import java.util.regex.Pattern;
29 import org.collectionspace.services.common.api.Tools;
30 import org.collectionspace.services.common.document.InvalidDocumentException;
31 import org.collectionspace.services.common.document.ValidatorHandlerImpl;
32 import org.collectionspace.services.place.PlaceTermGroup;
33 import org.collectionspace.services.place.PlaceTermGroupList;
34 import org.collectionspace.services.place.PlacesCommon;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * PlaceValidatorHandler
40  * 
41  * Performs validation when making requests related to Place records.
42  * As an example, you can modify this class to customize validation of
43  * payloads supplied in requests to create and/or update records.
44  */
45 public class PlaceValidatorHandler extends ValidatorHandlerImpl {
46
47     final Logger logger = LoggerFactory.getLogger(PlaceValidatorHandler.class);
48     // 'Bad pattern' for shortIdentifiers matches any non-word characters
49     private static final Pattern SHORT_ID_BAD_PATTERN = Pattern.compile("[\\W]");
50     private static final String VALIDATION_ERROR =
51             "The record payload was invalid. See log file for more details.";
52     private static final String SHORT_ID_BAD_CHARS_ERROR =
53             "shortIdentifier must only contain standard word characters";
54     private static final String HAS_NO_TERMS_ERROR =
55             "Authority items must contain at least one term.";
56     private static final String TERM_HAS_EMPTY_DISPLAYNAME_ERROR =
57             "Each term group in an authority item must contain "
58             + "a non-empty term display name.";
59
60     @Override
61     protected Class getCommonPartClass() {
62         return PlacesCommon.class;
63     }
64
65     @Override
66     protected void handleCreate() throws InvalidDocumentException {
67         PlacesCommon place = (PlacesCommon) getCommonPart();
68         // No guarantee that there is a common part in every post/update.
69         if (place != null) {
70             try {
71                 String shortId = place.getShortIdentifier();
72                 if (shortId != null) {
73                     CS_ASSERT(shortIdentifierContainsOnlyValidChars(shortId), SHORT_ID_BAD_CHARS_ERROR);
74                 }
75                 CS_ASSERT(containsAtLeastOneTerm(place), HAS_NO_TERMS_ERROR);
76                 CS_ASSERT(allTermsContainDisplayName(place), TERM_HAS_EMPTY_DISPLAYNAME_ERROR);
77             } catch (AssertionError e) {
78                 if (logger.isErrorEnabled()) {
79                     logger.error(e.getMessage(), e);
80                 }
81                 throw new InvalidDocumentException(VALIDATION_ERROR, e);
82             }
83         }
84     }
85
86     @Override
87     protected void handleGet() throws InvalidDocumentException {
88     }
89
90     @Override
91     protected void handleGetAll() throws InvalidDocumentException {
92     }
93
94     @Override
95     protected void handleUpdate() throws InvalidDocumentException {
96         PlacesCommon place = (PlacesCommon) getCommonPart();
97         // No guarantee that there is a common part in every post/update.
98         if (place != null) {
99             try {
100                 // shortIdentifier is among a set of fields that are
101                 // prevented from being changed on an update, and thus
102                 // we don't need to check its value here.
103                 CS_ASSERT(containsAtLeastOneTerm(place), HAS_NO_TERMS_ERROR);
104                 CS_ASSERT(allTermsContainDisplayName(place), TERM_HAS_EMPTY_DISPLAYNAME_ERROR);
105             } catch (AssertionError e) {
106                 if (logger.isErrorEnabled()) {
107                     logger.error(e.getMessage(), e);
108                 }
109                 throw new InvalidDocumentException(VALIDATION_ERROR, e);
110             }
111         }
112     }
113
114     @Override
115     protected void handleDelete() throws InvalidDocumentException {
116     }
117
118     private boolean shortIdentifierContainsOnlyValidChars(String shortId) {
119         // Check whether any characters match the 'bad' pattern
120         if (SHORT_ID_BAD_PATTERN.matcher(shortId).find()) {
121             return false;
122         }
123         return true;
124     }
125
126     private boolean containsAtLeastOneTerm(PlacesCommon place) {
127         PlaceTermGroupList termGroupList = place.getPlaceTermGroupList();
128         if (termGroupList == null) {
129             return false;
130         }
131         List<PlaceTermGroup> termGroups = termGroupList.getPlaceTermGroup();
132         if ((termGroups == null) || (termGroups.isEmpty())){ 
133             return false;
134         }
135         return true;
136     }
137
138     private boolean allTermsContainDisplayName(PlacesCommon place) {
139         PlaceTermGroupList termGroupList = place.getPlaceTermGroupList();
140         List<PlaceTermGroup> termGroups = termGroupList.getPlaceTermGroup();
141         for (PlaceTermGroup termGroup : termGroups) {
142             if (Tools.isBlank(termGroup.getTermDisplayName())) {
143                 return false;
144             }
145         }
146         return true;
147     }
148 }
149