001/*
002 * (C) Copyright 2006-2016 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 *     Tiago Cardoso <tcardoso@nuxeo.com>
018 */
019package org.nuxeo.ecm.platform.threed.listener;
020
021import org.nuxeo.ecm.core.api.Blob;
022import org.nuxeo.ecm.core.api.DocumentModel;
023import org.nuxeo.ecm.core.api.model.Property;
024import org.nuxeo.ecm.core.event.Event;
025import org.nuxeo.ecm.core.event.EventContext;
026import org.nuxeo.ecm.core.event.EventListener;
027import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
028import org.nuxeo.ecm.platform.threed.service.ThreeDService;
029import org.nuxeo.runtime.api.Framework;
030
031import static org.nuxeo.ecm.platform.threed.ThreeDConstants.THREED_FACET;
032
033/**
034 * Listener cleaning batch date when batch update is scheduled.
035 *
036 * @since 8.4
037 */
038public class ThreeDBatchCleanerListener implements EventListener {
039
040    public static final String GENERATE_BATCH_DATA = "generateBatchData";
041
042    @Override
043    public void handleEvent(Event event) {
044        EventContext ctx = event.getContext();
045        DocumentEventContext docCtx = (DocumentEventContext) ctx;
046        DocumentModel doc = docCtx.getSourceDocument();
047        if (doc.hasFacet(THREED_FACET) && !doc.isProxy()) {
048            Property origThreeDProperty = doc.getProperty("file:content");
049            Blob threedMain = (Blob) origThreeDProperty.getValue();
050            if ((origThreeDProperty.isDirty() || doc.getProperty("files:files").isDirty()) && threedMain != null) {
051                ThreeDService threeDService = Framework.getService(ThreeDService.class);
052                threeDService.cleanBatchData(doc);
053                docCtx.setProperty(GENERATE_BATCH_DATA, true);
054            }
055        }
056    }
057}