simple auto-completion for the search box
This commit is contained in:
@@ -2,9 +2,17 @@ package org.lucares.pdbui;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.lucares.ludb.Proposal;
|
||||
import org.lucares.pdbui.domain.AutocompleteProposal;
|
||||
import org.lucares.pdbui.domain.AutocompleteProposalByValue;
|
||||
import org.lucares.pdbui.domain.AutocompleteResponse;
|
||||
import org.lucares.pdbui.domain.PlotRequest;
|
||||
import org.lucares.pdbui.domain.PlotResponse;
|
||||
import org.lucares.performance.db.CollectionUtils;
|
||||
import org.lucares.recommind.logs.InternalPlottingException;
|
||||
import org.lucares.recommind.logs.Plotter;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
@@ -13,15 +21,18 @@ import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
@Controller
|
||||
@EnableAutoConfiguration
|
||||
public class PdbController implements HardcodedValues {
|
||||
public class PdbController implements HardcodedValues, CollectionUtils {
|
||||
|
||||
private final Plotter plotter;
|
||||
private final PdbRepository db;
|
||||
|
||||
public PdbController(final Plotter plotter) {
|
||||
public PdbController(final PdbRepository db, final Plotter plotter) {
|
||||
this.db = db;
|
||||
this.plotter = plotter;
|
||||
}
|
||||
|
||||
@@ -34,8 +45,12 @@ public class PdbController implements HardcodedValues {
|
||||
PlotResponse createPlot(@RequestBody final PlotRequest request) {
|
||||
|
||||
try {
|
||||
System.out.println(request.getQuery());
|
||||
final File image = plotter.plot(request.getQuery(), request.getHeight(), request.getWidth());
|
||||
final String query = request.getQuery();
|
||||
final int height = request.getHeight();
|
||||
final int width = request.getWidth();
|
||||
|
||||
System.out.println(query);
|
||||
final File image = plotter.plot(query, height, width);
|
||||
|
||||
final Path relativeImagePath = plotter.getOutputDir().relativize(image.toPath());
|
||||
return new PlotResponse(WEB_IMAGE_OUTPUT_PATH + "/" + relativeImagePath.toString());
|
||||
@@ -45,4 +60,47 @@ public class PdbController implements HardcodedValues {
|
||||
}
|
||||
}
|
||||
|
||||
@RequestMapping(path = "/autocomplete", //
|
||||
method = RequestMethod.GET, //
|
||||
consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, //
|
||||
produces = MediaType.APPLICATION_JSON_UTF8_VALUE //
|
||||
)
|
||||
@ResponseBody
|
||||
AutocompleteResponse autocomplete(@RequestParam(name = "query") final String query,
|
||||
@RequestParam(name = "caretIndex") final int caretIndex) {
|
||||
|
||||
try {
|
||||
final AutocompleteResponse result = new AutocompleteResponse();
|
||||
final int zeroBasedCaretIndex = caretIndex - 1;
|
||||
|
||||
final List<Proposal> proposals = db.autocomplete(query, zeroBasedCaretIndex);
|
||||
final List<Proposal> nonEmptyProposals = filter(proposals, p -> p.getResults() > 0);
|
||||
|
||||
final List<AutocompleteProposal> autocompleteProposals = toAutocompleteProposals(nonEmptyProposals);
|
||||
Collections.sort(autocompleteProposals, new AutocompleteProposalByValue());
|
||||
|
||||
result.setProposals(autocompleteProposals);
|
||||
return result;
|
||||
|
||||
} catch (final Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new InternalServerError(e);
|
||||
}
|
||||
}
|
||||
|
||||
private List<AutocompleteProposal> toAutocompleteProposals(final List<Proposal> proposals) {
|
||||
|
||||
final List<AutocompleteProposal> result = new ArrayList<>();
|
||||
|
||||
for (final Proposal proposal : proposals) {
|
||||
final AutocompleteProposal e = new AutocompleteProposal();
|
||||
e.setValue(proposal.getProposedTag());
|
||||
e.setProposedQuery(proposal.getProposedQuery());
|
||||
|
||||
result.add(e);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user