001/*
002 * (C) Copyright 2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
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.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 * Contributors:
014 * Nuxeo - initial API and implementation
015 */
016package org.nuxeo.ecm.platform.rendition.publisher;
017
018import static org.nuxeo.ecm.platform.rendition.Constants.RENDITION_SOURCE_VERSIONABLE_ID_PROPERTY;
019
020import java.io.Serializable;
021import java.util.ArrayList;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025
026import org.nuxeo.ecm.core.api.CoreSession;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.IdRef;
029import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner;
030import org.nuxeo.ecm.core.api.event.CoreEventConstants;
031import org.nuxeo.ecm.core.api.event.DocumentEventCategories;
032import org.nuxeo.ecm.core.event.Event;
033import org.nuxeo.ecm.core.event.EventService;
034import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
035import org.nuxeo.runtime.api.Framework;
036
037/**
038 * Remove proxy to the same stored rendition with a different version.
039 *
040 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
041 */
042public class RenditionsRemover extends UnrestrictedSessionRunner {
043
044    public static final String RENDITION_PROXY_PUBLISHED = "renditionProxyPublished";
045
046    protected final DocumentModel proxy;
047
048    protected RenditionsRemover(DocumentModel source) {
049        super(source.getCoreSession());
050        this.proxy = source;
051    }
052
053    @Override
054    public void run() {
055
056        String targetUUID = (String) proxy.getPropertyValue(RENDITION_SOURCE_VERSIONABLE_ID_PROPERTY);
057
058        String query = "select * from Document where ";
059        query = query + RENDITION_SOURCE_VERSIONABLE_ID_PROPERTY + "='" + targetUUID + "' ";
060
061        query = query + " AND ecm:parentId='" + proxy.getParentRef().toString() + "'";
062
063        List<String> removedProxyIds = new ArrayList<String>();
064        List<DocumentModel> docs = session.query(query);
065
066        // Get removed proxy ids
067        for (DocumentModel doc : docs) {
068            if (!doc.getId().equals(proxy.getId())) {
069                removedProxyIds.add(doc.getId());
070            }
071        }
072
073        // Notify rendition published for copy of relations from removed proxies
074        // to new proxy
075        notifyRenditionPublished(removedProxyIds);
076
077        // Perform remove
078        for (String docId : removedProxyIds) {
079            session.removeDocument(new IdRef(docId));
080        }
081    }
082
083    protected void notifyRenditionPublished(List<String> removedProxyIds) {
084        Map<String, Serializable> options = new HashMap<String, Serializable>();
085        options.put(CoreEventConstants.REPLACED_PROXY_IDS, (Serializable) removedProxyIds);
086        notifyEvent(RENDITION_PROXY_PUBLISHED, proxy, options);
087    }
088
089    protected void notifyEvent(String eventId, DocumentModel doc, Map<String, Serializable> options)
090            {
091        CoreSession session = doc.getCoreSession();
092        DocumentEventContext ctx = new DocumentEventContext(session, session.getPrincipal(), doc);
093        if (options != null) {
094            ctx.setProperties(options);
095        }
096        ctx.setProperty("category", DocumentEventCategories.EVENT_DOCUMENT_CATEGORY);
097        Event event = ctx.newEvent(eventId);
098        getEventService().fireEvent(event);
099    }
100
101    protected EventService getEventService() {
102        return Framework.getLocalService(EventService.class);
103    }
104
105}