Commit a5100448 authored by Gradl, Tobias's avatar Gradl, Tobias
Browse files

423: Implement GET with search parameters (OPENED)

Task-Url: #423
parent 17662de8
...@@ -14,6 +14,7 @@ import org.springframework.web.bind.annotation.RequestBody; ...@@ -14,6 +14,7 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
...@@ -31,7 +32,7 @@ public class SimpleCustomSearchController extends BaseSimpleSearchController { ...@@ -31,7 +32,7 @@ public class SimpleCustomSearchController extends BaseSimpleSearchController {
@Autowired private CustomSearchConverter customSearchConverter; @Autowired private CustomSearchConverter customSearchConverter;
@RequestMapping(method={GET, POST}, value="/") @RequestMapping(method={GET, POST}, value="/")
public String getViewAndQuery(@PathVariable String prefix, @RequestParam(required=false) String expression, Model model, HttpServletRequest request, Locale locale) { public ModelAndView getViewAndQuery(@PathVariable String prefix, @RequestParam(required=false) String expression, @RequestParam(required=false, name="sourceId") List<String> sourceIds, Model model, HttpServletRequest request, Locale locale) {
List<CustomSearch> cs = customSearchService.findByPrefix(prefix); List<CustomSearch> cs = customSearchService.findByPrefix(prefix);
if (cs.size()!=1) { if (cs.size()!=1) {
...@@ -53,7 +54,7 @@ public class SimpleCustomSearchController extends BaseSimpleSearchController { ...@@ -53,7 +54,7 @@ public class SimpleCustomSearchController extends BaseSimpleSearchController {
model.addAttribute("datasources", collectionConverter.convert(collectionService.findByIds(csPojo.getCollectionIds()))); model.addAttribute("datasources", collectionConverter.convert(collectionService.findByIds(csPojo.getCollectionIds())));
return super.getViewAndQuery(expression, model, request, locale); return super.getViewAndQuery(expression, sourceIds, model, locale);
} }
@RequestMapping(value = "/getCollectionSelectionDialog", method = RequestMethod.POST) @RequestMapping(value = "/getCollectionSelectionDialog", method = RequestMethod.POST)
......
package eu.dariah.de.search.controller.search; package eu.dariah.de.search.controller.search;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
...@@ -21,8 +21,7 @@ import org.springframework.web.servlet.mvc.support.RedirectAttributes; ...@@ -21,8 +21,7 @@ import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import eu.dariah.de.search.controller.search.base.BaseSimpleSearchController; import eu.dariah.de.search.controller.search.base.BaseSimpleSearchController;
import eu.dariah.de.search.model.Filter; import eu.dariah.de.search.model.Collection;
import eu.dariah.de.search.service.FilterService;
@Controller @Controller
@RequestMapping("/search/simple") @RequestMapping("/search/simple")
...@@ -39,20 +38,33 @@ public class SimpleSearchController extends BaseSimpleSearchController { ...@@ -39,20 +38,33 @@ public class SimpleSearchController extends BaseSimpleSearchController {
return new ModelAndView("redirect:/search/simple/"); return new ModelAndView("redirect:/search/simple/");
} }
@SuppressWarnings("unchecked")
@GetMapping(value="/") @GetMapping(value="/")
public String getViewAndQuery(@RequestParam(required=false) String expression, Model model, HttpServletRequest request, Locale locale) { public ModelAndView getViewAndQuery(@RequestParam(required=false, name="q") String expression, @RequestParam(required=false, name="sourceId") List<String> sourceIds, RedirectAttributes attributes, Model model, HttpServletRequest request, Locale locale) {
if (this.isNeedsRedirect(expression, sourceIds, attributes)) {
return new ModelAndView("redirect:/search/simple/");
}
if (model.asMap().containsKey("redirectExpression") && model.asMap().get("redirectExpression")!=null) { if (model.asMap().containsKey("redirectExpression") && model.asMap().get("redirectExpression")!=null) {
expression = (String)model.asMap().get("redirectExpression"); expression = (String)model.asMap().get("redirectExpression");
} }
model.addAttribute("datasources", collectionConverter.convert(collectionService.getAll(), locale)); if (model.asMap().containsKey("redirectSourceIds") && model.asMap().get("redirectSourceIds")!=null) {
sourceIds = (List<String>)model.asMap().get("redirectSourceIds");
}
List<String> notifications = new ArrayList<>();
List<Collection> collections = collectionService.getAll();
model.addAttribute("strSourceId", this.setCollectionSelection(collections, sourceIds, notifications));
model.addAttribute("datasources", collectionConverter.convert(collections, locale));
model.addAttribute("availableFilters", filterService.getAllAvailableFilters()); model.addAttribute("availableFilters", filterService.getAllAvailableFilters());
model.addAttribute("searchNotifications", notifications);
return super.getViewAndQuery(expression, model, request, locale);
return super.getViewAndQuery(expression, sourceIds, model, locale);
} }
@PostMapping(value = "/getCollectionSelectionDialog") @PostMapping(value = "/getCollectionSelectionDialog")
public String getCollectionSelectionDialog(@RequestBody JsonNode body, Locale locale, Model model, HttpServletRequest request) { public String getCollectionSelectionDialog(@RequestBody JsonNode body, Locale locale, Model model, HttpServletRequest request) {
return super.getCollectionSelectionDialog(body, collectionService.getAll(), locale, model, request); return super.getCollectionSelectionDialog(body, collectionService.getAll(), locale, model, request);
} }
} }
...@@ -5,6 +5,9 @@ import static org.springframework.web.bind.annotation.RequestMethod.GET; ...@@ -5,6 +5,9 @@ import static org.springframework.web.bind.annotation.RequestMethod.GET;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -65,4 +68,27 @@ public abstract class BaseSearchController extends BaseController { ...@@ -65,4 +68,27 @@ public abstract class BaseSearchController extends BaseController {
return "search/sourceSelectionForm"; return "search/sourceSelectionForm";
} }
protected String setCollectionSelection(List<Collection> collections, List<String> sourceIds, List<String> notifications) {
StringBuilder strSourceId = new StringBuilder();
strSourceId.append("[");
if (sourceIds!=null && collections!=null) {
Map<String, String> collectionIdMap = collections.stream().collect(Collectors.toMap(Collection::getColregEntityId, Collection::getId));
for (int i=0; i<sourceIds.size(); i++) {
if (!collectionIdMap.containsKey(sourceIds.get(i))) {
if (!notifications.contains("~eu.dariah.de.minfba.search.view.notifications.selected_collection_unknown")) {
notifications.add("~eu.dariah.de.minfba.search.view.notifications.selected_collection_unknown");
}
continue;
}
if (strSourceId.length()>1) {
strSourceId.append(",");
}
strSourceId.append("\"").append(collectionIdMap.get(sourceIds.get(i))).append("\"");
}
}
strSourceId.append("]");
return strSourceId.toString();
}
} }
package eu.dariah.de.search.controller.search.base; package eu.dariah.de.search.controller.search.base;
import java.util.List;
import java.util.Locale; import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import eu.dariah.de.search.query.SimpleQueryImpl; import eu.dariah.de.search.query.SimpleQueryImpl;
public abstract class BaseSimpleSearchController extends BaseSearchController { public abstract class BaseSimpleSearchController extends BaseSearchController {
public BaseSimpleSearchController() { protected BaseSimpleSearchController() {
super("simpleSearch"); super("simpleSearch");
} }
protected String getViewAndQuery(String expression, Model model, HttpServletRequest request, Locale locale) { protected ModelAndView getViewAndQuery(String expression, List<String> sourceIds, Model model, Locale locale) {
SimpleQueryImpl q = new SimpleQueryImpl(); SimpleQueryImpl q = new SimpleQueryImpl();
q.setSize(20); q.setSize(20);
q.setQueryString(expression==null || expression.trim().isEmpty() ? null : expression); q.setQueryString(expression==null || expression.trim().isEmpty() ? null : expression);
q.setSourceIds(sourceIds);
model.addAttribute("query", q); model.addAttribute("query", q);
return "search/simple";
}
return new ModelAndView("search/simple", model.asMap());
}
protected boolean isNeedsRedirect(String expression, List<String> sourceIds, RedirectAttributes attributes) {
if (expression!=null || (sourceIds!=null && !sourceIds.isEmpty())) {
if (expression!=null) {
attributes.addFlashAttribute("redirectExpression", expression);
}
if (sourceIds!=null && !sourceIds.isEmpty()) {
attributes.addFlashAttribute("redirectSourceIds", sourceIds);
}
return true;
}
return false;
}
} }
...@@ -32,7 +32,7 @@ log: ...@@ -32,7 +32,7 @@ log:
# pattern: "%-4relative [%thread] %-5level %logger{35}[%line]- %msg%n" # pattern: "%-4relative [%thread] %-5level %logger{35}[%line]- %msg%n"
indexing: indexing:
baseDir: /tmp/indexing_logs baseDir: /tmp/indexing_logs
#log_queries: false log_queries: true
indexing: indexing:
......
Subproject commit 4da45cfdee0f5a097715b83ac9e0d889ee916496 Subproject commit 6cbfa384269321c75742825c82d9c73c99b8afad
Subproject commit 355208a102f0d3712cfa144a37839f37db5a6b22 Subproject commit 7274639e8461b8c5970617dcdeb6305f7c819fb9
Subproject commit b8d42adc16ac531aee20efbd8576c9cfea5948a1 Subproject commit c32723268ee4c337bc5cf34d2b1fa004fbb99f7e
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment