]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
40907bcd0db99f7dd2a34503ab466d867ecdab34
[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.AccountTenant;
30 import org.collectionspace.services.account.AccountsCommon;
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.context.ServiceContext;
35 import org.collectionspace.services.common.document.AbstractDocumentHandlerImpl;
36 import org.collectionspace.services.common.document.DocumentFilter;
37 import org.collectionspace.services.common.document.DocumentWrapper;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  *
43  * @author 
44  */
45 public class AccountDocumentHandler
46         extends AbstractDocumentHandlerImpl<AccountsCommon, AccountsCommonList, AccountsCommon, List> {
47
48     private final Logger logger = LoggerFactory.getLogger(AccountDocumentHandler.class);
49     private AccountsCommon account;
50     private AccountsCommonList accountList;
51
52     @Override
53     public void handleCreate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
54         String id = UUID.randomUUID().toString();
55         AccountsCommon account = wrapDoc.getWrappedObject();
56         account.setCsid(id);
57         setTenant(account);
58         account.setStatus(Status.ACTIVE);
59     }
60
61     @Override
62     public void handleUpdate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
63     }
64
65     @Override
66     public void completeUpdate(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
67         AccountsCommon upAcc = wrapDoc.getWrappedObject();
68         getServiceContext().setOutput(account);
69         sanitize(upAcc);
70     }
71
72     @Override
73     public void handleGet(DocumentWrapper<AccountsCommon> wrapDoc) throws Exception {
74         setCommonPart(extractCommonPart(wrapDoc));
75         sanitize(getCommonPart());
76         getServiceContext().setOutput(account);
77     }
78
79     @Override
80     public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
81         AccountsCommonList accList = extractCommonPartList(wrapDoc);
82         setCommonPartList(accList);
83         getServiceContext().setOutput(getCommonPartList());
84     }
85
86     @Override
87     public AccountsCommon extractCommonPart(
88             DocumentWrapper<AccountsCommon> wrapDoc)
89             throws Exception {
90         return wrapDoc.getWrappedObject();
91     }
92
93     @Override
94     public void fillCommonPart(AccountsCommon obj, DocumentWrapper<AccountsCommon> wrapDoc)
95             throws Exception {
96         throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
97     }
98
99     @Override
100     public AccountsCommonList extractCommonPartList(
101             DocumentWrapper<List> wrapDoc)
102             throws Exception {
103
104         AccountsCommonList accList = new AccountsCommonList();
105         List<AccountsCommonList.AccountListItem> list = accList.getAccountListItem();
106
107         for (Object obj : wrapDoc.getWrappedObject()) {
108             AccountsCommon account = (AccountsCommon) obj;
109             AccountListItem accListItem = new AccountListItem();
110             accListItem.setScreenName(account.getScreenName());
111             accListItem.setEmail(account.getEmail());
112             accListItem.setStatus(account.getStatus());
113             String id = account.getCsid();
114             accListItem.setUri(getServiceContextPath() + id);
115             accListItem.setCsid(id);
116             list.add(accListItem);
117         }
118         return accList;
119     }
120
121     @Override
122     public AccountsCommon getCommonPart() {
123         return account;
124     }
125
126     @Override
127     public void setCommonPart(AccountsCommon account) {
128         this.account = account;
129     }
130
131     @Override
132     public AccountsCommonList getCommonPartList() {
133         return accountList;
134     }
135
136     @Override
137     public void setCommonPartList(AccountsCommonList accountList) {
138         this.accountList = accountList;
139     }
140
141     @Override
142     public String getQProperty(
143             String prop) {
144         return null;
145     }
146
147     @Override
148     public DocumentFilter createDocumentFilter(ServiceContext ctx) {
149         DocumentFilter  filter = new AccountJpaFilter();
150         filter.setPageSize(
151                         ctx.getServiceBindingPropertyValue(
152                                         DocumentFilter.PAGE_SIZE_DEFAULT_PROPERTY));
153         return filter;
154     }
155
156     private void setTenant(AccountsCommon account) {
157         //set tenant only if not available from input
158         ServiceContext ctx = getServiceContext();
159         if (account.getTenants() == null || account.getTenants().size() == 0) {
160             if (ctx.getTenantId() != null) {
161                 AccountTenant at = new AccountTenant();
162                 at.setTenantId(ctx.getTenantId());
163                 List<AccountTenant> atList = new ArrayList<AccountTenant>();
164                 atList.add(at);
165                 account.setTenants(atList);
166             }
167         }
168     }
169
170     /**
171      * sanitize removes data not needed to be sent to the consumer
172      * @param account
173      */
174     private void sanitize(AccountsCommon account) {
175         account.setPassword(null);
176         account.setTenants(new ArrayList<AccountTenant>(0));
177     }
178 }