]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
0bfb7897b50701e52696cad8602b8f4dacc62c7b
[tmp/jakarta-migration.git] /
1 /**
2  * This document is a part of the source code and related artifacts for
3  * CollectionSpace, an open source collections management system for museums and
4  * related institutions:
5  *
6  * http://www.collectionspace.org http://wiki.collectionspace.org
7  *
8  * Copyright 2009 University of California at Berkeley
9  *
10  * Licensed under the Educational Community License (ECL), Version 2.0. You may
11  * not use this file except in compliance with this License.
12  *
13  * You may obtain a copy of the ECL 2.0 License at
14  *
15  * https://source.collectionspace.org/collection-space/LICENSE.txt
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
20  * License for the specific language governing permissions and limitations under
21  * the License.
22  */
23 package org.collectionspace.services.person.nuxeo;
24
25 import java.util.List;
26 import java.util.regex.Pattern;
27 import org.collectionspace.services.common.api.Tools;
28 import org.collectionspace.services.person.PersonsCommon;
29 import org.collectionspace.services.common.context.MultipartServiceContext;
30 import org.collectionspace.services.common.context.ServiceContext;
31 import org.collectionspace.services.common.document.DocumentHandler.Action;
32 import org.collectionspace.services.common.document.InvalidDocumentException;
33 import org.collectionspace.services.common.document.ValidatorHandler;
34 import org.collectionspace.services.person.PersonTermGroup;
35 import org.collectionspace.services.person.PersonTermGroupList;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * PersonValidatorHandler
41  *
42  * Validates data supplied when attempting to create and/or update Person
43  * records.
44  *
45  * $LastChangedRevision: $ $LastChangedDate: $
46  */
47 public class PersonValidatorHandler implements ValidatorHandler {
48
49     final Logger logger = LoggerFactory.getLogger(PersonValidatorHandler.class);
50     private static final Pattern shortIdBadPattern = Pattern.compile("[\\W]"); //.matcher(input).matches()
51
52     @Override
53     public void validate(Action action, ServiceContext ctx)
54             throws InvalidDocumentException {
55         if (logger.isDebugEnabled()) {
56             logger.debug("validate() action=" + action.name());
57         }
58
59         // Bail out if the validation action is for delete.
60         if (action.equals(Action.DELETE)) {
61             return;
62         }
63
64         try {
65             MultipartServiceContext mctx = (MultipartServiceContext) ctx;
66             PersonsCommon person = (PersonsCommon) mctx.getInputPart(mctx.getCommonPartLabel(),
67                     PersonsCommon.class);
68             String msg = "";
69             boolean invalid = false;
70
71             if (person != null) {       // No guarantee that there is a common part in every post/update.
72
73                 // Validation occurring on both creates and updates
74
75                 /*
76                  * String displayName = person.getDisplayName(); if
77                  * (!person.isDisplayNameComputed() && ((displayName == null) ||
78                  * displayName.trim().isEmpty())) { invalid = true; msg +=
79                  * "displayName must be non-null and non-blank if
80                  * displayNameComputed is false!"; }
81                  *
82                  */
83
84                 if (!containsAtLeastOneTerm(person)) {
85                     invalid = true;
86                     msg += "Authority items must contain at least one term.";
87                 }
88
89                 if (!allTermsContainNameOrDisplayName(person)) {
90                     invalid = true;
91                     msg += "Each term group in an authority item must contain "
92                             + "a non-empty term name or "
93                             + "a non-empty term display name.";
94                 }
95
96                 // Validation specific to creates or updates
97                 if (action.equals(Action.CREATE)) {
98
99                     // shortIdentifier value must contain only word characters
100                     String shortId = person.getShortIdentifier();
101                     if ((shortId != null) && (shortIdBadPattern.matcher(shortId).find())) {
102                         invalid = true;
103                         msg += "shortIdentifier must only contain standard word characters";
104                     }
105
106                     // Note: Per CSPACE-2215, shortIdentifier values that are null (missing)
107                     // or the empty string are now legally accepted in create payloads.
108                     // In either of those cases, a short identifier will be synthesized from
109                     // a display name or supplied in another manner.
110
111                 } else if (action.equals(Action.UPDATE)) {
112                 }
113             }
114
115             if (invalid) {
116                 logger.error(msg);
117                 throw new InvalidDocumentException(msg);
118             }
119         } catch (InvalidDocumentException ide) {
120             throw ide;
121         } catch (Exception e) {
122             throw new InvalidDocumentException(e);
123         }
124     }
125
126     private boolean containsAtLeastOneTerm(PersonsCommon person) {
127         PersonTermGroupList termGroupList = person.getPersonTermGroupList();
128         if (termGroupList == null) {
129             return false;
130         }
131         List<PersonTermGroup> termGroups = termGroupList.getPersonTermGroup();
132         if ((termGroups == null) || (termGroups.size() == 0)) {
133             return false;
134         }
135         return true;
136     }
137
138     private boolean allTermsContainNameOrDisplayName(PersonsCommon person) {
139         PersonTermGroupList termGroupList = person.getPersonTermGroupList();
140         List<PersonTermGroup> termGroups = termGroupList.getPersonTermGroup();
141         for (PersonTermGroup termGroup : termGroups) {
142             if (Tools.isBlank(termGroup.getTermName()) || Tools.isBlank(termGroup.getTermDisplayName()) ){
143                 return false;
144             }
145         }
146         return true;
147     }
148     
149 }