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