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 accountRoles and
22 * limitations under the License.
24 package org.collectionspace.services.account.storage;
26 import java.util.ArrayList;
27 import java.util.List;
29 //import org.collectionspace.services.authorization.AccountRolesList;
30 //import org.collectionspace.services.authorization.AccountRolesList.AccountRoleListItem;
32 import org.collectionspace.services.common.authorization_mgt.AuthorizationRoleRel;
33 import org.collectionspace.services.authorization.AccountRole;
34 import org.collectionspace.services.authorization.AccountRoleRel;
35 import org.collectionspace.services.authorization.AccountValue;
36 import org.collectionspace.services.authorization.PermissionsRolesList;
37 import org.collectionspace.services.authorization.RoleValue;
38 import org.collectionspace.services.authorization.SubjectType;
39 import org.collectionspace.services.common.context.ServiceContext;
40 import org.collectionspace.services.common.storage.jpa.JpaDocumentHandler;
42 import org.collectionspace.services.common.document.AbstractDocumentHandlerImpl;
43 import org.collectionspace.services.common.document.DocumentFilter;
44 import org.collectionspace.services.common.document.DocumentWrapper;
45 import org.collectionspace.services.common.context.ServiceContextProperties;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
50 // TODO: Auto-generated Javadoc
52 * Document handler for AccountRole association.
56 public class AccountRoleDocumentHandler
57 extends JpaDocumentHandler<AccountRole, AccountRole, List<AccountRoleRel>, List<AccountRoleRel>> {
60 private final Logger logger = LoggerFactory.getLogger(AccountRoleDocumentHandler.class);
62 /** The account role. */
63 private AccountRole accountRole;
64 // private AccountRolesList accountRolesList;
65 /** The account role csid. */
66 private String accountRoleCsid = null;
69 * Gets the account role csid.
71 * @return the account role csid
73 public String getAccountRoleCsid() {
74 return this.accountRoleCsid;
78 * Sets the account role csid.
80 * @param theAccountRoleCsid the new account role csid
82 public void setAccountRoleCsid(String theAccountRoleCsid) {
83 this.accountRoleCsid = theAccountRoleCsid;
87 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
90 public void handleCreate(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
91 fillCommonPart(getCommonPart(), wrapDoc);
95 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
98 public void handleUpdate(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
99 throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
103 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#completeUpdate(org.collectionspace.services.common.document.DocumentWrapper)
106 public void completeUpdate(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
107 throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
111 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleGet(org.collectionspace.services.common.document.DocumentWrapper)
114 public void handleGet(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
115 AccountRole output = extractCommonPart(wrapDoc);
116 setCommonPart(extractCommonPart(wrapDoc));
117 // AccountRole accountRoleList = extractCommonPartList(wrapDoc);
118 getServiceContext().setOutput(output);
122 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleGetAll(org.collectionspace.services.common.document.DocumentWrapper)
125 public void handleGetAll(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
126 throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
130 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleDelete(org.collectionspace.services.common.document.DocumentWrapper)
133 public void handleDelete(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
134 fillCommonPart(getCommonPart(), wrapDoc, true);
138 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#extractCommonPartList(org.collectionspace.services.common.document.DocumentWrapper)
141 public AccountRole extractCommonPartList(
142 DocumentWrapper<List<AccountRoleRel>> wrapDoc)
145 throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
147 // AccountRolesList result = new AccountRolesList();
148 // List<AccountRoleRel> arrl = wrapDoc.getWrappedObject();
149 // AccountRole ar = new AccountRole();
150 // SubjectType subject = getSubject(getServiceContext());
151 // if (arrl.size() == 0) {
155 //// result.setSubject(subject);
156 // AccountRoleRel ar0 = arrl.get(0);
157 // AccountValue av = buildAccountValue(ar0);
158 //// result.setAccount(av);
160 // List<AccountRoleListItem> accountRoleListItems = result.getAccountRoleListItems();
161 // if (accountRoleListItems == null) {
162 // accountRoleListItems = new ArrayList<AccountRoleListItem>();
164 // for (AccountRoleRel e : arrl) {
165 // AccountRoleListItem accountRoleListItem = new AccountRoleListItem();
167 // accountRoleListItem.setRoleName(e.getRoleName());
168 // accountRoleListItem.setRoleId(e.getRoleId());
169 // accountRoleListItem.setCsid(e.getHjid().toString());
170 // // add item to result list
171 // accountRoleListItems.add(accountRoleListItem);
175 // // Old Sanjay code
178 // ar0 = arrl.get(0);
179 // if (SubjectType.ROLE.equals(subject)) {
181 // List<AccountValue> avs = new ArrayList<AccountValue>();
182 // ar.setAccounts(avs);
183 // av = buildAccountValue(ar0);
187 // List<RoleValue> rvs = new ArrayList<RoleValue>();
189 // for (AccountRoleRel arr : arrl) {
190 // RoleValue rv = buildRoleValue(arr);
193 // } else if (SubjectType.ACCOUNT.equals(subject)) {
195 // List<RoleValue> rvs = new ArrayList<RoleValue>();
197 // RoleValue rv = buildRoleValue(ar0);
201 // List<AccountValue> avs = new ArrayList<AccountValue>();
202 // ar.setAccounts(avs);
203 // for (AccountRoleRel arr : arrl) {
204 // av = buildAccountValue(arr);
211 public void fillCommonPart(AccountRole ar,
212 DocumentWrapper<List<AccountRoleRel>> wrapDoc,
213 boolean handleDelete)
215 List<AccountRoleRel> arrl = wrapDoc.getWrappedObject();
216 SubjectType subject = ar.getSubject();
217 if (subject == null) {
218 //it is not required to give subject as URI determines the subject
219 subject = getSubject(getServiceContext());
221 //subject mismatch should have been checked during validation
223 if (subject.equals(SubjectType.ROLE)) {
224 //FIXME: potential index out of bounds exception...negative test needed
225 AccountValue av = ar.getAccounts().get(0);
227 for (RoleValue rv : ar.getRoles()) {
228 AccountRoleRel arr = buildAccountRoleRel(av, rv, handleDelete);
231 } else if (SubjectType.ACCOUNT.equals(subject)) {
232 //FIXME: potential index out of bounds exception...negative test needed
233 RoleValue rv = ar.getRoles().get(0);
234 for (AccountValue av : ar.getAccounts()) {
235 AccountRoleRel arr = buildAccountRoleRel(av, rv, handleDelete);
242 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#fillCommonPart(java.lang.Object, org.collectionspace.services.common.document.DocumentWrapper)
245 public void fillCommonPart(AccountRole ar,
246 DocumentWrapper<List<AccountRoleRel>> wrapDoc)
248 fillCommonPart(ar, wrapDoc, false);
252 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#extractCommonPart(org.collectionspace.services.common.document.DocumentWrapper)
255 public AccountRole extractCommonPart(
256 DocumentWrapper<List<AccountRoleRel>> wrapDoc)
258 List<AccountRoleRel> arrl = wrapDoc.getWrappedObject();
259 AccountRole ar = new AccountRole();
260 SubjectType subject = getSubject(getServiceContext());
261 if (arrl.size() == 0) {
264 AccountRoleRel ar0 = arrl.get(0);
265 if (SubjectType.ROLE.equals(subject)) {
267 List<AccountValue> avs = new ArrayList<AccountValue>();
269 AccountValue av = buildAccountValue(ar0);
273 List<RoleValue> rvs = new ArrayList<RoleValue>();
275 for (AccountRoleRel arr : arrl) {
276 RoleValue rv = AuthorizationRoleRel.buildRoleValue(arr);
279 } else if (SubjectType.ACCOUNT.equals(subject)) {
281 List<RoleValue> rvs = new ArrayList<RoleValue>();
283 RoleValue rv = AuthorizationRoleRel.buildRoleValue(ar0);
287 List<AccountValue> avs = new ArrayList<AccountValue>();
289 for (AccountRoleRel arr : arrl) {
290 AccountValue av = buildAccountValue(arr);
298 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getCommonPart()
301 public AccountRole getCommonPart() {
306 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#setCommonPart(java.lang.Object)
309 public void setCommonPart(AccountRole accountRole) {
310 this.accountRole = accountRole;
314 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getCommonPartList()
317 public AccountRole getCommonPartList() {
322 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#setCommonPartList(java.lang.Object)
325 public void setCommonPartList(AccountRole theAccountRole) {
326 // this.accountRolesList = accountRolesList;
327 this.accountRole = theAccountRole;
331 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getQProperty(java.lang.String)
334 public String getQProperty(
340 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#createDocumentFilter()
343 public DocumentFilter createDocumentFilter() {
344 return new DocumentFilter(this.getServiceContext());
348 * Builds the account value.
351 * @return the account value
353 private AccountValue buildAccountValue(AccountRoleRel arr) {
354 AccountValue av = new AccountValue();
355 av.setAccountId(arr.getAccountId());
356 av.setUserId(arr.getUserId());
357 av.setScreenName(arr.getScreenName());
362 * Builds the account role rel.
366 * @return the account role rel
368 private AccountRoleRel buildAccountRoleRel(AccountValue av, RoleValue rv, boolean handleDelete) {
369 AccountRoleRel arr = new AccountRoleRel();
370 arr.setAccountId(av.getAccountId());
371 arr.setUserId(av.getUserId());
372 arr.setScreenName(av.getScreenName());
373 arr.setRoleId(rv.getRoleId());
374 arr.setRoleName(rv.getRoleName());
376 String relationshipId = rv.getRoleRelationshipId();
377 if (relationshipId != null && handleDelete == true) {
378 arr.setHjid(Long.parseLong(relationshipId)); // set this so we can convince JPA to del the relation
387 * @return the subject
389 static SubjectType getSubject(ServiceContext ctx) {
390 Object o = ctx.getProperty(ServiceContextProperties.SUBJECT);
392 throw new IllegalArgumentException(ServiceContextProperties.SUBJECT
393 + " property is missing in context "
396 return (SubjectType) o;