001/*
002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Thomas Roger
016 */
017
018package org.nuxeo.ecm.imaging.recompute;
019
020import static org.nuxeo.ecm.platform.picture.api.ImagingDocumentConstants.PICTURE_FACET;
021
022import java.io.Serializable;
023
024import org.apache.commons.lang.StringUtils;
025import org.jboss.seam.ScopeType;
026import org.jboss.seam.annotations.In;
027import org.jboss.seam.annotations.Name;
028import org.jboss.seam.annotations.Scope;
029import org.jboss.seam.core.Events;
030import org.jboss.seam.faces.FacesMessages;
031import org.jboss.seam.international.StatusMessage;
032import org.nuxeo.ecm.core.api.CoreSession;
033import org.nuxeo.ecm.core.api.DocumentModel;
034import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
035import org.nuxeo.ecm.core.work.api.WorkManager;
036import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
037import org.nuxeo.ecm.webapp.contentbrowser.DocumentActions;
038import org.nuxeo.ecm.webapp.helpers.EventNames;
039import org.nuxeo.runtime.api.Framework;
040
041@Name("imagingRecomputeActions")
042@Scope(ScopeType.CONVERSATION)
043public class ImagingRecomputeActions implements Serializable {
044
045    private static final long serialVersionUID = 1L;
046
047    public static final String DEFAULT_NXQL_QUERY = "SELECT * FROM Document WHERE ecm:mixinType = 'Picture' AND picture:views/*/title IS NULL";
048
049    @In(create = true, required = false)
050    protected transient CoreSession documentManager;
051
052    @In(create = true)
053    protected transient NavigationContext navigationContext;
054
055    @In(create = true)
056    protected transient DocumentActions documentActions;
057
058    @In(create = true, required = false)
059    protected FacesMessages facesMessages;
060
061    protected String nxqlQuery = DEFAULT_NXQL_QUERY;
062
063    public String getNxqlQuery() {
064        return nxqlQuery;
065    }
066
067    public void setNxqlQuery(String nxqlQuery) {
068        this.nxqlQuery = nxqlQuery;
069    }
070
071    public void recomputePictureViews() {
072        recomputePictureViews(navigationContext.getCurrentDocument());
073    }
074
075    public void recomputePictureViews(DocumentModel doc) {
076        if (doc.hasFacet(PICTURE_FACET)) {
077            BlobHolder blobHolder = doc.getAdapter(BlobHolder.class);
078            if (blobHolder.getBlob() != null) {
079                blobHolder.setBlob(blobHolder.getBlob());
080                Events.instance().raiseEvent(EventNames.BEFORE_DOCUMENT_CHANGED, doc);
081                documentManager.saveDocument(doc);
082                documentManager.save();
083                navigationContext.invalidateCurrentDocument();
084            }
085            facesMessages.addFromResourceBundle(StatusMessage.Severity.INFO, "label.imaging.recompute.views.done");
086        }
087    }
088
089    public void launchPictureViewsRecomputation() {
090        WorkManager workManager = Framework.getLocalService(WorkManager.class);
091        if (workManager == null) {
092            throw new RuntimeException("No WorkManager available");
093        }
094
095        if (!StringUtils.isBlank(nxqlQuery)) {
096            ImagingRecomputeWork work = new ImagingRecomputeWork(documentManager.getRepositoryName(), nxqlQuery);
097            workManager.schedule(work, WorkManager.Scheduling.IF_NOT_RUNNING_OR_SCHEDULED);
098
099            facesMessages.addFromResourceBundle(StatusMessage.Severity.INFO, "label.imaging.recompute.work.launched");
100        }
101
102    }
103}