001/*
002 * (C) Copyright 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 *     Nour Al Kotob
018 */
019
020package org.nuxeo.ecm.restapi.server.jaxrs.management;
021
022import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
023import static org.nuxeo.ecm.core.api.security.SecurityConstants.SYSTEM_USERNAME;
024
025import javax.ws.rs.FormParam;
026import javax.ws.rs.POST;
027import javax.ws.rs.Path;
028import javax.ws.rs.Produces;
029
030import org.apache.commons.lang3.StringUtils;
031import org.nuxeo.ecm.core.bulk.BulkService;
032import org.nuxeo.ecm.core.bulk.message.BulkCommand;
033import org.nuxeo.ecm.core.bulk.message.BulkStatus;
034import org.nuxeo.ecm.platform.thumbnail.action.RecomputeThumbnailsAction;
035import org.nuxeo.ecm.webengine.model.WebObject;
036import org.nuxeo.ecm.webengine.model.impl.AbstractResource;
037import org.nuxeo.ecm.webengine.model.impl.ResourceTypeImpl;
038import org.nuxeo.runtime.api.Framework;
039
040/**
041 * @since 11.3
042 */
043@WebObject(type = ManagementObject.MANAGEMENT_OBJECT_PREFIX + "thumbnails")
044@Produces(APPLICATION_JSON)
045public class ThumbnailsObject extends AbstractResource<ResourceTypeImpl> {
046
047    public static final String THUMBNAILS_DEFAULT_QUERY = "SELECT * FROM Document WHERE ecm:mixinType = 'Thumbnail' AND thumb:thumbnail/data IS NULL AND ecm:isVersion = 0 AND ecm:isProxy = 0 AND ecm:isTrashed = 0";
048
049    /**
050     * Recomputes the thumbnail for the documents matching the given query or {@link #THUMBNAILS_DEFAULT_QUERY} if not
051     * provided.
052     *
053     * @param query a custom query to specify which thumbnails should be processesd
054     * @return the {@link BulkStatus} of the command
055     */
056    @POST
057    @Path("recompute")
058    public BulkStatus doPostThumbnails(@FormParam("query") String query) {
059        final String finalQuery = StringUtils.defaultIfBlank(query, THUMBNAILS_DEFAULT_QUERY);
060        BulkService bulkService = Framework.getService(BulkService.class);
061        String commandId = bulkService.submit(
062                new BulkCommand.Builder(RecomputeThumbnailsAction.ACTION_NAME, finalQuery, SYSTEM_USERNAME).repository(
063                        ctx.getCoreSession().getRepositoryName()).build());
064        return bulkService.getStatus(commandId);
065    }
066
067}