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.RoleValue;
37 import org.collectionspace.services.authorization.SubjectType;
39 import org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl;
40 import org.collectionspace.services.common.context.RemoteServiceContextFactory;
41 import org.collectionspace.services.common.context.ServiceContext;
42 import org.collectionspace.services.common.context.ServiceContextFactory;
43 import org.collectionspace.services.common.document.DocumentHandler;
44 import org.collectionspace.services.common.storage.StorageClient;
45 import org.collectionspace.services.common.storage.jpa.JpaRelationshipStorageClient;
46 import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
47 import org.collectionspace.services.common.context.ServiceContextProperties;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
53 * AccountRoleSubResource is used to manage account-role relationship
56 public class AccountRoleSubResource
57 // extends AbstractCollectionSpaceResourceImpl<AccountRole, AccountRolesList> {
58 extends AbstractCollectionSpaceResourceImpl<AccountRole, AccountRole> {
60 //FIXME: These belong in an Authorization class, not here
61 private static String ROLE_SPRING_ADMIN_ID = "-1";
62 private static String ROLE_SPRING_ADMIN_NAME = "ROLE_SPRING_ADMIN";
64 final public static String ACCOUNT_ACCOUNTROLE_SERVICE = "accounts/accountroles";
65 final public static String ROLE_ACCOUNTROLE_SERVICE = "authorization/roles/accountroles";
66 //this service is never exposed as standalone RESTful service...just use unique
67 //service name to identify binding
68 /** The service name. */
69 private String serviceName = ACCOUNT_ACCOUNTROLE_SERVICE;
71 final Logger logger = LoggerFactory.getLogger(AccountRoleSubResource.class);
72 /** The storage client. */
73 final StorageClient storageClient = new JpaRelationshipStorageClient<AccountRole>();
77 * @param serviceName qualified service path
79 public AccountRoleSubResource(String serviceName) {
80 this.serviceName = serviceName;
84 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
87 protected String getVersionString() {
88 /** The last change revision. */
89 final String lastChangeRevision = "$LastChangedRevision: 1165 $";
90 return lastChangeRevision;
94 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
97 public String getServiceName() {
102 * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
105 public Class<AccountRole> getCommonPartClass() {
106 return AccountRole.class;
110 * @see org.collectionspace.services.common.CollectionSpaceResource#getServiceContextFactory()
113 public ServiceContextFactory<AccountRole, AccountRole> getServiceContextFactory() {
114 // public ServiceContextFactory<AccountRole, AccountRolesList> getServiceContextFactory() {
115 return RemoteServiceContextFactory.get();
119 * Creates the service context.
121 * @param input the input
122 * @param subject the subject
124 * @return the service context< account role, account role>
126 * @throws Exception the exception
128 private ServiceContext<AccountRole, AccountRole> createServiceContext(AccountRole input,
129 SubjectType subject) throws Exception {
130 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input);
131 ctx.setDocumentType(AccountRole.class.getPackage().getName()); //persistence unit
132 ctx.setProperty(ServiceContextProperties.ENTITY_NAME, AccountRoleRel.class.getName());
133 ctx.setProperty(ServiceContextProperties.ENTITY_CLASS, AccountRoleRel.class);
134 //subject name is necessary to indicate if role or account is a subject
135 ctx.setProperty(ServiceContextProperties.SUBJECT, subject);
137 //set context for the relationship query
138 if (subject == SubjectType.ROLE) {
139 ctx.setProperty(ServiceContextProperties.OBJECT_CLASS, AccountsCommon.class);
140 ctx.setProperty(ServiceContextProperties.OBJECT_ID, "account_id");
141 } else if (subject == SubjectType.ACCOUNT) {
142 ctx.setProperty(ServiceContextProperties.OBJECT_CLASS, Role.class);
143 ctx.setProperty(ServiceContextProperties.OBJECT_ID, "role_id");
150 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getStorageClient(org.collectionspace.services.common.context.ServiceContext)
153 public StorageClient getStorageClient(ServiceContext<AccountRole, AccountRole> ctx) {
154 //FIXME use ctx to identify storage client
155 return storageClient;
159 * createAccountRole creates one or more account-role relationships
160 * between object (account/role) and subject (role/account)
166 public String createAccountRole(AccountRole input, SubjectType subject)
170 // We need to associate every new account with the Spring Security Admin role so we can make
171 // changes to the Spring Security ACL tables. The Spring Security Admin role has NO CollectionSpace
172 // specific permissions. It is an internal/private role that service consumers and end-users NEVER see.
174 RoleValue springAdminRole = new RoleValue();
175 springAdminRole.setRoleId(ROLE_SPRING_ADMIN_ID);
176 springAdminRole.setRoleName(ROLE_SPRING_ADMIN_NAME);
177 List<RoleValue> roleValues = input.getRoles();
178 roleValues.add(springAdminRole);
180 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input, subject);
181 DocumentHandler handler = createDocumentHandler(ctx);
182 String bogusCsid = getStorageClient(ctx).create(ctx, handler);
188 * getAccountRole retrieves account-role relationships using given
189 * csid of object (account/role) and subject (role/account)
195 public AccountRole getAccountRole(
196 String csid, SubjectType subject) throws Exception {
198 if (logger.isDebugEnabled()) {
199 logger.debug("getAccountRole with csid=" + csid);
201 AccountRole result = null;
202 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext((AccountRole) null, subject);
203 DocumentHandler handler = createDocumentHandler(ctx);
204 getStorageClient(ctx).get(ctx, csid, handler);
205 result = (AccountRole) ctx.getOutput();
211 * Gets the account role.
213 * @param csid the csid
214 * @param subject the subject
215 * @param accountRoleCsid the account role csid
216 * @return the account role
217 * @throws Exception the exception
219 public AccountRoleRel getAccountRoleRel(String csid,
221 String accountRoleCsid) throws Exception {
223 if (logger.isDebugEnabled()) {
224 logger.debug("getAccountRole with csid=" + csid);
226 // AccountRolesList result = new AccountRolesList();
227 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext((AccountRole) null, subject);
228 AccountRoleDocumentHandler handler = (AccountRoleDocumentHandler)createDocumentHandler(ctx);
229 handler.setAccountRoleCsid(accountRoleCsid);
230 //getStorageClient(ctx).get(ctx, csid, handler);
231 AccountRoleRel accountRoleRel = (AccountRoleRel)JpaStorageUtils.getEntity(new Long(accountRoleCsid).longValue(), AccountRoleRel.class);
232 // List<AccountRoleListItem> accountRoleList = result.getAccountRoleListItems();
233 // AccountRoleListItem listItem = new AccountRoleListItem();
235 // listItem.setCsid(accountRoleRel.getHjid().toString());
236 // listItem.setRoleId(accountRoleRel.getRoleId());
237 // listItem.setRoleName(accountRoleRel.getRoleName());
238 // add item to result list
239 // result = (AccountRolesList) ctx.getOutput();
241 return accountRoleRel;
245 * X_delete account role.
247 * @param csid the csid
248 * @param subject the subject
249 * @throws Exception the exception
251 public void x_deleteAccountRole(String csid,
252 SubjectType subject) throws Exception {
254 if (logger.isDebugEnabled()) {
255 logger.debug("deleteAccountRole with csid=" + csid);
257 AccountRole toDelete = getAccountRole(csid, subject);
258 deleteAccountRole(csid, subject, toDelete);
262 * deleteAccountRole deletes all account-role relationships using given
263 * csid of object (account/role) and subject (role/account)
264 * @param csid of the object
269 public void deleteAccountRole(String csid,
270 SubjectType subject) throws Exception {
272 if (logger.isDebugEnabled()) {
273 logger.debug("deleteAccountRole with csid=" + csid);
275 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext((AccountRole) null, subject);
276 getStorageClient(ctx).delete(ctx, csid);
280 * deleteAccountRole deletes given account-role relationships using given
281 * csid of object (account/role) and subject (role/account)
282 * @param csid of the object
284 * @param input with account role relationships to delete
288 public void deleteAccountRole(String csid, SubjectType subject, AccountRole input)
291 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input, subject);
292 DocumentHandler handler = createDocumentHandler(ctx);
293 getStorageClient(ctx).delete(ctx, csid, handler);