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 AccountRole extractCommonPart(
83 DocumentWrapper<List<AccountRoleRel>> wrapDoc)
85 List<AccountRoleRel> arrl = wrapDoc.getWrappedObject();
86 AccountRole ar = new AccountRole();
87 SubjectType subject = getSubject(getServiceContext());
88 AccountRoleRel ar0 = arrl.get(0);
89 if (SubjectType.ROLE.equals(subject)) {
91 List<AccountValue> avs = new ArrayList<AccountValue>();
93 AccountValue av = buildAccountValue(ar0);
97 List<RoleValue> rvs = new ArrayList<RoleValue>();
99 for (AccountRoleRel arr : arrl) {
100 RoleValue rv = buildRoleValue(arr);
103 } else if (SubjectType.ACCOUNT.equals(subject)) {
105 List<RoleValue> rvs = new ArrayList<RoleValue>();
107 RoleValue rv = buildRoleValue(ar0);
111 List<AccountValue> avs = new ArrayList<AccountValue>();
113 for (AccountRoleRel arr : arrl) {
114 AccountValue av = buildAccountValue(arr);
122 public void fillCommonPart(AccountRole ar, DocumentWrapper<List<AccountRoleRel>> wrapDoc)
124 List<AccountRoleRel> arrl = wrapDoc.getWrappedObject();
125 SubjectType subject = ar.getSubject();
126 if (subject == null) {
127 //it is not required to give subject as URI determines the subject
128 subject = getSubject(getServiceContext());
130 //subject mismatch should have been checked during validation
132 if (subject.equals(SubjectType.ROLE)) {
133 AccountValue av = ar.getAccounts().get(0);
135 for (RoleValue rv : ar.getRoles()) {
136 AccountRoleRel arr = buildAccountRoleRel(av, rv);
139 } else if (SubjectType.ACCOUNT.equals(subject)) {
140 RoleValue rv = ar.getRoles().get(0);
141 for (AccountValue av : ar.getAccounts()) {
142 AccountRoleRel arr = buildAccountRoleRel(av, rv);
149 public PermissionsRolesList extractCommonPartList(
150 DocumentWrapper<List<AccountRoleRel>> wrapDoc)
153 throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
157 public AccountRole getCommonPart() {
162 public void setCommonPart(AccountRole accountRole) {
163 this.accountRole = accountRole;
167 public PermissionsRolesList getCommonPartList() {
168 return accountRolesList;
172 public void setCommonPartList(PermissionsRolesList accountRolesList) {
173 this.accountRolesList = accountRolesList;
177 public String getQProperty(
183 public DocumentFilter createDocumentFilter() {
184 return new DocumentFilter(this.getServiceContext());
187 private AccountValue buildAccountValue(AccountRoleRel arr) {
188 AccountValue av = new AccountValue();
189 av.setAccountId(arr.getAccountId());
190 av.setUserId(arr.getUserId());
191 av.setScreenName(arr.getScreenName());
195 private RoleValue buildRoleValue(AccountRoleRel arr) {
196 RoleValue rv = new RoleValue();
197 rv.setRoleId(arr.getRoleId());
198 rv.setRoleName(arr.getRoleName());
202 private AccountRoleRel buildAccountRoleRel(AccountValue av, RoleValue rv) {
203 AccountRoleRel arr = new AccountRoleRel();
204 arr.setAccountId(av.getAccountId());
205 arr.setUserId(av.getUserId());
206 arr.setScreenName(av.getScreenName());
208 arr.setRoleId(rv.getRoleId());
209 arr.setRoleName(rv.getRoleName());
213 static SubjectType getSubject(ServiceContext ctx) {
214 Object o = ctx.getProperty(ServiceContextProperties.SUBJECT);
216 throw new IllegalArgumentException(ServiceContextProperties.SUBJECT +
217 " property is missing in context "
220 return (SubjectType) o;