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