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.Arrays;
019
020import org.apache.commons.lang3.StringUtils;
021import org.apache.solr.client.solrj.response.QueryResponse;
022
023import service.tut.pori.contentanalysis.MediaObjectDAO;
024import core.tut.pori.context.ServiceInitializer;
025import core.tut.pori.http.parameters.DataGroups;
026import core.tut.pori.http.parameters.Limits;
027import core.tut.pori.users.UserIdentity;
028
029/**
030 * Content suggest core methods.
031 *
032 */
033public final class ContentSuggestCore {
034
035  /**
036   * Auto-complete method. Only the last part of the query ({@value service.tut.pori.contentsuggest.Definitions#QUERY_SEPARATOR} delimiter) is sent to the suggestion back-end.
037   * 
038   * @param authenticatedUser
039   * @param dataGroups 
040   * @param limits 
041   * @param query 
042   * @return response
043   * @throws IllegalArgumentException 
044   */
045  public static AutoCompleteResult suggest(UserIdentity authenticatedUser, DataGroups dataGroups, Limits limits, String query) throws IllegalArgumentException{
046    String queryArray[] = StringUtils.split(query, Definitions.QUERY_SEPARATOR);
047    if(queryArray.length < 1){
048      throw new IllegalArgumentException("Query cannot be empty");
049    }
050    QueryResponse res = ServiceInitializer.getDAOHandler().getSolrDAO(MediaObjectDAO.class).getSuggestions(authenticatedUser, dataGroups, limits, queryArray[queryArray.length-1]);
051    AutoCompleteResult acr = AutoCompleteResult.fromQueryResponse(res);
052    String collation[] = Arrays.copyOf(queryArray, queryArray.length-1);
053    acr.setCollation(StringUtils.join(collation, " "));
054    return acr;
055  }
056  
057  /**
058   * 
059   */
060  private ContentSuggestCore(){
061    // nothing needed
062  }
063}