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.storage.PermissionRoleDocumentHandler;
28 import org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl;
29 import org.collectionspace.services.common.context.RemoteServiceContextFactory;
30 import org.collectionspace.services.common.context.ServiceContext;
31 import org.collectionspace.services.common.context.ServiceContextFactory;
32 import org.collectionspace.services.common.document.DocumentHandler;
33 import org.collectionspace.services.common.storage.StorageClient;
34 import org.collectionspace.services.common.storage.jpa.JpaRelationshipStorageClient;
35 import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
36 import org.collectionspace.services.common.context.ServiceContextProperties;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * PermissionRoleSubResource is used to manage permission-role relationship
45 public class PermissionRoleSubResource
46 extends AbstractCollectionSpaceResourceImpl<PermissionRole, PermissionRole> {
48 public final static String ROLE_PERMROLE_SERVICE = "authorization/roles/permroles";
49 public final static String PERMISSION_PERMROLE_SERVICE = "authorization/permissions/permroles";
50 //this service is never exposed as standalone RESTful service...just use unique
51 //service name to identify binding
52 /** The service name. */
53 private String serviceName = "authorization/permroles";
55 final Logger logger = LoggerFactory.getLogger(PermissionRoleSubResource.class);
56 /** The storage client. */
57 final StorageClient storageClient = new JpaRelationshipStorageClient<PermissionRole>();
59 private String permissionRoleCsid = null;
62 * Instantiates a new permission role sub resource.
64 * @param serviceName the service name
66 public PermissionRoleSubResource(String serviceName) {
67 this.serviceName = serviceName;
71 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
74 protected String getVersionString() {
75 /** The last change revision. */
76 final String lastChangeRevision = "$LastChangedRevision: 1165 $";
77 return lastChangeRevision;
81 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
84 public String getServiceName() {
89 * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
92 public Class<PermissionRole> getCommonPartClass() {
93 return PermissionRole.class;
97 * @see org.collectionspace.services.common.CollectionSpaceResource#getServiceContextFactory()
100 public ServiceContextFactory<PermissionRole, PermissionRole> getServiceContextFactory() {
101 return RemoteServiceContextFactory.get();
105 * Creates the service context.
107 * @param input the input
108 * @param subject the subject
110 * @return the service context< permission role, permission role>
112 * @throws Exception the exception
114 private ServiceContext<PermissionRole, PermissionRole> createServiceContext(PermissionRole input,
115 SubjectType subject) throws Exception {
116 ServiceContext<PermissionRole, PermissionRole> ctx = createServiceContext(input);
117 ctx.setDocumentType(PermissionRole.class.getPackage().getName()); //persistence unit
118 ctx.setProperty(ServiceContextProperties.ENTITY_NAME, PermissionRoleRel.class.getName());
119 ctx.setProperty(ServiceContextProperties.ENTITY_CLASS, PermissionRoleRel.class);
120 //subject name is necessary to indicate if role or permission is a subject
121 ctx.setProperty(ServiceContextProperties.SUBJECT, subject);
122 //set context for the relationship query
123 if (subject == SubjectType.ROLE) {
124 ctx.setProperty(ServiceContextProperties.OBJECT_CLASS, Permission.class);
125 ctx.setProperty(ServiceContextProperties.OBJECT_ID, "permission_id");
126 } else if (subject == SubjectType.PERMISSION) {
127 ctx.setProperty(ServiceContextProperties.OBJECT_CLASS, Role.class);
128 ctx.setProperty(ServiceContextProperties.OBJECT_ID, "role_id");
134 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getStorageClient(org.collectionspace.services.common.context.ServiceContext)
137 public StorageClient getStorageClient(ServiceContext<PermissionRole, PermissionRole> ctx) {
138 //FIXME use ctx to identify storage client
139 return storageClient;
143 // public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
144 // DocumentHandler docHandler = ctx.getDocumentHandler();
145 // docHandler.setCommonPart(ctx.getInput());
146 // return docHandler;
149 * createPermissionRole creates one or more permission-role relationships
150 * between object (permission/role) and subject (role/permission)
156 public String createPermissionRole(PermissionRole input, SubjectType subject)
159 ServiceContext<PermissionRole, PermissionRole> ctx = createServiceContext(input, subject);
160 DocumentHandler handler = createDocumentHandler(ctx);
161 return getStorageClient(ctx).create(ctx, handler);
165 * Gets the permission role rel.
167 * @param csid the csid
168 * @param subject the subject
169 * @param permissionRoleCsid the permission role csid
170 * @return the permission role rel
171 * @throws Exception the exception
173 public PermissionRoleRel getPermissionRoleRel(String csid,
175 String permissionRoleCsid) throws Exception {
177 if (logger.isDebugEnabled()) {
178 logger.debug("getAccountRole with csid=" + csid);
180 // AccountRolesList result = new AccountRolesList();
181 ServiceContext<PermissionRole, PermissionRole> ctx = createServiceContext((PermissionRole) null, subject);
182 PermissionRoleDocumentHandler handler = (PermissionRoleDocumentHandler)createDocumentHandler(ctx);
183 handler.setPermissionRoleCsid(permissionRoleCsid);
184 //getStorageClient(ctx).get(ctx, csid, handler);
185 PermissionRoleRel permissionRoleRel = (PermissionRoleRel)JpaStorageUtils.getEntity(
186 new Long(permissionRoleCsid).longValue(), PermissionRoleRel.class);
187 // List<AccountRoleListItem> accountRoleList = result.getAccountRoleListItems();
188 // AccountRoleListItem listItem = new AccountRoleListItem();
190 // listItem.setCsid(accountRoleRel.getHjid().toString());
191 // listItem.setRoleId(accountRoleRel.getRoleId());
192 // listItem.setRoleName(accountRoleRel.getRoleName());
193 // add item to result list
194 // result = (AccountRolesList) ctx.getOutput();
196 return permissionRoleRel;
200 * getPermissionRole retrieves permission-role relationships using given
201 * csid of object (permission/role) and subject (role/permission)
207 public PermissionRole getPermissionRole(
208 String csid, SubjectType subject) throws Exception {
210 if (logger.isDebugEnabled()) {
211 logger.debug("getPermissionRole with csid=" + csid);
213 PermissionRole result = null;
214 ServiceContext<PermissionRole, PermissionRole> ctx = createServiceContext((PermissionRole) null, subject);
215 DocumentHandler handler = createDocumentHandler(ctx);
216 getStorageClient(ctx).get(ctx, csid, handler);
217 result = (PermissionRole) ctx.getOutput();
223 * deletePermissionRole deletes permission-role relationships using given
224 * csid of object (permission/role) and subject (role/permission)
230 public void deletePermissionRole(String csid,
231 SubjectType subject) throws Exception {
233 if (logger.isDebugEnabled()) {
234 logger.debug("deletePermissionRole with csid=" + csid);
236 PermissionRole permRole = this.getPermissionRole(csid, subject);
237 this.deletePermissionRole(csid, subject, permRole);
241 * deletePermissionRole deletes permission-role relationships using given
242 * csid of object (permission/role) and subject (role/permission)
245 * @param input with role and permission relationships to delete
249 public void deletePermissionRole(String csid,
250 SubjectType subject, PermissionRole input) throws Exception {
252 if (logger.isDebugEnabled()) {
253 logger.debug("deletePermissionRole(input) with csid=" + csid);
255 ServiceContext<PermissionRole, PermissionRole> ctx = createServiceContext(input, subject);
256 DocumentHandler handler = createDocumentHandler(ctx);
257 getStorageClient(ctx).delete(ctx, csid, handler);