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.List;
27 import java.util.UUID;
28 import org.collectionspace.services.account.AccountsCommon;
29 import org.collectionspace.services.account.AccountsCommonList;
30 import org.collectionspace.services.account.AccountsCommonList.AccountListItem;
31 import org.collectionspace.services.account.Status;
32 import org.collectionspace.services.common.document.AbstractDocumentHandler;
33 import org.collectionspace.services.common.document.BadRequestException;
34 import org.collectionspace.services.common.document.DocumentFilter;
35 import org.collectionspace.services.common.document.DocumentWrapper;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
43 public class AccountDocumentHandler
44 extends AbstractDocumentHandler<AccountsCommon, AccountsCommonList, AccountsCommon, List> {
46 private final Logger logger = LoggerFactory.getLogger(AccountDocumentHandler.class);
47 private AccountsCommon account;
48 private AccountsCommonList accountList;
51 public void handleCreate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
52 String id = UUID.randomUUID().toString();
53 AccountsCommon account = wrapDoc.getWrappedObject();
54 if (account.getUserId() == null || "".equals(account.getUserId())) {
55 String msg = "userId is missing";
57 throw new BadRequestException(msg);
59 List<AccountsCommon.Tenant> tl = account.getTenant();
60 if (tl == null || tl.size() == 0) {
61 String msg = "missing tenant information!";
63 throw new BadRequestException(msg);
66 account.setStatus(Status.ACTIVE);
70 public void handleUpdate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
71 if (account.getPassword() != null
72 && (account.getUserId() == null || "".equals(account.getUserId()))) {
73 String msg = "userId is missing";
75 throw new BadRequestException(msg);
80 public void completeUpdate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
81 AccountsCommon upAcc = wrapDoc.getWrappedObject();
82 getServiceContext().setOutput(account);
87 public void handleGet(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
88 setCommonPart(extractCommonPart(wrapDoc));
89 sanitize(getCommonPart());
90 getServiceContext().setOutput(account);
94 public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
95 AccountsCommonList accList = extractCommonPartList(wrapDoc);
96 setCommonPartList(accList);
97 getServiceContext().setOutput(getCommonPartList());
101 public AccountsCommon extractCommonPart(
102 DocumentWrapper<AccountsCommon> wrapDoc)
104 return wrapDoc.getWrappedObject();
108 public void fillCommonPart(AccountsCommon obj, DocumentWrapper<AccountsCommon> wrapDoc)
110 throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
114 public AccountsCommonList extractCommonPartList(
115 DocumentWrapper<List> wrapDoc)
118 AccountsCommonList accList = new AccountsCommonList();
119 List<AccountsCommonList.AccountListItem> list = accList.getAccountListItem();
121 for (Object obj : wrapDoc.getWrappedObject()) {
122 AccountsCommon account = (AccountsCommon) obj;
123 AccountListItem accListItem = new AccountListItem();
124 accListItem.setScreenName(account.getScreenName());
125 accListItem.setEmail(account.getEmail());
126 accListItem.setStatus(account.getStatus());
127 String id = account.getCsid();
128 accListItem.setUri(getServiceContextPath() + id);
129 accListItem.setCsid(id);
130 list.add(accListItem);
136 public AccountsCommon getCommonPart() {
141 public void setCommonPart(AccountsCommon account) {
142 this.account = account;
146 public AccountsCommonList getCommonPartList() {
151 public void setCommonPartList(AccountsCommonList accountList) {
152 this.accountList = accountList;
156 public String getQProperty(
162 public DocumentFilter createDocumentFilter() {
163 return new AccountJpaFilter();
167 * sanitize removes data not needed to be sent to the consumer
170 private void sanitize(AccountsCommon account) {
171 account.setPassword(null);