001/*
002 * (C) Copyright 2006-2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Thomas Roger <troger@nuxeo.com>
016 */
017
018package org.nuxeo.ecm.multi.tenant;
019
020import static org.nuxeo.ecm.core.api.LifeCycleConstants.DELETE_TRANSITION;
021import static org.nuxeo.ecm.core.api.LifeCycleConstants.TRANSITION_EVENT;
022import static org.nuxeo.ecm.core.api.LifeCycleConstants.TRANSTION_EVENT_OPTION_TRANSITION;
023import static org.nuxeo.ecm.core.api.LifeCycleConstants.UNDELETE_TRANSITION;
024import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_CREATED;
025import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_CREATED_BY_COPY;
026import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_REMOVED;
027
028import org.nuxeo.ecm.core.api.CoreSession;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.event.Event;
031import org.nuxeo.ecm.core.event.EventContext;
032import org.nuxeo.ecm.core.event.EventListener;
033import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
034import org.nuxeo.runtime.api.Framework;
035
036/**
037 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
038 * @since 5.6
039 */
040public class MultiTenantListener implements EventListener {
041
042    @Override
043    public void handleEvent(Event event) {
044        EventContext ctx = event.getContext();
045        if (!(ctx instanceof DocumentEventContext)) {
046            return;
047        }
048
049        MultiTenantService multiTenantService = Framework.getLocalService(MultiTenantService.class);
050        String tenantDocumentType = multiTenantService.getTenantDocumentType();
051        DocumentModel doc = ((DocumentEventContext) ctx).getSourceDocument();
052        if (!doc.getType().equals(tenantDocumentType)) {
053            return;
054        }
055
056        @SuppressWarnings("resource")
057        CoreSession session = ctx.getCoreSession();
058        if (!multiTenantService.isTenantIsolationEnabled(session)) {
059            return;
060        }
061
062        String eventName = event.getName();
063        if (DOCUMENT_CREATED.equals(eventName) || DOCUMENT_CREATED_BY_COPY.equals(eventName)) {
064            multiTenantService.enableTenantIsolationFor(session, doc);
065            session.save();
066        } else if (DOCUMENT_REMOVED.equals(eventName)) {
067            multiTenantService.disableTenantIsolationFor(session, doc);
068            session.save();
069        } else if (TRANSITION_EVENT.equals(eventName)) {
070            String transition = (String) ctx.getProperty(TRANSTION_EVENT_OPTION_TRANSITION);
071            if (DELETE_TRANSITION.equals(transition)) {
072                multiTenantService.disableTenantIsolationFor(session, doc);
073                session.save();
074            } else if (UNDELETE_TRANSITION.equals(transition)) {
075                multiTenantService.enableTenantIsolationFor(session, doc);
076                session.save();
077            }
078        }
079    }
080
081}