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.Date;
28 import java.util.List;
29 import java.util.UUID;
31 import org.collectionspace.services.authorization.Role;
32 import org.collectionspace.services.authorization.RolesList;
34 import org.collectionspace.services.common.document.AbstractDocumentHandlerImpl;
35 import org.collectionspace.services.common.document.BadRequestException;
36 import org.collectionspace.services.common.document.DocumentFilter;
37 import org.collectionspace.services.common.document.DocumentWrapper;
38 import org.collectionspace.services.common.document.JaxbUtils;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * Document handler for Role
46 public class RoleDocumentHandler
47 extends AbstractDocumentHandlerImpl<Role, RolesList, Role, List> {
49 private final Logger logger = LoggerFactory.getLogger(RoleDocumentHandler.class);
51 private RolesList rolesList;
54 public void handleCreate(DocumentWrapper<Role> wrapDoc) throws Exception {
55 String id = UUID.randomUUID().toString();
56 Role role = wrapDoc.getWrappedObject();
57 role.setRoleName(fixRoleName(role.getRoleName()));
59 //FIXME: if admin updating the role is a CS admin rather than
60 //the tenant admin, tenant id should be retrieved from the request
61 role.setTenantId(getServiceContext().getTenantId());
65 public void handleUpdate(DocumentWrapper<Role> wrapDoc) throws Exception {
66 Role roleFound = wrapDoc.getWrappedObject();
67 Role roleReceived = getCommonPart();
68 roleReceived.setRoleName(fixRoleName(roleReceived.getRoleName()));
69 merge(roleReceived, roleFound);
73 * merge manually merges the from from to the to role
74 * -this method is created due to inefficiency of JPA EM merge
79 private Role merge(Role from, Role to) throws Exception {
80 //role name cannot be changed
81 if (!(from.getRoleName().equalsIgnoreCase(to.getRoleName()))) {
82 String msg = "Role name cannot be changed " + to.getRoleName();
84 throw new BadRequestException(msg);
86 if (from.getRoleGroup() != null) {
87 to.setRoleGroup(from.getRoleGroup());
89 if (from.getDescription() != null) {
90 to.setDescription(from.getDescription());
92 if (logger.isDebugEnabled()) {
93 logger.debug("merged role=" + JaxbUtils.toString(to, Role.class));
99 public void completeUpdate(DocumentWrapper<Role> wrapDoc) throws Exception {
100 Role upAcc = wrapDoc.getWrappedObject();
101 getServiceContext().setOutput(role);
106 public void handleGet(DocumentWrapper<Role> wrapDoc) throws Exception {
107 setCommonPart(extractCommonPart(wrapDoc));
108 sanitize(getCommonPart());
109 getServiceContext().setOutput(role);
113 public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
114 RolesList rolesList = extractCommonPartList(wrapDoc);
115 setCommonPartList(rolesList);
116 getServiceContext().setOutput(getCommonPartList());
120 public Role extractCommonPart(
121 DocumentWrapper<Role> wrapDoc)
123 return wrapDoc.getWrappedObject();
127 public void fillCommonPart(Role obj, DocumentWrapper<Role> wrapDoc)
129 throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
133 public RolesList extractCommonPartList(
134 DocumentWrapper<List> wrapDoc)
137 RolesList rolesList = new RolesList();
138 List<Role> list = new ArrayList<Role>();
139 rolesList.setRoles(list);
140 for (Object obj : wrapDoc.getWrappedObject()) {
141 Role role = (Role) obj;
149 public Role getCommonPart() {
154 public void setCommonPart(Role role) {
159 public RolesList getCommonPartList() {
164 public void setCommonPartList(RolesList rolesList) {
165 this.rolesList = rolesList;
169 public String getQProperty(
175 public DocumentFilter createDocumentFilter() {
176 DocumentFilter filter = new RoleJpaFilter(this.getServiceContext());
181 * sanitize removes data not needed to be sent to the consumer
184 private void sanitize(Role role) {
185 role.setTenantId(null);
188 private String fixRoleName(String role) {
189 String roleName = role.toUpperCase();
190 String rolePrefix = "ROLE_";
191 if (!roleName.startsWith(rolePrefix)) {
192 roleName = rolePrefix + roleName;