001/** 002 * Copyright 2014 Tampere University of Technology, Pori Department 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 */ 016package service.tut.pori.contentsuggest; 017 018import java.util.ArrayList; 019import java.util.List; 020 021import javax.xml.bind.annotation.XmlAccessType; 022import javax.xml.bind.annotation.XmlAccessorType; 023import javax.xml.bind.annotation.XmlElement; 024import javax.xml.bind.annotation.XmlElementWrapper; 025import javax.xml.bind.annotation.XmlRootElement; 026 027import org.apache.log4j.Logger; 028import org.apache.solr.client.solrj.response.Group; 029import org.apache.solr.client.solrj.response.GroupCommand; 030import org.apache.solr.client.solrj.response.QueryResponse; 031 032import core.tut.pori.http.ResponseData; 033 034/** 035 * Class for representing a response received from Solr Autocomplete. This class can also be used directly as a http Response payload. 036 * 037 * <h3>XML Example</h3> 038 * 039 * {@doc.restlet service="[service.tut.pori.contentsuggest.reference.Definitions#SERVICE_CS_REFERENCE_EXAMPLE]" method="[service.tut.pori.contentsuggest.Definitions#ELEMENT_AUTOCOMPLETE_RESULTS]" type="GET" query="" body_uri=""} 040 */ 041@XmlRootElement(name=Definitions.ELEMENT_AUTOCOMPLETE_RESULTS) 042@XmlAccessorType(XmlAccessType.NONE) 043public class AutoCompleteResult extends ResponseData { 044 @XmlElement(name=Definitions.ELEMENT_COLLATION) 045 private String _collation = null; 046 @XmlElementWrapper(name=Definitions.ELEMENT_SUGGESTION_LIST) 047 @XmlElement(name=Definitions.ELEMENT_SUGGESTION) 048 private List<String> _suggestionList = null; 049 050 /** 051 * The collection of query strings that are not part of the suggested element. 052 * 053 * @return the collation 054 * @see #setCollation(String) 055 */ 056 public String getCollation() { 057 return _collation; 058 } 059 060 /** 061 * 062 * @param collation 063 */ 064 public void setCollation(String collation) { 065 _collation = collation; 066 } 067 068 /** 069 * 070 * @return the list of suggestions for this auto-complete response 071 * @see #setSuggestionList(List) 072 */ 073 public List<String> getSuggestionList() { 074 return _suggestionList; 075 } 076 077 /** 078 * 079 * @param suggestionList 080 * @see #setSuggestionList(List) 081 */ 082 public void setSuggestionList(List<String> suggestionList) { 083 _suggestionList = suggestionList; 084 } 085 086 /** 087 * Generates a new AutoCompleteResponse based on Solr QueryResponse 088 * @param response 089 * @return the given response converted to AutoCompleteResponse 090 * @see QueryResponse 091 */ 092 protected static AutoCompleteResult fromQueryResponse(QueryResponse response){ 093 Logger.getLogger(AutoCompleteResult.class).debug(response.getResponseHeader().getAll("params")); 094 095 AutoCompleteResult acr = new AutoCompleteResult(); 096 List<String> suggestionList = null; 097 if(response.getGroupResponse().getValues().size() > 0){ 098 suggestionList = new ArrayList<>(); 099 } 100 for(GroupCommand groupResult : response.getGroupResponse().getValues()){ 101 //Logger.getLogger(AutoCompleteResponse.class).debug(groupResult.getName()+" "+groupResult.getMatches()); 102 for(Group group : groupResult.getValues()){ 103 suggestionList.add(group.getGroupValue()); //group.getResult().get(0).getFieldValue("value_s")); 104 } 105 } 106 if(suggestionList != null && suggestionList.size() > 0){ 107 acr.setSuggestionList(suggestionList); 108 } 109 return acr; 110 } 111}