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