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.common.authorization_mgt.AuthorizationRoleRel;
30 import org.collectionspace.services.authorization.AccountRole;
31 import org.collectionspace.services.authorization.AccountRoleRel;
32 import org.collectionspace.services.authorization.AccountValue;
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.JpaDocumentFilter;
37 import org.collectionspace.services.common.storage.jpa.JpaDocumentHandler;
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;
44 // TODO: Auto-generated Javadoc
46 * Document handler for AccountRole association.
50 public class AccountRoleDocumentHandler
51 extends JpaDocumentHandler<AccountRole, AccountRole, List<AccountRoleRel>, List<AccountRoleRel>> {
54 private final Logger logger = LoggerFactory.getLogger(AccountRoleDocumentHandler.class);
56 /** The account role. */
57 private AccountRole accountRole;
58 // private AccountRolesList accountRolesList;
59 /** The account role csid. */
60 private String accountRoleCsid = null;
63 * Gets the account role csid.
65 * @return the account role csid
67 public String getAccountRoleCsid() {
68 return this.accountRoleCsid;
72 * Sets the account role csid.
74 * @param theAccountRoleCsid the new account role csid
76 public void setAccountRoleCsid(String theAccountRoleCsid) {
77 this.accountRoleCsid = theAccountRoleCsid;
81 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
84 public void handleCreate(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
85 fillCommonPart(getCommonPart(), wrapDoc);
89 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
92 public void handleUpdate(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
93 throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
97 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#completeUpdate(org.collectionspace.services.common.document.DocumentWrapper)
100 public void completeUpdate(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
101 throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
105 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleGet(org.collectionspace.services.common.document.DocumentWrapper)
108 public void handleGet(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
109 AccountRole output = extractCommonPart(wrapDoc);
110 setCommonPart(output);
111 getServiceContext().setOutput(output);
115 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleGetAll(org.collectionspace.services.common.document.DocumentWrapper)
118 public void handleGetAll(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
119 throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
123 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleDelete(org.collectionspace.services.common.document.DocumentWrapper)
126 public void handleDelete(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
127 fillCommonPart(getCommonPart(), wrapDoc, true);
131 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#extractCommonPartList(org.collectionspace.services.common.document.DocumentWrapper)
134 public AccountRole extractCommonPartList(
135 DocumentWrapper<List<AccountRoleRel>> wrapDoc)
138 throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
140 // AccountRolesList result = new AccountRolesList();
141 // List<AccountRoleRel> arrl = wrapDoc.getWrappedObject();
142 // AccountRole ar = new AccountRole();
143 // SubjectType subject = getSubject(getServiceContext());
144 // if (arrl.size() == 0) {
148 //// result.setSubject(subject);
149 // AccountRoleRel ar0 = arrl.get(0);
150 // AccountValue av = buildAccountValue(ar0);
151 //// result.setAccount(av);
153 // List<AccountRoleListItem> accountRoleListItems = result.getAccountRoleListItems();
154 // if (accountRoleListItems == null) {
155 // accountRoleListItems = new ArrayList<AccountRoleListItem>();
157 // for (AccountRoleRel e : arrl) {
158 // AccountRoleListItem accountRoleListItem = new AccountRoleListItem();
160 // accountRoleListItem.setRoleName(e.getRoleName());
161 // accountRoleListItem.setRoleId(e.getRoleId());
162 // accountRoleListItem.setCsid(e.getHjid().toString());
163 // // add item to result list
164 // accountRoleListItems.add(accountRoleListItem);
168 // // Old Sanjay code
171 // ar0 = arrl.get(0);
172 // if (SubjectType.ROLE.equals(subject)) {
174 // List<AccountValue> avs = new ArrayList<AccountValue>();
175 // ar.setAccounts(avs);
176 // av = buildAccountValue(ar0);
180 // List<RoleValue> rvs = new ArrayList<RoleValue>();
182 // for (AccountRoleRel arr : arrl) {
183 // RoleValue rv = buildRoleValue(arr);
186 // } else if (SubjectType.ACCOUNT.equals(subject)) {
188 // List<RoleValue> rvs = new ArrayList<RoleValue>();
190 // RoleValue rv = buildRoleValue(ar0);
194 // List<AccountValue> avs = new ArrayList<AccountValue>();
195 // ar.setAccounts(avs);
196 // for (AccountRoleRel arr : arrl) {
197 // av = buildAccountValue(arr);
204 public void fillCommonPart(AccountRole ar,
205 DocumentWrapper<List<AccountRoleRel>> wrapDoc,
206 boolean handleDelete)
208 List<AccountRoleRel> arrl = wrapDoc.getWrappedObject();
209 SubjectType subject = ar.getSubject();
210 if (subject == null) {
211 //it is not required to give subject as URI determines the subject
212 subject = getSubject(getServiceContext());
214 //subject mismatch should have been checked during validation
216 if (subject.equals(SubjectType.ROLE)) {
217 //FIXME: potential index out of bounds exception...negative test needed
218 AccountValue av = ar.getAccount().get(0);
220 for (RoleValue rv : ar.getRole()) {
221 AccountRoleRel arr = buildAccountRoleRel(av, rv, handleDelete);
224 } else if (SubjectType.ACCOUNT.equals(subject)) {
225 //FIXME: potential index out of bounds exception...negative test needed
226 RoleValue rv = ar.getRole().get(0);
227 for (AccountValue av : ar.getAccount()) {
228 AccountRoleRel arr = buildAccountRoleRel(av, rv, handleDelete);
235 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#fillCommonPart(java.lang.Object, org.collectionspace.services.common.document.DocumentWrapper)
238 public void fillCommonPart(AccountRole ar,
239 DocumentWrapper<List<AccountRoleRel>> wrapDoc)
241 fillCommonPart(ar, wrapDoc, false);
245 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#extractCommonPart(org.collectionspace.services.common.document.DocumentWrapper)
248 public AccountRole extractCommonPart(
249 DocumentWrapper<List<AccountRoleRel>> wrapDoc)
251 List<AccountRoleRel> arrl = wrapDoc.getWrappedObject();
252 AccountRole ar = new AccountRole();
253 SubjectType subject = getSubject(getServiceContext());
254 if (arrl.size() == 0) {
257 AccountRoleRel ar0 = arrl.get(0);
258 if (SubjectType.ROLE.equals(subject)) {
260 List<AccountValue> avs = new ArrayList<AccountValue>();
262 AccountValue av = AuthorizationRoleRel.buildAccountValue(ar0);
268 List<RoleValue> rvs = new ArrayList<RoleValue>();
270 for (AccountRoleRel arr : arrl) {
271 RoleValue rv = AuthorizationRoleRel.buildRoleValue(arr);
276 } else if (SubjectType.ACCOUNT.equals(subject)) {
278 List<RoleValue> rvs = new ArrayList<RoleValue>();
280 RoleValue rv = AuthorizationRoleRel.buildRoleValue(ar0);
284 List<AccountValue> avs = new ArrayList<AccountValue>();
286 for (AccountRoleRel arr : arrl) {
287 AccountValue av = AuthorizationRoleRel.buildAccountValue(arr);
295 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getCommonPart()
298 public AccountRole getCommonPart() {
303 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#setCommonPart(java.lang.Object)
306 public void setCommonPart(AccountRole accountRole) {
307 this.accountRole = accountRole;
311 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getCommonPartList()
314 public AccountRole getCommonPartList() {
319 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#setCommonPartList(java.lang.Object)
322 public void setCommonPartList(AccountRole theAccountRole) {
323 // this.accountRolesList = accountRolesList;
324 this.accountRole = theAccountRole;
328 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getQProperty(java.lang.String)
331 public String getQProperty(
337 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#createDocumentFilter()
340 public DocumentFilter createDocumentFilter() {
341 return new JpaDocumentFilter(this.getServiceContext());
345 * Builds the account role rel.
349 * @return the account role rel
351 private AccountRoleRel buildAccountRoleRel(AccountValue av, RoleValue rv, boolean handleDelete) {
352 AccountRoleRel arr = new AccountRoleRel();
353 arr.setAccountId(av.getAccountId());
354 arr.setUserId(av.getUserId());
355 arr.setScreenName(av.getScreenName());
356 arr.setRoleId(rv.getRoleId());
357 arr.setRoleName(rv.getRoleName());
359 String relationshipId = rv.getRoleRelationshipId();
360 if (relationshipId != null && handleDelete == true) {
361 arr.setHjid(Long.parseLong(relationshipId)); // set this so we can convince JPA to del the relation
370 * @return the subject
372 static SubjectType getSubject(ServiceContext ctx) {
373 Object o = ctx.getProperty(ServiceContextProperties.SUBJECT);
375 throw new IllegalArgumentException(ServiceContextProperties.SUBJECT
376 + " property is missing in context "
379 return (SubjectType) o;