]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
30ce709f07a9b5a2a7cddc4bd8ee4e8abeed0215
[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.ArrayList;
27 import java.util.List;
28 import java.util.UUID;
29 import org.collectionspace.services.account.AccountsCommon;
30 import org.collectionspace.services.account.AccountsCommon.Tenant;
31 import org.collectionspace.services.account.AccountsCommonList;
32 import org.collectionspace.services.account.AccountsCommonList.AccountListItem;
33 import org.collectionspace.services.account.Status;
34 import org.collectionspace.services.common.document.AbstractDocumentHandlerImpl;
35 import org.collectionspace.services.common.document.DocumentFilter;
36 import org.collectionspace.services.common.document.DocumentWrapper;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  *
42  * @author 
43  */
44 public class AccountDocumentHandler
45         extends AbstractDocumentHandlerImpl<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         account.setCsid(id);
56         
57         account.setStatus(Status.ACTIVE);
58     }
59
60     @Override
61     public void handleUpdate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
62     }
63
64     @Override
65     public void completeUpdate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
66         AccountsCommon upAcc = wrapDoc.getWrappedObject();
67         getServiceContext().setOutput(account);
68         sanitize(upAcc);
69     }
70
71     @Override
72     public void handleGet(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
73         setCommonPart(extractCommonPart(wrapDoc));
74         sanitize(getCommonPart());
75         getServiceContext().setOutput(account);
76     }
77
78     @Override
79     public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
80         AccountsCommonList accList = extractCommonPartList(wrapDoc);
81         setCommonPartList(accList);
82         getServiceContext().setOutput(getCommonPartList());
83     }
84
85     @Override
86     public AccountsCommon extractCommonPart(
87             DocumentWrapper<AccountsCommon> wrapDoc)
88             throws Exception {
89         return wrapDoc.getWrappedObject();
90     }
91
92     @Override
93     public void fillCommonPart(AccountsCommon obj, DocumentWrapper<AccountsCommon> wrapDoc)
94             throws Exception {
95         throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
96     }
97
98     @Override
99     public AccountsCommonList extractCommonPartList(
100             DocumentWrapper<List> wrapDoc)
101             throws Exception {
102
103         AccountsCommonList accList = new AccountsCommonList();
104         List<AccountsCommonList.AccountListItem> list = accList.getAccountListItem();
105
106         for (Object obj : wrapDoc.getWrappedObject()) {
107             AccountsCommon account = (AccountsCommon) obj;
108             AccountListItem accListItem = new AccountListItem();
109             accListItem.setScreenName(account.getScreenName());
110             accListItem.setEmail(account.getEmail());
111             accListItem.setStatus(account.getStatus());
112             String id = account.getCsid();
113             accListItem.setUri(getServiceContextPath() + id);
114             accListItem.setCsid(id);
115             list.add(accListItem);
116         }
117         return accList;
118     }
119
120     @Override
121     public AccountsCommon getCommonPart() {
122         return account;
123     }
124
125     @Override
126     public void setCommonPart(AccountsCommon account) {
127         this.account = account;
128     }
129
130     @Override
131     public AccountsCommonList getCommonPartList() {
132         return accountList;
133     }
134
135     @Override
136     public void setCommonPartList(AccountsCommonList accountList) {
137         this.accountList = accountList;
138     }
139
140     @Override
141     public String getQProperty(
142             String prop) {
143         return null;
144     }
145
146     @Override
147     public DocumentFilter createDocumentFilter() {
148         return new AccountJpaFilter();
149     }
150
151     /**
152      * sanitize removes data not needed to be sent to the consumer
153      * @param account
154      */
155     private void sanitize(AccountsCommon account) {
156         account.setPassword(null);
157         //FIXME once auth mode is mandatory, assume tenant could be retrieved
158         //from security context, remove tenant info being passed to the consumer
159         //account.setTenant(new ArrayList<Tenant>(0));
160     }
161 }