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.collectionspace.services.common.security.SecurityUtils;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
48 public class AccountDocumentHandler
49 extends AbstractDocumentHandlerImpl<AccountsCommon, AccountsCommonList, AccountsCommon, List> {
51 private final Logger logger = LoggerFactory.getLogger(AccountDocumentHandler.class);
52 private AccountsCommon account;
53 private AccountsCommonList accountList;
56 public void handleCreate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
57 String id = UUID.randomUUID().toString();
58 AccountsCommon account = wrapDoc.getWrappedObject();
61 account.setStatus(Status.ACTIVE);
65 public void handleUpdate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
66 AccountsCommon accountFound = wrapDoc.getWrappedObject();
67 AccountsCommon accountReceived = getCommonPart();
68 merge(accountReceived, accountFound);
72 * merge manually merges the from account to the to account
73 * -this method is created due to inefficiency of JPA EM merge
76 * @return merged account
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());
84 if (from.getPhone() != null) {
85 to.setPhone(from.getPhone());
87 if (from.getMobile() != null) {
88 to.setMobile(from.getMobile());
90 if (from.getScreenName() != null) {
91 to.setScreenName(from.getScreenName());
93 if (from.getStatus() != null) {
94 to.setStatus(from.getStatus());
96 if (from.getPersonRefName() != null) {
97 to.setPersonRefName(from.getPersonRefName());
99 //fixme update for tenant association
101 if (logger.isDebugEnabled()) {
102 logger.debug("merged account="
103 + JaxbUtils.toString(to, AccountsCommon.class));
109 public void completeUpdate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
110 AccountsCommon upAcc = wrapDoc.getWrappedObject();
111 getServiceContext().setOutput(account);
116 public void handleGet(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
117 setCommonPart(extractCommonPart(wrapDoc));
118 sanitize(getCommonPart());
119 getServiceContext().setOutput(account);
123 public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
124 AccountsCommonList accList = extractCommonPartList(wrapDoc);
125 setCommonPartList(accList);
126 getServiceContext().setOutput(getCommonPartList());
130 public AccountsCommon extractCommonPart(
131 DocumentWrapper<AccountsCommon> wrapDoc)
133 return wrapDoc.getWrappedObject();
137 public void fillCommonPart(AccountsCommon obj, DocumentWrapper<AccountsCommon> wrapDoc)
139 throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
143 public AccountsCommonList extractCommonPartList(
144 DocumentWrapper<List> wrapDoc)
147 AccountsCommonList accList = new AccountsCommonList();
148 List<AccountsCommonList.AccountListItem> list = accList.getAccountListItem();
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);
165 public AccountsCommon getCommonPart() {
170 public void setCommonPart(AccountsCommon account) {
171 this.account = account;
175 public AccountsCommonList getCommonPartList() {
180 public void setCommonPartList(AccountsCommonList accountList) {
181 this.accountList = accountList;
185 public String getQProperty(
191 public DocumentFilter createDocumentFilter() {
192 DocumentFilter filter = new AccountJpaFilter(this.getServiceContext());
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>();
205 account.setTenants(atList);
211 * sanitize removes data not needed to be sent to the consumer
214 private void sanitize(AccountsCommon account) {
215 account.setPassword(null);
216 if (!SecurityUtils.isCSpaceAdmin()) {
217 account.setTenants(new ArrayList<AccountTenant>(0));
222 * @see org.collectionspace.services.common.document.DocumentHandler#initializeDocumentFilter(org.collectionspace.services.common.context.ServiceContext)
224 public void initializeDocumentFilter(ServiceContext ctx) {
225 // set a default document filter in this method