]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
7acb87f907f99d774db9d70c44ed4a6943a65972
[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 AccountRole extractCommonPart(
83             DocumentWrapper<List<AccountRoleRel>> wrapDoc)
84             throws Exception {
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)) {
90
91             List<AccountValue> avs = new ArrayList<AccountValue>();
92             ar.setAccounts(avs);
93             AccountValue av = buildAccountValue(ar0);
94             avs.add(av);
95
96             //add roles
97             List<RoleValue> rvs = new ArrayList<RoleValue>();
98             ar.setRoles(rvs);
99             for (AccountRoleRel arr : arrl) {
100                 RoleValue rv = buildRoleValue(arr);
101                 rvs.add(rv);
102             }
103         } else if (SubjectType.ACCOUNT.equals(subject)) {
104
105             List<RoleValue> rvs = new ArrayList<RoleValue>();
106             ar.setRoles(rvs);
107             RoleValue rv = buildRoleValue(ar0);
108             rvs.add(rv);
109
110             //add accounts
111             List<AccountValue> avs = new ArrayList<AccountValue>();
112             ar.setAccounts(avs);
113             for (AccountRoleRel arr : arrl) {
114                 AccountValue av = buildAccountValue(arr);
115                 avs.add(av);
116             }
117         }
118         return ar;
119     }
120
121     @Override
122     public void fillCommonPart(AccountRole ar, DocumentWrapper<List<AccountRoleRel>> wrapDoc)
123             throws Exception {
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());
129         } else {
130             //subject mismatch should have been checked during validation
131         }
132         if (subject.equals(SubjectType.ROLE)) {
133             AccountValue av = ar.getAccounts().get(0);
134
135             for (RoleValue rv : ar.getRoles()) {
136                 AccountRoleRel arr = buildAccountRoleRel(av, rv);
137                 arrl.add(arr);
138             }
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);
143                 arrl.add(arr);
144             }
145         }
146     }
147
148     @Override
149     public PermissionsRolesList extractCommonPartList(
150             DocumentWrapper<List<AccountRoleRel>> wrapDoc)
151             throws Exception {
152
153         throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
154     }
155
156     @Override
157     public AccountRole getCommonPart() {
158         return accountRole;
159     }
160
161     @Override
162     public void setCommonPart(AccountRole accountRole) {
163         this.accountRole = accountRole;
164     }
165
166     @Override
167     public PermissionsRolesList getCommonPartList() {
168         return accountRolesList;
169     }
170
171     @Override
172     public void setCommonPartList(PermissionsRolesList accountRolesList) {
173         this.accountRolesList = accountRolesList;
174     }
175
176     @Override
177     public String getQProperty(
178             String prop) {
179         return null;
180     }
181
182     @Override
183     public DocumentFilter createDocumentFilter() {
184         return new DocumentFilter(this.getServiceContext());
185     }
186
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());
192         return av;
193     }
194
195     private RoleValue buildRoleValue(AccountRoleRel arr) {
196         RoleValue rv = new RoleValue();
197         rv.setRoleId(arr.getRoleId());
198         rv.setRoleName(arr.getRoleName());
199         return rv;
200     }
201
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());
207
208         arr.setRoleId(rv.getRoleId());
209         arr.setRoleName(rv.getRoleName());
210         return arr;
211     }
212
213     static SubjectType getSubject(ServiceContext ctx) {
214         Object o = ctx.getProperty(ServiceContextProperties.SUBJECT);
215         if (o == null) {
216             throw new IllegalArgumentException(ServiceContextProperties.SUBJECT +
217                     " property is missing in context "
218                     + ctx.toString());
219         }
220         return (SubjectType) o;
221     }
222 }