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.operation;
020
021import org.nuxeo.ecm.automation.OperationContext;
022import org.nuxeo.ecm.automation.core.Constants;
023import org.nuxeo.ecm.automation.core.annotations.Context;
024import org.nuxeo.ecm.automation.core.annotations.Operation;
025import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
026import org.nuxeo.ecm.automation.core.annotations.Param;
027import org.nuxeo.ecm.core.bulk.BulkService;
028import org.nuxeo.ecm.core.bulk.message.BulkCommand;
029import org.nuxeo.ecm.platform.thumbnail.action.RecomputeThumbnailsAction;
030import org.nuxeo.runtime.api.Framework;
031
032/**
033 * Recomputes the thumbnail of the documents resulting from the provided NXQL query.
034 *
035 * @since 10.10
036 */
037@Operation(id = RecomputeThumbnails.ID, category = Constants.CAT_SERVICES, label = "Recompute Thumbnails", description = "Recompute the thumbnail of the documents resulting from the provided NXQL query.", since = "10.10")
038public class RecomputeThumbnails {
039
040    /**
041     * @since 11.1
042     */
043    protected static final String THUMBNAIL_XPATH = "thumb:thumbnail/data";
044
045    public static final String ID = "RecomputeThumbnails";
046
047    public static final String DEFAULT_QUERY = "SELECT * FROM Document WHERE ecm:mixinType = 'Thumbnail' AND "
048            + THUMBNAIL_XPATH + " IS NULL AND ecm:isVersion = 0 AND ecm:isProxy = 0 AND ecm:isTrashed = 0";
049
050    /**
051     * @since 11.1
052     */
053    @Context
054    protected OperationContext ctx;
055
056    @Param(name = "query", description = "NXQL query to collect the documents whose thumnail to recompute.", values = {
057            DEFAULT_QUERY })
058    protected String query;
059
060    @OperationMethod
061    public void run() {
062        BulkService service = Framework.getService(BulkService.class);
063        String username = ctx.getPrincipal().getName();
064        service.submit(new BulkCommand.Builder(RecomputeThumbnailsAction.ACTION_NAME, query, username).build());
065    }
066
067}