]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
4d02fd137199ee55bf998c650959c35904865ca1
[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;
25
26 import org.collectionspace.services.authorization.PermissionRole;
27 import org.collectionspace.services.authorization.PermissionRoleRel;
28 import org.collectionspace.services.authorization.Role;
29 import org.collectionspace.services.authorization.SubjectType;
30 import org.collectionspace.services.authorization.perms.Permission;
31 import org.collectionspace.services.authorization.storage.PermissionRoleDocumentHandler;
32 import org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl;
33 import org.collectionspace.services.common.context.RemoteServiceContextFactory;
34 import org.collectionspace.services.common.context.ServiceContext;
35 import org.collectionspace.services.common.context.ServiceContextFactory;
36 import org.collectionspace.services.common.document.DocumentHandler;
37 import org.collectionspace.services.common.storage.StorageClient;
38 import org.collectionspace.services.common.storage.jpa.JPATransactionContext;
39 import org.collectionspace.services.common.storage.jpa.JpaRelationshipStorageClient;
40 import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
41 import org.collectionspace.services.common.context.ServiceContextProperties;
42
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 /**
47  * PermissionRoleSubResource is used to manage permission-role relationship
48  * @author
49  */
50 @SuppressWarnings("rawtypes")
51 public class PermissionRoleSubResource
52         extends AbstractCollectionSpaceResourceImpl<PermissionRole, PermissionRole> {
53
54     public final static String ROLE_PERMROLE_SERVICE = "authorization/roles/permroles";
55     public final static String PERMISSION_PERMROLE_SERVICE = "authorization/permissions/permroles";
56     //this service is never exposed as standalone RESTful service...just use unique
57     //service name to identify binding
58     /** The service name. */
59     private String serviceName = "authorization/permroles";
60     /** The logger. */
61     final Logger logger = LoggerFactory.getLogger(PermissionRoleSubResource.class);
62     /** The storage client. */
63     final StorageClient storageClient = new JpaRelationshipStorageClient<PermissionRole>();
64     /**
65      * Instantiates a new permission role sub resource.
66      *
67      * @param serviceName the service name
68      */
69     public PermissionRoleSubResource(String serviceName) {
70         this.serviceName = serviceName;
71     }
72     
73     /* (non-Javadoc)
74      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
75      */
76     @Override
77     protected String getVersionString() {
78         /** The last change revision. */
79         final String lastChangeRevision = "$LastChangedRevision: 1165 $";
80         return lastChangeRevision;
81     }
82
83     /* (non-Javadoc)
84      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
85      */
86     @Override
87     public String getServiceName() {
88         return serviceName;
89     }
90
91     /* (non-Javadoc)
92      * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
93      */
94     @Override
95     public Class<PermissionRole> getCommonPartClass() {
96         return PermissionRole.class;
97     }
98
99     /* (non-Javadoc)
100      * @see org.collectionspace.services.common.CollectionSpaceResource#getServiceContextFactory()
101      */
102     @SuppressWarnings("unchecked")
103         @Override
104     public ServiceContextFactory<PermissionRole, PermissionRole> getServiceContextFactory() {
105         return RemoteServiceContextFactory.get();
106     }
107
108     /**
109      * Creates the service context.
110      * 
111      * @param input the input
112      * @param subject the subject
113      * 
114      * @return the service context< permission role, permission role>
115      * 
116      * @throws Exception the exception
117      */
118     private ServiceContext<PermissionRole, PermissionRole> createServiceContext(
119                 ServiceContext parentCtx,
120                 PermissionRole input,
121             SubjectType subject) throws Exception {
122         ServiceContext<PermissionRole, PermissionRole> ctx = createServiceContext(input);
123         JPATransactionContext parentTransactionContext = parentCtx != null ? (JPATransactionContext)parentCtx.getCurrentTransactionContext() : null;
124         //
125         // If the parent context has an active JPA connection then we'll use it.
126         //
127         if (parentTransactionContext != null) {
128                 ctx.setTransactionContext(parentTransactionContext);
129         }
130         //
131         // Set other context values
132         //
133         ctx.setDocumentType(PermissionRole.class.getPackage().getName()); //persistence unit
134         ctx.setProperty(ServiceContextProperties.ENTITY_NAME, PermissionRoleRel.class.getName());
135         ctx.setProperty(ServiceContextProperties.ENTITY_CLASS, PermissionRoleRel.class);
136         //subject name is necessary to indicate if role or permission is a subject
137         ctx.setProperty(ServiceContextProperties.SUBJECT, subject);
138         //set context for the relationship query
139         if (subject == SubjectType.ROLE) {
140             ctx.setProperty(ServiceContextProperties.OBJECT_CLASS, Permission.class);
141             ctx.setProperty(ServiceContextProperties.OBJECT_ID, "permission_id");
142         } else if (subject == SubjectType.PERMISSION) {
143             ctx.setProperty(ServiceContextProperties.OBJECT_CLASS, Role.class);
144             ctx.setProperty(ServiceContextProperties.OBJECT_ID, "role_id");
145         }
146         return ctx;
147     }
148
149     /* (non-Javadoc)
150      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getStorageClient(org.collectionspace.services.common.context.ServiceContext)
151      */
152     @Override
153     public StorageClient getStorageClient(ServiceContext<PermissionRole, PermissionRole> ctx) {
154         //FIXME use ctx to identify storage client
155         return storageClient;
156     }
157
158 //    @Override
159 //    public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
160 //        DocumentHandler docHandler = ctx.getDocumentHandler();
161 //        docHandler.setCommonPart(ctx.getInput());
162 //        return docHandler;
163 //    }
164     /**
165      * createPermissionRole creates one or more permission-role relationships
166      * between object (permission/role) and subject (role/permission)
167      * @param input
168      * @param subject
169      * @return
170      * @throws Exception
171      */
172     public String createPermissionRole(ServiceContext parentCtx, PermissionRole input, SubjectType subject)
173             throws Exception {
174
175         ServiceContext<PermissionRole, PermissionRole> ctx = createServiceContext(parentCtx, input, subject);
176         DocumentHandler handler = createDocumentHandler(ctx);
177         return getStorageClient(ctx).create(ctx, handler);
178     }
179
180     /**
181      * Gets the permission role rel.
182      *
183      * @param csid the csid
184      * @param subject the subject
185      * @param permissionRoleCsid the permission role csid
186      * @return the permission role rel
187      * @throws Exception the exception
188      */
189     public PermissionRoleRel getPermissionRoleRel(
190                 ServiceContext parentCtx,
191                 String csid,
192                 SubjectType subject,
193                 String permissionRoleCsid) throws Exception {
194
195         if (logger.isDebugEnabled()) {
196             logger.debug("getAccountRole with csid=" + csid);
197         }
198
199         ServiceContext<PermissionRole, PermissionRole> ctx = createServiceContext(parentCtx, (PermissionRole) null, subject);
200         PermissionRoleDocumentHandler handler = (PermissionRoleDocumentHandler)createDocumentHandler(ctx);
201         handler.setPermissionRoleCsid(permissionRoleCsid);
202         //getStorageClient(ctx).get(ctx, csid, handler);
203         PermissionRoleRel permissionRoleRel = (PermissionRoleRel)JpaStorageUtils.getEntity(
204                         new Long(permissionRoleCsid).longValue(), PermissionRoleRel.class);
205         
206         return permissionRoleRel;
207     }
208     
209     /**
210      * getPermissionRole retrieves permission-role relationships using given
211      * csid of object (permission/role) and subject (role/permission)
212      * @param csid
213      * @param subject
214      * @return
215      * @throws Exception
216      */
217     public PermissionRole getPermissionRole(
218                 ServiceContext parentCtx,
219             String csid, 
220             SubjectType subject) throws Exception {
221
222         if (logger.isDebugEnabled()) {
223             logger.debug("getPermissionRole with csid=" + csid);
224         }
225         PermissionRole result = null;
226         ServiceContext<PermissionRole, PermissionRole> ctx = createServiceContext(parentCtx, (PermissionRole) null, subject);
227         DocumentHandler handler = createDocumentHandler(ctx);
228         getStorageClient(ctx).get(ctx, csid, handler);
229         result = (PermissionRole) ctx.getOutput();
230
231         return result;
232     }
233
234     /**
235      * deletePermissionRole deletes permission-role relationships using given
236      * csid of object (permission/role) and subject (role/permission)
237      * @param csid
238      * @param subject
239      * @return
240      * @throws Exception
241      */
242     public void deletePermissionRole(ServiceContext parentCtx,
243                 String csid,
244             SubjectType subject) throws Exception {
245
246         if (logger.isDebugEnabled()) {
247             logger.debug("deletePermissionRole with csid=" + csid);
248         }
249         PermissionRole permRole = this.getPermissionRole(parentCtx, csid, subject);
250         deletePermissionRole(parentCtx, csid, subject, permRole);
251     }
252
253     /**
254      * deletePermissionRole deletes permission-role relationships using given
255      * csid of object (permission/role) and subject (role/permission)
256      * @param csid
257      * @param subject
258      * @param input with role and permission relationships to delete
259      * @return
260      * @throws Exception
261      */
262         public void deletePermissionRole(ServiceContext parentCtx, String csid,
263             SubjectType subject, PermissionRole input) throws Exception {
264
265         if (logger.isDebugEnabled()) {
266             logger.debug("deletePermissionRole(input) with csid=" + csid);
267         }
268         ServiceContext<PermissionRole, PermissionRole> ctx = createServiceContext(parentCtx, input, subject);
269         DocumentHandler handler = createDocumentHandler(ctx);
270         getStorageClient(ctx).delete(ctx, csid, handler);
271     }
272 }