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;
36 import org.collectionspace.services.common.storage.jpa.JpaDocumentHandler;
38 import org.collectionspace.services.common.document.AbstractDocumentHandlerImpl;
39 import org.collectionspace.services.common.document.DocumentFilter;
40 import org.collectionspace.services.common.document.DocumentWrapper;
41 import org.collectionspace.services.common.context.ServiceContextProperties;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * Document handler for AccountRole association
50 public class AccountRoleDocumentHandler
51 extends JpaDocumentHandler<AccountRole, PermissionsRolesList, List<AccountRoleRel>, List<AccountRoleRel>> {
53 private final Logger logger = LoggerFactory.getLogger(AccountRoleDocumentHandler.class);
54 private AccountRole accountRole;
55 private PermissionsRolesList accountRolesList;
58 public void handleCreate(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
59 fillCommonPart(getCommonPart(), wrapDoc);
63 public void handleUpdate(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
64 throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
68 public void completeUpdate(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
69 throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
73 public void handleGet(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
74 setCommonPart(extractCommonPart(wrapDoc));
75 getServiceContext().setOutput(accountRole);
79 public void handleGetAll(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
80 throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
84 public void handleDelete(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
85 fillCommonPart(getCommonPart(), wrapDoc);
89 public AccountRole extractCommonPart(
90 DocumentWrapper<List<AccountRoleRel>> wrapDoc)
92 List<AccountRoleRel> arrl = wrapDoc.getWrappedObject();
93 AccountRole ar = new AccountRole();
94 SubjectType subject = getSubject(getServiceContext());
95 if (arrl.size() == 0) {
98 AccountRoleRel ar0 = arrl.get(0);
99 if (SubjectType.ROLE.equals(subject)) {
101 List<AccountValue> avs = new ArrayList<AccountValue>();
103 AccountValue av = buildAccountValue(ar0);
107 List<RoleValue> rvs = new ArrayList<RoleValue>();
109 for (AccountRoleRel arr : arrl) {
110 RoleValue rv = buildRoleValue(arr);
113 } else if (SubjectType.ACCOUNT.equals(subject)) {
115 List<RoleValue> rvs = new ArrayList<RoleValue>();
117 RoleValue rv = buildRoleValue(ar0);
121 List<AccountValue> avs = new ArrayList<AccountValue>();
123 for (AccountRoleRel arr : arrl) {
124 AccountValue av = buildAccountValue(arr);
132 public void fillCommonPart(AccountRole ar, DocumentWrapper<List<AccountRoleRel>> wrapDoc)
134 List<AccountRoleRel> arrl = wrapDoc.getWrappedObject();
135 SubjectType subject = ar.getSubject();
136 if (subject == null) {
137 //it is not required to give subject as URI determines the subject
138 subject = getSubject(getServiceContext());
140 //subject mismatch should have been checked during validation
142 if (subject.equals(SubjectType.ROLE)) {
143 //FIXME: potential index out of bounds exception...negative test needed
144 AccountValue av = ar.getAccounts().get(0);
146 for (RoleValue rv : ar.getRoles()) {
147 AccountRoleRel arr = buildAccountRoleRel(av, rv);
150 } else if (SubjectType.ACCOUNT.equals(subject)) {
151 //FIXME: potential index out of bounds exception...negative test needed
152 RoleValue rv = ar.getRoles().get(0);
153 for (AccountValue av : ar.getAccounts()) {
154 AccountRoleRel arr = buildAccountRoleRel(av, rv);
161 public PermissionsRolesList extractCommonPartList(
162 DocumentWrapper<List<AccountRoleRel>> wrapDoc)
165 throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
169 public AccountRole getCommonPart() {
174 public void setCommonPart(AccountRole accountRole) {
175 this.accountRole = accountRole;
179 public PermissionsRolesList getCommonPartList() {
180 return accountRolesList;
184 public void setCommonPartList(PermissionsRolesList accountRolesList) {
185 this.accountRolesList = accountRolesList;
189 public String getQProperty(
195 public DocumentFilter createDocumentFilter() {
196 return new DocumentFilter(this.getServiceContext());
199 private AccountValue buildAccountValue(AccountRoleRel arr) {
200 AccountValue av = new AccountValue();
201 av.setAccountId(arr.getAccountId());
202 av.setUserId(arr.getUserId());
203 av.setScreenName(arr.getScreenName());
207 private RoleValue buildRoleValue(AccountRoleRel arr) {
208 RoleValue rv = new RoleValue();
209 rv.setRoleId(arr.getRoleId());
210 rv.setRoleName(arr.getRoleName());
214 private AccountRoleRel buildAccountRoleRel(AccountValue av, RoleValue rv) {
215 AccountRoleRel arr = new AccountRoleRel();
216 arr.setAccountId(av.getAccountId());
217 arr.setUserId(av.getUserId());
218 arr.setScreenName(av.getScreenName());
220 arr.setRoleId(rv.getRoleId());
221 arr.setRoleName(rv.getRoleName());
225 static SubjectType getSubject(ServiceContext ctx) {
226 Object o = ctx.getProperty(ServiceContextProperties.SUBJECT);
228 throw new IllegalArgumentException(ServiceContextProperties.SUBJECT
229 + " property is missing in context "
232 return (SubjectType) o;