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()));
63 public void handleUpdate(DocumentWrapper<Role> wrapDoc) throws Exception {
64 Role roleFound = wrapDoc.getWrappedObject();
65 Role roleReceived = getCommonPart();
66 roleReceived.setRoleName(fixRoleName(roleReceived.getRoleName()));
67 merge(roleReceived, roleFound);
71 * merge manually merges the from from to the to role
72 * -this method is created due to inefficiency of JPA EM merge
77 private Role merge(Role from, Role to) throws Exception {
78 //role name cannot be changed
79 if (!(from.getRoleName().equalsIgnoreCase(to.getRoleName()))) {
80 String msg = "Role name cannot be changed " + to.getRoleName();
82 throw new BadRequestException(msg);
84 if (from.getRoleGroup() != null) {
85 to.setRoleGroup(from.getRoleGroup());
87 if (from.getDescription() != null) {
88 to.setDescription(from.getDescription());
90 if (logger.isDebugEnabled()) {
91 logger.debug("merged role=" + JaxbUtils.toString(to, Role.class));
97 public void completeUpdate(DocumentWrapper<Role> wrapDoc) throws Exception {
98 Role upAcc = wrapDoc.getWrappedObject();
99 getServiceContext().setOutput(role);
104 public void handleGet(DocumentWrapper<Role> wrapDoc) throws Exception {
105 setCommonPart(extractCommonPart(wrapDoc));
106 sanitize(getCommonPart());
107 getServiceContext().setOutput(role);
111 public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
112 RolesList rolesList = extractCommonPartList(wrapDoc);
113 setCommonPartList(rolesList);
114 getServiceContext().setOutput(getCommonPartList());
118 public Role extractCommonPart(
119 DocumentWrapper<Role> wrapDoc)
121 return wrapDoc.getWrappedObject();
125 public void fillCommonPart(Role obj, DocumentWrapper<Role> wrapDoc)
127 throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
131 public RolesList extractCommonPartList(
132 DocumentWrapper<List> wrapDoc)
135 RolesList rolesList = new RolesList();
136 List<Role> list = new ArrayList<Role>();
137 rolesList.setRoles(list);
138 for (Object obj : wrapDoc.getWrappedObject()) {
139 Role role = (Role) obj;
147 public Role getCommonPart() {
152 public void setCommonPart(Role role) {
157 public RolesList getCommonPartList() {
162 public void setCommonPartList(RolesList rolesList) {
163 this.rolesList = rolesList;
167 public String getQProperty(
173 public DocumentFilter createDocumentFilter() {
174 DocumentFilter filter = new RoleJpaFilter(this.getServiceContext());
179 * sanitize removes data not needed to be sent to the consumer
182 private void sanitize(Role role) {
183 role.setTenantId(null);
186 private String fixRoleName(String role) {
187 String roleName = role.toUpperCase();
188 String rolePrefix = "ROLE_";
189 if (!roleName.startsWith(rolePrefix)) {
190 roleName = rolePrefix + roleName;
195 private void setTenant(Role role) {
196 //set tenant only if not available from input
197 if (role.getTenantId() == null || role.getTenantId().isEmpty()) {
198 role.setTenantId(getServiceContext().getTenantId());