]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
f4d1446c818a3b10b89d8c9cd804ccf692b381ec
[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.Date;
28 import java.util.List;
29 import java.util.UUID;
30
31 import org.apache.commons.lang.StringUtils;
32 import org.collectionspace.services.account.Tenant;
33 import org.collectionspace.services.account.TenantsList;
34 import org.collectionspace.services.account.TenantListItem;
35
36 import org.collectionspace.services.client.TenantClient;
37 import org.collectionspace.services.common.storage.jpa.JpaDocumentHandler;
38 import org.collectionspace.services.common.context.ServiceContext;
39 import org.collectionspace.services.common.document.DocumentFilter;
40 import org.collectionspace.services.common.document.DocumentWrapper;
41 import org.collectionspace.services.common.document.JaxbUtils;
42 import org.collectionspace.services.common.security.SecurityUtils;
43
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 /**
48  *
49  * @author 
50  */
51 public class TenantDocumentHandler
52         extends JpaDocumentHandler<Tenant, TenantsList, Tenant, List> {
53
54     private final Logger logger = LoggerFactory.getLogger(AccountDocumentHandler.class);
55     private Tenant tenant;
56     private TenantsList tenantList;
57
58     @Override
59     public void handleCreate(DocumentWrapper<Tenant> wrapDoc) throws Exception {
60     }
61
62     @Override
63     public void handleUpdate(DocumentWrapper<Tenant> wrapDoc) throws Exception {
64         Tenant tenantFound = wrapDoc.getWrappedObject();
65         Tenant tenantReceived = getCommonPart();
66         // If marked as metadata immutable, do not do update
67         merge(tenantReceived, tenantFound);
68     }
69     
70     /**
71      * merge manually merges the from account to the to account
72      * -this method is created due to inefficiency of JPA EM merge
73      * @param from
74      * @param to
75      * @return merged account
76      */
77     private Tenant merge(Tenant from, Tenant to) {
78         Date now = new Date();
79         to.setUpdatedAtItem(now);
80         // The only thing we allow changing at this point is the disabled flag
81         to.setDisabled(from.isDisabled());
82
83         if (logger.isDebugEnabled()) {
84             logger.debug("merged account="
85                     + JaxbUtils.toString(to, Tenant.class));
86         }
87         return to;
88     }
89
90
91     @Override
92     public void completeUpdate(DocumentWrapper<Tenant> wrapDoc) throws Exception {
93         Tenant upAcc = wrapDoc.getWrappedObject();
94         getServiceContext().setOutput(upAcc);
95     }
96
97     @Override
98     public void handleGet(DocumentWrapper<Tenant> wrapDoc) throws Exception {
99         setCommonPart(extractCommonPart(wrapDoc));
100         getServiceContext().setOutput(tenant);
101     }
102
103     @Override
104     public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
105         TenantsList tenList = extractCommonPartList(wrapDoc);
106         setCommonPartList(tenList);
107         getServiceContext().setOutput(getCommonPartList());
108     }
109
110     @Override
111     public Tenant extractCommonPart(
112             DocumentWrapper<Tenant> wrapDoc)
113             throws Exception {
114         return wrapDoc.getWrappedObject();
115     }
116
117     @Override
118     public void fillCommonPart(Tenant obj, DocumentWrapper<Tenant> wrapDoc)
119             throws Exception {
120         throw new UnsupportedOperationException("operation not relevant for TenantDocumentHandler");
121     }
122
123     @Override
124     public TenantsList extractCommonPartList(
125             DocumentWrapper<List> wrapDoc)
126             throws Exception {
127
128         TenantsList tenList = this.extractPagingInfo(new TenantsList(), wrapDoc);
129 //        TenantsList accList = new TenantsList();
130         List<TenantListItem> list = tenList.getTenantListItem();
131
132         for (Object obj : wrapDoc.getWrappedObject()) {
133             Tenant tenant = (Tenant) obj;
134             TenantListItem tenListItem = new TenantListItem();
135             tenListItem.setId(tenant.getId());
136             tenListItem.setName(tenant.getName());
137             tenListItem.setDisabled(tenant.isDisabled());
138             list.add(tenListItem);
139         }
140         return tenList;
141     }
142
143     @Override
144     public Tenant getCommonPart() {
145         return tenant;
146     }
147
148     @Override
149     public void setCommonPart(Tenant tenant) {
150         this.tenant = tenant;
151     }
152
153     @Override
154     public TenantsList getCommonPartList() {
155         return tenantList;
156     }
157
158     @Override
159     public void setCommonPartList(TenantsList tenantList) {
160         this.tenantList = tenantList;
161     }
162
163     @Override
164     public String getQProperty(
165             String prop) {
166         return null;
167     }
168
169     @Override
170     public DocumentFilter createDocumentFilter() {
171         DocumentFilter filter = new TenantJpaFilter(this.getServiceContext());
172         return filter;
173     }
174
175     /* (non-Javadoc)
176      * @see org.collectionspace.services.common.document.DocumentHandler#initializeDocumentFilter(org.collectionspace.services.common.context.ServiceContext)
177      */
178     public void initializeDocumentFilter(ServiceContext ctx) {
179         // set a default document filter in this method
180     }
181 }