001/*
002 * (C) Copyright 2014-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 *     <a href="mailto:grenard@nuxeo.com">Guillaume Renard</a>
018 */
019package org.nuxeo.ecm.platform.tag.automation;
020
021import java.io.IOException;
022import java.util.ArrayList;
023import java.util.Collections;
024import java.util.LinkedHashMap;
025import java.util.List;
026import java.util.Map;
027
028import org.apache.commons.lang3.StringUtils;
029import org.nuxeo.ecm.automation.OperationContext;
030import org.nuxeo.ecm.automation.core.Constants;
031import org.nuxeo.ecm.automation.core.annotations.Context;
032import org.nuxeo.ecm.automation.core.annotations.Operation;
033import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
034import org.nuxeo.ecm.automation.core.annotations.Param;
035import org.nuxeo.ecm.automation.features.SuggestConstants;
036import org.nuxeo.ecm.core.api.Blob;
037import org.nuxeo.ecm.core.api.Blobs;
038import org.nuxeo.ecm.core.api.CoreSession;
039import org.nuxeo.ecm.core.api.DocumentModel;
040import org.nuxeo.ecm.platform.tag.TagService;
041
042/**
043 * @since 6.0
044 */
045@Operation(id = SuggestTagEntry.ID, category = Constants.CAT_SERVICES, label = "Get tag suggestion", description = "Get tag suggestion")
046public class SuggestTagEntry {
047
048    public static final String ID = "Tag.Suggestion";
049
050    @Context
051    protected OperationContext ctx;
052
053    @Context
054    protected CoreSession documentManager;
055
056    @Context
057    protected TagService tagService;
058
059    @Param(name = "searchTerm", required = false)
060    protected String searchTerm;
061
062    @Param(name = "value", required = false)
063    protected String value;
064
065    @Param(name = "xpath", required = false)
066    protected String xpath;
067
068    @Param(name = "document", required = false)
069    protected DocumentModel doc;
070
071    @OperationMethod
072    public Blob run() throws IOException {
073        List<Map<String, String>> result = new ArrayList<>();
074        if (tagService != null && tagService.isEnabled()) {
075            if (!StringUtils.isEmpty(value)) {
076                if (doc == null) {
077                    return null;
078                } else {
079                    String docId = doc.getId();
080                    List<String> tags = new ArrayList<>(tagService.getTags(documentManager, docId));
081                    Collections.sort(tags);
082                    for (String tag : tags) {
083                        Map<String, String> obj = new LinkedHashMap<>();
084                        obj.put(SuggestConstants.ID, tag);
085                        obj.put(SuggestConstants.LABEL, tag);
086                        result.add(obj);
087                    }
088                }
089            } else {
090                if (!StringUtils.isBlank(searchTerm)) {
091                    List<String> tags = new ArrayList<>(tagService.getSuggestions(documentManager, searchTerm));
092                    Collections.sort(tags);
093                    for (int i = 0; i < 10 && i < tags.size(); i++) {
094                        Map<String, String> obj = new LinkedHashMap<>();
095                        String tag = tags.get(i);
096                        obj.put(SuggestConstants.ID, tag);
097                        obj.put(SuggestConstants.LABEL, tag);
098                        result.add(obj);
099                    }
100                }
101            }
102        }
103        return Blobs.createJSONBlobFromValue(result);
104    }
105
106}