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 java.util.List;
28 import org.collectionspace.services.account.storage.AccountRoleDocumentHandler;
29 //import org.collectionspace.services.authorization.AccountRolesList;
30 //import org.collectionspace.services.authorization.AccountRolesList.AccountRoleListItem;
31 import org.collectionspace.services.authorization.AccountRole;
32 import org.collectionspace.services.authorization.AccountValue;
33 import org.collectionspace.services.authorization.AccountRoleRel;
34 import org.collectionspace.services.authorization.SubjectType;
36 import org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl;
37 import org.collectionspace.services.common.context.RemoteServiceContextFactory;
38 import org.collectionspace.services.common.context.ServiceContext;
39 import org.collectionspace.services.common.context.ServiceContextFactory;
40 import org.collectionspace.services.common.document.DocumentHandler;
41 import org.collectionspace.services.common.storage.StorageClient;
42 import org.collectionspace.services.common.storage.jpa.JpaRelationshipStorageClient;
43 import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
44 import org.collectionspace.services.common.context.ServiceContextProperties;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
50 * AccountRoleSubResource is used to manage account-role relationship
53 public class AccountRoleSubResource
54 // extends AbstractCollectionSpaceResourceImpl<AccountRole, AccountRolesList> {
55 extends AbstractCollectionSpaceResourceImpl<AccountRole, AccountRole> {
57 final public static String ACCOUNT_ACCOUNTROLE_SERVICE = "accounts/accountroles";
58 final public static String ROLE_ACCOUNTROLE_SERVICE = "roles/accountroles";
59 //this service is never exposed as standalone RESTful service...just use unique
60 //service name to identify binding
61 /** The service name. */
62 private String serviceName = ACCOUNT_ACCOUNTROLE_SERVICE;
64 final Logger logger = LoggerFactory.getLogger(AccountRoleSubResource.class);
65 /** The storage client. */
66 final StorageClient storageClient = new JpaRelationshipStorageClient<AccountRole>();
70 * @param serviceName qualified service path
72 AccountRoleSubResource(String serviceName) {
73 this.serviceName = serviceName;
77 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
80 protected String getVersionString() {
81 /** The last change revision. */
82 final String lastChangeRevision = "$LastChangedRevision: 1165 $";
83 return lastChangeRevision;
87 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
90 public String getServiceName() {
95 * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
98 public Class<AccountRole> getCommonPartClass() {
99 return AccountRole.class;
103 * @see org.collectionspace.services.common.CollectionSpaceResource#getServiceContextFactory()
106 public ServiceContextFactory<AccountRole, AccountRole> getServiceContextFactory() {
107 // public ServiceContextFactory<AccountRole, AccountRolesList> getServiceContextFactory() {
108 return RemoteServiceContextFactory.get();
112 * Creates the service context.
114 * @param input the input
115 * @param subject the subject
117 * @return the service context< account role, account role>
119 * @throws Exception the exception
121 private ServiceContext<AccountRole, AccountRole> createServiceContext(AccountRole input,
122 SubjectType subject) throws Exception {
123 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input);
124 ctx.setDocumentType(AccountRole.class.getPackage().getName()); //persistence unit
125 ctx.setProperty(ServiceContextProperties.ENTITY_NAME, AccountRoleRel.class.getName());
126 ctx.setProperty(ServiceContextProperties.ENTITY_CLASS, AccountRoleRel.class);
127 //subject name is necessary to indicate if role or account is a subject
128 ctx.setProperty(ServiceContextProperties.SUBJECT, subject);
129 //set context for the relationship query
130 ctx.setProperty(ServiceContextProperties.OBJECT_CLASS, AccountsCommon.class);
131 ctx.setProperty(ServiceContextProperties.OBJECT_ID, "account_id");
136 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getStorageClient(org.collectionspace.services.common.context.ServiceContext)
139 public StorageClient getStorageClient(ServiceContext<AccountRole, AccountRole> ctx) {
140 //FIXME use ctx to identify storage client
141 return storageClient;
145 * createAccountRole creates one or more account-role relationships
146 * between object (account/role) and subject (role/account)
152 public String createAccountRole(AccountRole input, SubjectType subject)
155 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input, subject);
156 DocumentHandler handler = createDocumentHandler(ctx);
158 String bogusCsid = getStorageClient(ctx).create(ctx, handler);
164 * getAccountRole retrieves account-role relationships using given
165 * csid of object (account/role) and subject (role/account)
171 public AccountRole getAccountRole(
172 String csid, SubjectType subject) throws Exception {
174 if (logger.isDebugEnabled()) {
175 logger.debug("getAccountRole with csid=" + csid);
177 AccountRole result = null;
178 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext((AccountRole) null, subject);
179 DocumentHandler handler = createDocumentHandler(ctx);
180 getStorageClient(ctx).get(ctx, csid, handler);
181 result = (AccountRole) ctx.getOutput();
187 * Gets the account role.
189 * @param csid the csid
190 * @param subject the subject
191 * @param accountRoleCsid the account role csid
192 * @return the account role
193 * @throws Exception the exception
195 public AccountRoleRel getAccountRoleRel(String csid,
197 String accountRoleCsid) throws Exception {
199 if (logger.isDebugEnabled()) {
200 logger.debug("getAccountRole with csid=" + csid);
202 // AccountRolesList result = new AccountRolesList();
203 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext((AccountRole) null, subject);
204 AccountRoleDocumentHandler handler = (AccountRoleDocumentHandler)createDocumentHandler(ctx);
205 handler.setAccountRoleCsid(accountRoleCsid);
206 //getStorageClient(ctx).get(ctx, csid, handler);
207 AccountRoleRel accountRoleRel = (AccountRoleRel)JpaStorageUtils.getEntity(new Long(accountRoleCsid).longValue(), AccountRoleRel.class);
208 // List<AccountRoleListItem> accountRoleList = result.getAccountRoleListItems();
209 // AccountRoleListItem listItem = new AccountRoleListItem();
211 // listItem.setCsid(accountRoleRel.getHjid().toString());
212 // listItem.setRoleId(accountRoleRel.getRoleId());
213 // listItem.setRoleName(accountRoleRel.getRoleName());
214 // add item to result list
215 // result = (AccountRolesList) ctx.getOutput();
217 return accountRoleRel;
221 * X_delete account role.
223 * @param csid the csid
224 * @param subject the subject
225 * @throws Exception the exception
227 public void x_deleteAccountRole(String csid,
228 SubjectType subject) throws Exception {
230 if (logger.isDebugEnabled()) {
231 logger.debug("deleteAccountRole with csid=" + csid);
233 AccountRole toDelete = getAccountRole(csid, subject);
234 deleteAccountRole(csid, subject, toDelete);
238 * deleteAccountRole deletes all account-role relationships using given
239 * csid of object (account/role) and subject (role/account)
240 * @param csid of the object
245 public void deleteAccountRole(String csid,
246 SubjectType subject) throws Exception {
248 if (logger.isDebugEnabled()) {
249 logger.debug("deleteAccountRole with csid=" + csid);
251 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext((AccountRole) null, subject);
252 getStorageClient(ctx).delete(ctx, csid);
256 * deleteAccountRole deletes given account-role relationships using given
257 * csid of object (account/role) and subject (role/account)
258 * @param csid of the object
260 * @param input with account role relationships to delete
264 public void deleteAccountRole(String csid, SubjectType subject, AccountRole input)
267 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input, subject);
268 DocumentHandler handler = createDocumentHandler(ctx);
269 getStorageClient(ctx).delete(ctx, csid, handler);