Search in sources :

Example 16 with Data

use of com.searchcode.app.dao.Data in project searchcode-server by boyter.

the class CodeRouteService method html.

public ModelAndView html(Request request, Response response) {
    Repo repo = Singleton.getRepo();
    Data data = Singleton.getData();
    SearchcodeLib scl = Singleton.getSearchcodeLib(data);
    CodeSearcher cs = new CodeSearcher();
    CodeMatcher cm = new CodeMatcher(data);
    Map<String, Object> map = new HashMap<>();
    map.put("repoCount", repo.getRepoCount());
    if (request.queryParams().contains("q")) {
        String query = request.queryParams("q").trim();
        String altquery = query.replaceAll("[^A-Za-z0-9 ]", " ").trim().replaceAll(" +", " ");
        int page = 0;
        if (request.queryParams().contains("p")) {
            try {
                page = Integer.parseInt(request.queryParams("p"));
                page = page > 19 ? 19 : page;
            } catch (NumberFormatException ex) {
                page = 0;
            }
        }
        String[] repos = new String[0];
        String[] langs = new String[0];
        String[] owners = new String[0];
        String reposFilter = Values.EMPTYSTRING;
        String langsFilter = Values.EMPTYSTRING;
        String ownersFilter = Values.EMPTYSTRING;
        String reposQueryString = Values.EMPTYSTRING;
        String langsQueryString = Values.EMPTYSTRING;
        String ownsQueryString = Values.EMPTYSTRING;
        if (request.queryParams().contains("repo")) {
            repos = request.queryParamsValues("repo");
            if (repos.length != 0) {
                List<String> reposList = Arrays.asList(repos).stream().map((s) -> "reponame:" + QueryParser.escape(s)).collect(Collectors.toList());
                reposFilter = " && (" + StringUtils.join(reposList, " || ") + ")";
                List<String> reposQueryList = Arrays.asList(repos).stream().map((s) -> "&repo=" + URLEncoder.encode(s)).collect(Collectors.toList());
                reposQueryString = StringUtils.join(reposQueryList, "");
            }
        }
        if (request.queryParams().contains("lan")) {
            langs = request.queryParamsValues("lan");
            if (langs.length != 0) {
                List<String> langsList = Arrays.asList(langs).stream().map((s) -> "languagename:" + QueryParser.escape(s)).collect(Collectors.toList());
                langsFilter = " && (" + StringUtils.join(langsList, " || ") + ")";
                List<String> langsQueryList = Arrays.asList(langs).stream().map((s) -> "&lan=" + URLEncoder.encode(s)).collect(Collectors.toList());
                langsQueryString = StringUtils.join(langsQueryList, "");
            }
        }
        if (request.queryParams().contains("own")) {
            owners = request.queryParamsValues("own");
            if (owners.length != 0) {
                List<String> ownersList = Arrays.asList(owners).stream().map((s) -> "codeowner:" + QueryParser.escape(s)).collect(Collectors.toList());
                ownersFilter = " && (" + StringUtils.join(ownersList, " || ") + ")";
                List<String> ownsQueryList = Arrays.asList(owners).stream().map((s) -> "&own=" + URLEncoder.encode(s)).collect(Collectors.toList());
                ownsQueryString = StringUtils.join(ownsQueryList, "");
            }
        }
        // split the query escape it and and it together
        String cleanQueryString = scl.formatQueryString(query);
        SearchResult searchResult = cs.search(cleanQueryString + reposFilter + langsFilter + ownersFilter, page);
        searchResult.setCodeResultList(cm.formatResults(searchResult.getCodeResultList(), query, true));
        for (CodeFacetRepo f : searchResult.getRepoFacetResults()) {
            if (Arrays.asList(repos).contains(f.getRepoName())) {
                f.setSelected(true);
            }
        }
        for (CodeFacetLanguage f : searchResult.getLanguageFacetResults()) {
            if (Arrays.asList(langs).contains(f.getLanguageName())) {
                f.setSelected(true);
            }
        }
        for (CodeFacetOwner f : searchResult.getOwnerFacetResults()) {
            if (Arrays.asList(owners).contains(f.getOwner())) {
                f.setSelected(true);
            }
        }
        map.put("searchValue", query);
        map.put("searchResult", searchResult);
        map.put("reposQueryString", reposQueryString);
        map.put("langsQueryString", langsQueryString);
        map.put("ownsQueryString", ownsQueryString);
        map.put("altQuery", altquery);
        map.put("totalPages", searchResult.getPages().size());
        map.put("isHtml", true);
        map.put("logoImage", CommonRouteService.getLogo());
        map.put("isCommunity", App.ISCOMMUNITY);
        map.put(Values.EMBED, Singleton.getData().getDataByName(Values.EMBED, Values.EMPTYSTRING));
        return new ModelAndView(map, "searchresults.ftl");
    }
    map.put("photoId", CommonRouteService.getPhotoId());
    map.put("numDocs", cs.getTotalNumberDocumentsIndexed());
    map.put("logoImage", CommonRouteService.getLogo());
    map.put("isCommunity", App.ISCOMMUNITY);
    map.put(Values.EMBED, Singleton.getData().getDataByName(Values.EMBED, Values.EMPTYSTRING));
    return new ModelAndView(map, "index.ftl");
}
Also used : java.util(java.util) Singleton(com.searchcode.app.service.Singleton) Spark.halt(spark.Spark.halt) RepoResult(com.searchcode.app.model.RepoResult) ModelAndView(spark.ModelAndView) Repo(com.searchcode.app.dao.Repo) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) Values(com.searchcode.app.config.Values) CodeMatcher(com.searchcode.app.service.CodeMatcher) com.searchcode.app.util(com.searchcode.app.util) CommonRouteService(com.searchcode.app.service.route.CommonRouteService) URLEncoder(java.net.URLEncoder) com.searchcode.app.dto(com.searchcode.app.dto) CodeSearcher(com.searchcode.app.service.CodeSearcher) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) Gson(com.google.gson.Gson) Request(spark.Request) Data(com.searchcode.app.dao.Data) Response(spark.Response) Properties(com.searchcode.app.util.Properties) StringEscapeUtils(org.apache.commons.lang3.StringEscapeUtils) App(com.searchcode.app.App) ModelAndView(spark.ModelAndView) Data(com.searchcode.app.dao.Data) CodeSearcher(com.searchcode.app.service.CodeSearcher) Repo(com.searchcode.app.dao.Repo) CodeMatcher(com.searchcode.app.service.CodeMatcher)

Example 17 with Data

use of com.searchcode.app.dao.Data in project searchcode-server by boyter.

the class CodeRouteService method literalSearch.

// This is very alpha and just for testing
// TODO improve or remove this
public Map<String, Object> literalSearch(Request request, Response response) {
    Repo repo = Singleton.getRepo();
    Data data = Singleton.getData();
    CodeSearcher cs = new CodeSearcher();
    CodeMatcher cm = new CodeMatcher(data);
    Map<String, Object> map = new HashMap<>();
    map.put("repoCount", repo.getRepoCount());
    if (request.queryParams().contains("q")) {
        String query = request.queryParams("q").trim();
        int page = 0;
        if (request.queryParams().contains("p")) {
            try {
                page = Integer.parseInt(request.queryParams("p"));
                page = page > 19 ? 19 : page;
            } catch (NumberFormatException ex) {
                page = 0;
            }
        }
        String altquery = query.replaceAll("[^A-Za-z0-9 ]", " ").trim().replaceAll(" +", " ");
        SearchResult searchResult = cs.search(query, page);
        searchResult.setCodeResultList(cm.formatResults(searchResult.getCodeResultList(), altquery, false));
        map.put("searchValue", query);
        map.put("searchResult", searchResult);
        map.put("reposQueryString", "");
        map.put("langsQueryString", "");
        map.put("altQuery", "");
        map.put("logoImage", CommonRouteService.getLogo());
        map.put("isCommunity", App.ISCOMMUNITY);
        return map;
    }
    map.put("photoId", 1);
    map.put("numDocs", cs.getTotalNumberDocumentsIndexed());
    map.put("logoImage", CommonRouteService.getLogo());
    map.put("isCommunity", App.ISCOMMUNITY);
    map.put(Values.EMBED, Singleton.getData().getDataByName(Values.EMBED, Values.EMPTYSTRING));
    return map;
}
Also used : Repo(com.searchcode.app.dao.Repo) CodeMatcher(com.searchcode.app.service.CodeMatcher) Data(com.searchcode.app.dao.Data) CodeSearcher(com.searchcode.app.service.CodeSearcher)

Example 18 with Data

use of com.searchcode.app.dao.Data in project searchcode-server by boyter.

the class TimeSearchRouteService method getTimeSearch.

public SearchResult getTimeSearch(Request request, Response response) {
    Data data = Singleton.getData();
    SearchcodeLib scl = Singleton.getSearchcodeLib(data);
    TimeCodeSearcher cs = new TimeCodeSearcher();
    CodeMatcher cm = new CodeMatcher(data);
    response.header("Content-Encoding", "gzip");
    if (request.queryParams().contains("q") == false || Values.EMPTYSTRING.equals(request.queryParams("q").trim())) {
        return null;
    }
    String query = request.queryParams("q").trim();
    int page = this.getPage(request);
    String[] repos;
    String[] langs;
    String[] owners;
    String[] year;
    String[] yearmonth;
    String[] yearmonthday;
    String[] revisions;
    String[] deleted;
    String reposFilter = Values.EMPTYSTRING;
    String langsFilter = Values.EMPTYSTRING;
    String ownersFilter = Values.EMPTYSTRING;
    String yearFilter = Values.EMPTYSTRING;
    String yearMonthFilter = Values.EMPTYSTRING;
    String yearMonthDayFilter = Values.EMPTYSTRING;
    String revisionsFilter = Values.EMPTYSTRING;
    String deletedFilter = Values.EMPTYSTRING;
    if (request.queryParams().contains("repo")) {
        repos = request.queryParamsValues("repo");
        reposFilter = getRepos(repos, reposFilter);
    }
    if (request.queryParams().contains("lan")) {
        langs = request.queryParamsValues("lan");
        langsFilter = getLanguages(langs, langsFilter);
    }
    if (request.queryParams().contains("own")) {
        owners = request.queryParamsValues("own");
        ownersFilter = getOwners(owners, ownersFilter);
    }
    if (request.queryParams().contains("year")) {
        year = request.queryParamsValues("year");
        yearFilter = this.getYears(year, yearFilter);
    }
    if (request.queryParams().contains("ym")) {
        yearmonth = request.queryParamsValues("ym");
        yearMonthFilter = this.getYearMonths(yearmonth, yearMonthFilter);
    }
    if (request.queryParams().contains("ymd")) {
        yearmonthday = request.queryParamsValues("ymd");
        yearMonthDayFilter = this.getYearMonthDays(yearmonthday, yearMonthDayFilter);
    }
    if (request.queryParams().contains("rev")) {
        revisions = request.queryParamsValues("rev");
        revisionsFilter = this.getRevisions(revisions, revisionsFilter);
    }
    if (request.queryParams().contains("del")) {
        deleted = request.queryParamsValues("del");
        deletedFilter = this.getDeleted(deleted, deletedFilter);
    }
    // split the query escape it and and it together
    String cleanQueryString = scl.formatQueryString(query);
    SearchResult searchResult = cs.search(cleanQueryString + reposFilter + langsFilter + ownersFilter + yearFilter + yearMonthFilter + yearMonthDayFilter + revisionsFilter + deletedFilter, page);
    searchResult.setCodeResultList(cm.formatResults(searchResult.getCodeResultList(), query, true));
    searchResult.setQuery(query);
    this.getAltQueries(scl, query, searchResult);
    // Null out code as it isn't required and there is no point in bloating our ajax requests
    for (CodeResult codeSearchResult : searchResult.getCodeResultList()) {
        codeSearchResult.setCode(null);
    }
    return searchResult;
}
Also used : SearchcodeLib(com.searchcode.app.util.SearchcodeLib) CodeResult(com.searchcode.app.dto.CodeResult) CodeMatcher(com.searchcode.app.service.CodeMatcher) Data(com.searchcode.app.dao.Data) TimeCodeSearcher(com.searchcode.app.service.TimeCodeSearcher) SearchResult(com.searchcode.app.dto.SearchResult)

Example 19 with Data

use of com.searchcode.app.dao.Data in project searchcode-server by boyter.

the class CommonRouteService method getBackoffValue.

public static double getBackoffValue() {
    if (App.ISCOMMUNITY) {
        return Double.parseDouble(Values.DEFAULTBACKOFFVALUE);
    }
    Data data = Singleton.getData();
    String backoffValue = data.getDataByName(Values.BACKOFFVALUE);
    if (backoffValue == null) {
        data.saveData(Values.BACKOFFVALUE, Values.DEFAULTBACKOFFVALUE);
        backoffValue = Values.DEFAULTBACKOFFVALUE;
    }
    return Double.parseDouble(backoffValue);
}
Also used : Data(com.searchcode.app.dao.Data)

Example 20 with Data

use of com.searchcode.app.dao.Data in project searchcode-server by boyter.

the class CommonRouteService method getSyntaxHighlighter.

public static String getSyntaxHighlighter() {
    if (App.ISCOMMUNITY) {
        return Values.DEFAULTSYNTAXHIGHLIGHTER;
    }
    Data data = Singleton.getData();
    String highlighter = data.getDataByName(Values.SYNTAXHIGHLIGHTER);
    if (highlighter == null || highlighter.trim().equals("")) {
        highlighter = Properties.getProperties().getProperty(Values.SYNTAXHIGHLIGHTER, Values.DEFAULTSYNTAXHIGHLIGHTER);
        data.saveData(Values.SYNTAXHIGHLIGHTER, highlighter);
    }
    return highlighter;
}
Also used : Data(com.searchcode.app.dao.Data)

Aggregations

Data (com.searchcode.app.dao.Data)22 Repo (com.searchcode.app.dao.Repo)4 CodeMatcher (com.searchcode.app.service.CodeMatcher)3 RepoResult (com.searchcode.app.model.RepoResult)2 CodeSearcher (com.searchcode.app.service.CodeSearcher)2 ModelAndView (spark.ModelAndView)2 Request (spark.Request)2 Gson (com.google.gson.Gson)1 App (com.searchcode.app.App)1 Values (com.searchcode.app.config.Values)1 Api (com.searchcode.app.dao.Api)1 com.searchcode.app.dto (com.searchcode.app.dto)1 CodeResult (com.searchcode.app.dto.CodeResult)1 SearchResult (com.searchcode.app.dto.SearchResult)1 Singleton (com.searchcode.app.service.Singleton)1 TimeCodeSearcher (com.searchcode.app.service.TimeCodeSearcher)1 CodeRouteService (com.searchcode.app.service.route.CodeRouteService)1 CommonRouteService (com.searchcode.app.service.route.CommonRouteService)1 com.searchcode.app.util (com.searchcode.app.util)1 Properties (com.searchcode.app.util.Properties)1