001/*
002 * (C) Copyright 2016 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 */
018package org.nuxeo.ecm.platform.rendition.automation;
019
020import java.util.Comparator;
021import java.util.List;
022import java.util.stream.Collectors;
023
024import org.apache.commons.lang3.StringUtils;
025import org.nuxeo.ecm.automation.core.Constants;
026import org.nuxeo.ecm.automation.core.annotations.Operation;
027import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
028import org.nuxeo.ecm.automation.core.annotations.Param;
029import org.nuxeo.ecm.core.api.Blob;
030import org.nuxeo.ecm.core.api.Blobs;
031import org.nuxeo.ecm.platform.rendition.service.RenditionDefinition;
032import org.nuxeo.ecm.platform.rendition.service.RenditionService;
033import org.nuxeo.ecm.platform.ui.select2.common.Select2Common;
034import org.nuxeo.runtime.api.Framework;
035
036import net.sf.json.JSONArray;
037import net.sf.json.JSONObject;
038
039/**
040 * @since 8.3
041 */
042@Operation(id = SuggestRenditionDefinitionEntry.ID, category = Constants.CAT_SERVICES, label = "Get rendition definition suggestion", description = "Get rendition definition suggestion")
043public class SuggestRenditionDefinitionEntry {
044
045    public static final String ID = "RenditionDefinition.Suggestion";
046
047    public static final int MAX_SUGGESTIONS = 10;
048
049    public static final Comparator<RenditionDefinition> LABEL_COMPARATOR = new RenditionDefinitionComparator();
050
051    @Param(name = "searchTerm", required = false)
052    protected String searchTerm;
053
054    @OperationMethod
055    public Blob run() {
056        JSONArray result = new JSONArray();
057        if (!StringUtils.isBlank(searchTerm)) {
058            for (RenditionDefinition def : getSuggestions(searchTerm)) {
059                JSONObject obj = new JSONObject();
060                String name = def.getName();
061                obj.element(Select2Common.ID, name);
062                obj.element(Select2Common.LABEL, name);
063                result.add(obj);
064            }
065        }
066        return Blobs.createJSONBlob(result.toString());
067    }
068
069    protected List<RenditionDefinition> getSuggestions(String searchTerm) {
070        RenditionService renditionService = Framework.getService(RenditionService.class);
071        List<RenditionDefinition> defs = renditionService.getDeclaredRenditionDefinitions();
072        return defs.stream().filter(def -> def.getName().startsWith(searchTerm)).sorted(LABEL_COMPARATOR).limit(
073                MAX_SUGGESTIONS).collect(Collectors.toList());
074    }
075
076    protected static class RenditionDefinitionComparator implements Comparator<RenditionDefinition> {
077        @Override
078        public int compare(RenditionDefinition o1, RenditionDefinition o2) {
079            return o1.getName().compareToIgnoreCase(o2.getName());
080        }
081    }
082
083}