]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
4e239b32dde75fb08b9482805a2aa04d02e7e1d1
[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  */
25
26 package org.collectionspace.services.authorization.storage;
27
28 import org.collectionspace.services.authorization.Role;
29 import org.collectionspace.services.common.ServiceMessages;
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  * RoleValidatorHandler executes validation rules for role
39  * @author 
40  */
41 public class RoleValidatorHandler implements ValidatorHandler {
42
43     final Logger logger = LoggerFactory.getLogger(RoleValidatorHandler.class);
44
45     @Override
46     public void validate(Action action, ServiceContext ctx)
47             throws InvalidDocumentException {
48         if (logger.isDebugEnabled()) {
49             logger.debug("validate() action=" + action.name());
50         }
51         try {
52             Role role = (Role) ctx.getInput();
53             StringBuilder msgBldr = new StringBuilder(ServiceMessages.VALIDATION_FAILURE);
54             boolean invalid = false;
55
56             if (action.equals(Action.CREATE)) {
57
58                 //create specific validation here
59                 if (role.getRoleName() == null || role.getRoleName().isEmpty()) {
60                     invalid = true;
61                     msgBldr.append("\nroleName : missing or empty");
62                 }
63             } else if (action.equals(Action.UPDATE)) {
64                 //update specific validation here
65                 if (role.getRoleName() == null || role.getRoleName().isEmpty()) {
66                     invalid = true;
67                     msgBldr.append("\nroleName : cannot be missing or empty");
68                 }
69             }
70             if (invalid) {
71                 String msg = msgBldr.toString();
72                 logger.error(msg);
73                 throw new InvalidDocumentException(msg);
74             }
75         } catch (InvalidDocumentException ide) {
76             throw ide;
77         } catch (Exception e) {
78             throw new InvalidDocumentException(e);
79         }
80     }
81
82 }