]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
0171d0325752db8ca455f372a4e378638bc1eecd
[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.Date;
28 import java.util.List;
29 import java.util.UUID;
30
31 import org.collectionspace.services.authorization.Role;
32 import org.collectionspace.services.authorization.RolesList;
33
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;
41
42 /**
43  * Document handler for Role
44  * @author 
45  */
46 public class RoleDocumentHandler
47         extends AbstractDocumentHandlerImpl<Role, RolesList, Role, List> {
48
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         role.setRoleName(fixRoleName(role.getRoleName()));
58         role.setCsid(id);
59         setTenant(role);
60     }
61
62     @Override
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);
68     }
69
70     /**
71      * merge manually merges the from from to the to role
72      * -this method is created due to inefficiency of JPA EM merge
73      * @param from
74      * @param to
75      * @return merged role
76      */
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();
81             logger.error(msg);
82             throw new BadRequestException(msg);
83         }
84         if (from.getRoleGroup() != null) {
85             to.setRoleGroup(from.getRoleGroup());
86         }
87         if (from.getDescription() != null) {
88             to.setDescription(from.getDescription());
89         }
90         if (logger.isDebugEnabled()) {
91             logger.debug("merged role=" + JaxbUtils.toString(to, Role.class));
92         }
93         return to;
94     }
95
96     @Override
97     public void completeUpdate(DocumentWrapper<Role> wrapDoc) throws Exception {
98         Role upAcc = wrapDoc.getWrappedObject();
99         getServiceContext().setOutput(role);
100         sanitize(upAcc);
101     }
102
103     @Override
104     public void handleGet(DocumentWrapper<Role> wrapDoc) throws Exception {
105         setCommonPart(extractCommonPart(wrapDoc));
106         sanitize(getCommonPart());
107         getServiceContext().setOutput(role);
108     }
109
110     @Override
111     public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
112         RolesList rolesList = extractCommonPartList(wrapDoc);
113         setCommonPartList(rolesList);
114         getServiceContext().setOutput(getCommonPartList());
115     }
116
117     @Override
118     public Role extractCommonPart(
119             DocumentWrapper<Role> wrapDoc)
120             throws Exception {
121         return wrapDoc.getWrappedObject();
122     }
123
124     @Override
125     public void fillCommonPart(Role obj, DocumentWrapper<Role> wrapDoc)
126             throws Exception {
127         throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
128     }
129
130     @Override
131     public RolesList extractCommonPartList(
132             DocumentWrapper<List> wrapDoc)
133             throws Exception {
134
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;
140             sanitize(role);
141             list.add(role);
142         }
143         return rolesList;
144     }
145
146     @Override
147     public Role getCommonPart() {
148         return role;
149     }
150
151     @Override
152     public void setCommonPart(Role role) {
153         this.role = role;
154     }
155
156     @Override
157     public RolesList getCommonPartList() {
158         return rolesList;
159     }
160
161     @Override
162     public void setCommonPartList(RolesList rolesList) {
163         this.rolesList = rolesList;
164     }
165
166     @Override
167     public String getQProperty(
168             String prop) {
169         return null;
170     }
171
172     @Override
173     public DocumentFilter createDocumentFilter() {
174         DocumentFilter filter = new RoleJpaFilter(this.getServiceContext());
175         return filter;
176     }
177
178     /**
179      * sanitize removes data not needed to be sent to the consumer
180      * @param roleFound
181      */
182     private void sanitize(Role role) {
183         role.setTenantId(null);
184     }
185
186     private String fixRoleName(String role) {
187         String roleName = role.toUpperCase();
188         String rolePrefix = "ROLE_";
189         if (!roleName.startsWith(rolePrefix)) {
190             roleName = rolePrefix + roleName;
191         }
192         return roleName;
193     }
194
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());
199         }
200     }
201 }