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.common.AbstractCollectionSpaceResourceImpl;
27 import org.collectionspace.services.common.context.RemoteServiceContextFactory;
28 import org.collectionspace.services.common.context.ServiceContext;
29 import org.collectionspace.services.common.context.ServiceContextFactory;
30 import org.collectionspace.services.common.document.DocumentHandler;
31 import org.collectionspace.services.common.storage.StorageClient;
32 import org.collectionspace.services.common.storage.jpa.JpaRelationshipStorageClient;
33 import org.collectionspace.services.common.context.ServiceContextProperties;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * PermissionRoleSubResource is used to manage permission-role relationship
41 public class PermissionRoleSubResource
42 extends AbstractCollectionSpaceResourceImpl<PermissionRole, PermissionRole> {
44 //this service is never exposed as standalone RESTful service...just use unique
45 //service name to identify binding
46 /** The service name. */
47 final private String serviceName = "authorization/permroles";
49 final Logger logger = LoggerFactory.getLogger(PermissionRoleSubResource.class);
50 /** The storage client. */
51 final StorageClient storageClient = new JpaRelationshipStorageClient<PermissionRole>();
54 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
57 protected String getVersionString() {
58 /** The last change revision. */
59 final String lastChangeRevision = "$LastChangedRevision: 1165 $";
60 return lastChangeRevision;
64 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
67 public String getServiceName() {
72 * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
75 public Class<PermissionRole> getCommonPartClass() {
76 return PermissionRole.class;
80 * @see org.collectionspace.services.common.CollectionSpaceResource#getServiceContextFactory()
83 public ServiceContextFactory<PermissionRole, PermissionRole> getServiceContextFactory() {
84 return RemoteServiceContextFactory.get();
88 * Creates the service context.
90 * @param input the input
91 * @param subject the subject
93 * @return the service context< permission role, permission role>
95 * @throws Exception the exception
97 private ServiceContext<PermissionRole, PermissionRole> createServiceContext(PermissionRole input,
98 SubjectType subject) throws Exception {
99 ServiceContext<PermissionRole, PermissionRole> ctx = createServiceContext(input);
100 ctx.setDocumentType(PermissionRole.class.getPackage().getName()); //persistence unit
101 ctx.setProperty(ServiceContextProperties.ENTITY_NAME, PermissionRoleRel.class.getName());
102 ctx.setProperty(ServiceContextProperties.ENTITY_CLASS, PermissionRoleRel.class);
103 //subject name is necessary to indicate if role or permission is a subject
104 ctx.setProperty(ServiceContextProperties.SUBJECT, subject);
105 //set context for the relationship query
106 ctx.setProperty(ServiceContextProperties.OBJECT_CLASS, Permission.class);
107 ctx.setProperty(ServiceContextProperties.OBJECT_ID, "permission_id");
112 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getStorageClient(org.collectionspace.services.common.context.ServiceContext)
115 public StorageClient getStorageClient(ServiceContext<PermissionRole, PermissionRole> ctx) {
116 //FIXME use ctx to identify storage client
117 return storageClient;
121 // public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
122 // DocumentHandler docHandler = ctx.getDocumentHandler();
123 // docHandler.setCommonPart(ctx.getInput());
124 // return docHandler;
127 * createPermissionRole creates one or more permission-role relationships
128 * between object (permission/role) and subject (role/permission)
134 public String createPermissionRole(PermissionRole input, SubjectType subject)
137 ServiceContext<PermissionRole, PermissionRole> ctx = createServiceContext(input, subject);
138 DocumentHandler handler = createDocumentHandler(ctx);
139 return getStorageClient(ctx).create(ctx, handler);
143 * getPermissionRole retrieves permission-role relationships using given
144 * csid of object (permission/role) and subject (role/permission)
150 public PermissionRole getPermissionRole(
151 String csid, SubjectType subject) throws Exception {
153 if (logger.isDebugEnabled()) {
154 logger.debug("getPermissionRole with csid=" + csid);
156 PermissionRole result = null;
157 ServiceContext<PermissionRole, PermissionRole> ctx = createServiceContext((PermissionRole) null, subject);
158 DocumentHandler handler = createDocumentHandler(ctx);
159 getStorageClient(ctx).get(ctx, csid, handler);
160 result = (PermissionRole) ctx.getOutput();
166 * deletePermissionRole deletes permission-role relationships using given
167 * csid of object (permission/role) and subject (role/permission)
173 public void deletePermissionRole(String csid,
174 SubjectType subject) throws Exception {
176 if (logger.isDebugEnabled()) {
177 logger.debug("deletePermissionRole with csid=" + csid);
179 ServiceContext<PermissionRole, PermissionRole> ctx = createServiceContext((PermissionRole) null, subject);
180 DocumentHandler handler = createDocumentHandler(ctx);
181 getStorageClient(ctx).delete(ctx, csid, handler);