001/*
002 * (C) Copyright 2013 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 *     Thomas Roger
018 */
019package org.nuxeo.ecm.platform.picture;
020
021import static org.nuxeo.ecm.core.api.CoreSession.ALLOW_VERSION_WRITE;
022
023import java.io.IOException;
024import java.util.List;
025
026import org.nuxeo.ecm.core.api.Blob;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.IdRef;
029import org.nuxeo.ecm.core.api.model.Property;
030import org.nuxeo.ecm.core.event.Event;
031import org.nuxeo.ecm.core.event.EventService;
032import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
033import org.nuxeo.ecm.core.work.AbstractWork;
034import org.nuxeo.ecm.core.work.api.WorkManager;
035import org.nuxeo.ecm.platform.picture.api.adapters.PictureResourceAdapter;
036import org.nuxeo.runtime.api.Framework;
037import org.nuxeo.runtime.transaction.TransactionHelper;
038
039/**
040 * Work generating the different picture views for a Picture.
041 *
042 * @since 5.7
043 */
044public class PictureViewsGenerationWork extends AbstractWork {
045
046    private static final long serialVersionUID = 1L;
047
048    public static final String CATEGORY_PICTURE_GENERATION = "pictureViewsGeneration";
049
050    public static final String PICTURE_VIEWS_GENERATION_DONE_EVENT = "pictureViewsGenerationDone";
051
052    protected final String xpath;
053
054    public PictureViewsGenerationWork(String repositoryName, String docId, String xpath) {
055        super(repositoryName + ':' + docId + ':' + xpath + ":pictureView");
056        setDocument(repositoryName, docId);
057        this.xpath = xpath;
058    }
059
060    @Override
061    public String getCategory() {
062        return CATEGORY_PICTURE_GENERATION;
063    }
064
065    @Override
066    public String getTitle() {
067        return "Picture views generation";
068    }
069
070    @Override
071    public void work() {
072        setProgress(Progress.PROGRESS_INDETERMINATE);
073        setStatus("Extracting");
074
075        openSystemSession();
076        if (!session.exists(new IdRef(docId))) {
077            setStatus("Nothing to process");
078            return;
079        }
080
081        DocumentModel workingDocument = session.getDocument(new IdRef(docId));
082        Property fileProp = workingDocument.getProperty(xpath);
083        Blob blob = (Blob) fileProp.getValue();
084        if (blob == null) {
085            // do nothing
086            return;
087        }
088
089        String title = workingDocument.getTitle();
090        setStatus("Generating views");
091        try {
092            PictureResourceAdapter picture = workingDocument.getAdapter(PictureResourceAdapter.class);
093            picture.fillPictureViews(blob, blob.getFilename(), title, null);
094        } catch (IOException e) {
095            throw new RuntimeException(e);
096        }
097
098        if (!TransactionHelper.isTransactionActive()) {
099            startTransaction();
100        }
101        openSystemSession();
102        if (!session.exists(new IdRef(docId))) {
103            setStatus("Nothing to process");
104            return;
105        }
106        setStatus("Saving");
107        if (workingDocument.isVersion()) {
108            workingDocument.putContextData(ALLOW_VERSION_WRITE, Boolean.TRUE);
109        }
110        workingDocument.putContextData("disableNotificationService", Boolean.TRUE);
111        workingDocument.putContextData("disableAuditLogger", Boolean.TRUE);
112        session.saveDocument(workingDocument);
113
114        firePictureViewsGenerationDoneEvent(workingDocument);
115
116        setStatus("Done");
117    }
118
119    /**
120     * Fire a {@code PICTURE_VIEWS_GENERATION_DONE_EVENT} event when no other PictureViewsGenerationWork is scheduled
121     * for this document.
122     *
123     * @since 5.8
124     */
125    protected void firePictureViewsGenerationDoneEvent(DocumentModel doc) {
126        WorkManager workManager = Framework.getLocalService(WorkManager.class);
127        List<String> workIds = workManager.listWorkIds(CATEGORY_PICTURE_GENERATION, null);
128        int worksCount = 0;
129        for (String workId : workIds) {
130            if (workId.equals(getId())) {
131                if (++worksCount > 1) {
132                    // another work scheduled
133                    return;
134                }
135            }
136        }
137        DocumentEventContext ctx = new DocumentEventContext(session, session.getPrincipal(), doc);
138        Event event = ctx.newEvent(PICTURE_VIEWS_GENERATION_DONE_EVENT);
139        Framework.getLocalService(EventService.class).fireEvent(event);
140    }
141
142}