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.Permission;
35 import org.collectionspace.services.authorization.Role;
36 import org.collectionspace.services.authorization.SubjectType;
38 import org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl;
39 import org.collectionspace.services.common.context.RemoteServiceContextFactory;
40 import org.collectionspace.services.common.context.ServiceContext;
41 import org.collectionspace.services.common.context.ServiceContextFactory;
42 import org.collectionspace.services.common.document.DocumentHandler;
43 import org.collectionspace.services.common.storage.StorageClient;
44 import org.collectionspace.services.common.storage.jpa.JpaRelationshipStorageClient;
45 import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
46 import org.collectionspace.services.common.context.ServiceContextProperties;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
52 * AccountRoleSubResource is used to manage account-role relationship
55 public class AccountRoleSubResource
56 // extends AbstractCollectionSpaceResourceImpl<AccountRole, AccountRolesList> {
57 extends AbstractCollectionSpaceResourceImpl<AccountRole, AccountRole> {
59 final public static String ACCOUNT_ACCOUNTROLE_SERVICE = "accounts/accountroles";
60 final public static String ROLE_ACCOUNTROLE_SERVICE = "authorization/roles/accountroles";
61 //this service is never exposed as standalone RESTful service...just use unique
62 //service name to identify binding
63 /** The service name. */
64 private String serviceName = ACCOUNT_ACCOUNTROLE_SERVICE;
66 final Logger logger = LoggerFactory.getLogger(AccountRoleSubResource.class);
67 /** The storage client. */
68 final StorageClient storageClient = new JpaRelationshipStorageClient<AccountRole>();
72 * @param serviceName qualified service path
74 public AccountRoleSubResource(String serviceName) {
75 this.serviceName = serviceName;
79 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
82 protected String getVersionString() {
83 /** The last change revision. */
84 final String lastChangeRevision = "$LastChangedRevision: 1165 $";
85 return lastChangeRevision;
89 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
92 public String getServiceName() {
97 * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
100 public Class<AccountRole> getCommonPartClass() {
101 return AccountRole.class;
105 * @see org.collectionspace.services.common.CollectionSpaceResource#getServiceContextFactory()
108 public ServiceContextFactory<AccountRole, AccountRole> getServiceContextFactory() {
109 // public ServiceContextFactory<AccountRole, AccountRolesList> getServiceContextFactory() {
110 return RemoteServiceContextFactory.get();
114 * Creates the service context.
116 * @param input the input
117 * @param subject the subject
119 * @return the service context< account role, account role>
121 * @throws Exception the exception
123 private ServiceContext<AccountRole, AccountRole> createServiceContext(AccountRole input,
124 SubjectType subject) throws Exception {
125 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input);
126 ctx.setDocumentType(AccountRole.class.getPackage().getName()); //persistence unit
127 ctx.setProperty(ServiceContextProperties.ENTITY_NAME, AccountRoleRel.class.getName());
128 ctx.setProperty(ServiceContextProperties.ENTITY_CLASS, AccountRoleRel.class);
129 //subject name is necessary to indicate if role or account is a subject
130 ctx.setProperty(ServiceContextProperties.SUBJECT, subject);
132 //set context for the relationship query
133 if (subject == SubjectType.ROLE) {
134 ctx.setProperty(ServiceContextProperties.OBJECT_CLASS, AccountsCommon.class);
135 ctx.setProperty(ServiceContextProperties.OBJECT_ID, "account_id");
136 } else if (subject == SubjectType.ACCOUNT) {
137 ctx.setProperty(ServiceContextProperties.OBJECT_CLASS, Role.class);
138 ctx.setProperty(ServiceContextProperties.OBJECT_ID, "role_id");
145 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getStorageClient(org.collectionspace.services.common.context.ServiceContext)
148 public StorageClient getStorageClient(ServiceContext<AccountRole, AccountRole> ctx) {
149 //FIXME use ctx to identify storage client
150 return storageClient;
154 * createAccountRole creates one or more account-role relationships
155 * between object (account/role) and subject (role/account)
161 public String createAccountRole(AccountRole input, SubjectType subject)
164 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input, subject);
165 DocumentHandler handler = createDocumentHandler(ctx);
167 String bogusCsid = getStorageClient(ctx).create(ctx, handler);
173 * getAccountRole retrieves account-role relationships using given
174 * csid of object (account/role) and subject (role/account)
180 public AccountRole getAccountRole(
181 String csid, SubjectType subject) throws Exception {
183 if (logger.isDebugEnabled()) {
184 logger.debug("getAccountRole with csid=" + csid);
186 AccountRole result = null;
187 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext((AccountRole) null, subject);
188 DocumentHandler handler = createDocumentHandler(ctx);
189 getStorageClient(ctx).get(ctx, csid, handler);
190 result = (AccountRole) ctx.getOutput();
196 * Gets the account role.
198 * @param csid the csid
199 * @param subject the subject
200 * @param accountRoleCsid the account role csid
201 * @return the account role
202 * @throws Exception the exception
204 public AccountRoleRel getAccountRoleRel(String csid,
206 String accountRoleCsid) throws Exception {
208 if (logger.isDebugEnabled()) {
209 logger.debug("getAccountRole with csid=" + csid);
211 // AccountRolesList result = new AccountRolesList();
212 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext((AccountRole) null, subject);
213 AccountRoleDocumentHandler handler = (AccountRoleDocumentHandler)createDocumentHandler(ctx);
214 handler.setAccountRoleCsid(accountRoleCsid);
215 //getStorageClient(ctx).get(ctx, csid, handler);
216 AccountRoleRel accountRoleRel = (AccountRoleRel)JpaStorageUtils.getEntity(new Long(accountRoleCsid).longValue(), AccountRoleRel.class);
217 // List<AccountRoleListItem> accountRoleList = result.getAccountRoleListItems();
218 // AccountRoleListItem listItem = new AccountRoleListItem();
220 // listItem.setCsid(accountRoleRel.getHjid().toString());
221 // listItem.setRoleId(accountRoleRel.getRoleId());
222 // listItem.setRoleName(accountRoleRel.getRoleName());
223 // add item to result list
224 // result = (AccountRolesList) ctx.getOutput();
226 return accountRoleRel;
230 * X_delete account role.
232 * @param csid the csid
233 * @param subject the subject
234 * @throws Exception the exception
236 public void x_deleteAccountRole(String csid,
237 SubjectType subject) throws Exception {
239 if (logger.isDebugEnabled()) {
240 logger.debug("deleteAccountRole with csid=" + csid);
242 AccountRole toDelete = getAccountRole(csid, subject);
243 deleteAccountRole(csid, subject, toDelete);
247 * deleteAccountRole deletes all account-role relationships using given
248 * csid of object (account/role) and subject (role/account)
249 * @param csid of the object
254 public void deleteAccountRole(String csid,
255 SubjectType subject) throws Exception {
257 if (logger.isDebugEnabled()) {
258 logger.debug("deleteAccountRole with csid=" + csid);
260 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext((AccountRole) null, subject);
261 getStorageClient(ctx).delete(ctx, csid);
265 * deleteAccountRole deletes given account-role relationships using given
266 * csid of object (account/role) and subject (role/account)
267 * @param csid of the object
269 * @param input with account role relationships to delete
273 public void deleteAccountRole(String csid, SubjectType subject, AccountRole input)
276 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input, subject);
277 DocumentHandler handler = createDocumentHandler(ctx);
278 getStorageClient(ctx).delete(ctx, csid, handler);