]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
5049b65038fb92c99c8c165c7580e6e85851c609
[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 permissions and
22  *  limitations under the License.
23  */
24 package org.collectionspace.services.account.storage;
25
26 import java.util.ArrayList;
27 import java.util.Date;
28 import java.util.List;
29 import java.util.UUID;
30
31 import org.collectionspace.services.account.AccountTenant;
32 import org.collectionspace.services.account.AccountsCommon;
33 import org.collectionspace.services.account.AccountsCommonList;
34 import org.collectionspace.services.account.AccountListItem;
35 import org.collectionspace.services.account.AccountRoleSubResource;
36 import org.collectionspace.services.account.Status;
37 import org.collectionspace.services.authorization.AccountRole;
38 import org.collectionspace.services.authorization.PermissionRole;
39 import org.collectionspace.services.authorization.PermissionRoleSubResource;
40 import org.collectionspace.services.authorization.SubjectType;
41 import org.collectionspace.services.account.RoleValue;
42 import org.collectionspace.services.client.AccountClient;
43 import org.collectionspace.services.client.AccountRoleFactory;
44 import org.collectionspace.services.client.RoleClient;
45 import org.collectionspace.services.common.storage.TransactionContext;
46 import org.collectionspace.services.common.storage.jpa.JpaDocumentHandler;
47 import org.collectionspace.services.common.api.Tools;
48 import org.collectionspace.services.common.context.ServiceContext;
49 import org.collectionspace.services.common.document.DocumentFilter;
50 import org.collectionspace.services.common.document.DocumentWrapper;
51 import org.collectionspace.services.common.document.JaxbUtils;
52 import org.collectionspace.services.common.security.SecurityUtils;
53
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57 /**
58  *
59  * @author 
60  */
61 public class AccountDocumentHandler
62         extends JpaDocumentHandler<AccountsCommon, AccountsCommonList, AccountsCommon, List<AccountsCommon>> {
63
64     private final Logger logger = LoggerFactory.getLogger(AccountDocumentHandler.class);
65     private AccountsCommon account;
66     private AccountsCommonList accountList;
67
68     @Override
69     public void handleCreate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
70         String id = UUID.randomUUID().toString();
71         AccountsCommon account = wrapDoc.getWrappedObject();
72         account.setCsid(id);
73         setTenant(account);
74         account.setStatus(Status.ACTIVE);
75         // We do not allow creation of locked accounts through the services.
76         account.setMetadataProtection(null);
77         account.setRolesProtection(null);
78     }
79
80     @Override
81     public void handleUpdate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
82         AccountsCommon accountFound = wrapDoc.getWrappedObject();
83         AccountsCommon accountReceived = getCommonPart();
84         // If marked as metadata immutable, do not do update
85         if (!AccountClient.IMMUTABLE.equals(accountFound.getMetadataProtection())) {
86                 merge(accountReceived, accountFound);
87         }
88         //
89         // Update the accountroles if supplied
90         //
91         List<RoleValue> roleValueList = accountReceived.getRole();
92         if (roleValueList != null && roleValueList.size() > 0) {
93                         AccountRoleSubResource subResource = 
94                                         new AccountRoleSubResource(AccountRoleSubResource.ACCOUNT_ACCOUNTROLE_SERVICE);
95                         //
96                         // First, delete the exist accountroles
97                         //
98                         subResource.deleteAccountRole(getServiceContext(), accountFound.getCsid(), SubjectType.ROLE);
99                         //
100                         // Next, create the new accountroles
101                         //
102                         AccountRole accountRole = AccountRoleFactory.createAccountRoleInstance(accountFound, 
103                                         roleValueList, true, true);
104                         String accountRoleCsid = subResource.createAccountRole(getServiceContext(), accountRole, SubjectType.ROLE);
105                         //
106                         // Finally, set the updated role list in the result
107                         //
108                         AccountRole newAccountRole = subResource.getAccountRole(getServiceContext(), accountFound.getCsid(), SubjectType.ROLE);
109                         accountFound.setRole(AccountRoleFactory.convert(newAccountRole.getRole()));
110         }
111     }
112
113     /**
114      * merge manually merges the from account to the to account
115      * -this method is created due to inefficiency of JPA EM merge
116      * @param from
117      * @param to
118      * @return merged account
119      */
120     private AccountsCommon merge(AccountsCommon from, AccountsCommon to) {
121         Date now = new Date();
122         to.setUpdatedAtItem(now);
123         if (from.getEmail() != null) {
124             to.setEmail(from.getEmail());
125         }
126         if (from.getPhone() != null) {
127             to.setPhone(from.getPhone());
128         }
129         if (from.getMobile() != null) {
130             to.setMobile(from.getMobile());
131         }
132         if (from.getScreenName() != null) {
133             to.setScreenName(from.getScreenName());
134         }
135         if (from.getStatus() != null) {
136             to.setStatus(from.getStatus());
137         }
138         if (from.getPersonRefName() != null) {
139             to.setPersonRefName(from.getPersonRefName());
140         }
141         // Note that we do not allow update of locks
142         //fixme update for tenant association
143
144         if (logger.isDebugEnabled()) {
145             logger.debug("merged account="
146                     + JaxbUtils.toString(to, AccountsCommon.class));
147         }
148         return to;
149     }
150
151     @Override
152     /**
153      * If the create payload included a list of role, relate them to the account.
154      */
155     public void completeCreate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
156         AccountsCommon accountsCommon = wrapDoc.getWrappedObject();
157         List<RoleValue> roleValueList = account.getRole();
158         if (roleValueList != null && roleValueList.size() > 0) {
159                 //
160                 // To prevent new Accounts being created (especially low-level Spring Security accounts/SIDs), we'll first flush the current
161                 // JPA context to ensure our Account can be successfully persisted.
162                 //
163                 TransactionContext jpaTransactionContext = this.getServiceContext().getCurrentTransactionContext();
164                 jpaTransactionContext.flush();
165
166                 AccountRoleSubResource subResource = new AccountRoleSubResource(AccountRoleSubResource.ACCOUNT_ACCOUNTROLE_SERVICE);
167                 AccountRole accountRole = AccountRoleFactory.createAccountRoleInstance(accountsCommon, roleValueList, true, true);
168                         subResource.createAccountRole(this.getServiceContext(), accountRole, SubjectType.ROLE);
169         }
170     }
171     
172     @Override
173     public void completeUpdate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
174         AccountsCommon upAcc = wrapDoc.getWrappedObject();
175         getServiceContext().setOutput(upAcc);        
176     }
177
178     @Override
179     public void handleGet(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
180         setCommonPart(extractCommonPart(wrapDoc));
181         sanitize(getCommonPart());
182         getServiceContext().setOutput(account);
183     }
184
185     @Override
186     public void handleGetAll(DocumentWrapper<List<AccountsCommon>> wrapDoc) throws Exception {
187         AccountsCommonList accList = extractCommonPartList(wrapDoc);
188         setCommonPartList(accList);
189         getServiceContext().setOutput(getCommonPartList());
190     }
191
192         @SuppressWarnings("unchecked")
193         @Override
194         public AccountsCommon extractCommonPart(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
195                 AccountsCommon account = wrapDoc.getWrappedObject();
196                 
197                 String includeRolesQueryParamValue = (String) getServiceContext().getQueryParams().getFirst(AccountClient.INCLUDE_ROLES_QP);
198                 boolean includeRoles = Tools.isTrue(includeRolesQueryParamValue);
199                 if (includeRoles) {
200                         AccountRoleSubResource accountRoleResource = new AccountRoleSubResource(
201                                         AccountRoleSubResource.ACCOUNT_ACCOUNTROLE_SERVICE);
202                         AccountRole accountRole = accountRoleResource.getAccountRole(getServiceContext(), account.getCsid(),
203                                         SubjectType.ROLE);
204                         account.setRole(AccountRoleFactory.convert(accountRole.getRole()));
205                 }
206                 
207                 return wrapDoc.getWrappedObject();
208         }
209
210     @Override
211     public void fillCommonPart(AccountsCommon obj, DocumentWrapper<AccountsCommon> wrapDoc)
212             throws Exception {
213         throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
214     }
215
216     @Override
217     public AccountsCommonList extractCommonPartList(
218             DocumentWrapper<List<AccountsCommon>> wrapDoc)
219             throws Exception {
220
221         AccountsCommonList accList = this.extractPagingInfo(new AccountsCommonList(), wrapDoc);
222 //        AccountsCommonList accList = new AccountsCommonList();
223         List<AccountListItem> list = accList.getAccountListItem();
224
225         for (Object obj : wrapDoc.getWrappedObject()) {
226             AccountsCommon account = (AccountsCommon) obj;
227             AccountListItem accListItem = new AccountListItem();
228             accListItem.setScreenName(account.getScreenName());
229             accListItem.setUserid(account.getUserId());
230             accListItem.setTenantid(account.getTenants().get(0).getTenantId()); // pick the default/first tenant
231             accListItem.setTenants(account.getTenants());
232             accListItem.setEmail(account.getEmail());
233             accListItem.setStatus(account.getStatus());
234             String id = account.getCsid();
235             accListItem.setUri(getServiceContextPath() + id);
236             accListItem.setCsid(id);
237             list.add(accListItem);
238         }
239         return accList;
240     }
241
242     @Override
243     public AccountsCommon getCommonPart() {
244         return account;
245     }
246
247     @Override
248     public void setCommonPart(AccountsCommon account) {
249         this.account = account;
250     }
251
252     @Override
253     public AccountsCommonList getCommonPartList() {
254         return accountList;
255     }
256
257     @Override
258     public void setCommonPartList(AccountsCommonList accountList) {
259         this.accountList = accountList;
260     }
261
262     @Override
263     public String getQProperty(
264             String prop) {
265         return null;
266     }
267
268     @Override
269     public DocumentFilter createDocumentFilter() {
270         DocumentFilter filter = new AccountJpaFilter(this.getServiceContext());
271         return filter;
272     }
273
274     private void setTenant(AccountsCommon account) {
275         //set tenant only if not available from input
276         ServiceContext ctx = getServiceContext();
277         if (account.getTenants() == null || account.getTenants().size() == 0) {
278             if (ctx.getTenantId() != null) {
279                 AccountTenant at = new AccountTenant();
280                 at.setTenantId(ctx.getTenantId());
281                 List<AccountTenant> atList = new ArrayList<AccountTenant>();
282                 atList.add(at);
283                 account.setTenants(atList);
284             }
285         }
286     }
287
288     /**
289      * sanitize removes data not needed to be sent to the consumer
290      * @param account
291      */
292     @Override
293         public void sanitize(DocumentWrapper<AccountsCommon> wrapDoc) {
294         AccountsCommon account = wrapDoc.getWrappedObject();
295         sanitize(account);
296     }
297     
298         private void sanitize(AccountsCommon account) {
299         account.setPassword(null);
300         if (!SecurityUtils.isCSpaceAdmin()) {
301             account.setTenants(new ArrayList<AccountTenant>(0));
302         }
303     }    
304
305     /* (non-Javadoc)
306      * @see org.collectionspace.services.common.document.DocumentHandler#initializeDocumentFilter(org.collectionspace.services.common.context.ServiceContext)
307      */
308     public void initializeDocumentFilter(ServiceContext<AccountsCommon, AccountsCommon> ctx) {
309         // set a default document filter in this method
310     }
311 }