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