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