001/* 002 * (C) Copyright 2006-2012 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Thomas Roger <troger@nuxeo.com> 018 */ 019 020package org.nuxeo.ecm.multi.tenant; 021 022import static org.nuxeo.ecm.core.api.LifeCycleConstants.DELETE_TRANSITION; 023import static org.nuxeo.ecm.core.api.LifeCycleConstants.TRANSITION_EVENT; 024import static org.nuxeo.ecm.core.api.LifeCycleConstants.TRANSTION_EVENT_OPTION_TRANSITION; 025import static org.nuxeo.ecm.core.api.LifeCycleConstants.UNDELETE_TRANSITION; 026import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_CREATED; 027import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_CREATED_BY_COPY; 028import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_REMOVED; 029 030import org.nuxeo.ecm.core.api.CoreSession; 031import org.nuxeo.ecm.core.api.DocumentModel; 032import org.nuxeo.ecm.core.event.Event; 033import org.nuxeo.ecm.core.event.EventContext; 034import org.nuxeo.ecm.core.event.EventListener; 035import org.nuxeo.ecm.core.event.impl.DocumentEventContext; 036import org.nuxeo.runtime.api.Framework; 037 038/** 039 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a> 040 * @since 5.6 041 */ 042public class MultiTenantListener implements EventListener { 043 044 @Override 045 public void handleEvent(Event event) { 046 EventContext ctx = event.getContext(); 047 if (!(ctx instanceof DocumentEventContext)) { 048 return; 049 } 050 051 MultiTenantService multiTenantService = Framework.getService(MultiTenantService.class); 052 String tenantDocumentType = multiTenantService.getTenantDocumentType(); 053 DocumentModel doc = ((DocumentEventContext) ctx).getSourceDocument(); 054 if (!doc.getType().equals(tenantDocumentType)) { 055 return; 056 } 057 058 @SuppressWarnings("resource") 059 CoreSession session = ctx.getCoreSession(); 060 if (!multiTenantService.isTenantIsolationEnabled(session)) { 061 return; 062 } 063 064 String eventName = event.getName(); 065 if (DOCUMENT_CREATED.equals(eventName) || DOCUMENT_CREATED_BY_COPY.equals(eventName)) { 066 multiTenantService.enableTenantIsolationFor(session, doc); 067 session.save(); 068 } else if (DOCUMENT_REMOVED.equals(eventName)) { 069 multiTenantService.disableTenantIsolationFor(session, doc); 070 session.save(); 071 } else if (TRANSITION_EVENT.equals(eventName)) { 072 String transition = (String) ctx.getProperty(TRANSTION_EVENT_OPTION_TRANSITION); 073 if (DELETE_TRANSITION.equals(transition)) { 074 multiTenantService.disableTenantIsolationFor(session, doc); 075 session.save(); 076 } else if (UNDELETE_TRANSITION.equals(transition)) { 077 multiTenantService.enableTenantIsolationFor(session, doc); 078 session.save(); 079 } 080 } 081 } 082 083}