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