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;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * AccountRoleSubResource is used to manage account-role relationship
45 public class AccountRoleSubResource
46 extends AbstractCollectionSpaceResourceImpl<AccountRole, AccountRole> {
48 //this service is never exposed as standalone RESTful service...just use unique
49 //service name to identify binding
50 /** The service name. */
51 final private String serviceName = "accounts/accountroles";
53 final Logger logger = LoggerFactory.getLogger(AccountRoleSubResource.class);
54 /** The storage client. */
55 final StorageClient storageClient = new JpaRelationshipStorageClient();
58 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
61 protected String getVersionString() {
62 /** The last change revision. */
63 final String lastChangeRevision = "$LastChangedRevision: 1165 $";
64 return lastChangeRevision;
68 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
71 public String getServiceName() {
76 * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
79 public Class<AccountRole> getCommonPartClass() {
80 return AccountRole.class;
84 * @see org.collectionspace.services.common.CollectionSpaceResource#getServiceContextFactory()
87 public ServiceContextFactory<AccountRole, AccountRole> getServiceContextFactory() {
88 return RemoteServiceContextFactory.get();
92 * Creates the service context.
94 * @param input the input
95 * @param subject the subject
97 * @return the service context< account role, account role>
99 * @throws Exception the exception
101 private ServiceContext<AccountRole, AccountRole> createServiceContext(AccountRole input,
102 SubjectType subject) throws Exception {
103 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input);
104 ctx.setDocumentType(AccountRole.class.getPackage().getName()); //persistence unit
105 ctx.setProperty("entity-name", AccountRoleRel.class.getName());
106 //subject name is necessary to indicate if role or account is a subject
107 ctx.setProperty("subject", subject);
108 //set context for the relationship query
109 ctx.setProperty("object-class", AccountsCommon.class);
110 ctx.setProperty("object-id", "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;
124 * createAccountRole creates one or more account-role relationships
125 * between object (account/role) and subject (role/account)
131 public String createAccountRole(AccountRole input, SubjectType subject)
134 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input, subject);
135 DocumentHandler handler = createDocumentHandler(ctx);
136 return getStorageClient(ctx).create(ctx, handler);
140 * getAccountRole retrieves account-role relationships using given
141 * csid of object (account/role) and subject (role/account)
147 public AccountRole getAccountRole(
148 String csid, SubjectType subject) throws Exception {
150 if (logger.isDebugEnabled()) {
151 logger.debug("getAccountRole with csid=" + csid);
153 AccountRole result = null;
154 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext((AccountRole) null, subject);
155 DocumentHandler handler = createDocumentHandler(ctx);
156 getStorageClient(ctx).get(ctx, csid, handler);
157 result = (AccountRole) ctx.getOutput();
163 * deleteAccountRole deletes account-role relationships using given
164 * csid of object (account/role) and subject (role/account)
170 public void deleteAccountRole(String csid,
171 SubjectType subject) throws Exception {
173 if (logger.isDebugEnabled()) {
174 logger.debug("deleteAccountRole with csid=" + csid);
176 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext((AccountRole) null, subject);
177 getStorageClient(ctx).delete(ctx, csid);