]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
09a4aa0bc342fb6bbd48259ce68093cc2cc7c083
[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.organization.nuxeo;
25
26 import java.util.regex.Pattern;
27 import org.collectionspace.services.organization.OrgauthoritiesCommon;
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  * OrgAuthorityValidatorHandler
38  * 
39  * Validates data supplied when attempting to create and/or update OrgAuthority records.
40  * 
41  * $LastChangedRevision$
42  * $LastChangedDate$
43  */
44 public class OrgAuthorityValidatorHandler implements ValidatorHandler {
45
46     final Logger logger = LoggerFactory.getLogger(OrgAuthorityValidatorHandler.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         try {
56             MultipartServiceContext mctx = (MultipartServiceContext) ctx;
57             OrgauthoritiesCommon organizationAuth =
58                     (OrgauthoritiesCommon) mctx.getInputPart(mctx.getCommonPartLabel(),
59                     OrgauthoritiesCommon.class);
60             String msg = "";
61             boolean invalid = false;
62
63             // Create-specific validation here
64             if (action.equals(Action.CREATE)) {
65                 String shortId = organizationAuth.getShortIdentifier();
66                 if (shortId == null) {
67                     invalid = true;
68                     msg += "shortIdentifier must be non-null";
69                 } else if (shortId.trim().isEmpty()) {
70                     invalid = true;
71                     msg += "shortIdentifier must have a non-empty value";
72                 } else if (shortIdBadPattern.matcher(shortId).find()) {
73                     invalid = true;
74                     msg += "shortIdentifier must only contain standard word characters";
75                 }
76             // Update-specific validation here
77             } else if (action.equals(Action.UPDATE)) {
78             }
79             
80             if (invalid) {
81                 logger.error(msg);
82                 throw new InvalidDocumentException(msg);
83             }
84         } catch (InvalidDocumentException ide) {
85             throw ide;
86         } catch (Exception e) {
87             throw new InvalidDocumentException(e);
88         }
89     }
90 }