001/*
002 * (C) Copyright 2018-2019 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.picture.operation;
020
021import static org.nuxeo.ecm.platform.picture.recompute.RecomputeViewsAction.ACTION_NAME;
022import static org.nuxeo.ecm.platform.picture.recompute.RecomputeViewsAction.PARAM_XPATH;
023
024import org.nuxeo.ecm.automation.OperationContext;
025import org.nuxeo.ecm.automation.core.Constants;
026import org.nuxeo.ecm.automation.core.annotations.Context;
027import org.nuxeo.ecm.automation.core.annotations.Operation;
028import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
029import org.nuxeo.ecm.automation.core.annotations.Param;
030import org.nuxeo.ecm.core.api.CoreSession;
031import org.nuxeo.ecm.core.bulk.BulkService;
032import org.nuxeo.ecm.core.bulk.message.BulkCommand;
033import org.nuxeo.runtime.api.Framework;
034
035/**
036 * Recomputes the picture views of the documents resulting from the provided NXQL query.
037 *
038 * @since 10.3
039 */
040@Operation(id = RecomputePictureViews.ID, category = Constants.CAT_SERVICES, label = "Recompute Picture Views", description = "Recompute the picture views of the documents resulting from the provided NXQL query.", since = "10.3")
041public class RecomputePictureViews {
042
043    public static final String ID = "Picture.RecomputeViews";
044
045    public static final String DEFAULT_QUERY = "SELECT * FROM Document WHERE ecm:mixinType = 'Picture' AND picture:views/*/title IS NULL";
046
047    @Context
048    protected CoreSession session;
049
050    @Context
051    protected OperationContext ctx;
052
053    @Param(name = "query", description = "NXQL query to collect the documents whose picture views to recompute.", values = {
054            DEFAULT_QUERY })
055    protected String query;
056
057    @OperationMethod
058    public void run() {
059        BulkService service = Framework.getService(BulkService.class);
060        String username = ctx.getPrincipal().getName();
061        service.submit(new BulkCommand.Builder(ACTION_NAME, query, username)
062                .param(PARAM_XPATH, "file:content")
063                .build());
064    }
065
066}