]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
ade5d8457c31de076ac16e749c81ff91b1b2b4e8
[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  *  This document is a part of the source code and related artifacts
25  *  for CollectionSpace, an open source collections management system
26  *  for museums and related institutions:
27
28  *  http://www.collectionspace.org
29  *  http://wiki.collectionspace.org
30
31  *  Copyright 2009 University of California at Berkeley
32
33  *  Licensed under the Educational Community License (ECL), Version 2.0.
34  *  You may not use this file except in compliance with this License.
35
36  *  You may obtain a copy of the ECL 2.0 License at
37
38  *  https://source.collectionspace.org/collection-space/LICENSE.txt
39
40  *  Unless required by applicable law or agreed to in writing, software
41  *  distributed under the License is distributed on an "AS IS" BASIS,
42  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
43  *  See the License for the specific language governing permissions and
44  *  limitations under the License.
45  */
46 /*
47  * To change this template, choose Tools | Templates
48  * and open the template in the editor.
49  */
50 package org.collectionspace.services.person.nuxeo;
51
52 import java.util.regex.Pattern;
53
54 import org.collectionspace.services.common.api.Tools;
55 import org.collectionspace.services.person.PersonsCommon;
56 import org.collectionspace.services.common.context.MultipartServiceContext;
57 import org.collectionspace.services.common.context.ServiceContext;
58 import org.collectionspace.services.common.document.DocumentHandler.Action;
59 import org.collectionspace.services.common.document.InvalidDocumentException;
60 import org.collectionspace.services.common.document.ValidatorHandler;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
63
64 /**
65  *
66  * @author 
67  */
68 public class PersonValidatorHandler implements ValidatorHandler {
69
70     final Logger logger = LoggerFactory.getLogger(PersonValidatorHandler.class);
71     private static final Pattern shortIdBadPattern = Pattern.compile("[\\W]"); //.matcher(input).matches()
72
73     @Override
74     public void validate(Action action, ServiceContext ctx)
75             throws InvalidDocumentException {
76         if(logger.isDebugEnabled()) {
77             logger.debug("validate() action=" + action.name());
78         }
79         try {
80             MultipartServiceContext mctx = (MultipartServiceContext) ctx;
81             PersonsCommon person = (PersonsCommon) mctx.getInputPart(mctx.getCommonPartLabel(),
82                     PersonsCommon.class);
83             String msg = "";
84             String displayName = person.getDisplayName();
85             boolean invalid = false;
86             if(!person.isDisplayNameComputed() && (displayName==null)) {
87                 invalid = true;
88                 msg += "displayName must be non-null if displayNameComputed is false!";
89             }
90             String shortId = person.getShortIdentifier();
91
92             if(shortId==null){
93                 if (Tools.notEmpty(displayName)){  //TODO: && action == CREATE   ......
94                     //CSPACE-3178  OK to have null shortIdentifier if displayName set, because we will compute the shortIdentifier before storing.
95                     invalid = false;
96                 } else {
97                     invalid = true;
98                     msg += "shortIdentifier must be non-null";
99                 }
100             } else if(shortIdBadPattern.matcher(shortId).find()) {
101                 invalid = true;
102                 msg += "shortIdentifier must only contain standard word characters";
103             }
104             /*
105             if(action.equals(Action.CREATE)) {
106                 //create specific validation here
107             } else if(action.equals(Action.UPDATE)) {
108                 //update specific validation here
109             }
110             */
111
112             if (invalid) {
113                 logger.error(msg);
114                 throw new InvalidDocumentException(msg);
115             }
116         } catch (InvalidDocumentException ide) {
117             throw ide;
118         } catch (Exception e) {
119             throw new InvalidDocumentException(e);
120         }
121     }
122 }