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.account;
26 import org.collectionspace.services.authorization.AccountRole;
27 import org.collectionspace.services.authorization.AccountRoleRel;
28 import org.collectionspace.services.authorization.SubjectType;
30 import org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl;
31 import org.collectionspace.services.common.context.RemoteServiceContextFactory;
32 import org.collectionspace.services.common.context.ServiceContext;
33 import org.collectionspace.services.common.context.ServiceContextFactory;
34 import org.collectionspace.services.common.document.DocumentHandler;
35 import org.collectionspace.services.common.storage.StorageClient;
36 import org.collectionspace.services.common.storage.jpa.JpaRelationshipStorageClient;
37 import org.collectionspace.services.common.context.ServiceContextProperties;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * AccountRoleSubResource is used to manage account-role relationship
46 public class AccountRoleSubResource
47 extends AbstractCollectionSpaceResourceImpl<AccountRole, AccountRole> {
49 final public static String ACCOUNT_ACCOUNTROLE_SERVICE = "accounts/accountroles";
50 final public static String ROLE_ACCOUNTROLE_SERVICE = "roles/accountroles";
51 //this service is never exposed as standalone RESTful service...just use unique
52 //service name to identify binding
53 /** The service name. */
54 private String serviceName = ACCOUNT_ACCOUNTROLE_SERVICE;
56 final Logger logger = LoggerFactory.getLogger(AccountRoleSubResource.class);
57 /** The storage client. */
58 final StorageClient storageClient = new JpaRelationshipStorageClient<AccountRole>();
62 * @param serviceName qualified service path
64 AccountRoleSubResource(String serviceName) {
65 this.serviceName = serviceName;
69 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
72 protected String getVersionString() {
73 /** The last change revision. */
74 final String lastChangeRevision = "$LastChangedRevision: 1165 $";
75 return lastChangeRevision;
79 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
82 public String getServiceName() {
87 * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
90 public Class<AccountRole> getCommonPartClass() {
91 return AccountRole.class;
95 * @see org.collectionspace.services.common.CollectionSpaceResource#getServiceContextFactory()
98 public ServiceContextFactory<AccountRole, AccountRole> getServiceContextFactory() {
99 return RemoteServiceContextFactory.get();
103 * Creates the service context.
105 * @param input the input
106 * @param subject the subject
108 * @return the service context< account role, account role>
110 * @throws Exception the exception
112 private ServiceContext<AccountRole, AccountRole> createServiceContext(AccountRole input,
113 SubjectType subject) throws Exception {
114 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input);
115 ctx.setDocumentType(AccountRole.class.getPackage().getName()); //persistence unit
116 ctx.setProperty(ServiceContextProperties.ENTITY_NAME, AccountRoleRel.class.getName());
117 ctx.setProperty(ServiceContextProperties.ENTITY_CLASS, AccountRoleRel.class);
118 //subject name is necessary to indicate if role or account is a subject
119 ctx.setProperty(ServiceContextProperties.SUBJECT, subject);
120 //set context for the relationship query
121 ctx.setProperty(ServiceContextProperties.OBJECT_CLASS, AccountsCommon.class);
122 ctx.setProperty(ServiceContextProperties.OBJECT_ID, "account_id");
127 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getStorageClient(org.collectionspace.services.common.context.ServiceContext)
130 public StorageClient getStorageClient(ServiceContext<AccountRole, AccountRole> ctx) {
131 //FIXME use ctx to identify storage client
132 return storageClient;
136 * createAccountRole creates one or more account-role relationships
137 * between object (account/role) and subject (role/account)
143 public String createAccountRole(AccountRole input, SubjectType subject)
146 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input, subject);
147 DocumentHandler handler = createDocumentHandler(ctx);
148 return getStorageClient(ctx).create(ctx, handler);
152 * getAccountRole retrieves account-role relationships using given
153 * csid of object (account/role) and subject (role/account)
159 public AccountRole getAccountRole(
160 String csid, SubjectType subject) throws Exception {
162 if (logger.isDebugEnabled()) {
163 logger.debug("getAccountRole with csid=" + csid);
165 AccountRole result = null;
166 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext((AccountRole) null, subject);
167 DocumentHandler handler = createDocumentHandler(ctx);
168 getStorageClient(ctx).get(ctx, csid, handler);
169 result = (AccountRole) ctx.getOutput();
175 * deleteAccountRole deletes all account-role relationships using given
176 * csid of object (account/role) and subject (role/account)
177 * @param csid of the object
182 public void deleteAccountRole(String csid,
183 SubjectType subject) throws Exception {
185 if (logger.isDebugEnabled()) {
186 logger.debug("deleteAccountRole with csid=" + csid);
188 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext((AccountRole) null, subject);
189 getStorageClient(ctx).delete(ctx, csid);
193 * deleteAccountRole deletes given account-role relationships using given
194 * csid of object (account/role) and subject (role/account)
195 * @param csid of the object
197 * @param input with account role relationships to delete
201 public void deleteAccountRole(String csid, SubjectType subject, AccountRole input)
204 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input, subject);
205 DocumentHandler handler = createDocumentHandler(ctx);
206 getStorageClient(ctx).delete(ctx, csid, handler);