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; 029import static org.nuxeo.ecm.core.api.trash.TrashService.DOCUMENT_TRASHED; 030import static org.nuxeo.ecm.core.api.trash.TrashService.DOCUMENT_UNTRASHED; 031 032import org.nuxeo.ecm.core.api.CoreSession; 033import org.nuxeo.ecm.core.api.DocumentModel; 034import org.nuxeo.ecm.core.event.Event; 035import org.nuxeo.ecm.core.event.EventContext; 036import org.nuxeo.ecm.core.event.EventListener; 037import org.nuxeo.ecm.core.event.impl.DocumentEventContext; 038import org.nuxeo.runtime.api.Framework; 039 040/** 041 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a> 042 * @since 5.6 043 */ 044public class MultiTenantListener implements EventListener { 045 046 @Override 047 public void handleEvent(Event event) { 048 EventContext ctx = event.getContext(); 049 if (!(ctx instanceof DocumentEventContext)) { 050 return; 051 } 052 053 MultiTenantService multiTenantService = Framework.getService(MultiTenantService.class); 054 String tenantDocumentType = multiTenantService.getTenantDocumentType(); 055 DocumentModel doc = ((DocumentEventContext) ctx).getSourceDocument(); 056 if (!doc.getType().equals(tenantDocumentType)) { 057 return; 058 } 059 060 CoreSession session = ctx.getCoreSession(); 061 if (!multiTenantService.isTenantIsolationEnabled(session)) { 062 return; 063 } 064 065 String eventName = event.getName(); 066 switch (eventName) { 067 case DOCUMENT_CREATED: 068 case DOCUMENT_CREATED_BY_COPY: 069 case DOCUMENT_UNTRASHED: 070 multiTenantService.enableTenantIsolationFor(session, doc); 071 break; 072 case DOCUMENT_REMOVED: 073 case DOCUMENT_TRASHED: 074 multiTenantService.disableTenantIsolationFor(session, doc); 075 break; 076 case TRANSITION_EVENT: 077 // backward compatibility with previous trashed state 078 String transition = (String) ctx.getProperty(TRANSTION_EVENT_OPTION_TRANSITION); 079 if (DELETE_TRANSITION.equals(transition)) { 080 multiTenantService.disableTenantIsolationFor(session, doc); 081 } else if (UNDELETE_TRANSITION.equals(transition)) { 082 multiTenantService.enableTenantIsolationFor(session, doc); 083 } 084 break; 085 } 086 session.save(); 087 } 088 089}