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(output);
117 getServiceContext().setOutput(output);
121 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleGetAll(org.collectionspace.services.common.document.DocumentWrapper)
124 public void handleGetAll(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
125 throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
129 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleDelete(org.collectionspace.services.common.document.DocumentWrapper)
132 public void handleDelete(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
133 fillCommonPart(getCommonPart(), wrapDoc, true);
137 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#extractCommonPartList(org.collectionspace.services.common.document.DocumentWrapper)
140 public AccountRole extractCommonPartList(
141 DocumentWrapper<List<AccountRoleRel>> wrapDoc)
144 throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
146 // AccountRolesList result = new AccountRolesList();
147 // List<AccountRoleRel> arrl = wrapDoc.getWrappedObject();
148 // AccountRole ar = new AccountRole();
149 // SubjectType subject = getSubject(getServiceContext());
150 // if (arrl.size() == 0) {
154 //// result.setSubject(subject);
155 // AccountRoleRel ar0 = arrl.get(0);
156 // AccountValue av = buildAccountValue(ar0);
157 //// result.setAccount(av);
159 // List<AccountRoleListItem> accountRoleListItems = result.getAccountRoleListItems();
160 // if (accountRoleListItems == null) {
161 // accountRoleListItems = new ArrayList<AccountRoleListItem>();
163 // for (AccountRoleRel e : arrl) {
164 // AccountRoleListItem accountRoleListItem = new AccountRoleListItem();
166 // accountRoleListItem.setRoleName(e.getRoleName());
167 // accountRoleListItem.setRoleId(e.getRoleId());
168 // accountRoleListItem.setCsid(e.getHjid().toString());
169 // // add item to result list
170 // accountRoleListItems.add(accountRoleListItem);
174 // // Old Sanjay code
177 // ar0 = arrl.get(0);
178 // if (SubjectType.ROLE.equals(subject)) {
180 // List<AccountValue> avs = new ArrayList<AccountValue>();
181 // ar.setAccounts(avs);
182 // av = buildAccountValue(ar0);
186 // List<RoleValue> rvs = new ArrayList<RoleValue>();
188 // for (AccountRoleRel arr : arrl) {
189 // RoleValue rv = buildRoleValue(arr);
192 // } else if (SubjectType.ACCOUNT.equals(subject)) {
194 // List<RoleValue> rvs = new ArrayList<RoleValue>();
196 // RoleValue rv = buildRoleValue(ar0);
200 // List<AccountValue> avs = new ArrayList<AccountValue>();
201 // ar.setAccounts(avs);
202 // for (AccountRoleRel arr : arrl) {
203 // av = buildAccountValue(arr);
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 DocumentFilter(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;