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.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * AccountRoleSubResource is used to manage account-role relationship
44 public class AccountRoleSubResource
45 extends AbstractCollectionSpaceResourceImpl<AccountRole, AccountRole> {
47 //this service is never exposed as standalone RESTful service...just use unique
48 //service name to identify binding
49 /** The service name. */
50 final private String serviceName = "accounts/accountroles";
53 final Logger logger = LoggerFactory.getLogger(AccountRoleSubResource.class);
55 /** The storage client. */
56 final StorageClient storageClient = new JpaRelationshipStorageClient();
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("entity-name", AccountRoleRel.class.getName());
107 //subject name is necessary to indicate if role or account is a subject
108 ctx.setProperty("subject", subject);
109 //set context that for the relationship query
110 ctx.setProperty("objectId", "account_id");
115 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getStorageClient(org.collectionspace.services.common.context.ServiceContext)
118 public StorageClient getStorageClient(ServiceContext<AccountRole, AccountRole> ctx) {
119 //FIXME use ctx to identify storage client
120 return storageClient;
125 * createAccountRole creates one or more account-role relationships
126 * between object (account/role) and subject (role/account)
132 public String createAccountRole(AccountRole input, SubjectType subject)
135 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input, subject);
136 DocumentHandler handler = createDocumentHandler(ctx);
137 return getStorageClient(ctx).create(ctx, handler);
141 * getAccountRole retrieves account-role relationships using given
142 * csid of object (account/role) and subject (role/account)
148 public AccountRole getAccountRole(
149 String csid, SubjectType subject) throws Exception {
151 if (logger.isDebugEnabled()) {
152 logger.debug("getAccountRole with csid=" + csid);
154 AccountRole result = null;
155 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext((AccountRole) null, subject);
156 DocumentHandler handler = createDocumentHandler(ctx);
157 getStorageClient(ctx).get(ctx, csid, handler);
158 result = (AccountRole) ctx.getOutput();
164 * deleteAccountRole deletes account-role relationships using given
165 * csid of object (account/role) and subject (role/account)
171 public void deleteAccountRole(String csid,
172 SubjectType subject) throws Exception {
174 if (logger.isDebugEnabled()) {
175 logger.debug("deleteAccountRole with csid=" + csid);
177 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext((AccountRole) null, subject);
178 getStorageClient(ctx).delete(ctx, csid);