<commit_before>package com.searchbox.framework.web;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView;

import com.searchbox.core.dm.FieldAttribute;
import com.searchbox.core.engine.SearchEngine;
import com.searchbox.core.search.CachedContent;
import com.searchbox.core.search.SearchCondition;
import com.searchbox.core.search.SearchElement;
import com.searchbox.core.search.SearchResult;
import com.searchbox.framework.domain.FieldAttributeDefinition;
import com.searchbox.framework.domain.PresetDefinition;
import com.searchbox.framework.domain.SearchElementDefinition;
import com.searchbox.framework.domain.Searchbox;
import com.searchbox.framework.repository.FieldAttributeRepository;
import com.searchbox.framework.repository.PresetRepository;
import com.searchbox.framework.repository.SearchEngineRepository;
import com.searchbox.framework.repository.SearchboxRepository;
import com.searchbox.framework.service.DirectoryService;
import com.searchbox.framework.service.SearchEngineService;
import com.searchbox.framework.service.SearchService;

@Controller
@RequestMapping("/{searchbox}/search/{preset}")
public class SearchController {

	private static Logger logger = LoggerFactory
			.getLogger(SearchController.class);
	
	protected String getView() {
		return "search/index";
	}

	@Autowired
	ApplicationConversionService applicationConversionService;
	
	@Autowired
	ConversionService conversionService;
	
	@Autowired
	SearchService searchService;
	
	@Autowired
	SearchEngineService searchEngineService;

	@Autowired
	DirectoryService directoryService;

	@Autowired
	SearchboxRepository searchboxRepository;

	@Autowired
	FieldAttributeRepository fieldAttributeRepository;

	@Autowired
	protected PresetRepository presetRepository;
	
	@Autowired
	protected SearchEngineRepository searchEngineRepository;

	public SearchController() {
	}

	@ModelAttribute("searchboxes")
	public List<Searchbox> getAllSearchboxes() {
		ArrayList<Searchbox> searchboxes = new ArrayList<Searchbox>();
		Iterator<Searchbox> sbx = searchboxRepository.findAll().iterator();
		while (sbx.hasNext()) {
			searchboxes.add(sbx.next());
		}
		return searchboxes;
	}	
	
	@ModelAttribute("presets")
	public List<PresetDefinition> getAllPresets(@PathVariable Searchbox searchbox) {
		return presetRepository.findAllBySearchbox(searchbox);
	}	

	@RequestMapping()
	public ModelAndView executeSearch(
			@ModelAttribute("searchboxes") List<Searchbox> searchboxes,
			@PathVariable Searchbox searchbox,
			@PathVariable PresetDefinition preset,
			HttpServletRequest request, ModelAndView model,
			RedirectAttributes redirectAttributes) {
		
		if (searchbox == null) {
			// TODO We shoudl never get here...
			searchbox = searchboxes.get(0);
			redirectAttributes.addAttribute("searchbox", searchbox);
			ModelAndView redirect = new ModelAndView(new RedirectView(searchbox.getSlug(),
					true));
			return redirect;
		} else if(preset == null){
			logger.info("Missing preset. Redirecting to first preset of searchbox: " +
						searchbox.getName());
			String slug = searchbox.getPresets().get(0).getSlug();
			ModelAndView redirect = new ModelAndView(
					new RedirectView(searchbox.getSlug()+"/"+slug,true));
			return redirect;
		}
		
		logger.info("search page for: " + searchbox + 
				" with preset:" + preset);
		model.setViewName(this.getView());	

		// Fetch all search Conditions within HTTP params
		Set<SearchCondition> conditions = new HashSet<SearchCondition>();
		for (String param : applicationConversionService.getSearchConditionParams()) {
			if (request.getParameterValues(param) != null) {
				for (String value : request.getParameterValues(param)) {
					if (value != null && !value.isEmpty()) {
						try {
							SearchCondition cond = (SearchCondition) conversionService
									.convert(value, applicationConversionService
											.getSearchConditionClass(param));
							conditions.add(cond);
						} catch (Exception e) {
							logger.error("Could not convert " + value, e);
						}
					}
				}
			}
		}

		SearchResult result = new SearchResult();

		// Build the Preset DTO with dependancies
		Set<SearchElement> searchElements = new TreeSet<SearchElement>();

		for (SearchElementDefinition elementdefinition : preset.getSearchElements()) {
			try {
				SearchElement searchElement = elementdefinition.getInstance();
				if (CachedContent.class.isAssignableFrom(searchElement
						.getClass())) {
					Integer hash = ((CachedContent) searchElement)
							.getContentHash();

					String tempFile = searchElement.getClass().getSimpleName()
							+ "_" + searchElement.getPosition() + "_" + hash
							+ ".jspx";
					if (!directoryService.fileExists(tempFile)) {
						directoryService.createFile(tempFile,
								((CachedContent) searchElement).getContent());
					}
					((CachedContent) searchElement).setPath(directoryService
							.getApplicationRelativePath(tempFile));
				}
				
				searchElements.add(searchElement);
			} catch (Exception e) {
				logger.error("Could not get SearchElement for: "
						+ elementdefinition, e);
			}
		}
	
		Set<FieldAttribute> fieldAttributes = new HashSet<FieldAttribute>();
		for(FieldAttributeDefinition def:preset.getFieldAttributes()){
			fieldAttributes.add(def.getInstance());
		}
		
		SearchEngine<?, ?> searchEngine = searchEngineService.getSearchEngine(
				preset.getCollection().getSearchEngine().getName());
		
		Set<SearchElement> resultElements =  searchService.execute(searchEngine,
				searchElements, fieldAttributes, conditions);
		
		for (SearchElement element : resultElements) {
			logger.debug("Adding to result view element["
					+ element.getPosition() + "] = " + element.getLabel());
			result.addElement(element);
		}
	
		model.addObject("result", result);
		model.addObject("preset", preset);

		return model;
	}
}<commit_msg>Added a no-preset redirect<commit_after>package com.searchbox.framework.web;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView;

import com.searchbox.core.dm.FieldAttribute;
import com.searchbox.core.engine.SearchEngine;
import com.searchbox.core.search.CachedContent;
import com.searchbox.core.search.SearchCondition;
import com.searchbox.core.search.SearchElement;
import com.searchbox.core.search.SearchResult;
import com.searchbox.framework.domain.FieldAttributeDefinition;
import com.searchbox.framework.domain.PresetDefinition;
import com.searchbox.framework.domain.SearchElementDefinition;
import com.searchbox.framework.domain.Searchbox;
import com.searchbox.framework.repository.FieldAttributeRepository;
import com.searchbox.framework.repository.PresetRepository;
import com.searchbox.framework.repository.SearchEngineRepository;
import com.searchbox.framework.repository.SearchboxRepository;
import com.searchbox.framework.service.DirectoryService;
import com.searchbox.framework.service.SearchEngineService;
import com.searchbox.framework.service.SearchService;

@Controller
@RequestMapping("/{searchbox}/search")
public class SearchController {

	private static Logger logger = LoggerFactory
			.getLogger(SearchController.class);

	protected String getView() {
		return "search/index";
	}

	@Autowired
	ApplicationConversionService applicationConversionService;

	@Autowired
	ConversionService conversionService;

	@Autowired
	SearchService searchService;

	@Autowired
	SearchEngineService searchEngineService;

	@Autowired
	DirectoryService directoryService;

	@Autowired
	SearchboxRepository searchboxRepository;

	@Autowired
	FieldAttributeRepository fieldAttributeRepository;

	@Autowired
	protected PresetRepository presetRepository;

	@Autowired
	protected SearchEngineRepository searchEngineRepository;

	public SearchController() {
	}

	@ModelAttribute("searchboxes")
	public List<Searchbox> getAllSearchboxes() {
		ArrayList<Searchbox> searchboxes = new ArrayList<Searchbox>();
		Iterator<Searchbox> sbx = searchboxRepository.findAll().iterator();
		while (sbx.hasNext()) {
			searchboxes.add(sbx.next());
		}
		return searchboxes;
	}

	@ModelAttribute("presets")
	public List<PresetDefinition> getAllPresets(
			@PathVariable Searchbox searchbox) {
		return presetRepository.findAllBySearchbox(searchbox);
	}

	@RequestMapping(value = { "", "/" })
	public ModelAndView getDefaultPreset(@PathVariable Searchbox searchbox,
			HttpServletRequest request, ModelAndView model,
			RedirectAttributes redirectAttributes) {

		logger.info("Missing preset. Redirecting to first preset of searchbox: "
				+ searchbox.getName());
		String slug = searchbox.getPresets().get(0).getSlug();
		ModelAndView redirect = new ModelAndView(new RedirectView(
				"/"+searchbox.getSlug()+"/search/"+slug, true));
		return redirect;

	}

	@RequestMapping("/{preset}")
	public ModelAndView executeSearch(
			@ModelAttribute("searchboxes") List<Searchbox> searchboxes,
			@PathVariable Searchbox searchbox,
			@PathVariable PresetDefinition preset, HttpServletRequest request,
			ModelAndView model, RedirectAttributes redirectAttributes) {

		logger.info("search page for: " + searchbox + " with preset:" + preset);
		model.setViewName(this.getView());

		// Fetch all search Conditions within HTTP params
		Set<SearchCondition> conditions = new HashSet<SearchCondition>();
		for (String param : applicationConversionService
				.getSearchConditionParams()) {
			if (request.getParameterValues(param) != null) {
				for (String value : request.getParameterValues(param)) {
					if (value != null && !value.isEmpty()) {
						try {
							SearchCondition cond = (SearchCondition) conversionService
									.convert(
											value,
											applicationConversionService
													.getSearchConditionClass(param));
							conditions.add(cond);
						} catch (Exception e) {
							logger.error("Could not convert " + value, e);
						}
					}
				}
			}
		}

		SearchResult result = new SearchResult();

		// Build the Preset DTO with dependancies
		Set<SearchElement> searchElements = new TreeSet<SearchElement>();

		for (SearchElementDefinition elementdefinition : preset
				.getSearchElements()) {
			try {
				SearchElement searchElement = elementdefinition.getInstance();
				if (CachedContent.class.isAssignableFrom(searchElement
						.getClass())) {
					Integer hash = ((CachedContent) searchElement)
							.getContentHash();

					String tempFile = searchElement.getClass().getSimpleName()
							+ "_" + searchElement.getPosition() + "_" + hash
							+ ".jspx";
					if (!directoryService.fileExists(tempFile)) {
						directoryService.createFile(tempFile,
								((CachedContent) searchElement).getContent());
					}
					((CachedContent) searchElement).setPath(directoryService
							.getApplicationRelativePath(tempFile));
				}

				searchElements.add(searchElement);
			} catch (Exception e) {
				logger.error("Could not get SearchElement for: "
						+ elementdefinition, e);
			}
		}

		Set<FieldAttribute> fieldAttributes = new HashSet<FieldAttribute>();
		for (FieldAttributeDefinition def : preset.getFieldAttributes()) {
			fieldAttributes.add(def.getInstance());
		}

		SearchEngine<?, ?> searchEngine = searchEngineService
				.getSearchEngine(preset.getCollection().getSearchEngine()
						.getName());

		Set<SearchElement> resultElements = searchService.execute(searchEngine,
				searchElements, fieldAttributes, conditions);

		for (SearchElement element : resultElements) {
			logger.debug("Adding to result view element["
					+ element.getPosition() + "] = " + element.getLabel());
			result.addElement(element);
		}

		model.addObject("result", result);
		model.addObject("preset", preset);

		return model;
	}
}
{'fraction_non_alphanumeric': 0.10526315789473684, 'fraction_numerical': 0.0005354957160342717, 'mean_word_length': 5.08046511627907, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7589179', 'n_tokens_mistral': 4098, 'n_tokens_neox': 3555, 'n_words': 693}