]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
3da9334a23dbc52000f693d021c2138ad186b97f
[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(extractCommonPart(wrapDoc));
117 //      AccountRole accountRoleList = extractCommonPartList(wrapDoc);
118         getServiceContext().setOutput(output);
119     }
120
121     /* (non-Javadoc)
122      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleGetAll(org.collectionspace.services.common.document.DocumentWrapper)
123      */
124     @Override
125     public void handleGetAll(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
126         throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
127     }
128
129     /* (non-Javadoc)
130      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleDelete(org.collectionspace.services.common.document.DocumentWrapper)
131      */
132     @Override
133     public void handleDelete(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
134         fillCommonPart(getCommonPart(), wrapDoc, true);
135     }
136
137     /* (non-Javadoc)
138      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#extractCommonPartList(org.collectionspace.services.common.document.DocumentWrapper)
139      */
140     @Override
141     public AccountRole extractCommonPartList(
142             DocumentWrapper<List<AccountRoleRel>> wrapDoc)
143             throws Exception {
144         
145         throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
146         
147 //      AccountRolesList result = new AccountRolesList();
148 //        List<AccountRoleRel> arrl = wrapDoc.getWrappedObject();
149 //        AccountRole ar = new AccountRole();
150 //        SubjectType subject = getSubject(getServiceContext());
151 //        if (arrl.size() == 0) {
152 //            return result;
153 //        }
154 //        
155 ////        result.setSubject(subject);
156 //        AccountRoleRel ar0 = arrl.get(0);
157 //        AccountValue av = buildAccountValue(ar0);
158 ////        result.setAccount(av);
159 //        
160 //      List<AccountRoleListItem> accountRoleListItems = result.getAccountRoleListItems();
161 //      if (accountRoleListItems == null) {
162 //              accountRoleListItems = new ArrayList<AccountRoleListItem>();
163 //      }
164 //      for (AccountRoleRel e : arrl) {
165 //              AccountRoleListItem accountRoleListItem = new AccountRoleListItem();
166 //              // fill the item
167 //              accountRoleListItem.setRoleName(e.getRoleName());
168 //              accountRoleListItem.setRoleId(e.getRoleId());
169 //              accountRoleListItem.setCsid(e.getHjid().toString());
170 //              // add item to result list
171 //              accountRoleListItems.add(accountRoleListItem);
172 //      }
173 //      
174 //        //
175 //      // Old Sanjay code
176 //      //
177 //      
178 //        ar0 = arrl.get(0);
179 //        if (SubjectType.ROLE.equals(subject)) {
180 //
181 //            List<AccountValue> avs = new ArrayList<AccountValue>();
182 //            ar.setAccounts(avs);
183 //            av = buildAccountValue(ar0);
184 //            avs.add(av);
185 //
186 //            //add roles
187 //            List<RoleValue> rvs = new ArrayList<RoleValue>();
188 //            ar.setRoles(rvs);
189 //            for (AccountRoleRel arr : arrl) {
190 //                RoleValue rv = buildRoleValue(arr);
191 //                rvs.add(rv);
192 //            }
193 //        } else if (SubjectType.ACCOUNT.equals(subject)) {
194 //
195 //            List<RoleValue> rvs = new ArrayList<RoleValue>();
196 //            ar.setRoles(rvs);
197 //            RoleValue rv = buildRoleValue(ar0);
198 //            rvs.add(rv);
199 //
200 //            //add accounts
201 //            List<AccountValue> avs = new ArrayList<AccountValue>();
202 //            ar.setAccounts(avs);
203 //            for (AccountRoleRel arr : arrl) {
204 //                av = buildAccountValue(arr);
205 //                avs.add(av);
206 //            }
207 //        }
208 //        return result;
209     }
210
211     public void fillCommonPart(AccountRole ar,
212                 DocumentWrapper<List<AccountRoleRel>> wrapDoc,
213                 boolean handleDelete)
214                 throws Exception {
215         List<AccountRoleRel> arrl = wrapDoc.getWrappedObject();
216         SubjectType subject = ar.getSubject();
217         if (subject == null) {
218             //it is not required to give subject as URI determines the subject
219             subject = getSubject(getServiceContext());
220         } else {
221             //subject mismatch should have been checked during validation
222         }
223         if (subject.equals(SubjectType.ROLE)) {
224             //FIXME: potential index out of bounds exception...negative test needed
225             AccountValue av = ar.getAccounts().get(0);
226
227             for (RoleValue rv : ar.getRoles()) {
228                 AccountRoleRel arr = buildAccountRoleRel(av, rv, handleDelete);
229                 arrl.add(arr);
230             }
231         } else if (SubjectType.ACCOUNT.equals(subject)) {
232             //FIXME: potential index out of bounds exception...negative test needed
233                 RoleValue rv = ar.getRoles().get(0);
234             for (AccountValue av : ar.getAccounts()) {
235                 AccountRoleRel arr = buildAccountRoleRel(av, rv, handleDelete);
236                 arrl.add(arr);
237             }
238         }
239     }
240     
241     /* (non-Javadoc)
242      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#fillCommonPart(java.lang.Object, org.collectionspace.services.common.document.DocumentWrapper)
243      */
244     @Override
245     public void fillCommonPart(AccountRole ar,
246                 DocumentWrapper<List<AccountRoleRel>> wrapDoc)
247                         throws Exception {
248         fillCommonPart(ar, wrapDoc, false);
249     }
250     
251     /* (non-Javadoc)
252      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#extractCommonPart(org.collectionspace.services.common.document.DocumentWrapper)
253      */
254     @Override
255     public AccountRole extractCommonPart(
256             DocumentWrapper<List<AccountRoleRel>> wrapDoc)
257             throws Exception {
258         List<AccountRoleRel> arrl = wrapDoc.getWrappedObject();
259         AccountRole ar = new AccountRole();
260         SubjectType subject = getSubject(getServiceContext());
261         if (arrl.size() == 0) {
262             return ar;
263         }
264         AccountRoleRel ar0 = arrl.get(0);
265         if (SubjectType.ROLE.equals(subject)) {
266
267             List<AccountValue> avs = new ArrayList<AccountValue>();
268             ar.setAccounts(avs);
269             AccountValue av = buildAccountValue(ar0);
270             avs.add(av);
271
272             //add roles
273             List<RoleValue> rvs = new ArrayList<RoleValue>();
274             ar.setRoles(rvs);
275             for (AccountRoleRel arr : arrl) {
276                 RoleValue rv = AuthorizationRoleRel.buildRoleValue(arr);
277                 rvs.add(rv);
278             }
279         } else if (SubjectType.ACCOUNT.equals(subject)) {
280
281             List<RoleValue> rvs = new ArrayList<RoleValue>();
282             ar.setRoles(rvs);
283             RoleValue rv = AuthorizationRoleRel.buildRoleValue(ar0);
284             rvs.add(rv);
285
286             //add accounts
287             List<AccountValue> avs = new ArrayList<AccountValue>();
288             ar.setAccounts(avs);
289             for (AccountRoleRel arr : arrl) {
290                 AccountValue av = buildAccountValue(arr);
291                 avs.add(av);
292             }
293         }
294         return ar;
295     }
296
297     /* (non-Javadoc)
298      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getCommonPart()
299      */
300     @Override
301     public AccountRole getCommonPart() {
302         return accountRole;
303     }
304
305     /* (non-Javadoc)
306      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#setCommonPart(java.lang.Object)
307      */
308     @Override
309     public void setCommonPart(AccountRole accountRole) {
310         this.accountRole = accountRole;
311     }
312
313     /* (non-Javadoc)
314      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getCommonPartList()
315      */
316     @Override
317     public AccountRole getCommonPartList() {
318         return accountRole;
319     }
320
321     /* (non-Javadoc)
322      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#setCommonPartList(java.lang.Object)
323      */
324     @Override
325     public void setCommonPartList(AccountRole theAccountRole) {
326 //        this.accountRolesList = accountRolesList;
327         this.accountRole = theAccountRole;
328     }
329
330     /* (non-Javadoc)
331      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getQProperty(java.lang.String)
332      */
333     @Override
334     public String getQProperty(
335             String prop) {
336         return null;
337     }
338
339     /* (non-Javadoc)
340      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#createDocumentFilter()
341      */
342     @Override
343     public DocumentFilter createDocumentFilter() {
344         return new DocumentFilter(this.getServiceContext());
345     }
346
347     /**
348      * Builds the account value.
349      *
350      * @param arr the arr
351      * @return the account value
352      */
353     private AccountValue buildAccountValue(AccountRoleRel arr) {
354         AccountValue av = new AccountValue();
355         av.setAccountId(arr.getAccountId());
356         av.setUserId(arr.getUserId());
357         av.setScreenName(arr.getScreenName());
358         return av;
359     }
360
361     /**
362      * Builds the account role rel.
363      *
364      * @param av the av
365      * @param rv the rv
366      * @return the account role rel
367      */
368     private AccountRoleRel buildAccountRoleRel(AccountValue av, RoleValue rv, boolean handleDelete) {
369         AccountRoleRel arr = new AccountRoleRel();
370         arr.setAccountId(av.getAccountId());
371         arr.setUserId(av.getUserId());
372         arr.setScreenName(av.getScreenName());
373         arr.setRoleId(rv.getRoleId());
374         arr.setRoleName(rv.getRoleName());
375         
376         String relationshipId = rv.getRoleRelationshipId();
377         if (relationshipId != null && handleDelete == true) {
378                 arr.setHjid(Long.parseLong(relationshipId));  // set this so we can convince JPA to del the relation
379         }        
380         return arr;
381     }
382
383     /**
384      * Gets the subject.
385      *
386      * @param ctx the ctx
387      * @return the subject
388      */
389     static SubjectType getSubject(ServiceContext ctx) {
390         Object o = ctx.getProperty(ServiceContextProperties.SUBJECT);
391         if (o == null) {
392             throw new IllegalArgumentException(ServiceContextProperties.SUBJECT
393                     + " property is missing in context "
394                     + ctx.toString());
395         }
396         return (SubjectType) o;
397     }
398 }