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 //this service is never exposed as standalone RESTful service...just use unique
50 //service name to identify binding
51 /** The service name. */
52 final private String serviceName = "accounts/accountroles";
54 final Logger logger = LoggerFactory.getLogger(AccountRoleSubResource.class);
55 /** The storage client. */
56 final StorageClient storageClient = new JpaRelationshipStorageClient<AccountRole>();
59 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
62 protected String getVersionString() {
63 /** The last change revision. */
64 final String lastChangeRevision = "$LastChangedRevision: 1165 $";
65 return lastChangeRevision;
69 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
72 public String getServiceName() {
77 * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
80 public Class<AccountRole> getCommonPartClass() {
81 return AccountRole.class;
85 * @see org.collectionspace.services.common.CollectionSpaceResource#getServiceContextFactory()
88 public ServiceContextFactory<AccountRole, AccountRole> getServiceContextFactory() {
89 return RemoteServiceContextFactory.get();
93 * Creates the service context.
95 * @param input the input
96 * @param subject the subject
98 * @return the service context< account role, account role>
100 * @throws Exception the exception
102 private ServiceContext<AccountRole, AccountRole> createServiceContext(AccountRole input,
103 SubjectType subject) throws Exception {
104 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input);
105 ctx.setDocumentType(AccountRole.class.getPackage().getName()); //persistence unit
106 ctx.setProperty(ServiceContextProperties.ENTITY_NAME, AccountRoleRel.class.getName());
107 ctx.setProperty(ServiceContextProperties.ENTITY_CLASS, AccountRoleRel.class);
108 //subject name is necessary to indicate if role or account is a subject
109 ctx.setProperty(ServiceContextProperties.SUBJECT, subject);
110 //set context for the relationship query
111 ctx.setProperty(ServiceContextProperties.OBJECT_CLASS, AccountsCommon.class);
112 ctx.setProperty(ServiceContextProperties.OBJECT_ID, "account_id");
117 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getStorageClient(org.collectionspace.services.common.context.ServiceContext)
120 public StorageClient getStorageClient(ServiceContext<AccountRole, AccountRole> ctx) {
121 //FIXME use ctx to identify storage client
122 return storageClient;
126 * createAccountRole creates one or more account-role relationships
127 * between object (account/role) and subject (role/account)
133 public String createAccountRole(AccountRole input, SubjectType subject)
136 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input, subject);
137 DocumentHandler handler = createDocumentHandler(ctx);
138 return getStorageClient(ctx).create(ctx, handler);
142 * getAccountRole retrieves account-role relationships using given
143 * csid of object (account/role) and subject (role/account)
149 public AccountRole getAccountRole(
150 String csid, SubjectType subject) throws Exception {
152 if (logger.isDebugEnabled()) {
153 logger.debug("getAccountRole with csid=" + csid);
155 AccountRole result = null;
156 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext((AccountRole) null, subject);
157 DocumentHandler handler = createDocumentHandler(ctx);
158 getStorageClient(ctx).get(ctx, csid, handler);
159 result = (AccountRole) ctx.getOutput();
165 * deleteAccountRole deletes account-role relationships using given
166 * csid of object (account/role) and subject (role/account)
172 public void deleteAccountRole(String csid,
173 SubjectType subject) throws Exception {
175 if (logger.isDebugEnabled()) {
176 logger.debug("deleteAccountRole with csid=" + csid);
178 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext((AccountRole) null, subject);
179 getStorageClient(ctx).delete(ctx, csid);