001/* 002 * (C) Copyright 2014 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 * Vladimir Pasquier <vpasquier@nuxeo.com> 016 */ 017 018package org.nuxeo.binary.metadata.internals.listeners; 019 020import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.ABOUT_TO_CREATE; 021import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.BEFORE_DOC_UPDATE; 022 023import org.nuxeo.binary.metadata.api.BinaryMetadataConstants; 024import org.nuxeo.binary.metadata.api.BinaryMetadataService; 025import org.nuxeo.ecm.core.api.DocumentModel; 026import org.nuxeo.ecm.core.event.Event; 027import org.nuxeo.ecm.core.event.EventContext; 028import org.nuxeo.ecm.core.event.EventListener; 029import org.nuxeo.ecm.core.event.impl.DocumentEventContext; 030import org.nuxeo.runtime.api.Framework; 031 032/** 033 * Handle document and blob updates according to following rules in an event context: - Define if rule should be 034 * executed in async or sync mode. - If Blob dirty and document metadata dirty, write metadata from doc to Blob. - If 035 * Blob dirty and document metadata not dirty, write metadata from Blob to doc. - If Blob not dirty and document 036 * metadata dirty, write metadata from doc to Blob. 037 * 038 * @since 7.1 039 */ 040public class BinaryMetadataSyncListener 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 BinaryMetadataService binaryMetadataService = Framework.getLocalService(BinaryMetadataService.class); 049 DocumentEventContext docCtx = (DocumentEventContext) ctx; 050 DocumentModel doc = docCtx.getSourceDocument(); 051 Boolean disable = (Boolean) event.getContext().getProperty( 052 BinaryMetadataConstants.DISABLE_BINARY_METADATA_LISTENER); 053 if (ABOUT_TO_CREATE.equals(event.getName()) && !doc.isProxy() && disable == null) { 054 binaryMetadataService.writeMetadata(doc, docCtx.getCoreSession()); 055 } else if (BEFORE_DOC_UPDATE.equals(event.getName()) && !doc.isProxy() && disable == null) { 056 doc.putContextData(BinaryMetadataConstants.DISABLE_BINARY_METADATA_LISTENER, Boolean.TRUE); 057 binaryMetadataService.handleSyncUpdate(doc, docCtx); 058 } 059 } 060 061}