]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
8d41506ce4917a74a7e2b43e7c599cfab991e9a7
[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 package org.collectionspace.services.authorization.storage;
26
27 import org.collectionspace.services.common.document.DocumentNotFoundException;
28 import org.collectionspace.services.authorization.Permission;
29 import org.collectionspace.services.authorization.PermissionRole;
30 import org.collectionspace.services.authorization.PermissionValue;
31 import org.collectionspace.services.authorization.Role;
32 import org.collectionspace.services.authorization.RoleValue;
33 import org.collectionspace.services.common.ServiceMessages;
34 import org.collectionspace.services.common.context.ServiceContext;
35 import org.collectionspace.services.common.document.DocumentHandler.Action;
36 import org.collectionspace.services.common.document.InvalidDocumentException;
37 import org.collectionspace.services.common.document.ValidatorHandler;
38 import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * PermissionRoleValidatorHandler executes validation rules for permRole permission permRole
44  * @author 
45  */
46 public class PermissionRoleValidatorHandler implements ValidatorHandler {
47
48     final Logger logger = LoggerFactory.getLogger(PermissionRoleValidatorHandler.class);
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         try {
57             PermissionRole permRole = (PermissionRole) ctx.getInput();
58             StringBuilder msgBldr = new StringBuilder(ServiceMessages.VALIDATION_FAILURE);
59             boolean invalid = false;
60
61             if (action.equals(Action.CREATE)) {
62
63                 for (PermissionValue pv : permRole.getPermissions()) {
64                     if (isPermissionInvalid(pv.getPermissionId(), msgBldr)) {
65                         invalid = true;
66                     }
67                 }
68                 for (RoleValue rv : permRole.getRoles()) {
69                     if (isRoleInvalid(rv.getRoleId(), msgBldr)) {
70                         invalid = true;
71                     }
72                 }
73                     }
74             if (invalid) {
75                 String msg = msgBldr.toString();
76                 logger.error(msg);
77                 throw new InvalidDocumentException(msg);
78             }
79         } catch (InvalidDocumentException ide) {
80             throw ide;
81         } catch (Exception e) {
82             throw new InvalidDocumentException(e);
83         }
84     }
85
86     private boolean isPermissionInvalid(String id, StringBuilder msgBldr)
87                 throws DocumentNotFoundException {
88         boolean invalid = false;
89
90         if (id == null || id.isEmpty()) {
91             invalid = true;
92             msgBldr.append("\n permissionId : permissionId is missing");
93             return invalid;
94         }
95         Object permissionFound = JpaStorageUtils.getEntity(id, Permission.class);
96         if (permissionFound == null) {
97             invalid = true;
98             msgBldr.append("\n permissionId : permission for permissionId=" + id
99                     + " not found");
100         }
101
102         return invalid;
103     }
104
105     private boolean isRoleInvalid(String id, StringBuilder msgBldr)
106                 throws DocumentNotFoundException {
107         boolean invalid = false;
108
109         if (id == null || id.isEmpty()) {
110             invalid = true;
111             msgBldr.append("\n roleId : roleId is missing");
112             return invalid;
113         }
114         Object roleFound = JpaStorageUtils.getEntity(id, Role.class);
115         if (roleFound == null) {
116             invalid = true;
117             msgBldr.append("\n roleId : role for roleId=" + id
118                     + " not found");
119         }
120
121         return invalid;
122     }
123 }