]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
f35de77316a17a88aa7518e738ede6e3c0ecbc56
[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         account.setCsid(id);
55         account.setStatus(Status.ACTIVE);
56     }
57
58     @Override
59     public void handleUpdate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
60     }
61
62     @Override
63     public void completeUpdate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
64         AccountsCommon upAcc = wrapDoc.getWrappedObject();
65         getServiceContext().setOutput(account);
66         sanitize(upAcc);
67     }
68
69     @Override
70     public void handleGet(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
71         setCommonPart(extractCommonPart(wrapDoc));
72         sanitize(getCommonPart());
73         getServiceContext().setOutput(account);
74     }
75
76     @Override
77     public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
78         AccountsCommonList accList = extractCommonPartList(wrapDoc);
79         setCommonPartList(accList);
80         getServiceContext().setOutput(getCommonPartList());
81     }
82
83     @Override
84     public AccountsCommon extractCommonPart(
85             DocumentWrapper<AccountsCommon> wrapDoc)
86             throws Exception {
87         return wrapDoc.getWrappedObject();
88     }
89
90     @Override
91     public void fillCommonPart(AccountsCommon obj, DocumentWrapper<AccountsCommon> wrapDoc)
92             throws Exception {
93         throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
94     }
95
96     @Override
97     public AccountsCommonList extractCommonPartList(
98             DocumentWrapper<List> wrapDoc)
99             throws Exception {
100
101         AccountsCommonList accList = new AccountsCommonList();
102         List<AccountsCommonList.AccountListItem> list = accList.getAccountListItem();
103
104         for (Object obj : wrapDoc.getWrappedObject()) {
105             AccountsCommon account = (AccountsCommon) obj;
106             AccountListItem accListItem = new AccountListItem();
107             accListItem.setScreenName(account.getScreenName());
108             accListItem.setEmail(account.getEmail());
109             accListItem.setStatus(account.getStatus());
110             String id = account.getCsid();
111             accListItem.setUri(getServiceContextPath() + id);
112             accListItem.setCsid(id);
113             list.add(accListItem);
114         }
115         return accList;
116     }
117
118     @Override
119     public AccountsCommon getCommonPart() {
120         return account;
121     }
122
123     @Override
124     public void setCommonPart(AccountsCommon account) {
125         this.account = account;
126     }
127
128     @Override
129     public AccountsCommonList getCommonPartList() {
130         return accountList;
131     }
132
133     @Override
134     public void setCommonPartList(AccountsCommonList accountList) {
135         this.accountList = accountList;
136     }
137
138     @Override
139     public String getQProperty(
140             String prop) {
141         return null;
142     }
143
144     @Override
145     public DocumentFilter createDocumentFilter() {
146         return new AccountJpaFilter();
147     }
148
149     /**
150      * sanitize removes data not needed to be sent to the consumer
151      * @param account
152      */
153     private void sanitize(AccountsCommon account) {
154         account.setPassword(null);
155
156     }
157 }