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.List;
28 import java.util.UUID;
29 import org.collectionspace.services.account.AccountTenant;
30 import org.collectionspace.services.account.AccountsCommon;
31 import org.collectionspace.services.account.AccountsCommonList;
32 import org.collectionspace.services.account.AccountsCommonList.AccountListItem;
33 import org.collectionspace.services.account.Status;
34 import org.collectionspace.services.common.context.ServiceContext;
35 import org.collectionspace.services.common.document.AbstractDocumentHandlerImpl;
36 import org.collectionspace.services.common.document.DocumentFilter;
37 import org.collectionspace.services.common.document.DocumentWrapper;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
45 public class AccountDocumentHandler
46 extends AbstractDocumentHandlerImpl<AccountsCommon, AccountsCommonList, AccountsCommon, List> {
48 private final Logger logger = LoggerFactory.getLogger(AccountDocumentHandler.class);
49 private AccountsCommon account;
50 private AccountsCommonList accountList;
53 public void handleCreate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
54 String id = UUID.randomUUID().toString();
55 AccountsCommon account = wrapDoc.getWrappedObject();
58 account.setStatus(Status.ACTIVE);
62 public void handleUpdate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
66 public void completeUpdate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
67 AccountsCommon upAcc = wrapDoc.getWrappedObject();
68 getServiceContext().setOutput(account);
73 public void handleGet(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
74 setCommonPart(extractCommonPart(wrapDoc));
75 sanitize(getCommonPart());
76 getServiceContext().setOutput(account);
80 public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
81 AccountsCommonList accList = extractCommonPartList(wrapDoc);
82 setCommonPartList(accList);
83 getServiceContext().setOutput(getCommonPartList());
87 public AccountsCommon extractCommonPart(
88 DocumentWrapper<AccountsCommon> wrapDoc)
90 return wrapDoc.getWrappedObject();
94 public void fillCommonPart(AccountsCommon obj, DocumentWrapper<AccountsCommon> wrapDoc)
96 throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
100 public AccountsCommonList extractCommonPartList(
101 DocumentWrapper<List> wrapDoc)
104 AccountsCommonList accList = new AccountsCommonList();
105 List<AccountsCommonList.AccountListItem> list = accList.getAccountListItem();
107 for (Object obj : wrapDoc.getWrappedObject()) {
108 AccountsCommon account = (AccountsCommon) obj;
109 AccountListItem accListItem = new AccountListItem();
110 accListItem.setScreenName(account.getScreenName());
111 accListItem.setEmail(account.getEmail());
112 accListItem.setStatus(account.getStatus());
113 String id = account.getCsid();
114 accListItem.setUri(getServiceContextPath() + id);
115 accListItem.setCsid(id);
116 list.add(accListItem);
122 public AccountsCommon getCommonPart() {
127 public void setCommonPart(AccountsCommon account) {
128 this.account = account;
132 public AccountsCommonList getCommonPartList() {
137 public void setCommonPartList(AccountsCommonList accountList) {
138 this.accountList = accountList;
142 public String getQProperty(
148 public DocumentFilter createDocumentFilter(ServiceContext ctx) {
149 DocumentFilter filter = new AccountJpaFilter();
151 ctx.getServiceBindingPropertyValue(
152 DocumentFilter.PAGE_SIZE_DEFAULT_PROPERTY));
156 private void setTenant(AccountsCommon account) {
157 //set tenant only if not available from input
158 ServiceContext ctx = getServiceContext();
159 if (account.getTenants() == null || account.getTenants().size() == 0) {
160 if (ctx.getTenantId() != null) {
161 AccountTenant at = new AccountTenant();
162 at.setTenantId(ctx.getTenantId());
163 List<AccountTenant> atList = new ArrayList<AccountTenant>();
165 account.setTenants(atList);
171 * sanitize removes data not needed to be sent to the consumer
174 private void sanitize(AccountsCommon account) {
175 account.setPassword(null);
176 account.setTenants(new ArrayList<AccountTenant>(0));