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