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