001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     <a href="mailto:grenard@nuxeo.com">Guillaume Renard</a>
016 */
017package org.nuxeo.ecm.platform.tag.automation;
018
019import java.util.Collections;
020import java.util.List;
021
022import net.sf.json.JSONArray;
023import net.sf.json.JSONObject;
024
025import org.apache.commons.lang.StringUtils;
026import org.jboss.seam.Component;
027import org.nuxeo.ecm.automation.OperationContext;
028import org.nuxeo.ecm.automation.core.Constants;
029import org.nuxeo.ecm.automation.core.annotations.Context;
030import org.nuxeo.ecm.automation.core.annotations.Operation;
031import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
032import org.nuxeo.ecm.automation.core.annotations.Param;
033import org.nuxeo.ecm.core.api.Blob;
034import org.nuxeo.ecm.core.api.Blobs;
035import org.nuxeo.ecm.core.api.CoreSession;
036import org.nuxeo.ecm.core.api.DocumentModel;
037import org.nuxeo.ecm.platform.tag.Tag;
038import org.nuxeo.ecm.platform.tag.TagService;
039import org.nuxeo.ecm.platform.ui.select2.common.Select2Common;
040import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
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    @OperationMethod
069    public Blob run() {
070        JSONArray result = new JSONArray();
071        if (tagService != null && tagService.isEnabled()) {
072            if (!StringUtils.isEmpty(value)) {
073                final NavigationContext navigationContext = (NavigationContext) Component.getInstance("navigationContext");
074                DocumentModel currentDocument = navigationContext.getCurrentDocument();
075                if (currentDocument == null) {
076                    return null;
077                } else {
078                    String docId = currentDocument.getId();
079                    List<Tag> tags = tagService.getDocumentTags(documentManager, docId, null);
080                    Collections.sort(tags, Tag.LABEL_COMPARATOR);
081                    for (Tag tag : tags) {
082                        JSONObject obj = new JSONObject();
083                        obj.element(Select2Common.ID, tag.getLabel());
084                        obj.element(Select2Common.LABEL, tag.getLabel());
085                        result.add(obj);
086                    }
087                }
088            } else {
089                if (!StringUtils.isBlank(searchTerm)) {
090                    List<Tag> tags = tagService.getSuggestions(documentManager, searchTerm, null);
091                    Collections.sort(tags, Tag.LABEL_COMPARATOR);
092                    for (int i = 0; i < 10 && i < tags.size(); i++) {
093                        JSONObject obj = new JSONObject();
094                        Tag tag = tags.get(i);
095                        obj.element(Select2Common.ID, tag.getLabel());
096                        obj.element(Select2Common.LABEL, tag.getLabel());
097                        result.add(obj);
098                    }
099                }
100            }
101        }
102        return Blobs.createBlob(result.toString(), "application/json");
103    }
104
105}