]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
9233d9f1047aba6d294641fed7fe286181b446a2
[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 import org.collectionspace.services.common.storage.jpa.JpaDocumentHandler;
37
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;
42
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 /**
47  * Document handler for AccountRole association
48  * @author 
49  */
50 public class AccountRoleDocumentHandler
51         extends JpaDocumentHandler<AccountRole, PermissionsRolesList, List<AccountRoleRel>, List<AccountRoleRel>> {
52
53     private final Logger logger = LoggerFactory.getLogger(AccountRoleDocumentHandler.class);
54     private AccountRole accountRole;
55     private PermissionsRolesList accountRolesList;
56
57     @Override
58     public void handleCreate(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
59         fillCommonPart(getCommonPart(), wrapDoc);
60     }
61
62     @Override
63     public void handleUpdate(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
64         throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
65     }
66
67     @Override
68     public void completeUpdate(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
69         throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
70     }
71
72     @Override
73     public void handleGet(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
74         setCommonPart(extractCommonPart(wrapDoc));
75         getServiceContext().setOutput(accountRole);
76     }
77
78     @Override
79     public void handleGetAll(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
80         throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
81     }
82
83     @Override
84     public void handleDelete(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
85         fillCommonPart(getCommonPart(), wrapDoc);
86     }
87
88     @Override
89     public AccountRole extractCommonPart(
90             DocumentWrapper<List<AccountRoleRel>> wrapDoc)
91             throws Exception {
92         List<AccountRoleRel> arrl = wrapDoc.getWrappedObject();
93         AccountRole ar = new AccountRole();
94         SubjectType subject = getSubject(getServiceContext());
95         if (arrl.size() == 0) {
96             return ar;
97         }
98         AccountRoleRel ar0 = arrl.get(0);
99         if (SubjectType.ROLE.equals(subject)) {
100
101             List<AccountValue> avs = new ArrayList<AccountValue>();
102             ar.setAccounts(avs);
103             AccountValue av = buildAccountValue(ar0);
104             avs.add(av);
105
106             //add roles
107             List<RoleValue> rvs = new ArrayList<RoleValue>();
108             ar.setRoles(rvs);
109             for (AccountRoleRel arr : arrl) {
110                 RoleValue rv = buildRoleValue(arr);
111                 rvs.add(rv);
112             }
113         } else if (SubjectType.ACCOUNT.equals(subject)) {
114
115             List<RoleValue> rvs = new ArrayList<RoleValue>();
116             ar.setRoles(rvs);
117             RoleValue rv = buildRoleValue(ar0);
118             rvs.add(rv);
119
120             //add accounts
121             List<AccountValue> avs = new ArrayList<AccountValue>();
122             ar.setAccounts(avs);
123             for (AccountRoleRel arr : arrl) {
124                 AccountValue av = buildAccountValue(arr);
125                 avs.add(av);
126             }
127         }
128         return ar;
129     }
130
131     @Override
132     public void fillCommonPart(AccountRole ar, DocumentWrapper<List<AccountRoleRel>> wrapDoc)
133             throws Exception {
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());
139         } else {
140             //subject mismatch should have been checked during validation
141         }
142         if (subject.equals(SubjectType.ROLE)) {
143             //FIXME: potential index out of bounds exception...negative test needed
144             AccountValue av = ar.getAccounts().get(0);
145
146             for (RoleValue rv : ar.getRoles()) {
147                 AccountRoleRel arr = buildAccountRoleRel(av, rv);
148                 arrl.add(arr);
149             }
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);
155                 arrl.add(arr);
156             }
157         }
158     }
159
160     @Override
161     public PermissionsRolesList extractCommonPartList(
162             DocumentWrapper<List<AccountRoleRel>> wrapDoc)
163             throws Exception {
164
165         throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
166     }
167
168     @Override
169     public AccountRole getCommonPart() {
170         return accountRole;
171     }
172
173     @Override
174     public void setCommonPart(AccountRole accountRole) {
175         this.accountRole = accountRole;
176     }
177
178     @Override
179     public PermissionsRolesList getCommonPartList() {
180         return accountRolesList;
181     }
182
183     @Override
184     public void setCommonPartList(PermissionsRolesList accountRolesList) {
185         this.accountRolesList = accountRolesList;
186     }
187
188     @Override
189     public String getQProperty(
190             String prop) {
191         return null;
192     }
193
194     @Override
195     public DocumentFilter createDocumentFilter() {
196         return new DocumentFilter(this.getServiceContext());
197     }
198
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());
204         return av;
205     }
206
207     private RoleValue buildRoleValue(AccountRoleRel arr) {
208         RoleValue rv = new RoleValue();
209         rv.setRoleId(arr.getRoleId());
210         rv.setRoleName(arr.getRoleName());
211         return rv;
212     }
213
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());
219
220         arr.setRoleId(rv.getRoleId());
221         arr.setRoleName(rv.getRoleName());
222         return arr;
223     }
224
225     static SubjectType getSubject(ServiceContext ctx) {
226         Object o = ctx.getProperty(ServiceContextProperties.SUBJECT);
227         if (o == null) {
228             throw new IllegalArgumentException(ServiceContextProperties.SUBJECT
229                     + " property is missing in context "
230                     + ctx.toString());
231         }
232         return (SubjectType) o;
233     }
234 }