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.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * Document handler for AccountRole association
47 public class AccountRoleDocumentHandler
48 extends AbstractDocumentHandlerImpl<AccountRole, PermissionsRolesList, List<AccountRoleRel>, List<AccountRoleRel>> {
50 private final Logger logger = LoggerFactory.getLogger(AccountRoleDocumentHandler.class);
51 private AccountRole accountRole;
52 private PermissionsRolesList accountRolesList;
55 public void handleCreate(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
56 fillCommonPart(getCommonPart(), wrapDoc);
60 public void handleUpdate(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
61 throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
65 public void completeUpdate(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
66 throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
70 public void handleGet(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
71 setCommonPart(extractCommonPart(wrapDoc));
72 getServiceContext().setOutput(accountRole);
76 public void handleGetAll(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
77 throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
81 public AccountRole extractCommonPart(
82 DocumentWrapper<List<AccountRoleRel>> wrapDoc)
84 List<AccountRoleRel> arrl = wrapDoc.getWrappedObject();
85 AccountRole ar = new AccountRole();
86 SubjectType subject = getSubject(getServiceContext());
87 AccountRoleRel ar0 = arrl.get(0);
88 if (SubjectType.ROLE.equals(subject)) {
90 List<AccountValue> avs = new ArrayList<AccountValue>();
92 AccountValue av = buildAccountValue(ar0);
96 List<RoleValue> rvs = new ArrayList<RoleValue>();
98 for (AccountRoleRel arr : arrl) {
99 RoleValue rv = buildRoleValue(arr);
102 } else if (SubjectType.ACCOUNT.equals(subject)) {
104 List<RoleValue> rvs = new ArrayList<RoleValue>();
106 RoleValue rv = buildRoleValue(ar0);
110 List<AccountValue> avs = new ArrayList<AccountValue>();
112 for (AccountRoleRel arr : arrl) {
113 AccountValue av = buildAccountValue(arr);
121 public void fillCommonPart(AccountRole ar, DocumentWrapper<List<AccountRoleRel>> wrapDoc)
123 List<AccountRoleRel> arrl = wrapDoc.getWrappedObject();
124 SubjectType subject = ar.getSubject();
125 if (subject == null) {
126 //it is not required to give subject as URI determines the subject
127 subject = getSubject(getServiceContext());
129 //subject mismatch should have been checked during validation
131 if (subject.equals(SubjectType.ROLE)) {
132 AccountValue av = ar.getAccounts().get(0);
134 for (RoleValue rv : ar.getRoles()) {
135 AccountRoleRel arr = buildAccountRoleRel(av, rv);
138 } else if (SubjectType.ACCOUNT.equals(subject)) {
139 RoleValue rv = ar.getRoles().get(0);
140 for (AccountValue av : ar.getAccounts()) {
141 AccountRoleRel arr = buildAccountRoleRel(av, rv);
148 public PermissionsRolesList extractCommonPartList(
149 DocumentWrapper<List<AccountRoleRel>> wrapDoc)
152 throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
156 public AccountRole getCommonPart() {
161 public void setCommonPart(AccountRole accountRole) {
162 this.accountRole = accountRole;
166 public PermissionsRolesList getCommonPartList() {
167 return accountRolesList;
171 public void setCommonPartList(PermissionsRolesList accountRolesList) {
172 this.accountRolesList = accountRolesList;
176 public String getQProperty(
182 public DocumentFilter createDocumentFilter() {
183 return new DocumentFilter(this.getServiceContext());
186 private AccountValue buildAccountValue(AccountRoleRel arr) {
187 AccountValue av = new AccountValue();
188 av.setAccountId(arr.getAccountId());
189 av.setUserId(arr.getUserId());
190 av.setScreenName(arr.getScreenName());
194 private RoleValue buildRoleValue(AccountRoleRel arr) {
195 RoleValue rv = new RoleValue();
196 rv.setRoleId(arr.getRoleId());
197 rv.setRoleName(arr.getRoleName());
201 private AccountRoleRel buildAccountRoleRel(AccountValue av, RoleValue rv) {
202 AccountRoleRel arr = new AccountRoleRel();
203 arr.setAccountId(av.getAccountId());
204 arr.setUserId(av.getUserId());
205 arr.setScreenName(av.getScreenName());
207 arr.setRoleId(rv.getRoleId());
208 arr.setRoleName(rv.getRoleName());
212 static SubjectType getSubject(ServiceContext ctx) {
213 Object o = ctx.getProperty("subject");
215 throw new IllegalArgumentException("property subject missing in context "
218 return (SubjectType) o;