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