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