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