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