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.collectionspace.services.common.storage.jpa.JpaDocumentFilter;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
44 public class AccountDocumentHandler
45 extends AbstractDocumentHandler<AccountsCommon, AccountsCommonList, AccountsCommon, List> {
47 private final Logger logger = LoggerFactory.getLogger(AccountDocumentHandler.class);
48 private AccountsCommon account;
49 private AccountsCommonList accountList;
52 public void handleCreate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
53 String id = UUID.randomUUID().toString();
54 AccountsCommon account = wrapDoc.getWrappedObject();
55 if (account.getUserId() == null || "".equals(account.getUserId())) {
56 String msg = "userId is missing";
58 throw new BadRequestException(msg);
60 List<AccountsCommon.Tenant> tl = account.getTenant();
61 if (tl == null || tl.size() == 0) {
62 String msg = "missing tenant information!";
64 throw new BadRequestException(msg);
67 account.setStatus(Status.ACTIVE);
71 public void handleUpdate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
72 if (account.getPassword() != null
73 && (account.getUserId() == null || "".equals(account.getUserId()))) {
74 String msg = "userId is missing";
76 throw new BadRequestException(msg);
81 public void completeUpdate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
82 AccountsCommon upAcc = wrapDoc.getWrappedObject();
83 getServiceContext().setOutput(account);
88 public void handleGet(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
89 setCommonPart(extractCommonPart(wrapDoc));
90 sanitize(getCommonPart());
91 getServiceContext().setOutput(account);
95 public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
96 AccountsCommonList accList = extractCommonPartList(wrapDoc);
97 setCommonPartList(accList);
98 getServiceContext().setOutput(getCommonPartList());
102 public AccountsCommon extractCommonPart(
103 DocumentWrapper<AccountsCommon> wrapDoc)
105 return wrapDoc.getWrappedObject();
109 public void fillCommonPart(AccountsCommon obj, DocumentWrapper<AccountsCommon> wrapDoc)
111 throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
115 public AccountsCommonList extractCommonPartList(
116 DocumentWrapper<List> wrapDoc)
119 AccountsCommonList accList = new AccountsCommonList();
120 List<AccountsCommonList.AccountListItem> list = accList.getAccountListItem();
122 for (Object obj : wrapDoc.getWrappedObject()) {
123 AccountsCommon account = (AccountsCommon) obj;
124 AccountListItem accListItem = new AccountListItem();
125 accListItem.setScreenName(account.getScreenName());
126 accListItem.setEmail(account.getEmail());
127 accListItem.setStatus(account.getStatus());
128 String id = account.getCsid();
129 accListItem.setUri(getServiceContextPath() + id);
130 accListItem.setCsid(id);
131 list.add(accListItem);
137 public AccountsCommon getCommonPart() {
142 public void setCommonPart(AccountsCommon account) {
143 this.account = account;
147 public AccountsCommonList getCommonPartList() {
152 public void setCommonPartList(AccountsCommonList accountList) {
153 this.accountList = accountList;
157 public String getQProperty(
163 public DocumentFilter createDocumentFilter() {
164 return new JpaDocumentFilter();
166 private void setWhereForGetAll(StringBuilder strBld) {
167 DocumentFilter filter = getDocumentFilter();
168 String screenName = null;
169 if (screenName != null && !screenName.isEmpty()) {
171 "WHERE UPPER(a.screenName)"
174 filter.addQueryParam("sn", "%" + screenName.toUpperCase() + "%");
175 filter.setWhereClause(ptClause);
180 * sanitize removes data not needed to be sent to the consumer
183 private void sanitize(AccountsCommon account) {
184 account.setPassword(null);