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