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:glefevre@nuxeo.com">Gildas</a>
016 */
017package org.nuxeo.ecm.platform.suggestbox.automation;
018
019import java.util.List;
020
021import net.sf.json.JSONArray;
022import net.sf.json.JSONObject;
023
024import org.nuxeo.ecm.automation.core.Constants;
025import org.nuxeo.ecm.automation.core.annotations.Context;
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.core.api.CoreSession;
032import org.nuxeo.ecm.platform.suggestbox.service.Suggestion;
033import org.nuxeo.ecm.platform.suggestbox.service.SuggestionContext;
034import org.nuxeo.ecm.platform.suggestbox.service.SuggestionException;
035import org.nuxeo.ecm.platform.suggestbox.service.SuggestionService;
036
037/**
038 * Operation used to suggest result by getting and calling all the suggesters defined in contributions.
039 *
040 * @since 6.0
041 */
042@Operation(id = SuggestOperation.ID, category = Constants.CAT_UI, label = "Suggesters launcher", description = "Get and launch the suggesters defined and return a list of Suggestion objects.", addToStudio = false)
043public class SuggestOperation {
044
045    public static final String ID = "Search.SuggestersLauncher";
046
047    private static final String SUGGESTER_GROUP = "searchbox";
048
049    @Context
050    protected CoreSession session;
051
052    @Context
053    protected SuggestionService serviceSuggestion;
054
055    @Param(name = "searchTerm", required = false)
056    protected String searchTerm;
057
058    @OperationMethod
059    public Blob run() throws SuggestionException {
060        JSONArray result = new JSONArray();
061
062        SuggestionContext suggestionContext = new SuggestionContext(SUGGESTER_GROUP, session.getPrincipal());
063        suggestionContext.withSession(session);
064
065        List<Suggestion> listSuggestions = serviceSuggestion.suggest(searchTerm, suggestionContext);
066
067        // For each suggestion, create a JSON object and add it to the result
068        for (Suggestion suggestion : listSuggestions) {
069            JSONObject suggestionJSON = new JSONObject();
070            suggestionJSON.put("id", suggestion.getId());
071            suggestionJSON.put("label", suggestion.getLabel());
072            suggestionJSON.put("type", suggestion.getType());
073            suggestionJSON.put("icon", suggestion.getIconURL());
074            suggestionJSON.put("url", suggestion.getObjectUrl());
075
076            result.add(suggestionJSON);
077        }
078
079        return Blobs.createBlob(result.toString(), "application/json");
080    }
081}