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