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