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