]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
34edb72b7f86bafbdd857ba7b952700e68221583
[tmp/jakarta-migration.git] /
1 /**
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:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
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.
23  */
24 package org.collectionspace.services.account.storage;
25
26 import java.util.ArrayList;
27 import java.util.List;
28
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
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;
43
44 /**
45  * Document handler for AccountRole association
46  * @author 
47  */
48 public class AccountRoleDocumentHandler
49         extends AbstractDocumentHandlerImpl<AccountRole, PermissionsRolesList, List<AccountRoleRel>, List<AccountRoleRel>> {
50
51     private final Logger logger = LoggerFactory.getLogger(AccountRoleDocumentHandler.class);
52     private AccountRole accountRole;
53     private PermissionsRolesList accountRolesList;
54
55     @Override
56     public void handleCreate(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
57         fillCommonPart(getCommonPart(), wrapDoc);
58     }
59
60     @Override
61     public void handleUpdate(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
62         throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
63     }
64
65     @Override
66     public void completeUpdate(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
67         throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
68     }
69
70     @Override
71     public void handleGet(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
72         setCommonPart(extractCommonPart(wrapDoc));
73         getServiceContext().setOutput(accountRole);
74     }
75
76     @Override
77     public void handleGetAll(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
78         throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
79     }
80
81     @Override
82     public void handleDelete(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
83         fillCommonPart(getCommonPart(), wrapDoc);
84     }
85
86     @Override
87     public AccountRole extractCommonPart(
88             DocumentWrapper<List<AccountRoleRel>> wrapDoc)
89             throws Exception {
90         List<AccountRoleRel> arrl = wrapDoc.getWrappedObject();
91         AccountRole ar = new AccountRole();
92         SubjectType subject = getSubject(getServiceContext());
93         if (arrl.size() == 0) {
94             return ar;
95         }
96         AccountRoleRel ar0 = arrl.get(0);
97         if (SubjectType.ROLE.equals(subject)) {
98
99             List<AccountValue> avs = new ArrayList<AccountValue>();
100             ar.setAccounts(avs);
101             AccountValue av = buildAccountValue(ar0);
102             avs.add(av);
103
104             //add roles
105             List<RoleValue> rvs = new ArrayList<RoleValue>();
106             ar.setRoles(rvs);
107             for (AccountRoleRel arr : arrl) {
108                 RoleValue rv = buildRoleValue(arr);
109                 rvs.add(rv);
110             }
111         } else if (SubjectType.ACCOUNT.equals(subject)) {
112
113             List<RoleValue> rvs = new ArrayList<RoleValue>();
114             ar.setRoles(rvs);
115             RoleValue rv = buildRoleValue(ar0);
116             rvs.add(rv);
117
118             //add accounts
119             List<AccountValue> avs = new ArrayList<AccountValue>();
120             ar.setAccounts(avs);
121             for (AccountRoleRel arr : arrl) {
122                 AccountValue av = buildAccountValue(arr);
123                 avs.add(av);
124             }
125         }
126         return ar;
127     }
128
129     @Override
130     public void fillCommonPart(AccountRole ar, DocumentWrapper<List<AccountRoleRel>> wrapDoc)
131             throws Exception {
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());
137         } else {
138             //subject mismatch should have been checked during validation
139         }
140         if (subject.equals(SubjectType.ROLE)) {
141             //FIXME: potential index out of bounds exception...negative test needed
142             AccountValue av = ar.getAccounts().get(0);
143
144             for (RoleValue rv : ar.getRoles()) {
145                 AccountRoleRel arr = buildAccountRoleRel(av, rv);
146                 arrl.add(arr);
147             }
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);
153                 arrl.add(arr);
154             }
155         }
156     }
157
158     @Override
159     public PermissionsRolesList extractCommonPartList(
160             DocumentWrapper<List<AccountRoleRel>> wrapDoc)
161             throws Exception {
162
163         throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
164     }
165
166     @Override
167     public AccountRole getCommonPart() {
168         return accountRole;
169     }
170
171     @Override
172     public void setCommonPart(AccountRole accountRole) {
173         this.accountRole = accountRole;
174     }
175
176     @Override
177     public PermissionsRolesList getCommonPartList() {
178         return accountRolesList;
179     }
180
181     @Override
182     public void setCommonPartList(PermissionsRolesList accountRolesList) {
183         this.accountRolesList = accountRolesList;
184     }
185
186     @Override
187     public String getQProperty(
188             String prop) {
189         return null;
190     }
191
192     @Override
193     public DocumentFilter createDocumentFilter() {
194         return new DocumentFilter(this.getServiceContext());
195     }
196
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());
202         return av;
203     }
204
205     private RoleValue buildRoleValue(AccountRoleRel arr) {
206         RoleValue rv = new RoleValue();
207         rv.setRoleId(arr.getRoleId());
208         rv.setRoleName(arr.getRoleName());
209         return rv;
210     }
211
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());
217
218         arr.setRoleId(rv.getRoleId());
219         arr.setRoleName(rv.getRoleName());
220         return arr;
221     }
222
223     static SubjectType getSubject(ServiceContext ctx) {
224         Object o = ctx.getProperty(ServiceContextProperties.SUBJECT);
225         if (o == null) {
226             throw new IllegalArgumentException(ServiceContextProperties.SUBJECT
227                     + " property is missing in context "
228                     + ctx.toString());
229         }
230         return (SubjectType) o;
231     }
232 }