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