001/*
002 * (C) Copyright 2014 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 *     <a href="mailto:glefevre@nuxeo.com">Gildas</a>
018 */
019package org.nuxeo.ecm.platform.suggestbox.automation;
020
021import java.io.IOException;
022import java.util.ArrayList;
023import java.util.LinkedHashMap;
024import java.util.List;
025import java.util.Map;
026import java.util.Map.Entry;
027
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.platform.suggestbox.service.Suggestion;
037import org.nuxeo.ecm.platform.suggestbox.service.SuggestionContext;
038import org.nuxeo.ecm.platform.suggestbox.service.SuggestionException;
039import org.nuxeo.ecm.platform.suggestbox.service.SuggestionService;
040
041/**
042 * Operation used to suggest result by getting and calling all the suggesters defined in contributions.
043 *
044 * @since 6.0
045 */
046@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)
047public class SuggestOperation {
048
049    public static final String ID = "Search.SuggestersLauncher";
050
051    private static final String SUGGESTER_GROUP = "searchbox";
052
053    @Context
054    protected CoreSession session;
055
056    @Context
057    protected SuggestionService serviceSuggestion;
058
059    @Param(name = "searchTerm", required = false)
060    protected String searchTerm;
061
062    @OperationMethod
063    public Blob run() throws SuggestionException, IOException {
064        List<Map<String, Object>> result = new ArrayList<>();
065
066        SuggestionContext suggestionContext = new SuggestionContext(SUGGESTER_GROUP, session.getPrincipal());
067        suggestionContext.withSession(session);
068
069        List<Suggestion> listSuggestions = serviceSuggestion.suggest(searchTerm, suggestionContext);
070
071        // For each suggestion, create a JSON object and add it to the result
072        for (Suggestion suggestion : listSuggestions) {
073            Map<String, Object> suggestionJSON = new LinkedHashMap<>();
074            suggestionJSON.put("id", suggestion.getId());
075            suggestionJSON.put("label", suggestion.getLabel());
076            suggestionJSON.put("type", suggestion.getType());
077            suggestionJSON.put("icon", suggestion.getIconURL());
078            suggestionJSON.put("thumbnailUrl", suggestion.getThumbnailURL());
079            suggestionJSON.put("url", suggestion.getObjectUrl());
080
081            List<Map<String, Object>> highlights = new ArrayList<>();
082            if (suggestion.getHighlights() != null) {
083                for (Entry<String, List<String>> e : suggestion.getHighlights().entrySet()) {
084                    Map<String, Object> h = new LinkedHashMap<>();
085                    h.put("field", e.getKey());
086                    h.put("segments", e.getValue());
087                    highlights.add(h);
088                }
089            }
090            suggestionJSON.put("highlights", highlights);
091
092            result.add(suggestionJSON);
093        }
094
095        return Blobs.createJSONBlobFromValue(result);
096    }
097}