001/*
002 * (C) Copyright 2018 Nuxeo (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 *     Antoine Taillefer <ataillefer@nuxeo.com>
018 */
019package org.nuxeo.ecm.platform.thumbnail.work;
020
021import org.nuxeo.ecm.core.api.DocumentModel;
022import org.nuxeo.ecm.core.api.DocumentModelList;
023import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
024import org.nuxeo.ecm.core.work.AbstractWork;
025import org.nuxeo.ecm.platform.thumbnail.ThumbnailConstants;
026import org.nuxeo.ecm.platform.thumbnail.action.RecomputeThumbnailsAction;
027
028/**
029 * Work to recompute the thumbnail of the documents resulting from the provided NXQL query.
030 *
031 * @since 10.10
032 * @deprecated since 11.1. Use {@link RecomputeThumbnailsAction} instead
033 */
034@Deprecated
035public class ThumbnailRecomputeWork extends AbstractWork {
036
037    private static final long serialVersionUID = 1L;
038
039    protected static final int BATCH_SIZE = 10;
040
041    protected String nxqlQuery;
042
043    public ThumbnailRecomputeWork(String repositoryName, String nxqlQuery) {
044        this.repositoryName = repositoryName;
045        this.nxqlQuery = nxqlQuery;
046    }
047
048    @Override
049    public String getTitle() {
050        return "Thumbnails Recomputation";
051    }
052
053    @Override
054    public void work() {
055        setProgress(Progress.PROGRESS_INDETERMINATE);
056
057        openSystemSession();
058        DocumentModelList docs = session.query(nxqlQuery);
059        long docsUpdated = 0;
060
061        setStatus("Generating thumbnails");
062        for (DocumentModel doc : docs) {
063            if (doc.hasFacet(ThumbnailConstants.THUMBNAIL_FACET)) {
064                BlobHolder blobHolder = doc.getAdapter(BlobHolder.class);
065                if (blobHolder.getBlob() != null) {
066                    // make sure the blob property is dirty for the CheckBlobUpdateListener to trigger the
067                    // UpdateThumbnailListener
068                    blobHolder.setBlob(blobHolder.getBlob());
069                    session.saveDocument(doc);
070                    docsUpdated++;
071                    if (docsUpdated % BATCH_SIZE == 0) {
072                        commitOrRollbackTransaction();
073                        startTransaction();
074                    }
075                }
076            }
077        }
078        setStatus("Done");
079    }
080
081}