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 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 */ 019 020package org.nuxeo.ecm.platform.video.listener; 021 022import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.BEFORE_DOC_UPDATE; 023import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_CREATED; 024import static org.nuxeo.ecm.platform.video.VideoConstants.CTX_FORCE_INFORMATIONS_GENERATION; 025import static org.nuxeo.ecm.platform.video.VideoConstants.TRANSCODED_VIDEOS_PROPERTY; 026import static org.nuxeo.ecm.platform.video.VideoConstants.VIDEO_FACET; 027 028import java.io.IOException; 029 030import org.apache.commons.logging.Log; 031import org.apache.commons.logging.LogFactory; 032import org.nuxeo.ecm.core.api.DocumentModel; 033import org.nuxeo.ecm.core.api.NuxeoException; 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.ecm.core.work.api.WorkManager; 039import org.nuxeo.ecm.platform.video.VideoHelper; 040import org.nuxeo.ecm.platform.video.service.VideoInfoWork; 041import org.nuxeo.runtime.api.Framework; 042 043/** 044 * Light synchronous listener that schedules an asynchronous work to process the video info of a document. 045 * <p> 046 * This {@link VideoInfoWork} will in turn schedule two asynchronous works to process the video storyboard and 047 * conversions. 048 * 049 * @since 5.5 050 */ 051public class VideoChangedListener implements EventListener { 052 053 private static final Log log = LogFactory.getLog(VideoChangedListener.class); 054 055 @Override 056 public void handleEvent(Event event) { 057 EventContext ctx = event.getContext(); 058 if (!(ctx instanceof DocumentEventContext)) { 059 return; 060 } 061 DocumentEventContext docCtx = (DocumentEventContext) ctx; 062 DocumentModel doc = docCtx.getSourceDocument(); 063 String eventName = event.getName(); 064 if (shouldProcess(doc, eventName)) { 065 if (BEFORE_DOC_UPDATE.equals(eventName)) { 066 try { 067 resetProperties(doc); 068 } catch (IOException e) { 069 throw new NuxeoException( 070 String.format("Error while resetting video properties of document %s.", doc), e); 071 } 072 } 073 scheduleAsyncProcessing(doc); 074 } 075 } 076 077 protected boolean shouldProcess(DocumentModel doc, String eventName) { 078 return doc.hasFacet(VIDEO_FACET) && !doc.isProxy() 079 && (Boolean.TRUE.equals(doc.getContextData(CTX_FORCE_INFORMATIONS_GENERATION)) 080 || DOCUMENT_CREATED.equals(eventName) || doc.getProperty("file:content").isDirty()); 081 } 082 083 protected void resetProperties(DocumentModel doc) throws IOException { 084 log.debug(String.format("Resetting video info, storyboard, previews and conversions of document %s.", doc)); 085 VideoHelper.updateVideoInfo(doc, null); 086 VideoHelper.updateStoryboard(doc, null); 087 VideoHelper.updatePreviews(doc, null); 088 doc.setPropertyValue(TRANSCODED_VIDEOS_PROPERTY, null); 089 } 090 091 protected void scheduleAsyncProcessing(DocumentModel doc) { 092 WorkManager workManager = Framework.getService(WorkManager.class); 093 VideoInfoWork work = new VideoInfoWork(doc.getRepositoryName(), doc.getId()); 094 log.debug(String.format("Scheduling work: video info of document %s.", doc)); 095 workManager.schedule(work, true); 096 } 097 098}