package org.nuxeo.ecm.platform.picture;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;

import org.nuxeo.ecm.core.api.Blob;
import org.nuxeo.ecm.core.api.DocumentLocation;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.IdRef;
import org.nuxeo.ecm.core.api.impl.DocumentLocationImpl;
import org.nuxeo.ecm.core.api.model.Property;
import org.nuxeo.ecm.core.work.AbstractWork;
import org.nuxeo.ecm.platform.picture.api.adapters.PictureResourceAdapter;

/**
 * Work generating the different picture views for a Picture.
 *
 * @since 5.7
 */
public class PictureViewsGenerationWork extends AbstractWork {

    private static final long serialVersionUID = 1L;

    public static final String CATEGORY_PICTURE_GENERATION = "pictureViewsGeneration";

    protected final String xpath;

    public PictureViewsGenerationWork(String repositoryName,
            String docId, String xpath) {
        super(repositoryName + ':' + docId + ':' + xpath + ":pictureView");
        setDocument(repositoryName, docId);
        this.xpath = xpath;
    }

    @Override
    public String getCategory() {
        return CATEGORY_PICTURE_GENERATION;
    }

    @Override
    public String getTitle() {
        return "Picture views generation";
    }

    @Override
    public void work() throws Exception {
        DocumentModel workingDocument = null;

        setProgress(Progress.PROGRESS_INDETERMINATE);
        setStatus("Extracting");
        try {
            initSession();
            workingDocument = session.getDocument(new IdRef(docId));
            if (workingDocument != null) {
                workingDocument.detach(true);
            }
            commitOrRollbackTransaction();
        } finally {
            cleanUp(true, null);
        }

        if (workingDocument != null) {
            setStatus("Generating views");
            Property fileProp = workingDocument.getProperty(xpath);
            ArrayList<Map<String, Object>> pictureTemplates = null;
            PictureResourceAdapter picture = workingDocument.getAdapter(PictureResourceAdapter.class);
            Blob blob = (Blob) fileProp.getValue();
            String filename = blob == null ? null : blob.getFilename();
            String title = workingDocument.getTitle();
            picture.fillPictureViews(blob, filename, title, pictureTemplates);
        } else {
            setStatus("Nothing to process");
            return;
        }

        startTransaction();
        setStatus("Saving");
        initSession();
        session.saveDocument(workingDocument);
        setStatus("Done");
    }

}

@@ -83,48 +83,12 @@
 til.
-Collection;%0Aimport java.util.Collections
+List
 ;%0Aim
@@ -174,28 +174,27 @@
 ore.api.
-DocumentLoca
+ClientExcep
 tion;%0Aim
@@ -307,124 +307,259 @@
 api.
-impl.DocumentLocationImpl;%0Aimport org.nuxeo.ecm.core.api.model.Property;%0Aimport org.nuxeo.ecm.core.work.AbstractWork
+model.Property;%0Aimport org.nuxeo.ecm.core.event.Event;%0Aimport org.nuxeo.ecm.core.event.EventService;%0Aimport org.nuxeo.ecm.core.event.impl.DocumentEventContext;%0Aimport org.nuxeo.ecm.core.work.AbstractWork;%0Aimport org.nuxeo.ecm.core.work.api.WorkManager
 ;%0Aim
@@ -630,16 +630,56 @@
 Adapter;
+%0Aimport org.nuxeo.runtime.api.Framework;
 %0A%0A/**%0A *
@@ -965,16 +965,116 @@
 tion%22;%0A%0A
+    public static final String PICTURE_VIEWS_GENERATION_DONE_EVENT = %22pictureViewsGenerationDone%22;%0A%0A
     prot
@@ -1160,28 +1160,16 @@
 oryName,
-%0A           
  String 
@@ -1174,16 +1174,28 @@
 g docId,
+%0A           
  String 
@@ -2865,32 +2865,1113 @@
 t);%0A
-        setStatus(%22Done%22
+%0A        firePictureViewsGenerationDoneEvent(workingDocument);%0A%0A        setStatus(%22Done%22);%0A    %7D%0A%0A    /**%0A     * Fire a %7B@code PICTURE_VIEWS_GENERATION_DONE_EVENT%7D event when no other%0A     * PictureViewsGenerationWork is scheduled for this document.%0A     *%0A     * @since 5.8%0A     */%0A    protected void firePictureViewsGenerationDoneEvent(DocumentModel doc)%0A            throws ClientException %7B%0A        WorkManager workManager = Framework.getLocalService(WorkManager.class);%0A        List%3CString%3E workIds = workManager.listWorkIds(%0A                CATEGORY_PICTURE_GENERATION, null);%0A        int worksCount = 0;%0A        for (String workId : workIds) %7B%0A            if (workId.equals(getId())) %7B%0A                if (worksCount++ %3E 1) %7B%0A                    // another work scheduled%0A                    return;%0A                %7D%0A            %7D%0A        %7D%0A        DocumentEventContext ctx = new DocumentEventContext(session,%0A                session.getPrincipal(), doc);%0A        Event event = ctx.newEvent(PICTURE_VIEWS_GENERATION_DONE_EVENT);%0A        Framework.getLocalService(EventService.class).fireEvent(event
 );%0A 

send a 'pictureViewsGenerationDone' event when there is no more PictureViewsGenerationWork scheduled on the document