]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
11995de3c20cca5f067122780a11ae64a6c673a0
[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 SHORT_ID_BAD_CHARS_ERROR =
51             "shortIdentifier must only contain standard word characters";
52     private static final String HAS_NO_TERMS_ERROR =
53             "Authority items must contain at least one term.";
54     private static final String TERM_HAS_EMPTY_DISPLAYNAME_ERROR =
55             "Each term in an authority item must contain "
56             + "a non-empty display name.";
57
58     @Override
59     protected Class getCommonPartClass() {
60         return PlacesCommon.class;
61     }
62
63     @Override
64     protected void handleCreate() throws InvalidDocumentException {
65         PlacesCommon place = (PlacesCommon) getCommonPart();
66         // No guarantee that there is a common part in every post/update.
67         if (place != null) {
68             try {
69                 String shortId = place.getShortIdentifier();
70                 if (shortId != null) {
71                     CS_ASSERT(shortIdentifierContainsOnlyValidChars(shortId), SHORT_ID_BAD_CHARS_ERROR);
72                 }
73                 CS_ASSERT(containsAtLeastOneTerm(place), HAS_NO_TERMS_ERROR);
74                 CS_ASSERT(allTermsContainDisplayName(place), TERM_HAS_EMPTY_DISPLAYNAME_ERROR);
75             } catch (AssertionError e) {
76                 if (logger.isErrorEnabled()) {
77                     logger.error(e.getMessage(), e);
78                 }
79                 throw new InvalidDocumentException(e.getMessage(), e);
80             }
81         }
82     }
83
84     @Override
85     protected void handleGet() throws InvalidDocumentException {
86     }
87
88     @Override
89     protected void handleGetAll() throws InvalidDocumentException {
90     }
91
92     @Override
93     protected void handleUpdate() throws InvalidDocumentException {
94         PlacesCommon place = (PlacesCommon) getCommonPart();
95         // No guarantee that there is a common part in every post/update.
96         if (place != null) {
97             try {
98                 // shortIdentifier is among a set of fields that are
99                 // prevented from being changed on an update, and thus
100                 // we don't need to check its value here.
101                 CS_ASSERT(containsAtLeastOneTerm(place), HAS_NO_TERMS_ERROR);
102                 CS_ASSERT(allTermsContainDisplayName(place), TERM_HAS_EMPTY_DISPLAYNAME_ERROR);
103             } catch (AssertionError e) {
104                 if (logger.isErrorEnabled()) {
105                     logger.error(e.getMessage(), e);
106                 }
107                 throw new InvalidDocumentException(e.getMessage(), e);
108             }
109         }
110     }
111
112     @Override
113     protected void handleDelete() throws InvalidDocumentException {
114     }
115
116     private boolean shortIdentifierContainsOnlyValidChars(String shortId) {
117         // Check whether any characters match the 'bad' pattern
118         if (SHORT_ID_BAD_PATTERN.matcher(shortId).find()) {
119             return false;
120         }
121         return true;
122     }
123
124     private boolean containsAtLeastOneTerm(PlacesCommon place) {
125         PlaceTermGroupList termGroupList = place.getPlaceTermGroupList();
126         if (termGroupList == null) {
127             return false;
128         }
129         List<PlaceTermGroup> termGroups = termGroupList.getPlaceTermGroup();
130         if ((termGroups == null) || (termGroups.isEmpty())){ 
131             return false;
132         }
133         return true;
134     }
135
136     private boolean allTermsContainDisplayName(PlacesCommon place) {
137         PlaceTermGroupList termGroupList = place.getPlaceTermGroupList();
138         List<PlaceTermGroup> termGroups = termGroupList.getPlaceTermGroup();
139         for (PlaceTermGroup termGroup : termGroups) {
140             if (Tools.isBlank(termGroup.getTermDisplayName())) {
141                 return false;
142             }
143         }
144         return true;
145     }
146 }
147