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.common.authorization_mgt.AuthorizationRoleRel;
30 import org.collectionspace.services.authorization.AccountRole;
31 import org.collectionspace.services.authorization.AccountRoleRel;
32 import org.collectionspace.services.authorization.AccountValue;
33 import org.collectionspace.services.authorization.RoleValue;
34 import org.collectionspace.services.authorization.SubjectType;
35 import org.collectionspace.services.common.context.ServiceContext;
36 import org.collectionspace.services.common.storage.jpa.JpaDocumentHandler;
38 import org.collectionspace.services.common.document.DocumentFilter;
39 import org.collectionspace.services.common.document.DocumentWrapper;
40 import org.collectionspace.services.common.context.ServiceContextProperties;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 // TODO: Auto-generated Javadoc
47 * Document handler for AccountRole association.
51 public class AccountRoleDocumentHandler
52 extends JpaDocumentHandler<AccountRole, AccountRole, List<AccountRoleRel>, List<AccountRoleRel>> {
55 private final Logger logger = LoggerFactory.getLogger(AccountRoleDocumentHandler.class);
57 /** The account role. */
58 private AccountRole accountRole;
59 // private AccountRolesList accountRolesList;
60 /** The account role csid. */
61 private String accountRoleCsid = null;
64 * Gets the account role csid.
66 * @return the account role csid
68 public String getAccountRoleCsid() {
69 return this.accountRoleCsid;
73 * Sets the account role csid.
75 * @param theAccountRoleCsid the new account role csid
77 public void setAccountRoleCsid(String theAccountRoleCsid) {
78 this.accountRoleCsid = theAccountRoleCsid;
82 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
85 public void handleCreate(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
86 fillCommonPart(getCommonPart(), wrapDoc);
90 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
93 public void handleUpdate(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
94 throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
98 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#completeUpdate(org.collectionspace.services.common.document.DocumentWrapper)
101 public void completeUpdate(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
102 throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
106 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleGet(org.collectionspace.services.common.document.DocumentWrapper)
109 public void handleGet(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
110 AccountRole output = extractCommonPart(wrapDoc);
111 setCommonPart(output);
112 getServiceContext().setOutput(output);
116 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleGetAll(org.collectionspace.services.common.document.DocumentWrapper)
119 public void handleGetAll(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
120 throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
124 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleDelete(org.collectionspace.services.common.document.DocumentWrapper)
127 public void handleDelete(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
128 fillCommonPart(getCommonPart(), wrapDoc, true);
132 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#extractCommonPartList(org.collectionspace.services.common.document.DocumentWrapper)
135 public AccountRole extractCommonPartList(
136 DocumentWrapper<List<AccountRoleRel>> wrapDoc)
139 throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
141 // AccountRolesList result = new AccountRolesList();
142 // List<AccountRoleRel> arrl = wrapDoc.getWrappedObject();
143 // AccountRole ar = new AccountRole();
144 // SubjectType subject = getSubject(getServiceContext());
145 // if (arrl.size() == 0) {
149 //// result.setSubject(subject);
150 // AccountRoleRel ar0 = arrl.get(0);
151 // AccountValue av = buildAccountValue(ar0);
152 //// result.setAccount(av);
154 // List<AccountRoleListItem> accountRoleListItems = result.getAccountRoleListItems();
155 // if (accountRoleListItems == null) {
156 // accountRoleListItems = new ArrayList<AccountRoleListItem>();
158 // for (AccountRoleRel e : arrl) {
159 // AccountRoleListItem accountRoleListItem = new AccountRoleListItem();
161 // accountRoleListItem.setRoleName(e.getRoleName());
162 // accountRoleListItem.setRoleId(e.getRoleId());
163 // accountRoleListItem.setCsid(e.getHjid().toString());
164 // // add item to result list
165 // accountRoleListItems.add(accountRoleListItem);
169 // // Old Sanjay code
172 // ar0 = arrl.get(0);
173 // if (SubjectType.ROLE.equals(subject)) {
175 // List<AccountValue> avs = new ArrayList<AccountValue>();
176 // ar.setAccounts(avs);
177 // av = buildAccountValue(ar0);
181 // List<RoleValue> rvs = new ArrayList<RoleValue>();
183 // for (AccountRoleRel arr : arrl) {
184 // RoleValue rv = buildRoleValue(arr);
187 // } else if (SubjectType.ACCOUNT.equals(subject)) {
189 // List<RoleValue> rvs = new ArrayList<RoleValue>();
191 // RoleValue rv = buildRoleValue(ar0);
195 // List<AccountValue> avs = new ArrayList<AccountValue>();
196 // ar.setAccounts(avs);
197 // for (AccountRoleRel arr : arrl) {
198 // av = buildAccountValue(arr);
205 public void fillCommonPart(AccountRole ar,
206 DocumentWrapper<List<AccountRoleRel>> wrapDoc,
207 boolean handleDelete)
209 List<AccountRoleRel> arrl = wrapDoc.getWrappedObject();
210 SubjectType subject = ar.getSubject();
211 if (subject == null) {
212 //it is not required to give subject as URI determines the subject
213 subject = getSubject(getServiceContext());
215 //subject mismatch should have been checked during validation
217 if (subject.equals(SubjectType.ROLE)) {
218 //FIXME: potential index out of bounds exception...negative test needed
219 AccountValue av = ar.getAccount().get(0);
221 for (RoleValue rv : ar.getRole()) {
222 AccountRoleRel arr = buildAccountRoleRel(av, rv, handleDelete);
225 } else if (SubjectType.ACCOUNT.equals(subject)) {
226 //FIXME: potential index out of bounds exception...negative test needed
227 RoleValue rv = ar.getRole().get(0);
228 for (AccountValue av : ar.getAccount()) {
229 AccountRoleRel arr = buildAccountRoleRel(av, rv, handleDelete);
236 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#fillCommonPart(java.lang.Object, org.collectionspace.services.common.document.DocumentWrapper)
239 public void fillCommonPart(AccountRole ar,
240 DocumentWrapper<List<AccountRoleRel>> wrapDoc)
242 fillCommonPart(ar, wrapDoc, false);
246 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#extractCommonPart(org.collectionspace.services.common.document.DocumentWrapper)
249 public AccountRole extractCommonPart(
250 DocumentWrapper<List<AccountRoleRel>> wrapDoc)
252 List<AccountRoleRel> arrl = wrapDoc.getWrappedObject();
253 AccountRole ar = new AccountRole();
254 SubjectType subject = getSubject(getServiceContext());
255 if (arrl.size() == 0) {
258 AccountRoleRel ar0 = arrl.get(0);
259 if (SubjectType.ROLE.equals(subject)) {
261 List<AccountValue> avs = new ArrayList<AccountValue>();
263 AccountValue av = AuthorizationRoleRel.buildAccountValue(ar0);
269 List<RoleValue> rvs = new ArrayList<RoleValue>();
271 for (AccountRoleRel arr : arrl) {
272 RoleValue rv = AuthorizationRoleRel.buildRoleValue(arr);
277 } else if (SubjectType.ACCOUNT.equals(subject)) {
279 List<RoleValue> rvs = new ArrayList<RoleValue>();
281 RoleValue rv = AuthorizationRoleRel.buildRoleValue(ar0);
285 List<AccountValue> avs = new ArrayList<AccountValue>();
287 for (AccountRoleRel arr : arrl) {
288 AccountValue av = AuthorizationRoleRel.buildAccountValue(arr);
296 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getCommonPart()
299 public AccountRole getCommonPart() {
304 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#setCommonPart(java.lang.Object)
307 public void setCommonPart(AccountRole accountRole) {
308 this.accountRole = accountRole;
312 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getCommonPartList()
315 public AccountRole getCommonPartList() {
320 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#setCommonPartList(java.lang.Object)
323 public void setCommonPartList(AccountRole theAccountRole) {
324 // this.accountRolesList = accountRolesList;
325 this.accountRole = theAccountRole;
329 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getQProperty(java.lang.String)
332 public String getQProperty(
338 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#createDocumentFilter()
341 public DocumentFilter createDocumentFilter() {
342 return new DocumentFilter(this.getServiceContext());
346 * Builds the account role rel.
350 * @return the account role rel
352 private AccountRoleRel buildAccountRoleRel(AccountValue av, RoleValue rv, boolean handleDelete) {
353 AccountRoleRel arr = new AccountRoleRel();
354 arr.setAccountId(av.getAccountId());
355 arr.setUserId(av.getUserId());
356 arr.setScreenName(av.getScreenName());
357 arr.setRoleId(rv.getRoleId());
358 arr.setRoleName(rv.getRoleName());
360 String relationshipId = rv.getRoleRelationshipId();
361 if (relationshipId != null && handleDelete == true) {
362 arr.setHjid(Long.parseLong(relationshipId)); // set this so we can convince JPA to del the relation
371 * @return the subject
373 static SubjectType getSubject(ServiceContext ctx) {
374 Object o = ctx.getProperty(ServiceContextProperties.SUBJECT);
376 throw new IllegalArgumentException(ServiceContextProperties.SUBJECT
377 + " property is missing in context "
380 return (SubjectType) o;