]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
15318c7612fdf868f77073436ad57830f70d4565
[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 are the 'disabled' and 'authoritiesInitialized' flags
81         to.setDisabled(from.isDisabled());
82         to.setAuthoritiesInitialized(from.isAuthoritiesInitialized());
83
84         if (logger.isDebugEnabled()) {
85             logger.debug("merged account="
86                     + JaxbUtils.toString(to, Tenant.class));
87         }
88         return to;
89     }
90
91
92     @Override
93     public void completeUpdate(DocumentWrapper<Tenant> wrapDoc) throws Exception {
94         Tenant upAcc = wrapDoc.getWrappedObject();
95         getServiceContext().setOutput(upAcc);
96     }
97
98     @Override
99     public void handleGet(DocumentWrapper<Tenant> wrapDoc) throws Exception {
100         setCommonPart(extractCommonPart(wrapDoc));
101         getServiceContext().setOutput(tenant);
102     }
103
104     @Override
105     public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
106         TenantsList tenList = extractCommonPartList(wrapDoc);
107         setCommonPartList(tenList);
108         getServiceContext().setOutput(getCommonPartList());
109     }
110
111     @Override
112     public Tenant extractCommonPart(
113             DocumentWrapper<Tenant> wrapDoc)
114             throws Exception {
115         return wrapDoc.getWrappedObject();
116     }
117
118     @Override
119     public void fillCommonPart(Tenant obj, DocumentWrapper<Tenant> wrapDoc)
120             throws Exception {
121         throw new UnsupportedOperationException("operation not relevant for TenantDocumentHandler");
122     }
123
124     @Override
125     public TenantsList extractCommonPartList(
126             DocumentWrapper<List> wrapDoc)
127             throws Exception {
128
129         TenantsList tenList = this.extractPagingInfo(new TenantsList(), wrapDoc);
130 //        TenantsList accList = new TenantsList();
131         List<TenantListItem> list = tenList.getTenantListItem();
132
133         for (Object obj : wrapDoc.getWrappedObject()) {
134             Tenant tenant = (Tenant) obj;
135             TenantListItem tenListItem = new TenantListItem();
136             tenListItem.setId(tenant.getId());
137             tenListItem.setName(tenant.getName());
138             tenListItem.setDisabled(tenant.isDisabled());
139             list.add(tenListItem);
140         }
141         return tenList;
142     }
143
144     @Override
145     public Tenant getCommonPart() {
146         return tenant;
147     }
148
149     @Override
150     public void setCommonPart(Tenant tenant) {
151         this.tenant = tenant;
152     }
153
154     @Override
155     public TenantsList getCommonPartList() {
156         return tenantList;
157     }
158
159     @Override
160     public void setCommonPartList(TenantsList tenantList) {
161         this.tenantList = tenantList;
162     }
163
164     @Override
165     public String getQProperty(
166             String prop) {
167         return null;
168     }
169
170     @Override
171     public DocumentFilter createDocumentFilter() {
172         DocumentFilter filter = new TenantJpaFilter(this.getServiceContext());
173         return filter;
174     }
175
176     /* (non-Javadoc)
177      * @see org.collectionspace.services.common.document.DocumentHandler#initializeDocumentFilter(org.collectionspace.services.common.context.ServiceContext)
178      */
179     public void initializeDocumentFilter(ServiceContext ctx) {
180         // set a default document filter in this method
181     }
182 }