]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
feb798c1a86c61d445cc913e31e0e2fc22cdfe1e
[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.place.nuxeo;
51
52 import java.util.regex.Pattern;
53
54 import org.collectionspace.services.place.PlacesCommon;
55 import org.collectionspace.services.common.context.MultipartServiceContext;
56 import org.collectionspace.services.common.context.ServiceContext;
57 import org.collectionspace.services.common.document.DocumentHandler.Action;
58 import org.collectionspace.services.common.document.InvalidDocumentException;
59 import org.collectionspace.services.common.document.ValidatorHandler;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
62
63 /**
64  *
65  * @author 
66  */
67 public class PlaceValidatorHandler implements ValidatorHandler {
68
69     final Logger logger = LoggerFactory.getLogger(PlaceValidatorHandler.class);
70     private static final Pattern shortIdBadPattern = Pattern.compile("[\\W]"); //.matcher(input).matches()
71
72     @Override
73     public void validate(Action action, ServiceContext ctx)
74             throws InvalidDocumentException {
75         if(logger.isDebugEnabled()) {
76             logger.debug("validate() action=" + action.name());
77         }
78         try {
79             MultipartServiceContext mctx = (MultipartServiceContext) ctx;
80             PlacesCommon place = (PlacesCommon) mctx.getInputPart(mctx.getCommonPartLabel(),
81                     PlacesCommon.class);
82             String msg = "";
83             boolean invalid = false;
84
85             // Validation occurring on both creates and updates
86             String displayName = place.getDisplayName();
87             if (!place.isDisplayNameComputed() && ((displayName == null) || displayName.trim().isEmpty())) {
88                 invalid = true;
89                 msg += "displayName must be non-null and non-blank if displayNameComputed is false";
90             }
91
92             // Validation specific to creates or updates
93             if (action.equals(Action.CREATE)) {
94                 String shortId = place.getShortIdentifier();
95                 // Per CSPACE-2215, shortIdentifier values that are null (missing)
96                 // oe the empty string are now legally accepted in create payloads.
97                 // In either of those cases, a short identifier will be synthesized from
98                 // a display name or supplied in another manner.
99                 if ((shortId != null) && (shortIdBadPattern.matcher(shortId).find())) {
100                     invalid = true;
101                     msg += "shortIdentifier must only contain standard word characters";
102                 }
103             } else if (action.equals(Action.UPDATE)) {
104             }
105
106             if (invalid) {
107                 logger.error(msg);
108                 throw new InvalidDocumentException(msg);
109             }
110         } catch (InvalidDocumentException ide) {
111             throw ide;
112         } catch (Exception e) {
113             throw new InvalidDocumentException(e);
114         }
115     }
116 }