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