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 */
019
020package org.nuxeo.ecm.platform.picture.recompute;
021
022import org.apache.commons.lang.StringUtils;
023import org.jboss.seam.ScopeType;
024import org.jboss.seam.annotations.In;
025import org.jboss.seam.annotations.Name;
026import org.jboss.seam.annotations.Scope;
027import org.jboss.seam.core.Events;
028import org.jboss.seam.faces.FacesMessages;
029import org.jboss.seam.international.StatusMessage;
030import org.nuxeo.ecm.core.api.CoreSession;
031import org.nuxeo.ecm.core.api.DocumentModel;
032import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
033import org.nuxeo.ecm.core.work.api.WorkManager;
034import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
035import org.nuxeo.ecm.webapp.contentbrowser.DocumentActions;
036import org.nuxeo.ecm.webapp.helpers.EventNames;
037import org.nuxeo.runtime.api.Framework;
038
039import java.io.Serializable;
040
041import static org.nuxeo.ecm.platform.picture.api.ImagingDocumentConstants.PICTURE_FACET;
042
043@Name("imagingRecomputeActions")
044@Scope(ScopeType.CONVERSATION)
045public class ImagingRecomputeActions implements Serializable {
046
047    private static final long serialVersionUID = 1L;
048
049    public static final String DEFAULT_NXQL_QUERY = "SELECT * FROM Document WHERE ecm:mixinType = 'Picture' AND picture:views/*/title IS NULL";
050
051    @In(create = true, required = false)
052    protected transient CoreSession documentManager;
053
054    @In(create = true)
055    protected transient NavigationContext navigationContext;
056
057    @In(create = true)
058    protected transient DocumentActions documentActions;
059
060    @In(create = true, required = false)
061    protected FacesMessages facesMessages;
062
063    protected String nxqlQuery = DEFAULT_NXQL_QUERY;
064
065    public String getNxqlQuery() {
066        return nxqlQuery;
067    }
068
069    public void setNxqlQuery(String nxqlQuery) {
070        this.nxqlQuery = nxqlQuery;
071    }
072
073    public void recomputePictureViews() {
074        recomputePictureViews(navigationContext.getCurrentDocument());
075    }
076
077    public void recomputePictureViews(DocumentModel doc) {
078        if (doc.hasFacet(PICTURE_FACET)) {
079            BlobHolder blobHolder = doc.getAdapter(BlobHolder.class);
080            if (blobHolder.getBlob() != null) {
081                blobHolder.setBlob(blobHolder.getBlob());
082                Events.instance().raiseEvent(EventNames.BEFORE_DOCUMENT_CHANGED, doc);
083                documentManager.saveDocument(doc);
084                documentManager.save();
085                navigationContext.invalidateCurrentDocument();
086            }
087            facesMessages.addFromResourceBundle(StatusMessage.Severity.INFO, "label.imaging.recompute.views.done");
088        }
089    }
090
091    public void launchPictureViewsRecomputation() {
092        WorkManager workManager = Framework.getLocalService(WorkManager.class);
093        if (workManager == null) {
094            throw new RuntimeException("No WorkManager available");
095        }
096
097        if (!StringUtils.isBlank(nxqlQuery)) {
098            ImagingRecomputeWork work = new ImagingRecomputeWork(documentManager.getRepositoryName(), nxqlQuery);
099            workManager.schedule(work, WorkManager.Scheduling.IF_NOT_RUNNING_OR_SCHEDULED);
100
101            facesMessages.addFromResourceBundle(StatusMessage.Severity.INFO, "label.imaging.recompute.work.launched");
102        }
103
104    }
105}