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;
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;
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;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
52 public class AccountDocumentHandler
53 extends JpaDocumentHandler<AccountsCommon, AccountsCommonList, AccountsCommon, List> {
55 private final Logger logger = LoggerFactory.getLogger(AccountDocumentHandler.class);
56 private AccountsCommon account;
57 private AccountsCommonList accountList;
60 public void handleCreate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
61 String id = UUID.randomUUID().toString();
62 AccountsCommon account = wrapDoc.getWrappedObject();
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);
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);
82 * merge manually merges the from account to the to account
83 * -this method is created due to inefficiency of JPA EM merge
86 * @return merged account
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());
94 if (from.getPhone() != null) {
95 to.setPhone(from.getPhone());
97 if (from.getMobile() != null) {
98 to.setMobile(from.getMobile());
100 if (from.getScreenName() != null) {
101 to.setScreenName(from.getScreenName());
103 if (from.getStatus() != null) {
104 to.setStatus(from.getStatus());
106 if (from.getPersonRefName() != null) {
107 to.setPersonRefName(from.getPersonRefName());
109 // Note that we do not allow update of locks
110 //fixme update for tenant association
112 if (logger.isDebugEnabled()) {
113 logger.debug("merged account="
114 + JaxbUtils.toString(to, AccountsCommon.class));
120 public void completeUpdate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
121 AccountsCommon upAcc = wrapDoc.getWrappedObject();
122 getServiceContext().setOutput(upAcc);
127 public void handleGet(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
128 setCommonPart(extractCommonPart(wrapDoc));
129 sanitize(getCommonPart());
130 getServiceContext().setOutput(account);
134 public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
135 AccountsCommonList accList = extractCommonPartList(wrapDoc);
136 setCommonPartList(accList);
137 getServiceContext().setOutput(getCommonPartList());
141 public AccountsCommon extractCommonPart(
142 DocumentWrapper<AccountsCommon> wrapDoc)
144 return wrapDoc.getWrappedObject();
148 public void fillCommonPart(AccountsCommon obj, DocumentWrapper<AccountsCommon> wrapDoc)
150 throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
154 public AccountsCommonList extractCommonPartList(
155 DocumentWrapper<List> wrapDoc)
158 AccountsCommonList accList = this.extractPagingInfo(new AccountsCommonList(), wrapDoc);
159 // AccountsCommonList accList = new AccountsCommonList();
160 List<AccountListItem> list = accList.getAccountListItem();
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);
177 public AccountsCommon getCommonPart() {
182 public void setCommonPart(AccountsCommon account) {
183 this.account = account;
187 public AccountsCommonList getCommonPartList() {
192 public void setCommonPartList(AccountsCommonList accountList) {
193 this.accountList = accountList;
197 public String getQProperty(
203 public DocumentFilter createDocumentFilter() {
204 DocumentFilter filter = new AccountJpaFilter(this.getServiceContext());
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>();
217 account.setTenants(atList);
223 * sanitize removes data not needed to be sent to the consumer
226 private void sanitize(AccountsCommon account) {
227 account.setPassword(null);
228 if (!SecurityUtils.isCSpaceAdmin()) {
229 account.setTenants(new ArrayList<AccountTenant>(0));
234 * @see org.collectionspace.services.common.document.DocumentHandler#initializeDocumentFilter(org.collectionspace.services.common.context.ServiceContext)
236 public void initializeDocumentFilter(ServiceContext ctx) {
237 // set a default document filter in this method