]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
9ea09060917ab084da3490891cb0fdf0ce77065c
[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.JpaDocumentFilter;
37 import org.collectionspace.services.common.storage.jpa.JpaDocumentHandler;
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 // TODO: Auto-generated Javadoc
45 /**
46  * Document handler for AccountRole association.
47  *
48  * @author
49  */
50 public class AccountRoleDocumentHandler
51         extends JpaDocumentHandler<AccountRole, AccountRole, List<AccountRoleRel>, List<AccountRoleRel>> {
52
53     /** The logger. */
54     private final Logger logger = LoggerFactory.getLogger(AccountRoleDocumentHandler.class);
55     
56     /** The account role. */
57     private AccountRole accountRole;
58 //    private AccountRolesList accountRolesList;
59     /** The account role csid. */
60     private String accountRoleCsid = null;
61
62     /**
63      * Gets the account role csid.
64      *
65      * @return the account role csid
66      */
67     public String getAccountRoleCsid() {
68         return this.accountRoleCsid;
69     }
70     
71     /**
72      * Sets the account role csid.
73      *
74      * @param theAccountRoleCsid the new account role csid
75      */
76     public void setAccountRoleCsid(String theAccountRoleCsid) {
77         this.accountRoleCsid = theAccountRoleCsid;
78     }
79     
80     /* (non-Javadoc)
81      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
82      */
83     @Override
84     public void handleCreate(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
85         fillCommonPart(getCommonPart(), wrapDoc);
86     }
87
88     /* (non-Javadoc)
89      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
90      */
91     @Override
92     public void handleUpdate(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
93         throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
94     }
95
96     /* (non-Javadoc)
97      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#completeUpdate(org.collectionspace.services.common.document.DocumentWrapper)
98      */
99     @Override
100     public void completeUpdate(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
101         throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
102     }
103
104     /* (non-Javadoc)
105      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleGet(org.collectionspace.services.common.document.DocumentWrapper)
106      */
107     @Override
108     public void handleGet(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
109         AccountRole output = extractCommonPart(wrapDoc);
110         setCommonPart(output);
111         getServiceContext().setOutput(output);
112     }
113
114     /* (non-Javadoc)
115      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleGetAll(org.collectionspace.services.common.document.DocumentWrapper)
116      */
117     @Override
118     public void handleGetAll(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
119         throw new UnsupportedOperationException("operation not relevant for AccountRoleDocumentHandler");
120     }
121
122     /* (non-Javadoc)
123      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleDelete(org.collectionspace.services.common.document.DocumentWrapper)
124      */
125     @Override
126     public boolean handleDelete(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
127         fillCommonPart(getCommonPart(), wrapDoc, true);
128         return 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     @Override
206     public void prepareCreate() throws Exception {
207         // Ensure the roles exist
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 JpaDocumentFilter(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 }