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.JpaDocumentFilter;
37 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;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
44 // TODO: Auto-generated Javadoc
46 * Document handler for AccountRole association.
50 public class AccountRoleDocumentHandler
51 extends JpaDocumentHandler<AccountRole, AccountRole, List<AccountRoleRel>, List<AccountRoleRel>> {
54 private final Logger logger = LoggerFactory.getLogger(AccountRoleDocumentHandler.class);
56 /** The account role. */
57 private AccountRole accountRole;
58 // private AccountRolesList accountRolesList;
59 /** The account role csid. */
60 private String accountRoleCsid = null;
63 * Gets the account role csid.
65 * @return the account role csid
67 public String getAccountRoleCsid() {
68 return this.accountRoleCsid;
72 * Sets the account role csid.
74 * @param theAccountRoleCsid the new account role csid
76 public void setAccountRoleCsid(String theAccountRoleCsid) {
77 this.accountRoleCsid = theAccountRoleCsid;
81 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
84 public void handleCreate(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
85 fillCommonPart(getCommonPart(), wrapDoc);
89 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
92 public void handleUpdate(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
93 throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
97 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#completeUpdate(org.collectionspace.services.common.document.DocumentWrapper)
100 public void completeUpdate(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
101 throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
105 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleGet(org.collectionspace.services.common.document.DocumentWrapper)
108 public void handleGet(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
109 AccountRole output = extractCommonPart(wrapDoc);
110 setCommonPart(output);
111 getServiceContext().setOutput(output);
115 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleGetAll(org.collectionspace.services.common.document.DocumentWrapper)
118 public void handleGetAll(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
119 throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
123 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleDelete(org.collectionspace.services.common.document.DocumentWrapper)
126 public boolean handleDelete(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
127 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);
206 public void prepareCreate() throws Exception {
207 // Ensure the roles exist
210 public void fillCommonPart(AccountRole ar,
211 DocumentWrapper<List<AccountRoleRel>> wrapDoc,
212 boolean handleDelete)
214 List<AccountRoleRel> arrl = wrapDoc.getWrappedObject();
215 SubjectType subject = ar.getSubject();
216 if (subject == null) {
217 //it is not required to give subject as URI determines the subject
218 subject = getSubject(getServiceContext());
220 //subject mismatch should have been checked during validation
222 if (subject.equals(SubjectType.ROLE)) {
223 //FIXME: potential index out of bounds exception...negative test needed
224 AccountValue av = ar.getAccount().get(0);
226 for (RoleValue rv : ar.getRole()) {
227 AccountRoleRel arr = buildAccountRoleRel(av, rv, handleDelete);
230 } else if (SubjectType.ACCOUNT.equals(subject)) {
231 //FIXME: potential index out of bounds exception...negative test needed
232 RoleValue rv = ar.getRole().get(0);
233 for (AccountValue av : ar.getAccount()) {
234 AccountRoleRel arr = buildAccountRoleRel(av, rv, handleDelete);
241 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#fillCommonPart(java.lang.Object, org.collectionspace.services.common.document.DocumentWrapper)
244 public void fillCommonPart(AccountRole ar,
245 DocumentWrapper<List<AccountRoleRel>> wrapDoc)
247 fillCommonPart(ar, wrapDoc, false);
251 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#extractCommonPart(org.collectionspace.services.common.document.DocumentWrapper)
254 public AccountRole extractCommonPart(
255 DocumentWrapper<List<AccountRoleRel>> wrapDoc)
257 List<AccountRoleRel> arrl = wrapDoc.getWrappedObject();
258 AccountRole ar = new AccountRole();
259 SubjectType subject = getSubject(getServiceContext());
260 if (arrl.size() == 0) {
263 AccountRoleRel ar0 = arrl.get(0);
264 if (SubjectType.ROLE.equals(subject)) {
266 List<AccountValue> avs = new ArrayList<AccountValue>();
268 AccountValue av = AuthorizationRoleRel.buildAccountValue(ar0);
274 List<RoleValue> rvs = new ArrayList<RoleValue>();
276 for (AccountRoleRel arr : arrl) {
277 RoleValue rv = AuthorizationRoleRel.buildRoleValue(arr);
282 } else if (SubjectType.ACCOUNT.equals(subject)) {
284 List<RoleValue> rvs = new ArrayList<RoleValue>();
286 RoleValue rv = AuthorizationRoleRel.buildRoleValue(ar0);
290 List<AccountValue> avs = new ArrayList<AccountValue>();
292 for (AccountRoleRel arr : arrl) {
293 AccountValue av = AuthorizationRoleRel.buildAccountValue(arr);
301 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getCommonPart()
304 public AccountRole getCommonPart() {
309 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#setCommonPart(java.lang.Object)
312 public void setCommonPart(AccountRole accountRole) {
313 this.accountRole = accountRole;
317 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getCommonPartList()
320 public AccountRole getCommonPartList() {
325 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#setCommonPartList(java.lang.Object)
328 public void setCommonPartList(AccountRole theAccountRole) {
329 // this.accountRolesList = accountRolesList;
330 this.accountRole = theAccountRole;
334 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getQProperty(java.lang.String)
337 public String getQProperty(
343 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#createDocumentFilter()
346 public DocumentFilter createDocumentFilter() {
347 return new JpaDocumentFilter(this.getServiceContext());
351 * Builds the account role rel.
355 * @return the account role rel
357 private AccountRoleRel buildAccountRoleRel(AccountValue av, RoleValue rv, boolean handleDelete) {
358 AccountRoleRel arr = new AccountRoleRel();
359 arr.setAccountId(av.getAccountId());
360 arr.setUserId(av.getUserId());
361 arr.setScreenName(av.getScreenName());
362 arr.setRoleId(rv.getRoleId());
363 arr.setRoleName(rv.getRoleName());
365 String relationshipId = rv.getRoleRelationshipId();
366 if (relationshipId != null && handleDelete == true) {
367 arr.setHjid(Long.parseLong(relationshipId)); // set this so we can convince JPA to del the relation
376 * @return the subject
378 static SubjectType getSubject(ServiceContext ctx) {
379 Object o = ctx.getProperty(ServiceContextProperties.SUBJECT);
381 throw new IllegalArgumentException(ServiceContextProperties.SUBJECT
382 + " property is missing in context "
385 return (SubjectType) o;