]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
5ed610a13befc064d327e82800a14a7b732e5e9e
[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 permissionRoles and
22  *  limitations under the License.
23  */
24 package org.collectionspace.services.authorization.storage;
25
26 import java.util.ArrayList;
27 import java.util.List;
28
29 import org.collectionspace.services.authorization.PermissionRole;
30 import org.collectionspace.services.authorization.PermissionRoleRel;
31 import org.collectionspace.services.authorization.PermissionValue;
32 import org.collectionspace.services.authorization.PermissionsRolesList;
33 import org.collectionspace.services.authorization.RoleValue;
34 import org.collectionspace.services.authorization.SubjectType;
35 import org.collectionspace.services.common.authorization_mgt.AuthorizationRoleRel;
36 import org.collectionspace.services.common.authorization_mgt.PermissionRoleUtil;
37 import org.collectionspace.services.common.document.DocumentFilter;
38 import org.collectionspace.services.common.document.DocumentWrapper;
39 import org.collectionspace.services.common.storage.jpa.JpaDocumentFilter;
40 import org.collectionspace.services.common.storage.jpa.JpaDocumentHandler;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 // TODO: Auto-generated Javadoc
45 /**
46  * Document handler for PermissionRole association.
47  *
48  * @author
49  */
50 public class PermissionRoleDocumentHandler
51                 extends JpaDocumentHandler<PermissionRole, PermissionsRolesList, List<PermissionRoleRel>, List<PermissionRoleRel>> {
52
53     /** The logger. */
54     private final Logger logger = LoggerFactory.getLogger(PermissionRoleDocumentHandler.class);
55     
56     /** The permission role. */
57     private PermissionRole permissionRole;
58     
59     /** The permission roles list. */
60     private PermissionsRolesList permissionRolesList;
61
62     //
63     /** The permission role csid. */
64     private String permissionRoleCsid = null;
65
66     /**
67      * Sets the permission role csid.
68      *
69      * @param thePermissionRoleCsid the new permission role csid
70      */
71     public void setPermissionRoleCsid(String thePermissionRoleCsid) {
72         this.permissionRoleCsid = thePermissionRoleCsid;
73     }
74     
75     /* (non-Javadoc)
76      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
77      */
78     @Override
79     public void handleCreate(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
80         fillCommonPart(getCommonPart(), wrapDoc);
81     }
82
83     /* (non-Javadoc)
84      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#completeCreate(org.collectionspace.services.common.document.DocumentWrapper)
85      */
86     @Override
87     public void completeCreate(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
88         PermissionRole pr = getCommonPart();
89         AuthorizationDelegate.addPermissions(getServiceContext(), pr);
90     }
91
92     /* (non-Javadoc)
93      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
94      */
95     @Override
96     public void handleUpdate(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
97         throw new UnsupportedOperationException("operation not relevant for PermissionRoleDocumentHandler");
98     }
99
100     /* (non-Javadoc)
101      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#completeUpdate(org.collectionspace.services.common.document.DocumentWrapper)
102      */
103     @Override
104     public void completeUpdate(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
105         throw new UnsupportedOperationException("operation not relevant for PermissionRoleDocumentHandler");
106     }
107
108     /* (non-Javadoc)
109      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleGet(org.collectionspace.services.common.document.DocumentWrapper)
110      */
111     @Override
112     public void handleGet(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
113         setCommonPart(extractCommonPart(wrapDoc));
114         getServiceContext().setOutput(permissionRole);
115     }
116
117     /* (non-Javadoc)
118      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleGetAll(org.collectionspace.services.common.document.DocumentWrapper)
119      */
120     @Override
121     public void handleGetAll(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
122         throw new UnsupportedOperationException("operation not relevant for PermissionRoleDocumentHandler");
123     }
124
125     /* (non-Javadoc)
126      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleDelete(org.collectionspace.services.common.document.DocumentWrapper)
127      */
128     @Override
129     public boolean handleDelete(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
130         fillCommonPart(getCommonPart(), wrapDoc, true);
131         return true;
132     }
133
134     /* (non-Javadoc)
135      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#completeDelete(org.collectionspace.services.common.document.DocumentWrapper)
136      */
137     @Override
138     public void completeDelete(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
139         PermissionRole pr = getCommonPart();
140         AuthorizationDelegate.deletePermissions(getServiceContext(), pr);
141     }
142
143     /* (non-Javadoc)
144      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#extractCommonPart(org.collectionspace.services.common.document.DocumentWrapper)
145      */
146     @Override
147     public PermissionRole extractCommonPart(
148             DocumentWrapper<List<PermissionRoleRel>> wrapDoc)
149             throws Exception {
150         List<PermissionRoleRel> prrl = wrapDoc.getWrappedObject();
151         PermissionRole pr = new PermissionRole();
152         SubjectType subject = PermissionRoleUtil.getRelationSubject(getServiceContext());
153         if (prrl.size() == 0) {
154             return pr;
155         }
156         PermissionRoleRel prr0 = prrl.get(0);
157         if (SubjectType.ROLE.equals(subject)) {
158
159             List<PermissionValue> pvs = new ArrayList<PermissionValue>();
160             pr.setPermission(pvs);
161             PermissionValue pv = AuthorizationRoleRel.buildPermissionValue(prr0);
162             pvs.add(pv);
163
164             //add roles
165             List<RoleValue> rvs = new ArrayList<RoleValue>();
166             pr.setRole(rvs);
167             for (PermissionRoleRel prr : prrl) {
168                 RoleValue rv = AuthorizationRoleRel.buildRoleValue(prr);
169                 rvs.add(rv);
170             }
171         } else if (SubjectType.PERMISSION.equals(subject)) {
172
173             List<RoleValue> rvs = new ArrayList<RoleValue>();
174             pr.setRole(rvs);
175             RoleValue rv = AuthorizationRoleRel.buildRoleValue(prr0);
176             rvs.add(rv);
177
178             //add permssions
179             List<PermissionValue> pvs = new ArrayList<PermissionValue>();
180             pr.setPermission(pvs);
181             for (PermissionRoleRel prr : prrl) {
182                 PermissionValue pv = AuthorizationRoleRel.buildPermissionValue(prr);
183                 pvs.add(pv);
184             }
185         }
186         return pr;
187     }
188
189     /**
190      * Fill common part.
191      *
192      * @param pr the pr
193      * @param wrapDoc the wrap doc
194      * @param handleDelete the handle delete
195      * @throws Exception the exception
196      */
197     public void fillCommonPart(PermissionRole pr,
198                         DocumentWrapper<List<PermissionRoleRel>> wrapDoc,
199                         boolean handleDelete)
200             throws Exception {
201         List<PermissionRoleRel> prrl = wrapDoc.getWrappedObject();
202         SubjectType subject = pr.getSubject();
203         if (subject == null) {
204             //it is not required to give subject as URI determines the subject
205             subject = PermissionRoleUtil.getRelationSubject(getServiceContext());
206         } else {
207             //subject mismatch should have been checked during validation
208         }
209         PermissionRoleUtil.buildPermissionRoleRel(pr, subject, prrl, handleDelete);
210     }
211     
212     /* (non-Javadoc)
213      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#fillCommonPart(java.lang.Object, org.collectionspace.services.common.document.DocumentWrapper)
214      */
215     @Override
216     public void fillCommonPart(PermissionRole ar,
217                 DocumentWrapper<List<PermissionRoleRel>> wrapDoc)
218                         throws Exception {
219         fillCommonPart(ar, wrapDoc, false);
220     }    
221
222     /* (non-Javadoc)
223      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#extractCommonPartList(org.collectionspace.services.common.document.DocumentWrapper)
224      */
225     @Override
226     public PermissionsRolesList extractCommonPartList(
227             DocumentWrapper<List<PermissionRoleRel>> wrapDoc)
228             throws Exception {
229
230         throw new UnsupportedOperationException("operation not relevant for PermissionRoleDocumentHandler");
231     }
232
233     /* (non-Javadoc)
234      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getCommonPart()
235      */
236     @Override
237     public PermissionRole getCommonPart() {
238         return permissionRole;
239     }
240
241     /* (non-Javadoc)
242      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#setCommonPart(java.lang.Object)
243      */
244     @Override
245     public void setCommonPart(PermissionRole permissionRole) {
246         this.permissionRole = permissionRole;
247     }
248
249     /* (non-Javadoc)
250      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getCommonPartList()
251      */
252     @Override
253     public PermissionsRolesList getCommonPartList() {
254         return permissionRolesList;
255     }
256
257     /* (non-Javadoc)
258      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#setCommonPartList(java.lang.Object)
259      */
260     @Override
261     public void setCommonPartList(PermissionsRolesList permissionRolesList) {
262         this.permissionRolesList = permissionRolesList;
263     }
264
265     /* (non-Javadoc)
266      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getQProperty(java.lang.String)
267      */
268     @Override
269     public String getQProperty(
270             String prop) {
271         return null;
272     }
273
274     /* (non-Javadoc)
275      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#createDocumentFilter()
276      */
277     @Override
278     public DocumentFilter createDocumentFilter() {
279         return new JpaDocumentFilter(this.getServiceContext());
280     }
281 }