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.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * PermissionRoleSubResource is used to manage permission-role relationship
40 public class PermissionRoleSubResource
41 extends AbstractCollectionSpaceResourceImpl<PermissionRole, PermissionRole> {
43 //this service is never exposed as standalone RESTful service...just use unique
44 //service name to identify binding
45 /** The service name. */
46 final private String serviceName = "authorization/permroles";
48 final Logger logger = LoggerFactory.getLogger(PermissionRoleSubResource.class);
49 /** The storage client. */
50 final StorageClient storageClient = new JpaRelationshipStorageClient();
53 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
56 protected String getVersionString() {
57 /** The last change revision. */
58 final String lastChangeRevision = "$LastChangedRevision: 1165 $";
59 return lastChangeRevision;
63 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
66 public String getServiceName() {
71 * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
74 public Class<PermissionRole> getCommonPartClass() {
75 return PermissionRole.class;
79 * @see org.collectionspace.services.common.CollectionSpaceResource#getServiceContextFactory()
82 public ServiceContextFactory<PermissionRole, PermissionRole> getServiceContextFactory() {
83 return RemoteServiceContextFactory.get();
87 * Creates the service context.
89 * @param input the input
90 * @param subject the subject
92 * @return the service context< permission role, permission role>
94 * @throws Exception the exception
96 private ServiceContext<PermissionRole, PermissionRole> createServiceContext(PermissionRole input,
97 SubjectType subject) throws Exception {
98 ServiceContext<PermissionRole, PermissionRole> ctx = createServiceContext(input);
99 // ServiceContext ctx = new RemoteServiceContextImpl<T, T>(getServiceName());
100 // ctx.setInput(input);
101 ctx.setDocumentType(PermissionRole.class.getPackage().getName()); //persistence unit
102 ctx.setProperty("entity-name", PermissionRoleRel.class.getName());
103 //subject name is necessary to indicate if role or permission is a subject
104 ctx.setProperty("subject", subject);
105 //set context for the relationship query
106 ctx.setProperty("objectId", "permission_id");
111 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getStorageClient(org.collectionspace.services.common.context.ServiceContext)
114 public StorageClient getStorageClient(ServiceContext<PermissionRole, PermissionRole> ctx) {
115 //FIXME use ctx to identify storage client
116 return storageClient;
120 // public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
121 // DocumentHandler docHandler = ctx.getDocumentHandler();
122 // docHandler.setCommonPart(ctx.getInput());
123 // return docHandler;
126 * createPermissionRole creates one or more permission-role relationships
127 * between object (permission/role) and subject (role/permission)
133 public String createPermissionRole(PermissionRole input, SubjectType subject)
136 ServiceContext<PermissionRole, PermissionRole> ctx = createServiceContext(input, subject);
137 DocumentHandler handler = createDocumentHandler(ctx);
138 return getStorageClient(ctx).create(ctx, handler);
142 * getPermissionRole retrieves permission-role relationships using given
143 * csid of object (permission/role) and subject (role/permission)
149 public PermissionRole getPermissionRole(
150 String csid, SubjectType subject) throws Exception {
152 if (logger.isDebugEnabled()) {
153 logger.debug("getPermissionRole with csid=" + csid);
155 PermissionRole result = null;
156 ServiceContext<PermissionRole, PermissionRole> ctx = createServiceContext((PermissionRole) null, subject);
157 DocumentHandler handler = createDocumentHandler(ctx);
158 getStorageClient(ctx).get(ctx, csid, handler);
159 result = (PermissionRole) ctx.getOutput();
165 * deletePermissionRole deletes permission-role relationships using given
166 * csid of object (permission/role) and subject (role/permission)
172 public void deletePermissionRole(String csid,
173 SubjectType subject) throws Exception {
175 if (logger.isDebugEnabled()) {
176 logger.debug("deletePermissionRole with csid=" + csid);
178 ServiceContext<PermissionRole, PermissionRole> ctx = createServiceContext((PermissionRole) null, subject);
179 getStorageClient(ctx).delete(ctx, csid);