]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
ec11b7dd971abf868d4a0c4e4242d90fbd00bf1b
[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.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  *
41  * @author 
42  */
43 public class AccountDocumentHandler
44         extends AbstractDocumentHandler<AccountsCommon, AccountsCommonList, AccountsCommon, List> {
45
46     private final Logger logger = LoggerFactory.getLogger(AccountDocumentHandler.class);
47     private AccountsCommon account;
48     private AccountsCommonList accountList;
49
50     @Override
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";
56             logger.error(msg);
57             throw new BadRequestException(msg);
58         }
59         List<AccountsCommon.Tenant> tl = account.getTenant();
60         if (tl == null || tl.size() == 0) {
61             String msg = "missing tenant information!";
62             logger.error(msg);
63             throw new BadRequestException(msg);
64         }
65         account.setCsid(id);
66         account.setStatus(Status.ACTIVE);
67     }
68
69     @Override
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";
74             logger.error(msg);
75             throw new BadRequestException(msg);
76         }
77     }
78
79     @Override
80     public void completeUpdate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
81         AccountsCommon upAcc = wrapDoc.getWrappedObject();
82         getServiceContext().setOutput(account);
83         sanitize(upAcc);
84     }
85
86     @Override
87     public void handleGet(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
88         setCommonPart(extractCommonPart(wrapDoc));
89         sanitize(getCommonPart());
90         getServiceContext().setOutput(account);
91     }
92
93     @Override
94     public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
95         AccountsCommonList accList = extractCommonPartList(wrapDoc);
96         setCommonPartList(accList);
97         getServiceContext().setOutput(getCommonPartList());
98     }
99
100     @Override
101     public AccountsCommon extractCommonPart(
102             DocumentWrapper<AccountsCommon> wrapDoc)
103             throws Exception {
104         return wrapDoc.getWrappedObject();
105     }
106
107     @Override
108     public void fillCommonPart(AccountsCommon obj, DocumentWrapper<AccountsCommon> wrapDoc)
109             throws Exception {
110         throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
111     }
112
113     @Override
114     public AccountsCommonList extractCommonPartList(
115             DocumentWrapper<List> wrapDoc)
116             throws Exception {
117
118         AccountsCommonList accList = new AccountsCommonList();
119         List<AccountsCommonList.AccountListItem> list = accList.getAccountListItem();
120
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);
131         }
132         return accList;
133     }
134
135     @Override
136     public AccountsCommon getCommonPart() {
137         return account;
138     }
139
140     @Override
141     public void setCommonPart(AccountsCommon account) {
142         this.account = account;
143     }
144
145     @Override
146     public AccountsCommonList getCommonPartList() {
147         return accountList;
148     }
149
150     @Override
151     public void setCommonPartList(AccountsCommonList accountList) {
152         this.accountList = accountList;
153     }
154
155     @Override
156     public String getQProperty(
157             String prop) {
158         return null;
159     }
160
161     @Override
162     public DocumentFilter createDocumentFilter() {
163         return new AccountJpaFilter();
164     }
165
166     /**
167      * sanitize removes data not needed to be sent to the consumer
168      * @param account
169      */
170     private void sanitize(AccountsCommon account) {
171         account.setPassword(null);
172
173     }
174 }