]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
c55b5c4e68a995c973d68f698f1ad27afe38034a
[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.DocumentFilter;
36 import org.collectionspace.services.common.document.DocumentWrapper;
37 import org.collectionspace.services.common.document.JaxbUtils;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * Document handler for Role
43  * @author 
44  */
45 public class RoleDocumentHandler
46         extends AbstractDocumentHandlerImpl<Role, RolesList, Role, List> {
47
48     private final Logger logger = LoggerFactory.getLogger(RoleDocumentHandler.class);
49     private Role role;
50     private RolesList rolesList;
51
52     @Override
53     public void handleCreate(DocumentWrapper<Role> wrapDoc) throws Exception {
54         String id = UUID.randomUUID().toString();
55         Role role = wrapDoc.getWrappedObject();
56         role.setCsid(id);
57         //FIXME: if admin updating the role is a CS admin rather than
58         //the tenant admin, tenant id should be retrieved from the request
59         role.setTenantId(getServiceContext().getTenantId());
60     }
61
62     @Override
63     public void handleUpdate(DocumentWrapper<Role> wrapDoc) throws Exception {
64         Role roleFound = wrapDoc.getWrappedObject();
65         Role roleReceived = getCommonPart();
66         merge(roleReceived, roleFound);
67     }
68
69     /**
70      * merge manually merges the from from to the to role
71      * -this method is created due to inefficiency of JPA EM merge
72      * @param from
73      * @param to
74      * @return merged role
75      */
76     private Role merge(Role from, Role to) {
77         Date now = new Date();
78         to.setUpdatedAtItem(now);
79         if (from.getRoleName() != null) {
80             to.setRoleName(from.getRoleName());
81         }
82         if (from.getRoleGroup() != null) {
83             to.setRoleGroup(from.getRoleGroup());
84         }
85         if (from.getDescription() != null) {
86             to.setDescription(from.getDescription());
87         }
88         if (logger.isDebugEnabled()) {
89             logger.debug("merged role=" + JaxbUtils.toString(to, Role.class));
90         }
91         return to;
92     }
93
94     @Override
95     public void completeUpdate(DocumentWrapper<Role> wrapDoc) throws Exception {
96         Role upAcc = wrapDoc.getWrappedObject();
97         getServiceContext().setOutput(role);
98         sanitize(upAcc);
99     }
100
101     @Override
102     public void handleGet(DocumentWrapper<Role> wrapDoc) throws Exception {
103         setCommonPart(extractCommonPart(wrapDoc));
104         sanitize(getCommonPart());
105         getServiceContext().setOutput(role);
106     }
107
108     @Override
109     public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
110         RolesList rolesList = extractCommonPartList(wrapDoc);
111         setCommonPartList(rolesList);
112         getServiceContext().setOutput(getCommonPartList());
113     }
114
115     @Override
116     public Role extractCommonPart(
117             DocumentWrapper<Role> wrapDoc)
118             throws Exception {
119         return wrapDoc.getWrappedObject();
120     }
121
122     @Override
123     public void fillCommonPart(Role obj, DocumentWrapper<Role> wrapDoc)
124             throws Exception {
125         throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
126     }
127
128     @Override
129     public RolesList extractCommonPartList(
130             DocumentWrapper<List> wrapDoc)
131             throws Exception {
132
133         RolesList rolesList = new RolesList();
134         List<Role> list = new ArrayList<Role>();
135         rolesList.setRoles(list);
136         for (Object obj : wrapDoc.getWrappedObject()) {
137             Role role = (Role) obj;
138             sanitize(role);
139             list.add(role);
140         }
141         return rolesList;
142     }
143
144     @Override
145     public Role getCommonPart() {
146         return role;
147     }
148
149     @Override
150     public void setCommonPart(Role role) {
151         this.role = role;
152     }
153
154     @Override
155     public RolesList getCommonPartList() {
156         return rolesList;
157     }
158
159     @Override
160     public void setCommonPartList(RolesList rolesList) {
161         this.rolesList = rolesList;
162     }
163
164     @Override
165     public String getQProperty(
166             String prop) {
167         return null;
168     }
169
170     @Override
171     public DocumentFilter createDocumentFilter() {
172         DocumentFilter filter = new RoleJpaFilter(this.getServiceContext());
173         return filter;
174     }
175
176     /**
177      * sanitize removes data not needed to be sent to the consumer
178      * @param roleFound
179      */
180     private void sanitize(Role role) {
181         role.setTenantId(null);
182     }
183 }