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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
24 package org.collectionspace.services.account.storage;
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;
47 public class AccountDocumentHandler
48 extends AbstractDocumentHandlerImpl<AccountsCommon, AccountsCommonList, AccountsCommon, List> {
50 private final Logger logger = LoggerFactory.getLogger(AccountDocumentHandler.class);
51 private AccountsCommon account;
52 private AccountsCommonList accountList;
55 public void handleCreate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
56 String id = UUID.randomUUID().toString();
57 AccountsCommon account = wrapDoc.getWrappedObject();
60 account.setStatus(Status.ACTIVE);
64 public void handleUpdate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
65 AccountsCommon accountFound = wrapDoc.getWrappedObject();
66 AccountsCommon accountReceived = getCommonPart();
67 merge(accountReceived, accountFound);
71 * merge manually merges the from account to the to account
72 * -this method is created due to inefficiency of JPA EM merge
75 * @return merged account
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());
83 if (from.getPhone() != null) {
84 to.setPhone(from.getPhone());
86 if (from.getMobile() != null) {
87 to.setMobile(from.getMobile());
89 if (from.getScreenName() != null) {
90 to.setScreenName(from.getScreenName());
92 if (from.getStatus() != null) {
93 to.setStatus(from.getStatus());
95 if (from.getPersonRefName() != null) {
96 to.setPersonRefName(from.getPersonRefName());
98 //fixme update for tenant association
100 if (logger.isDebugEnabled()) {
101 logger.debug("merged account="
102 + JaxbUtils.toString(to, AccountsCommon.class));
108 public void completeUpdate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
109 AccountsCommon upAcc = wrapDoc.getWrappedObject();
110 getServiceContext().setOutput(account);
115 public void handleGet(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
116 setCommonPart(extractCommonPart(wrapDoc));
117 sanitize(getCommonPart());
118 getServiceContext().setOutput(account);
122 public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
123 AccountsCommonList accList = extractCommonPartList(wrapDoc);
124 setCommonPartList(accList);
125 getServiceContext().setOutput(getCommonPartList());
129 public AccountsCommon extractCommonPart(
130 DocumentWrapper<AccountsCommon> wrapDoc)
132 return wrapDoc.getWrappedObject();
136 public void fillCommonPart(AccountsCommon obj, DocumentWrapper<AccountsCommon> wrapDoc)
138 throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
142 public AccountsCommonList extractCommonPartList(
143 DocumentWrapper<List> wrapDoc)
146 AccountsCommonList accList = new AccountsCommonList();
147 List<AccountsCommonList.AccountListItem> list = accList.getAccountListItem();
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);
164 public AccountsCommon getCommonPart() {
169 public void setCommonPart(AccountsCommon account) {
170 this.account = account;
174 public AccountsCommonList getCommonPartList() {
179 public void setCommonPartList(AccountsCommonList accountList) {
180 this.accountList = accountList;
184 public String getQProperty(
190 public DocumentFilter createDocumentFilter() {
191 DocumentFilter filter = new AccountJpaFilter(this.getServiceContext());
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>();
204 account.setTenants(atList);
210 * sanitize removes data not needed to be sent to the consumer
213 private void sanitize(AccountsCommon account) {
214 account.setPassword(null);
215 account.setTenants(new ArrayList<AccountTenant>(0));
219 * @see org.collectionspace.services.common.document.DocumentHandler#initializeDocumentFilter(org.collectionspace.services.common.context.ServiceContext)
221 public void initializeDocumentFilter(ServiceContext ctx) {
222 // set a default document filter in this method