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