]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
d2681fb8e48fb24bec41d5d311d656eeb1fceab0
[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.vocabulary.nuxeo;
51
52 import java.util.regex.Pattern;
53
54 import org.collectionspace.services.vocabulary.VocabulariesCommon;
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 VocabularyValidatorHandler implements ValidatorHandler {
68
69     final Logger logger = LoggerFactory.getLogger(VocabularyValidatorHandler.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             VocabulariesCommon vocab = (VocabulariesCommon) mctx.getInputPart(mctx.getCommonPartLabel(),
81                     VocabulariesCommon.class);
82             String msg = "";
83             boolean invalid = false;
84                         String displayName = vocab.getDisplayName();
85             if((displayName==null)||(displayName.length()<2)) {
86                 invalid = true;
87                 msg += "displayName must be non-null, and at least 2 chars long!";
88             }
89                         String shortId = vocab.getShortIdentifier();
90             if(shortId==null){
91                 invalid = true;
92                 msg += "shortIdentifier must be non-null";
93             } else if(shortIdBadPattern.matcher(shortId).find()) {
94                 invalid = true;
95                 msg += "shortIdentifier must only contain standard word characters";
96             }
97             /*
98             if(action.equals(Action.CREATE)) {
99                 //create specific validation here
100             } else if(action.equals(Action.UPDATE)) {
101                 //update specific validation here
102             }
103             */
104
105             if (invalid) {
106                 logger.error(msg);
107                 throw new InvalidDocumentException(msg);
108             }
109         } catch (InvalidDocumentException ide) {
110             throw ide;
111         } catch (Exception e) {
112             throw new InvalidDocumentException(e);
113         }
114     }
115 }