]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
344eadb16aa9d033cb28609f425bed72f00ccc22
[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.authorization.storage;
25
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.UUID;
29
30 import org.collectionspace.services.authorization.Role;
31 import org.collectionspace.services.authorization.RolesList;
32
33 import org.collectionspace.services.common.document.BadRequestException;
34 import org.collectionspace.services.common.document.DocumentFilter;
35 import org.collectionspace.services.common.document.DocumentWrapper;
36 import org.collectionspace.services.common.document.JaxbUtils;
37 import org.collectionspace.services.common.security.SecurityUtils;
38 import org.collectionspace.services.common.storage.jpa.JpaDocumentHandler;
39
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * Document handler for Role
45  * @author 
46  */
47 public class RoleDocumentHandler
48                 extends JpaDocumentHandler<Role, RolesList, Role, List> {
49     private final Logger logger = LoggerFactory.getLogger(RoleDocumentHandler.class);
50     private Role role;
51     private RolesList rolesList;
52
53     @Override
54     public void handleCreate(DocumentWrapper<Role> wrapDoc) throws Exception {
55         String id = UUID.randomUUID().toString();
56         Role role = wrapDoc.getWrappedObject();
57         
58         // Synthesize the display name if it was not passed in.
59         String displayName = role.getDisplayName();
60         boolean displayNameEmpty = true;
61         if (displayName != null) {
62                 displayNameEmpty = displayName.trim().isEmpty();        
63         }
64         if (displayNameEmpty == true) {
65                 role.setDisplayName(role.getRoleName());
66         }
67         
68         setTenant(role);
69         role.setRoleName(fixRoleName(role.getRoleName(),
70                         role.getTenantId()));
71         role.setCsid(id);
72     }
73
74     @Override
75     public void handleUpdate(DocumentWrapper<Role> wrapDoc) throws Exception {
76         Role roleFound = wrapDoc.getWrappedObject();
77         Role roleReceived = getCommonPart();
78         roleReceived.setRoleName(fixRoleName(roleReceived.getRoleName(),
79                         roleFound.getTenantId()));
80         merge(roleReceived, roleFound);
81     }
82
83     /**
84      * merge manually merges the from from to the to role
85      * -this method is created due to inefficiency of JPA EM merge
86      * @param from
87      * @param to
88      * @return merged role
89      */
90     private Role merge(Role from, Role to) throws Exception {
91         //role name cannot be changed
92         if (!(from.getRoleName().equalsIgnoreCase(to.getRoleName()))) {
93             String msg = "Role name cannot be changed " + to.getRoleName();
94             logger.error(msg);
95             throw new BadRequestException(msg);
96         }
97         if (from.getDisplayName() != null) {
98                 to.setDisplayName(from.getDisplayName());
99         }
100         if (from.getRoleGroup() != null) {
101             to.setRoleGroup(from.getRoleGroup());
102         }
103         if (from.getDescription() != null) {
104             to.setDescription(from.getDescription());
105         }
106         if (logger.isDebugEnabled()) {
107             logger.debug("merged role=" + JaxbUtils.toString(to, Role.class));
108         }
109         return to;
110     }
111
112     @Override
113     public void completeUpdate(DocumentWrapper<Role> wrapDoc) throws Exception {
114         Role upAcc = wrapDoc.getWrappedObject();
115         getServiceContext().setOutput(role);
116         sanitize(upAcc);
117     }
118
119     @Override
120     public void handleGet(DocumentWrapper<Role> wrapDoc) throws Exception {
121         setCommonPart(extractCommonPart(wrapDoc));
122         sanitize(getCommonPart());
123         getServiceContext().setOutput(role);
124     }
125
126     @Override
127     public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
128         RolesList rolesList = extractCommonPartList(wrapDoc);
129         setCommonPartList(rolesList);
130         getServiceContext().setOutput(getCommonPartList());
131     }
132
133     @Override
134     public Role extractCommonPart(
135             DocumentWrapper<Role> wrapDoc)
136             throws Exception {
137         return wrapDoc.getWrappedObject();
138     }
139
140     @Override
141     public void fillCommonPart(Role obj, DocumentWrapper<Role> wrapDoc)
142             throws Exception {
143         throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
144     }
145
146     @Override
147     public RolesList extractCommonPartList(
148             DocumentWrapper<List> wrapDoc)
149             throws Exception {
150
151         RolesList rolesList = new RolesList();
152         List<Role> list = new ArrayList<Role>();
153         rolesList.setRoles(list);
154         for (Object obj : wrapDoc.getWrappedObject()) {
155             Role role = (Role) obj;
156             sanitize(role);
157             list.add(role);
158         }
159         return rolesList;
160     }
161
162     @Override
163     public Role getCommonPart() {
164         return role;
165     }
166
167     @Override
168     public void setCommonPart(Role role) {
169         this.role = role;
170     }
171
172     @Override
173     public RolesList getCommonPartList() {
174         return rolesList;
175     }
176
177     @Override
178     public void setCommonPartList(RolesList rolesList) {
179         this.rolesList = rolesList;
180     }
181
182     @Override
183     public String getQProperty(
184             String prop) {
185         return null;
186     }
187
188     @Override
189     public DocumentFilter createDocumentFilter() {
190         DocumentFilter filter = new RoleJpaFilter(this.getServiceContext());
191         return filter;
192     }
193
194     /**
195      * sanitize removes data not needed to be sent to the consumer
196      * @param roleFound
197      */
198     private void sanitize(Role role) {
199         if (!SecurityUtils.isCSpaceAdmin()) {
200             role.setTenantId(null);
201         }
202     }
203
204     private String fixRoleName(String role, String tenantId) {
205         String roleName = role.toUpperCase();
206         String rolePrefix = "ROLE_" + tenantId + "_";
207         if (!roleName.startsWith(rolePrefix)) {
208             roleName = rolePrefix + roleName;
209         }
210         return roleName;
211     }
212
213     private void setTenant(Role role) {
214         //set tenant only if not available from input
215         if (role.getTenantId() == null || role.getTenantId().isEmpty()) {
216             role.setTenantId(getServiceContext().getTenantId());
217         }
218     }
219 }