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 ctx.setDocumentType(PermissionRole.class.getPackage().getName()); //persistence unit
100 ctx.setProperty("entity-name", PermissionRoleRel.class.getName());
101 //subject name is necessary to indicate if role or permission is a subject
102 ctx.setProperty("subject", subject);
103 //set context for the relationship query
104 ctx.setProperty("object-class", Permission.class);
105 ctx.setProperty("object-id", "permission_id");
110 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getStorageClient(org.collectionspace.services.common.context.ServiceContext)
113 public StorageClient getStorageClient(ServiceContext<PermissionRole, PermissionRole> ctx) {
114 //FIXME use ctx to identify storage client
115 return storageClient;
119 // public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
120 // DocumentHandler docHandler = ctx.getDocumentHandler();
121 // docHandler.setCommonPart(ctx.getInput());
122 // return docHandler;
125 * createPermissionRole creates one or more permission-role relationships
126 * between object (permission/role) and subject (role/permission)
132 public String createPermissionRole(PermissionRole input, SubjectType subject)
135 ServiceContext<PermissionRole, PermissionRole> ctx = createServiceContext(input, subject);
136 DocumentHandler handler = createDocumentHandler(ctx);
137 return getStorageClient(ctx).create(ctx, handler);
141 * getPermissionRole retrieves permission-role relationships using given
142 * csid of object (permission/role) and subject (role/permission)
148 public PermissionRole getPermissionRole(
149 String csid, SubjectType subject) throws Exception {
151 if (logger.isDebugEnabled()) {
152 logger.debug("getPermissionRole with csid=" + csid);
154 PermissionRole result = null;
155 ServiceContext<PermissionRole, PermissionRole> ctx = createServiceContext((PermissionRole) null, subject);
156 DocumentHandler handler = createDocumentHandler(ctx);
157 getStorageClient(ctx).get(ctx, csid, handler);
158 result = (PermissionRole) ctx.getOutput();
164 * deletePermissionRole deletes permission-role relationships using given
165 * csid of object (permission/role) and subject (role/permission)
171 public void deletePermissionRole(String csid,
172 SubjectType subject) throws Exception {
174 if (logger.isDebugEnabled()) {
175 logger.debug("deletePermissionRole with csid=" + csid);
177 ServiceContext<PermissionRole, PermissionRole> ctx = createServiceContext((PermissionRole) null, subject);
178 getStorageClient(ctx).delete(ctx, csid);