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.AccountRole;
30 import org.collectionspace.services.authorization.AccountRoleRel;
31 import org.collectionspace.services.authorization.AccountValue;
32 import org.collectionspace.services.authorization.PermissionsRolesList;
33 import org.collectionspace.services.authorization.RoleValue;
34 import org.collectionspace.services.authorization.SubjectType;
35 import org.collectionspace.services.common.context.ServiceContext;
37 import org.collectionspace.services.common.document.AbstractDocumentHandlerImpl;
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;
45 * Document handler for AccountRole association
48 public class AccountRoleDocumentHandler
49 extends AbstractDocumentHandlerImpl<AccountRole, PermissionsRolesList, List<AccountRoleRel>, List<AccountRoleRel>> {
51 private final Logger logger = LoggerFactory.getLogger(AccountRoleDocumentHandler.class);
52 private AccountRole accountRole;
53 private PermissionsRolesList accountRolesList;
56 public void handleCreate(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
57 fillCommonPart(getCommonPart(), wrapDoc);
61 public void handleUpdate(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
62 throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
66 public void completeUpdate(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
67 throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
71 public void handleGet(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
72 setCommonPart(extractCommonPart(wrapDoc));
73 getServiceContext().setOutput(accountRole);
77 public void handleGetAll(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
78 throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
82 public void handleDelete(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
83 fillCommonPart(getCommonPart(), wrapDoc);
87 public AccountRole extractCommonPart(
88 DocumentWrapper<List<AccountRoleRel>> wrapDoc)
90 List<AccountRoleRel> arrl = wrapDoc.getWrappedObject();
91 AccountRole ar = new AccountRole();
92 SubjectType subject = getSubject(getServiceContext());
93 if (arrl.size() == 0) {
96 AccountRoleRel ar0 = arrl.get(0);
97 if (SubjectType.ROLE.equals(subject)) {
99 List<AccountValue> avs = new ArrayList<AccountValue>();
101 AccountValue av = buildAccountValue(ar0);
105 List<RoleValue> rvs = new ArrayList<RoleValue>();
107 for (AccountRoleRel arr : arrl) {
108 RoleValue rv = buildRoleValue(arr);
111 } else if (SubjectType.ACCOUNT.equals(subject)) {
113 List<RoleValue> rvs = new ArrayList<RoleValue>();
115 RoleValue rv = buildRoleValue(ar0);
119 List<AccountValue> avs = new ArrayList<AccountValue>();
121 for (AccountRoleRel arr : arrl) {
122 AccountValue av = buildAccountValue(arr);
130 public void fillCommonPart(AccountRole ar, DocumentWrapper<List<AccountRoleRel>> wrapDoc)
132 List<AccountRoleRel> arrl = wrapDoc.getWrappedObject();
133 SubjectType subject = ar.getSubject();
134 if (subject == null) {
135 //it is not required to give subject as URI determines the subject
136 subject = getSubject(getServiceContext());
138 //subject mismatch should have been checked during validation
140 if (subject.equals(SubjectType.ROLE)) {
141 //FIXME: potential index out of bounds exception...negative test needed
142 AccountValue av = ar.getAccounts().get(0);
144 for (RoleValue rv : ar.getRoles()) {
145 AccountRoleRel arr = buildAccountRoleRel(av, rv);
148 } else if (SubjectType.ACCOUNT.equals(subject)) {
149 //FIXME: potential index out of bounds exception...negative test needed
150 RoleValue rv = ar.getRoles().get(0);
151 for (AccountValue av : ar.getAccounts()) {
152 AccountRoleRel arr = buildAccountRoleRel(av, rv);
159 public PermissionsRolesList extractCommonPartList(
160 DocumentWrapper<List<AccountRoleRel>> wrapDoc)
163 throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
167 public AccountRole getCommonPart() {
172 public void setCommonPart(AccountRole accountRole) {
173 this.accountRole = accountRole;
177 public PermissionsRolesList getCommonPartList() {
178 return accountRolesList;
182 public void setCommonPartList(PermissionsRolesList accountRolesList) {
183 this.accountRolesList = accountRolesList;
187 public String getQProperty(
193 public DocumentFilter createDocumentFilter() {
194 return new DocumentFilter(this.getServiceContext());
197 private AccountValue buildAccountValue(AccountRoleRel arr) {
198 AccountValue av = new AccountValue();
199 av.setAccountId(arr.getAccountId());
200 av.setUserId(arr.getUserId());
201 av.setScreenName(arr.getScreenName());
205 private RoleValue buildRoleValue(AccountRoleRel arr) {
206 RoleValue rv = new RoleValue();
207 rv.setRoleId(arr.getRoleId());
208 rv.setRoleName(arr.getRoleName());
212 private AccountRoleRel buildAccountRoleRel(AccountValue av, RoleValue rv) {
213 AccountRoleRel arr = new AccountRoleRel();
214 arr.setAccountId(av.getAccountId());
215 arr.setUserId(av.getUserId());
216 arr.setScreenName(av.getScreenName());
218 arr.setRoleId(rv.getRoleId());
219 arr.setRoleName(rv.getRoleName());
223 static SubjectType getSubject(ServiceContext ctx) {
224 Object o = ctx.getProperty(ServiceContextProperties.SUBJECT);
226 throw new IllegalArgumentException(ServiceContextProperties.SUBJECT
227 + " property is missing in context "
230 return (SubjectType) o;