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