]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
f20d9464b4010d33c5872aba96c5f2ac1d34688f
[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.organization.nuxeo;
51
52 import org.collectionspace.services.organization.OrganizationsCommon;
53 import org.collectionspace.services.common.context.MultipartServiceContext;
54 import org.collectionspace.services.common.context.ServiceContext;
55 import org.collectionspace.services.common.document.DocumentHandler.Action;
56 import org.collectionspace.services.common.document.InvalidDocumentException;
57 import org.collectionspace.services.common.document.ValidatorHandler;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
60 import java.util.regex.Pattern;
61
62 /**
63  *
64  * @author 
65  */
66 public class OrganizationValidatorHandler implements ValidatorHandler {
67
68     final Logger logger = LoggerFactory.getLogger(OrganizationValidatorHandler.class);
69     private static final Pattern shortIdBadPattern = Pattern.compile("[\\W]"); //.matcher(input).matches()
70
71     @Override
72     public void validate(Action action, ServiceContext ctx)
73             throws InvalidDocumentException {
74         if(logger.isDebugEnabled()) {
75             logger.debug("validate() action=" + action.name());
76         }
77         try {
78             MultipartServiceContext mctx = (MultipartServiceContext) ctx;
79             OrganizationsCommon org = (OrganizationsCommon) mctx.getInputPart(mctx.getCommonPartLabel(),
80                     OrganizationsCommon.class);
81             String msg = "";
82             boolean invalid = false;
83             if(!org.isDisplayNameComputed() && (org.getDisplayName()==null)) {
84                 invalid = true;
85                 msg += "displayName must be non-null if displayNameComputed is false!";
86             }
87                         String shortId = org.getShortIdentifier();
88             if(shortId==null){
89                 invalid = true;
90                 msg += "shortIdentifier must be non-null";
91             } else if(shortIdBadPattern.matcher(shortId).find()) {
92                 invalid = true;
93                 msg += "shortIdentifier must only contain standard word characters";
94             }
95             /*
96             if(action.equals(Action.CREATE)) {
97                 //create specific validation here
98             } else if(action.equals(Action.UPDATE)) {
99                 //update specific validation here
100             }
101             */
102
103             if (invalid) {
104                 logger.error(msg);
105                 throw new InvalidDocumentException(msg);
106             }
107         } catch (InvalidDocumentException ide) {
108             throw ide;
109         } catch (Exception e) {
110             throw new InvalidDocumentException(e);
111         }
112     }
113 }