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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
24 package org.collectionspace.services.account.storage;
26 import java.util.ArrayList;
27 import java.util.Date;
28 import java.util.List;
29 import java.util.UUID;
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;
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;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
51 public class TenantDocumentHandler
52 extends JpaDocumentHandler<Tenant, TenantsList, Tenant, List> {
54 private final Logger logger = LoggerFactory.getLogger(AccountDocumentHandler.class);
55 private Tenant tenant;
56 private TenantsList tenantList;
59 public void handleCreate(DocumentWrapper<Tenant> wrapDoc) throws Exception {
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);
71 * merge manually merges the from account to the to account
72 * -this method is created due to inefficiency of JPA EM merge
75 * @return merged account
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());
84 if (logger.isDebugEnabled()) {
85 logger.debug("merged account="
86 + JaxbUtils.toString(to, Tenant.class));
93 public void completeUpdate(DocumentWrapper<Tenant> wrapDoc) throws Exception {
94 Tenant upAcc = wrapDoc.getWrappedObject();
95 getServiceContext().setOutput(upAcc);
99 public void handleGet(DocumentWrapper<Tenant> wrapDoc) throws Exception {
100 setCommonPart(extractCommonPart(wrapDoc));
101 getServiceContext().setOutput(tenant);
105 public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
106 TenantsList tenList = extractCommonPartList(wrapDoc);
107 setCommonPartList(tenList);
108 getServiceContext().setOutput(getCommonPartList());
112 public Tenant extractCommonPart(
113 DocumentWrapper<Tenant> wrapDoc)
115 return wrapDoc.getWrappedObject();
119 public void fillCommonPart(Tenant obj, DocumentWrapper<Tenant> wrapDoc)
121 throw new UnsupportedOperationException("operation not relevant for TenantDocumentHandler");
125 public TenantsList extractCommonPartList(
126 DocumentWrapper<List> wrapDoc)
129 TenantsList tenList = this.extractPagingInfo(new TenantsList(), wrapDoc);
130 // TenantsList accList = new TenantsList();
131 List<TenantListItem> list = tenList.getTenantListItem();
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);
145 public Tenant getCommonPart() {
150 public void setCommonPart(Tenant tenant) {
151 this.tenant = tenant;
155 public TenantsList getCommonPartList() {
160 public void setCommonPartList(TenantsList tenantList) {
161 this.tenantList = tenantList;
165 public String getQProperty(
171 public DocumentFilter createDocumentFilter() {
172 DocumentFilter filter = new TenantJpaFilter(this.getServiceContext());
177 * @see org.collectionspace.services.common.document.DocumentHandler#initializeDocumentFilter(org.collectionspace.services.common.context.ServiceContext)
179 public void initializeDocumentFilter(ServiceContext ctx) {
180 // set a default document filter in this method