]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
c483de0a3d7ea6eb4587eda0f80b87a0983f7dd5
[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.vocabulary.nuxeo;
25
26 import java.util.regex.Pattern;
27
28 import org.collectionspace.services.relation.RelationsCommonList;
29 import org.collectionspace.services.vocabulary.VocabularyitemsCommon;
30 import org.collectionspace.services.client.RelationClient;
31 import org.collectionspace.services.common.context.MultipartServiceContext;
32 import org.collectionspace.services.common.context.ServiceContext;
33 import org.collectionspace.services.common.document.DocumentHandler.Action;
34 import org.collectionspace.services.common.document.InvalidDocumentException;
35 import org.collectionspace.services.common.document.ValidatorHandler;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * VocabularyItemValidatorHandler
41  * 
42  * Validates data supplied when attempting to create and/or update VocabularyItem records.
43  * 
44  * $LastChangedRevision: $
45  * $LastChangedDate: $
46  */
47 public class VocabularyItemValidatorHandler implements ValidatorHandler {
48
49     final Logger logger = LoggerFactory.getLogger(VocabularyItemValidatorHandler.class);
50     private static final Pattern shortIdBadPattern = Pattern.compile("[\\W]"); //.matcher(input).matches()
51
52     @Override
53     public void validate(Action action, ServiceContext ctx)
54             throws InvalidDocumentException {
55         if (logger.isDebugEnabled()) {
56             logger.debug("validate() action=" + action.name());
57         }
58         
59         // Bail out if the validation action is for delete.
60         if (action.equals(Action.DELETE)) {
61                 return;
62         }
63         
64         try {
65             String errMessage = "";
66             boolean invalid = false;
67             MultipartServiceContext mctx = (MultipartServiceContext) ctx;
68             VocabularyitemsCommon vocabItem = (VocabularyitemsCommon) mctx.getInputPart(mctx.getCommonPartLabel());
69             
70             if (vocabItem != null) {
71                     // Validation occurring on both creates and updates
72                     String displayName = vocabItem.getDisplayName();
73                     if (displayName == null || displayName.trim().length() < 2) {
74                         invalid = true;
75                         errMessage += "displayName must be non-null and contain at least 2 non-whitespace characters";
76                     }
77                     
78                     // Validation specific to creates or updates
79                     if (action.equals(Action.CREATE)) {
80                         String shortId = vocabItem.getShortIdentifier();
81                         // Per CSPACE-2215, shortIdentifier values that are null (missing
82                         // or the empty string) are now legally accepted in CREATE/POST payloads.
83                         // In either of these cases, a short identifier will be synthesized from
84                         // a display name or supplied in another manner.
85                         if ((shortId != null) && (shortIdBadPattern.matcher(shortId).find())) {
86                             invalid = true;
87                             errMessage += "shortIdentifier must only contain standard word characters";
88                         }
89                     } else if (action.equals(Action.UPDATE)) {
90                         // What is this ELSE clause for?
91                     }
92             } else {
93                 RelationsCommonList rcl = (RelationsCommonList) mctx.getInputPart(RelationClient.SERVICE_COMMON_LIST_NAME);
94                 if (rcl == null) {
95                     invalid = true;
96                     errMessage += "The vocabulary item payload is missing both a common payload and relations part.  At lease one of these must exist in the payload.";
97                 }               
98             }
99             
100             if (invalid) {
101                 logger.error(errMessage);
102                 throw new InvalidDocumentException(errMessage);
103             }
104         } catch (InvalidDocumentException ide) {
105             throw ide;
106         } catch (Exception e) {
107             throw new InvalidDocumentException(e);
108         }
109     }
110 }