package com.solrstart.javadoc.server;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.result.HighlightEntry;
import org.springframework.data.solr.core.query.result.HighlightPage;
import org.springframework.data.solr.repository.config.EnableSolrRepositories;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

@Controller
@EnableAutoConfiguration
@EnableSolrRepositories("com.solrstart.javadoc.server")
public class AutoCompleteServer {

    @Autowired
    private MatchRepository matchRepository;


    @RequestMapping("/lookup")
    @ResponseBody
    List<Match> home(
            @RequestParam(value="query", required=false, defaultValue="solr") String query
    ){

        List<Match> results = new LinkedList<>();

        //remove any characters that are not alphanumerics, spaces, underscores, dollars, or fullstops
        query = query.replaceAll("[^a-zA-Z0-9 _$.]", " ");
        query = query.replaceAll(" +", " "); //replace multiple spaces with one
        if (query.length()<3) {
            return null;
        }

        HighlightPage<Match> searchResults = matchRepository.find(query, new PageRequest(0, 10));
        Map<String, String> overrides = new TreeMap<>();
        for (HighlightEntry<Match> hightlightEntity : searchResults.getHighlighted()) {
            Match match = hightlightEntity.getEntity();
            overrides.clear();
            hightlightEntity.getHighlights().forEach(hl -> overrides.put(hl.getField().getName(), hl.getSnipplets().get(0))); //ignore multiple snippets for now
            match.createHTMLDescription(overrides);
//            match.lockTheURL("http://localhost:8983/javadoc");
            match.lockTheURL("/javadoc");
            results.add(match);
        }

        return results;
    }


    @Bean
    public SolrServer solrServer() {
        return new HttpSolrServer("http://localhost:8983/solr/JavadocCollection");
    }

    @Bean
    public SolrTemplate solrTemplate(SolrServer server) throws Exception {
        return new SolrTemplate(server);
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(AutoCompleteServer.class, args);
    }

}

@@ -200,16 +200,75 @@
 owired;%0A
+import org.springframework.beans.factory.annotation.Value;%0A
 import o
@@ -306,24 +306,24 @@
 pplication;%0A
-
 import org.s
@@ -1287,16 +1287,181 @@
 itory;%0A%0A
+    // Base URL (with final /) of where Javadoc lives. use --baseURL=/xyz to pass it in or via properties file%0A    @Value(%22$%7BbaseURL%7D%22)%0A    private String baseURL;%0A%0A
 %0A    @Re
@@ -2584,32 +2584,34 @@
 8983/javadoc%22);%0A
+//
             matc
@@ -2632,24 +2632,63 @@
 /javadoc%22);%0A
+            match.lockTheURL(baseURL);%0A
             

Switch to using properties for javadoc base URL