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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
24 package org.collectionspace.services.authorization;
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;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * PermissionRoleSubResource is used to manage permission-role relationship
51 @SuppressWarnings("rawtypes")
52 public class PermissionRoleSubResource
53 extends AbstractCollectionSpaceResourceImpl<PermissionRole, PermissionRole> {
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";
62 final Logger logger = LoggerFactory.getLogger(PermissionRoleSubResource.class);
63 /** The storage client. */
64 final StorageClient storageClient = new JpaRelationshipStorageClient<PermissionRole>();
66 * Instantiates a new permission role sub resource.
68 * @param serviceName the service name
70 public PermissionRoleSubResource(String serviceName) {
71 this.serviceName = serviceName;
75 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
78 protected String getVersionString() {
79 /** The last change revision. */
80 final String lastChangeRevision = "$LastChangedRevision: 1165 $";
81 return lastChangeRevision;
85 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
88 public String getServiceName() {
93 * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
96 public Class<PermissionRole> getCommonPartClass() {
97 return PermissionRole.class;
101 * @see org.collectionspace.services.common.CollectionSpaceResource#getServiceContextFactory()
103 @SuppressWarnings("unchecked")
105 public ServiceContextFactory<PermissionRole, PermissionRole> getServiceContextFactory() {
106 return RemoteServiceContextFactory.get();
110 * Creates the service context.
112 * @param input the input
113 * @param subject the subject
115 * @return the service context< permission role, permission role>
117 * @throws Exception the exception
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;
126 // If the parent context has an active JPA connection then we'll use it.
128 if (parentTransactionContext != null) {
129 ctx.setTransactionContext(parentTransactionContext);
132 // Set other context values
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");
151 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getStorageClient(org.collectionspace.services.common.context.ServiceContext)
154 public StorageClient getStorageClient(ServiceContext<PermissionRole, PermissionRole> ctx) {
155 //FIXME use ctx to identify storage client
156 return storageClient;
160 // public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
161 // DocumentHandler docHandler = ctx.getDocumentHandler();
162 // docHandler.setCommonPart(ctx.getInput());
163 // return docHandler;
166 * createPermissionRole creates one or more permission-role relationships
167 * between object (permission/role) and subject (role/permission)
173 public String createPermissionRole(ServiceContext parentCtx, PermissionRole input, SubjectType subject)
176 ServiceContext<PermissionRole, PermissionRole> ctx = createServiceContext(parentCtx, input, subject);
177 DocumentHandler handler = createDocumentHandler(ctx);
178 return getStorageClient(ctx).create(ctx, handler);
182 * Gets the permission role rel.
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
190 public PermissionRoleRel getPermissionRoleRel(
191 ServiceContext parentCtx,
194 String permissionRoleCsid) throws Exception {
196 if (logger.isDebugEnabled()) {
197 logger.debug("getAccountRole with csid=" + csid);
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);
207 return permissionRoleRel;
211 * getPermissionRole retrieves permission-role relationships using given
212 * csid of object (permission/role) and subject (role/permission)
218 public PermissionRole getPermissionRole(
219 ServiceContext parentCtx,
221 SubjectType subject) throws Exception {
223 if (logger.isDebugEnabled()) {
224 logger.debug("getPermissionRole with csid=" + csid);
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();
236 * deletePermissionRole deletes permission-role relationships using given
237 * csid of object (permission/role) and subject (role/permission)
243 public void deletePermissionRole(ServiceContext parentCtx,
245 SubjectType subject) throws Exception {
247 if (logger.isDebugEnabled()) {
248 logger.debug("deletePermissionRole with csid=" + csid);
250 PermissionRole permRole = getPermissionRole(parentCtx, csid, subject);
251 if (permRole != null) {
252 deletePermissionRole(parentCtx, csid, subject, permRole);
254 String msg = String.format("The permission CSID=%s is missing or not related to any roles.", csid);
255 throw new DocumentNotFoundException(msg);
260 * deletePermissionRole deletes permission-role relationships using given
261 * csid of object (permission/role) and subject (role/permission)
264 * @param input with role and permission relationships to delete
268 public void deletePermissionRole(ServiceContext parentCtx, String csid,
269 SubjectType subject, PermissionRole input) throws Exception {
271 if (logger.isDebugEnabled()) {
272 logger.debug("deletePermissionRole(input) with csid=" + csid);
274 ServiceContext<PermissionRole, PermissionRole> ctx = createServiceContext(parentCtx, input, subject);
275 DocumentHandler handler = createDocumentHandler(ctx);
276 getStorageClient(ctx).delete(ctx, csid, handler);