]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
fe36ecd5f4daa9edf9ac352c2ef5750e1b38af69
[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 at 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 package org.collectionspace.services.person.nuxeo;
25
26 import java.util.regex.Pattern;
27 import org.collectionspace.services.person.PersonsCommon;
28 import org.collectionspace.services.common.context.MultipartServiceContext;
29 import org.collectionspace.services.common.context.ServiceContext;
30 import org.collectionspace.services.common.document.DocumentHandler.Action;
31 import org.collectionspace.services.common.document.InvalidDocumentException;
32 import org.collectionspace.services.common.document.ValidatorHandler;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * PersonValidatorHandler
38  * 
39  * Validates data supplied when attempting to create and/or update Person records.
40  * 
41  * $LastChangedRevision: $
42  * $LastChangedDate: $
43  */
44 public class PersonValidatorHandler implements ValidatorHandler {
45
46     final Logger logger = LoggerFactory.getLogger(PersonValidatorHandler.class);
47     private static final Pattern shortIdBadPattern = Pattern.compile("[\\W]"); //.matcher(input).matches()
48
49     @Override
50     public void validate(Action action, ServiceContext ctx)
51             throws InvalidDocumentException {
52         if (logger.isDebugEnabled()) {
53             logger.debug("validate() action=" + action.name());
54         }
55         
56         // Bail out if the validation action is for delete.
57         if (action.equals(Action.DELETE)) {
58                 return;
59         }
60         
61         try {
62             MultipartServiceContext mctx = (MultipartServiceContext) ctx;
63             PersonsCommon person = (PersonsCommon) mctx.getInputPart(mctx.getCommonPartLabel(),
64                     PersonsCommon.class);
65             String msg = "";
66             boolean invalid = false;
67             
68             if(person != null) {        // No guarantee that there is a common part in every post/update.
69                 
70                     // Validation occurring on both creates and updates
71                 
72                     // FIXME Add validation logic here to ensure that every term info group must contain
73                     // any required data elements.  (Potential example: every term info group must contain
74                     // a non-null, non-whitespace-only value in either of the term or displayName fields.)
75                 
76                     /*
77                     String displayName = person.getDisplayName();
78                     if (!person.isDisplayNameComputed() && ((displayName == null) || displayName.trim().isEmpty())) {
79                         invalid = true;
80                         msg += "displayName must be non-null and non-blank if displayNameComputed is false!";
81                     }
82                     * 
83                     */
84                     
85                     // Validation specific to creates or updates
86                     if (action.equals(Action.CREATE)) {
87                         String shortId = person.getShortIdentifier();
88                         // Per CSPACE-2215, shortIdentifier values that are null (missing)
89                         // oe the empty string are now legally accepted in create payloads.
90                         // In either of those cases, a short identifier will be synthesized from
91                         // a display name or supplied in another manner.
92                         if ((shortId != null) && (shortIdBadPattern.matcher(shortId).find())) {
93                             invalid = true;
94                             msg += "shortIdentifier must only contain standard word characters";
95                         }
96                     } else if (action.equals(Action.UPDATE)) {
97                     }
98             }
99
100             if (invalid) {
101                 logger.error(msg);
102                 throw new InvalidDocumentException(msg);
103             }
104         } catch (InvalidDocumentException ide) {
105             throw ide;
106         } catch (Exception e) {
107             throw new InvalidDocumentException(e);
108         }
109     }
110 }