]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
150978b19041789cd1ae45662c283bfb0e360df3
[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.AccountRolesList;
30 //import org.collectionspace.services.authorization.AccountRolesList.AccountRoleListItem;
31
32 import org.collectionspace.services.common.authorization_mgt.AuthorizationRoleRel;
33 import org.collectionspace.services.authorization.AccountRole;
34 import org.collectionspace.services.authorization.AccountRoleRel;
35 import org.collectionspace.services.authorization.AccountValue;
36 import org.collectionspace.services.authorization.PermissionsRolesList;
37 import org.collectionspace.services.authorization.RoleValue;
38 import org.collectionspace.services.authorization.SubjectType;
39 import org.collectionspace.services.common.context.ServiceContext;
40 import org.collectionspace.services.common.storage.jpa.JpaDocumentHandler;
41
42 import org.collectionspace.services.common.document.AbstractDocumentHandlerImpl;
43 import org.collectionspace.services.common.document.DocumentFilter;
44 import org.collectionspace.services.common.document.DocumentWrapper;
45 import org.collectionspace.services.common.context.ServiceContextProperties;
46
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 // TODO: Auto-generated Javadoc
51 /**
52  * Document handler for AccountRole association.
53  *
54  * @author
55  */
56 public class AccountRoleDocumentHandler
57         extends JpaDocumentHandler<AccountRole, AccountRole, List<AccountRoleRel>, List<AccountRoleRel>> {
58
59     /** The logger. */
60     private final Logger logger = LoggerFactory.getLogger(AccountRoleDocumentHandler.class);
61     
62     /** The account role. */
63     private AccountRole accountRole;
64 //    private AccountRolesList accountRolesList;
65     /** The account role csid. */
66     private String accountRoleCsid = null;
67
68     /**
69      * Gets the account role csid.
70      *
71      * @return the account role csid
72      */
73     public String getAccountRoleCsid() {
74         return this.accountRoleCsid;
75     }
76     
77     /**
78      * Sets the account role csid.
79      *
80      * @param theAccountRoleCsid the new account role csid
81      */
82     public void setAccountRoleCsid(String theAccountRoleCsid) {
83         this.accountRoleCsid = theAccountRoleCsid;
84     }
85     
86     /* (non-Javadoc)
87      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
88      */
89     @Override
90     public void handleCreate(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
91         fillCommonPart(getCommonPart(), wrapDoc);
92     }
93
94     /* (non-Javadoc)
95      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
96      */
97     @Override
98     public void handleUpdate(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
99         throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
100     }
101
102     /* (non-Javadoc)
103      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#completeUpdate(org.collectionspace.services.common.document.DocumentWrapper)
104      */
105     @Override
106     public void completeUpdate(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
107         throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
108     }
109
110     /* (non-Javadoc)
111      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleGet(org.collectionspace.services.common.document.DocumentWrapper)
112      */
113     @Override
114     public void handleGet(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
115         AccountRole output = extractCommonPart(wrapDoc);
116         setCommonPart(output);
117         getServiceContext().setOutput(output);
118     }
119
120     /* (non-Javadoc)
121      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleGetAll(org.collectionspace.services.common.document.DocumentWrapper)
122      */
123     @Override
124     public void handleGetAll(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
125         throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
126     }
127
128     /* (non-Javadoc)
129      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleDelete(org.collectionspace.services.common.document.DocumentWrapper)
130      */
131     @Override
132     public void handleDelete(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
133         fillCommonPart(getCommonPart(), wrapDoc, true);
134     }
135
136     /* (non-Javadoc)
137      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#extractCommonPartList(org.collectionspace.services.common.document.DocumentWrapper)
138      */
139     @Override
140     public AccountRole extractCommonPartList(
141             DocumentWrapper<List<AccountRoleRel>> wrapDoc)
142             throws Exception {
143         
144         throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
145         
146 //      AccountRolesList result = new AccountRolesList();
147 //        List<AccountRoleRel> arrl = wrapDoc.getWrappedObject();
148 //        AccountRole ar = new AccountRole();
149 //        SubjectType subject = getSubject(getServiceContext());
150 //        if (arrl.size() == 0) {
151 //            return result;
152 //        }
153 //        
154 ////        result.setSubject(subject);
155 //        AccountRoleRel ar0 = arrl.get(0);
156 //        AccountValue av = buildAccountValue(ar0);
157 ////        result.setAccount(av);
158 //        
159 //      List<AccountRoleListItem> accountRoleListItems = result.getAccountRoleListItems();
160 //      if (accountRoleListItems == null) {
161 //              accountRoleListItems = new ArrayList<AccountRoleListItem>();
162 //      }
163 //      for (AccountRoleRel e : arrl) {
164 //              AccountRoleListItem accountRoleListItem = new AccountRoleListItem();
165 //              // fill the item
166 //              accountRoleListItem.setRoleName(e.getRoleName());
167 //              accountRoleListItem.setRoleId(e.getRoleId());
168 //              accountRoleListItem.setCsid(e.getHjid().toString());
169 //              // add item to result list
170 //              accountRoleListItems.add(accountRoleListItem);
171 //      }
172 //      
173 //        //
174 //      // Old Sanjay code
175 //      //
176 //      
177 //        ar0 = arrl.get(0);
178 //        if (SubjectType.ROLE.equals(subject)) {
179 //
180 //            List<AccountValue> avs = new ArrayList<AccountValue>();
181 //            ar.setAccounts(avs);
182 //            av = buildAccountValue(ar0);
183 //            avs.add(av);
184 //
185 //            //add roles
186 //            List<RoleValue> rvs = new ArrayList<RoleValue>();
187 //            ar.setRoles(rvs);
188 //            for (AccountRoleRel arr : arrl) {
189 //                RoleValue rv = buildRoleValue(arr);
190 //                rvs.add(rv);
191 //            }
192 //        } else if (SubjectType.ACCOUNT.equals(subject)) {
193 //
194 //            List<RoleValue> rvs = new ArrayList<RoleValue>();
195 //            ar.setRoles(rvs);
196 //            RoleValue rv = buildRoleValue(ar0);
197 //            rvs.add(rv);
198 //
199 //            //add accounts
200 //            List<AccountValue> avs = new ArrayList<AccountValue>();
201 //            ar.setAccounts(avs);
202 //            for (AccountRoleRel arr : arrl) {
203 //                av = buildAccountValue(arr);
204 //                avs.add(av);
205 //            }
206 //        }
207 //        return result;
208     }
209
210     public void fillCommonPart(AccountRole ar,
211                 DocumentWrapper<List<AccountRoleRel>> wrapDoc,
212                 boolean handleDelete)
213                 throws Exception {
214         List<AccountRoleRel> arrl = wrapDoc.getWrappedObject();
215         SubjectType subject = ar.getSubject();
216         if (subject == null) {
217             //it is not required to give subject as URI determines the subject
218             subject = getSubject(getServiceContext());
219         } else {
220             //subject mismatch should have been checked during validation
221         }
222         if (subject.equals(SubjectType.ROLE)) {
223             //FIXME: potential index out of bounds exception...negative test needed
224             AccountValue av = ar.getAccount().get(0);
225
226             for (RoleValue rv : ar.getRole()) {
227                 AccountRoleRel arr = buildAccountRoleRel(av, rv, handleDelete);
228                 arrl.add(arr);
229             }
230         } else if (SubjectType.ACCOUNT.equals(subject)) {
231             //FIXME: potential index out of bounds exception...negative test needed
232                 RoleValue rv = ar.getRole().get(0);
233             for (AccountValue av : ar.getAccount()) {
234                 AccountRoleRel arr = buildAccountRoleRel(av, rv, handleDelete);
235                 arrl.add(arr);
236             }
237         }
238     }
239     
240     /* (non-Javadoc)
241      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#fillCommonPart(java.lang.Object, org.collectionspace.services.common.document.DocumentWrapper)
242      */
243     @Override
244     public void fillCommonPart(AccountRole ar,
245                 DocumentWrapper<List<AccountRoleRel>> wrapDoc)
246                         throws Exception {
247         fillCommonPart(ar, wrapDoc, false);
248     }
249     
250     /* (non-Javadoc)
251      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#extractCommonPart(org.collectionspace.services.common.document.DocumentWrapper)
252      */
253     @Override
254     public AccountRole extractCommonPart(
255             DocumentWrapper<List<AccountRoleRel>> wrapDoc)
256             throws Exception {
257         List<AccountRoleRel> arrl = wrapDoc.getWrappedObject();
258         AccountRole ar = new AccountRole();
259         SubjectType subject = getSubject(getServiceContext());
260         if (arrl.size() == 0) {
261             return ar;
262         }
263         AccountRoleRel ar0 = arrl.get(0);
264         if (SubjectType.ROLE.equals(subject)) {
265
266             List<AccountValue> avs = new ArrayList<AccountValue>();
267             ar.setAccount(avs);
268             AccountValue av = AuthorizationRoleRel.buildAccountValue(ar0);
269             if (av != null) {
270                 avs.add(av);
271             }
272
273             //add roles
274             List<RoleValue> rvs = new ArrayList<RoleValue>();
275             ar.setRole(rvs);
276             for (AccountRoleRel arr : arrl) {
277                 RoleValue rv = AuthorizationRoleRel.buildRoleValue(arr);
278                 if (rv != null) {
279                         rvs.add(rv);
280                 }
281             }
282         } else if (SubjectType.ACCOUNT.equals(subject)) {
283
284             List<RoleValue> rvs = new ArrayList<RoleValue>();
285             ar.setRole(rvs);
286             RoleValue rv = AuthorizationRoleRel.buildRoleValue(ar0);
287             rvs.add(rv);
288
289             //add accounts
290             List<AccountValue> avs = new ArrayList<AccountValue>();
291             ar.setAccount(avs);
292             for (AccountRoleRel arr : arrl) {
293                 AccountValue av = AuthorizationRoleRel.buildAccountValue(arr);
294                 avs.add(av);
295             }
296         }
297         return ar;
298     }
299
300     /* (non-Javadoc)
301      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getCommonPart()
302      */
303     @Override
304     public AccountRole getCommonPart() {
305         return accountRole;
306     }
307
308     /* (non-Javadoc)
309      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#setCommonPart(java.lang.Object)
310      */
311     @Override
312     public void setCommonPart(AccountRole accountRole) {
313         this.accountRole = accountRole;
314     }
315
316     /* (non-Javadoc)
317      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getCommonPartList()
318      */
319     @Override
320     public AccountRole getCommonPartList() {
321         return accountRole;
322     }
323
324     /* (non-Javadoc)
325      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#setCommonPartList(java.lang.Object)
326      */
327     @Override
328     public void setCommonPartList(AccountRole theAccountRole) {
329 //        this.accountRolesList = accountRolesList;
330         this.accountRole = theAccountRole;
331     }
332
333     /* (non-Javadoc)
334      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getQProperty(java.lang.String)
335      */
336     @Override
337     public String getQProperty(
338             String prop) {
339         return null;
340     }
341
342     /* (non-Javadoc)
343      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#createDocumentFilter()
344      */
345     @Override
346     public DocumentFilter createDocumentFilter() {
347         return new DocumentFilter(this.getServiceContext());
348     }
349
350     /**
351      * Builds the account role rel.
352      *
353      * @param av the av
354      * @param rv the rv
355      * @return the account role rel
356      */
357     private AccountRoleRel buildAccountRoleRel(AccountValue av, RoleValue rv, boolean handleDelete) {
358         AccountRoleRel arr = new AccountRoleRel();
359         arr.setAccountId(av.getAccountId());
360         arr.setUserId(av.getUserId());
361         arr.setScreenName(av.getScreenName());
362         arr.setRoleId(rv.getRoleId());
363         arr.setRoleName(rv.getRoleName());
364         
365         String relationshipId = rv.getRoleRelationshipId();
366         if (relationshipId != null && handleDelete == true) {
367                 arr.setHjid(Long.parseLong(relationshipId));  // set this so we can convince JPA to del the relation
368         }        
369         return arr;
370     }
371
372     /**
373      * Gets the subject.
374      *
375      * @param ctx the ctx
376      * @return the subject
377      */
378     static SubjectType getSubject(ServiceContext ctx) {
379         Object o = ctx.getProperty(ServiceContextProperties.SUBJECT);
380         if (o == null) {
381             throw new IllegalArgumentException(ServiceContextProperties.SUBJECT
382                     + " property is missing in context "
383                     + ctx.toString());
384         }
385         return (SubjectType) o;
386     }
387 }