]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
6863aca45b64b11e2f5a3b72e58f694d9e3c1071
[tmp/jakarta-migration.git] /
1 /**
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:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
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.
23  */
24 package org.collectionspace.services.account.storage;
25
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;
39
40 /**
41  *
42  * @author 
43  */
44 public class AccountDocumentHandler
45         extends AbstractDocumentHandler<AccountsCommon, AccountsCommonList, AccountsCommon, List> {
46
47     private final Logger logger = LoggerFactory.getLogger(AccountDocumentHandler.class);
48     private AccountsCommon account;
49     private AccountsCommonList accountList;
50
51     @Override
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";
57             logger.error(msg);
58             throw new BadRequestException(msg);
59         }
60         List<AccountsCommon.Tenant> tl = account.getTenant();
61         if (tl == null || tl.size() == 0) {
62             String msg = "missing tenant information!";
63             logger.error(msg);
64             throw new BadRequestException(msg);
65         }
66         account.setCsid(id);
67         account.setStatus(Status.ACTIVE);
68     }
69
70     @Override
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";
75             logger.error(msg);
76             throw new BadRequestException(msg);
77         }
78     }
79
80     @Override
81     public void completeUpdate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
82         AccountsCommon upAcc = wrapDoc.getWrappedObject();
83         getServiceContext().setOutput(account);
84         sanitize(upAcc);
85     }
86
87     @Override
88     public void handleGet(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
89         setCommonPart(extractCommonPart(wrapDoc));
90         sanitize(getCommonPart());
91         getServiceContext().setOutput(account);
92     }
93
94     @Override
95     public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
96         AccountsCommonList accList = extractCommonPartList(wrapDoc);
97         setCommonPartList(accList);
98         getServiceContext().setOutput(getCommonPartList());
99     }
100
101     @Override
102     public AccountsCommon extractCommonPart(
103             DocumentWrapper<AccountsCommon> wrapDoc)
104             throws Exception {
105         return wrapDoc.getWrappedObject();
106     }
107
108     @Override
109     public void fillCommonPart(AccountsCommon obj, DocumentWrapper<AccountsCommon> wrapDoc)
110             throws Exception {
111         throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
112     }
113
114     @Override
115     public AccountsCommonList extractCommonPartList(
116             DocumentWrapper<List> wrapDoc)
117             throws Exception {
118
119         AccountsCommonList accList = new AccountsCommonList();
120         List<AccountsCommonList.AccountListItem> list = accList.getAccountListItem();
121
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);
132         }
133         return accList;
134     }
135
136     @Override
137     public AccountsCommon getCommonPart() {
138         return account;
139     }
140
141     @Override
142     public void setCommonPart(AccountsCommon account) {
143         this.account = account;
144     }
145
146     @Override
147     public AccountsCommonList getCommonPartList() {
148         return accountList;
149     }
150
151     @Override
152     public void setCommonPartList(AccountsCommonList accountList) {
153         this.accountList = accountList;
154     }
155
156     @Override
157     public String getQProperty(
158             String prop) {
159         return null;
160     }
161
162     @Override
163     public DocumentFilter createDocumentFilter() {
164         return new JpaDocumentFilter();
165     }
166     private void setWhereForGetAll(StringBuilder strBld) {
167         DocumentFilter filter = getDocumentFilter();
168         String screenName = null;
169         if (screenName != null && !screenName.isEmpty()) {
170             String ptClause =
171                     "WHERE UPPER(a.screenName)"
172                     + " LIKE "
173                     + ":sn";
174             filter.addQueryParam("sn", "%" + screenName.toUpperCase() + "%");
175             filter.setWhereClause(ptClause);
176         }
177
178     }
179     /**
180      * sanitize removes data not needed to be sent to the consumer
181      * @param account
182      */
183     private void sanitize(AccountsCommon account) {
184         account.setPassword(null);
185     }
186 }