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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
24 package org.collectionspace.services.authorization.storage;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.UUID;
30 import org.collectionspace.services.authorization.Role;
31 import org.collectionspace.services.authorization.RolesList;
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;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * Document handler for Role
48 public class RoleDocumentHandler
49 extends JpaDocumentHandler<Role, RolesList, Role, List> {
50 private final Logger logger = LoggerFactory.getLogger(RoleDocumentHandler.class);
52 private RolesList rolesList;
55 public void handleCreate(DocumentWrapper<Role> wrapDoc) throws Exception {
56 String id = UUID.randomUUID().toString();
57 Role role = wrapDoc.getWrappedObject();
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();
65 if (displayNameEmpty == true) {
66 role.setDisplayName(role.getRoleName());
70 role.setRoleName(RoleClient.getBackendRoleName(role.getRoleName(), role.getTenantId()));
72 // We do not allow creation of locked roles through the services.
73 role.setMetadataProtection(null);
74 role.setPermsProtection(null);
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);
90 * merge manually merges the from from to the to role
91 * -this method is created due to inefficiency of JPA EM merge
96 private Role merge(Role from, Role to) throws Exception {
97 //role name cannot be changed
98 if (!(from.getRoleName().equalsIgnoreCase(to.getRoleName()))) {
99 String msg = "Role name cannot be changed " + to.getRoleName();
101 throw new BadRequestException(msg);
103 if (from.getDisplayName() != null) {
104 to.setDisplayName(from.getDisplayName());
106 if (from.getRoleGroup() != null) {
107 to.setRoleGroup(from.getRoleGroup());
109 if (from.getDescription() != null) {
110 to.setDescription(from.getDescription());
112 // Note that we do not allow update of locks
113 if (logger.isDebugEnabled()) {
114 org.collectionspace.services.authorization.ObjectFactory objectFactory =
115 new org.collectionspace.services.authorization.ObjectFactory();
116 logger.debug("merged role=" + JaxbUtils.toString(objectFactory.createRole(to) ,Role.class));
122 public void completeUpdate(DocumentWrapper<Role> wrapDoc) throws Exception {
123 Role upAcc = wrapDoc.getWrappedObject();
124 getServiceContext().setOutput(upAcc);
129 public void handleGet(DocumentWrapper<Role> wrapDoc) throws Exception {
130 setCommonPart(extractCommonPart(wrapDoc));
131 sanitize(getCommonPart());
132 getServiceContext().setOutput(role);
136 public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
137 RolesList rolesList = extractCommonPartList(wrapDoc);
138 setCommonPartList(rolesList);
139 getServiceContext().setOutput(getCommonPartList());
143 public Role extractCommonPart(
144 DocumentWrapper<Role> wrapDoc)
146 return wrapDoc.getWrappedObject();
150 public void fillCommonPart(Role obj, DocumentWrapper<Role> wrapDoc)
152 throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
156 public RolesList extractCommonPartList(
157 DocumentWrapper<List> wrapDoc)
160 RolesList rolesList = new RolesList();
161 List<Role> list = new ArrayList<Role>();
162 rolesList.setRole(list);
163 for (Object obj : wrapDoc.getWrappedObject()) {
164 Role role = (Role) obj;
172 public Role getCommonPart() {
177 public void setCommonPart(Role role) {
182 public RolesList getCommonPartList() {
187 public void setCommonPartList(RolesList rolesList) {
188 this.rolesList = rolesList;
192 public String getQProperty(
198 public DocumentFilter createDocumentFilter() {
199 DocumentFilter filter = new RoleJpaFilter(this.getServiceContext());
204 * sanitize removes data not needed to be sent to the consumer
207 private void sanitize(Role role) {
208 if (!SecurityUtils.isCSpaceAdmin()) {
209 role.setTenantId(null);
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());