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;
27 import java.util.ArrayList;
29 import javax.persistence.PersistenceException;
31 import org.collectionspace.services.account.storage.AccountRoleDocumentHandler;
32 //import org.collectionspace.services.authorization.AccountRolesList;
33 //import org.collectionspace.services.authorization.AccountRolesList.AccountRoleListItem;
34 import org.collectionspace.services.authorization.AccountRole;
35 import org.collectionspace.services.authorization.AccountRoleRel;
36 import org.collectionspace.services.authorization.Role;
37 import org.collectionspace.services.authorization.RoleValue;
38 import org.collectionspace.services.authorization.SubjectType;
40 import org.collectionspace.services.common.authorization_mgt.AuthorizationCommon;
41 import org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl;
42 import org.collectionspace.services.common.context.RemoteServiceContextFactory;
43 import org.collectionspace.services.common.context.ServiceContext;
44 import org.collectionspace.services.common.context.ServiceContextFactory;
45 import org.collectionspace.services.common.document.DocumentHandler;
46 import org.collectionspace.services.common.storage.StorageClient;
47 import org.collectionspace.services.common.storage.jpa.JpaRelationshipStorageClient;
48 import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
49 import org.collectionspace.services.common.context.ServiceContextProperties;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
55 * AccountRoleSubResource is used to manage account-role relationship
58 public class AccountRoleSubResource
59 // extends AbstractCollectionSpaceResourceImpl<AccountRole, AccountRolesList> {
60 extends AbstractCollectionSpaceResourceImpl<AccountRole, AccountRole> {
62 final public static String ACCOUNT_ACCOUNTROLE_SERVICE = "accounts/accountroles";
63 final public static String ROLE_ACCOUNTROLE_SERVICE = "authorization/roles/accountroles";
64 //this service is never exposed as standalone RESTful service...just use unique
65 //service name to identify binding
66 /** The service name. */
67 private String serviceName = ACCOUNT_ACCOUNTROLE_SERVICE;
69 final Logger logger = LoggerFactory.getLogger(AccountRoleSubResource.class);
70 /** The storage client. */
71 final StorageClient storageClient = new JpaRelationshipStorageClient<AccountRole>();
75 * @param serviceName qualified service path
77 public AccountRoleSubResource(String serviceName) {
78 this.serviceName = serviceName;
82 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
85 protected String getVersionString() {
86 /** The last change revision. */
87 final String lastChangeRevision = "$LastChangedRevision: 1165 $";
88 return lastChangeRevision;
92 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
95 public String getServiceName() {
100 * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
103 public Class<AccountRole> getCommonPartClass() {
104 return AccountRole.class;
108 * @see org.collectionspace.services.common.CollectionSpaceResource#getServiceContextFactory()
111 public ServiceContextFactory<AccountRole, AccountRole> getServiceContextFactory() {
112 // public ServiceContextFactory<AccountRole, AccountRolesList> getServiceContextFactory() {
113 return RemoteServiceContextFactory.get();
117 * Creates the service context.
119 * @param input the input
120 * @param subject the subject
122 * @return the service context< account role, account role>
124 * @throws Exception the exception
126 private ServiceContext<AccountRole, AccountRole> createServiceContext(AccountRole input,
127 SubjectType subject) throws Exception {
128 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input);
129 ctx.setDocumentType(AccountRole.class.getPackage().getName()); //persistence unit
130 ctx.setProperty(ServiceContextProperties.ENTITY_NAME, AccountRoleRel.class.getName());
131 ctx.setProperty(ServiceContextProperties.ENTITY_CLASS, AccountRoleRel.class);
132 //subject name is necessary to indicate if role or account is a subject
133 ctx.setProperty(ServiceContextProperties.SUBJECT, subject);
135 //set context for the relationship query
136 if (subject == SubjectType.ROLE) {
137 ctx.setProperty(ServiceContextProperties.OBJECT_CLASS, AccountsCommon.class);
138 ctx.setProperty(ServiceContextProperties.OBJECT_ID, "account_id");
139 } else if (subject == SubjectType.ACCOUNT) {
140 ctx.setProperty(ServiceContextProperties.OBJECT_CLASS, Role.class);
141 ctx.setProperty(ServiceContextProperties.OBJECT_ID, "role_id");
148 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getStorageClient(org.collectionspace.services.common.context.ServiceContext)
151 public StorageClient getStorageClient(ServiceContext<AccountRole, AccountRole> ctx) {
152 //FIXME use ctx to identify storage client
153 return storageClient;
157 * createAccountRole creates one or more account-role relationships
158 * between object (account/role) and subject (role/account)
164 public String createAccountRole(AccountRole input, SubjectType subject)
168 // We need to associate every new account with the Spring Security Admin role so we can make
169 // changes to the Spring Security ACL tables. The Spring Security Admin role has NO CollectionSpace
170 // specific permissions. It is an internal/private role that service consumers and end-users NEVER see.
173 //Preserve the original incoming list of roles
174 List<RoleValue> inputRoleValues = input.getRole();
176 //Change the role list to be just the Spring role
177 List<RoleValue> springRoles = new ArrayList<RoleValue>();
178 input.setRole(springRoles);
179 RoleValue springAdminRole = new RoleValue();
180 springRoles.add(springAdminRole);
181 springAdminRole.setRoleId(AuthorizationCommon.ROLE_SPRING_ADMIN_ID);
182 springAdminRole.setRoleName(AuthorizationCommon.ROLE_SPRING_ADMIN_NAME);
184 // The Spring role relationship may already exist, if it does then we'll get a PersistenceException that
185 // we'll just ignore.
187 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input, subject);
188 DocumentHandler handler = createDocumentHandler(ctx);
189 getStorageClient(ctx).create(ctx, handler);
190 } catch (PersistenceException e) {
191 //If we get this exception, it means that the role relationship already exists, so
192 //we can just ignore this exception.
193 if (logger.isTraceEnabled() == true) {
194 logger.trace(AuthorizationCommon.ROLE_SPRING_ADMIN_NAME +
195 " relationship already exists for account: " +
196 input.getAccount().get(0).getAccountId(), e);
201 // Now we'll add the account relationships for the original incoming roles.
203 input.setRole(inputRoleValues);
204 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input, subject);
205 DocumentHandler handler = createDocumentHandler(ctx);
206 String bogusCsid = getStorageClient(ctx).create(ctx, handler);
212 * getAccountRole retrieves account-role relationships using given
213 * csid of object (account/role) and subject (role/account)
219 public AccountRole getAccountRole(
220 String csid, SubjectType subject) throws Exception {
222 if (logger.isDebugEnabled()) {
223 logger.debug("getAccountRole with csid=" + csid);
225 AccountRole result = null;
226 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext((AccountRole) null, subject);
227 DocumentHandler handler = createDocumentHandler(ctx);
228 getStorageClient(ctx).get(ctx, csid, handler);
229 result = (AccountRole) ctx.getOutput();
235 * Gets the account role.
237 * @param csid the csid
238 * @param subject the subject
239 * @param accountRoleCsid the account role csid
240 * @return the account role
241 * @throws Exception the exception
243 public AccountRoleRel getAccountRoleRel(String csid,
245 String accountRoleCsid) throws Exception {
247 if (logger.isDebugEnabled()) {
248 logger.debug("getAccountRole with csid=" + csid);
250 // AccountRolesList result = new AccountRolesList();
251 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext((AccountRole) null, subject);
252 AccountRoleDocumentHandler handler = (AccountRoleDocumentHandler)createDocumentHandler(ctx);
253 handler.setAccountRoleCsid(accountRoleCsid);
254 //getStorageClient(ctx).get(ctx, csid, handler);
255 AccountRoleRel accountRoleRel = (AccountRoleRel)JpaStorageUtils.getEntity(new Long(accountRoleCsid).longValue(), AccountRoleRel.class);
256 // List<AccountRoleListItem> accountRoleList = result.getAccountRoleListItems();
257 // AccountRoleListItem listItem = new AccountRoleListItem();
259 // listItem.setCsid(accountRoleRel.getHjid().toString());
260 // listItem.setRoleId(accountRoleRel.getRoleId());
261 // listItem.setRoleName(accountRoleRel.getRoleName());
262 // add item to result list
263 // result = (AccountRolesList) ctx.getOutput();
265 return accountRoleRel;
269 * X_delete account role.
271 * @param csid the csid
272 * @param subject the subject
273 * @throws Exception the exception
275 public void x_deleteAccountRole(String csid,
276 SubjectType subject) throws Exception {
278 if (logger.isDebugEnabled()) {
279 logger.debug("deleteAccountRole with csid=" + csid);
281 AccountRole toDelete = getAccountRole(csid, subject);
282 deleteAccountRole(csid, subject, toDelete);
286 * deleteAccountRole deletes all account-role relationships using given
287 * csid of object (account/role) and subject (role/account)
288 * @param csid of the object
293 public void deleteAccountRole(String csid,
294 SubjectType subject) throws Exception {
296 if (logger.isDebugEnabled()) {
297 logger.debug("deleteAccountRole with csid=" + csid);
299 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext((AccountRole) null, subject);
300 getStorageClient(ctx).delete(ctx, csid);
304 * deleteAccountRole deletes given account-role relationships using given
305 * csid of object (account/role) and subject (role/account)
306 * @param csid of the object
308 * @param input with account role relationships to delete
312 public void deleteAccountRole(String csid, SubjectType subject, AccountRole input)
315 ServiceContext<AccountRole, AccountRole> ctx = createServiceContext(input, subject);
316 DocumentHandler handler = createDocumentHandler(ctx);
317 getStorageClient(ctx).delete(ctx, csid, handler);