]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
03c192217628586c9e63d9db7414e2229e734217
[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.AccountsCommonList.AccountListItem;
35 import org.collectionspace.services.account.Status;
36
37 import org.collectionspace.services.common.storage.jpa.JpaDocumentHandler;
38 import org.collectionspace.services.common.context.ServiceContext;
39 import org.collectionspace.services.common.document.DocumentFilter;
40 import org.collectionspace.services.common.document.DocumentWrapper;
41 import org.collectionspace.services.common.document.JaxbUtils;
42 import org.collectionspace.services.common.security.SecurityUtils;
43
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 /**
48  *
49  * @author 
50  */
51 public class AccountDocumentHandler
52         extends JpaDocumentHandler<AccountsCommon, AccountsCommonList, AccountsCommon, List> {
53
54     private final Logger logger = LoggerFactory.getLogger(AccountDocumentHandler.class);
55     private AccountsCommon account;
56     private AccountsCommonList accountList;
57
58     @Override
59     public void handleCreate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
60         String id = UUID.randomUUID().toString();
61         AccountsCommon account = wrapDoc.getWrappedObject();
62         account.setCsid(id);
63         setTenant(account);
64         account.setStatus(Status.ACTIVE);
65     }
66
67     @Override
68     public void handleUpdate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
69         AccountsCommon accountFound = wrapDoc.getWrappedObject();
70         AccountsCommon accountReceived = getCommonPart();
71         merge(accountReceived, accountFound);
72     }
73
74     /**
75      * merge manually merges the from account to the to account
76      * -this method is created due to inefficiency of JPA EM merge
77      * @param from
78      * @param to
79      * @return merged account
80      */
81     private AccountsCommon merge(AccountsCommon from, AccountsCommon to) {
82         Date now = new Date();
83         to.setUpdatedAtItem(now);
84         if (from.getEmail() != null) {
85             to.setEmail(from.getEmail());
86         }
87         if (from.getPhone() != null) {
88             to.setPhone(from.getPhone());
89         }
90         if (from.getMobile() != null) {
91             to.setMobile(from.getMobile());
92         }
93         if (from.getScreenName() != null) {
94             to.setScreenName(from.getScreenName());
95         }
96         if (from.getStatus() != null) {
97             to.setStatus(from.getStatus());
98         }
99         if (from.getPersonRefName() != null) {
100             to.setPersonRefName(from.getPersonRefName());
101         }
102         //fixme update for tenant association
103
104         if (logger.isDebugEnabled()) {
105             logger.debug("merged account="
106                     + JaxbUtils.toString(to, AccountsCommon.class));
107         }
108         return to;
109     }
110
111     @Override
112     public void completeUpdate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
113         AccountsCommon upAcc = wrapDoc.getWrappedObject();
114         getServiceContext().setOutput(account);
115         sanitize(upAcc);
116     }
117
118     @Override
119     public void handleGet(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
120         setCommonPart(extractCommonPart(wrapDoc));
121         sanitize(getCommonPart());
122         getServiceContext().setOutput(account);
123     }
124
125     @Override
126     public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
127         AccountsCommonList accList = extractCommonPartList(wrapDoc);
128         setCommonPartList(accList);
129         getServiceContext().setOutput(getCommonPartList());
130     }
131
132     @Override
133     public AccountsCommon extractCommonPart(
134             DocumentWrapper<AccountsCommon> wrapDoc)
135             throws Exception {
136         return wrapDoc.getWrappedObject();
137     }
138
139     @Override
140     public void fillCommonPart(AccountsCommon obj, DocumentWrapper<AccountsCommon> wrapDoc)
141             throws Exception {
142         throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
143     }
144
145     @Override
146     public AccountsCommonList extractCommonPartList(
147             DocumentWrapper<List> wrapDoc)
148             throws Exception {
149
150         AccountsCommonList accList = this.extractPagingInfo(new AccountsCommonList(), wrapDoc);
151 //        AccountsCommonList accList = new AccountsCommonList();
152         List<AccountsCommonList.AccountListItem> list = accList.getAccountListItem();
153
154         for (Object obj : wrapDoc.getWrappedObject()) {
155             AccountsCommon account = (AccountsCommon) obj;
156             AccountListItem accListItem = new AccountListItem();
157             accListItem.setScreenName(account.getScreenName());
158             accListItem.setEmail(account.getEmail());
159             accListItem.setStatus(account.getStatus());
160             String id = account.getCsid();
161             accListItem.setUri(getServiceContextPath() + id);
162             accListItem.setCsid(id);
163             list.add(accListItem);
164         }
165         return accList;
166     }
167
168     @Override
169     public AccountsCommon getCommonPart() {
170         return account;
171     }
172
173     @Override
174     public void setCommonPart(AccountsCommon account) {
175         this.account = account;
176     }
177
178     @Override
179     public AccountsCommonList getCommonPartList() {
180         return accountList;
181     }
182
183     @Override
184     public void setCommonPartList(AccountsCommonList accountList) {
185         this.accountList = accountList;
186     }
187
188     @Override
189     public String getQProperty(
190             String prop) {
191         return null;
192     }
193
194     @Override
195     public DocumentFilter createDocumentFilter() {
196         DocumentFilter filter = new AccountJpaFilter(this.getServiceContext());
197         return filter;
198     }
199
200     private void setTenant(AccountsCommon account) {
201         //set tenant only if not available from input
202         ServiceContext ctx = getServiceContext();
203         if (account.getTenants() == null || account.getTenants().size() == 0) {
204             if (ctx.getTenantId() != null) {
205                 AccountTenant at = new AccountTenant();
206                 at.setTenantId(ctx.getTenantId());
207                 List<AccountTenant> atList = new ArrayList<AccountTenant>();
208                 atList.add(at);
209                 account.setTenants(atList);
210             }
211         }
212     }
213
214     /**
215      * sanitize removes data not needed to be sent to the consumer
216      * @param account
217      */
218     private void sanitize(AccountsCommon account) {
219         account.setPassword(null);
220         if (!SecurityUtils.isCSpaceAdmin()) {
221             account.setTenants(new ArrayList<AccountTenant>(0));
222         }
223     }
224
225     /* (non-Javadoc)
226      * @see org.collectionspace.services.common.document.DocumentHandler#initializeDocumentFilter(org.collectionspace.services.common.context.ServiceContext)
227      */
228     public void initializeDocumentFilter(ServiceContext ctx) {
229         // set a default document filter in this method
230     }
231 }