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