001/**
002 * Copyright 2015 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.reference;
017
018import java.util.ArrayList;
019import java.util.Arrays;
020import java.util.List;
021
022import org.apache.commons.lang3.RandomStringUtils;
023import org.apache.commons.lang3.StringUtils;
024import org.apache.log4j.Logger;
025
026import service.tut.pori.contentsuggest.AutoCompleteResult;
027import core.tut.pori.http.parameters.Limits;
028
029/**
030 * 
031 * class that can be used to created example objects/object lists
032 *
033 */
034public class CSXMLObjectCreator {
035  private static final Logger LOGGER = Logger.getLogger(CSXMLObjectCreator.class);
036  private static final int MAX_SUGGESTIONS = 100;
037  private static final int QUERY_LENGTH = 10;
038  private static final int SUGGESTION_LENGTH = 10;
039
040  /**
041   * 
042   * @param limits
043   * @param query
044   * @return randomly generated autocomplete result
045   */
046  public AutoCompleteResult createAutoCompleteResult(Limits limits, String query) {
047    String queryArray[] = StringUtils.split(query, " ");
048    if(queryArray.length < 1){
049      throw new IllegalArgumentException("Query cannot be empty");
050    }
051    int maxSuggestions = limits.getMaxItems();
052    if(maxSuggestions > MAX_SUGGESTIONS){
053      LOGGER.debug("Max items more than max, limiting to "+MAX_SUGGESTIONS);
054      maxSuggestions = MAX_SUGGESTIONS;
055    }
056    List<String> suggestions = new ArrayList<>(maxSuggestions);
057    for(int i=0;i<maxSuggestions;++i){
058      suggestions.add(queryArray[queryArray.length-1] + RandomStringUtils.randomAlphabetic(SUGGESTION_LENGTH));
059    }
060    
061    AutoCompleteResult acr = new AutoCompleteResult();
062    acr.setSuggestionList(suggestions);
063    String collation[] = Arrays.copyOf(queryArray, queryArray.length-1);
064    acr.setCollation(StringUtils.join(collation, " "));
065    return acr;
066  }
067
068  /**
069   * 
070   * @param limits
071   * @return randomly generated autocomplete result
072   */
073  public AutoCompleteResult createAutoCompleteResult(Limits limits) {
074    return createAutoCompleteResult(limits, RandomStringUtils.randomAlphabetic(QUERY_LENGTH));
075  }
076
077}