]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
1c7d796f7c8e944c5ca58485853be723575fa86f
[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.setUserid(account.getUserId());
167             accListItem.setTenantid(account.getTenants().get(0).getTenantId()); // pick the default/first tenant
168             accListItem.setTenants(account.getTenants());
169             accListItem.setEmail(account.getEmail());
170             accListItem.setStatus(account.getStatus());
171             String id = account.getCsid();
172             accListItem.setUri(getServiceContextPath() + id);
173             accListItem.setCsid(id);
174             list.add(accListItem);
175         }
176         return accList;
177     }
178
179     @Override
180     public AccountsCommon getCommonPart() {
181         return account;
182     }
183
184     @Override
185     public void setCommonPart(AccountsCommon account) {
186         this.account = account;
187     }
188
189     @Override
190     public AccountsCommonList getCommonPartList() {
191         return accountList;
192     }
193
194     @Override
195     public void setCommonPartList(AccountsCommonList accountList) {
196         this.accountList = accountList;
197     }
198
199     @Override
200     public String getQProperty(
201             String prop) {
202         return null;
203     }
204
205     @Override
206     public DocumentFilter createDocumentFilter() {
207         DocumentFilter filter = new AccountJpaFilter(this.getServiceContext());
208         return filter;
209     }
210
211     private void setTenant(AccountsCommon account) {
212         //set tenant only if not available from input
213         ServiceContext ctx = getServiceContext();
214         if (account.getTenants() == null || account.getTenants().size() == 0) {
215             if (ctx.getTenantId() != null) {
216                 AccountTenant at = new AccountTenant();
217                 at.setTenantId(ctx.getTenantId());
218                 List<AccountTenant> atList = new ArrayList<AccountTenant>();
219                 atList.add(at);
220                 account.setTenants(atList);
221             }
222         }
223     }
224
225     /**
226      * sanitize removes data not needed to be sent to the consumer
227      * @param account
228      */
229     private void sanitize(AccountsCommon account) {
230         account.setPassword(null);
231         if (!SecurityUtils.isCSpaceAdmin()) {
232             account.setTenants(new ArrayList<AccountTenant>(0));
233         }
234     }
235
236     /* (non-Javadoc)
237      * @see org.collectionspace.services.common.document.DocumentHandler#initializeDocumentFilter(org.collectionspace.services.common.context.ServiceContext)
238      */
239     public void initializeDocumentFilter(ServiceContext ctx) {
240         // set a default document filter in this method
241     }
242 }