@@ -0,0 +1,2986 @@
+package com.coding4people.mosquitoreport.api.indexers;%0A%0Aimport java.io.ByteArrayInputStream;%0Aimport java.nio.charset.StandardCharsets;%0Aimport java.util.List;%0Aimport java.util.Optional;%0A%0Aimport javax.annotation.PostConstruct;%0Aimport javax.inject.Inject;%0Aimport javax.ws.rs.InternalServerErrorException;%0A%0Aimport com.amazonaws.services.cloudsearchdomain.AmazonCloudSearchDomain;%0Aimport com.amazonaws.services.cloudsearchdomain.AmazonCloudSearchDomainClient;%0Aimport com.amazonaws.services.cloudsearchdomain.model.SearchRequest;%0Aimport com.amazonaws.services.cloudsearchdomain.model.UploadDocumentsRequest;%0Aimport com.amazonaws.services.cloudsearchv2.AmazonCloudSearch;%0Aimport com.amazonaws.services.cloudsearchv2.model.DescribeDomainsRequest;%0Aimport com.amazonaws.services.cloudsearchv2.model.DomainStatus;%0Aimport com.amazonaws.services.cloudsearchv2.model.ServiceEndpoint;%0Aimport com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;%0Aimport com.coding4people.mosquitoreport.api.Env;%0Aimport com.fasterxml.jackson.core.JsonProcessingException;%0Aimport com.fasterxml.jackson.databind.ObjectMapper;%0A%0Aabstract public class Indexer%3CT%3E %7B%0A    @Inject AmazonCloudSearch amazonCloudSearch;%0A    %0A    @Inject Env env;%0A%0A    AmazonCloudSearchDomain domain;%0A%0A    abstract protected Class%3CT%3E getType();%0A    %0A    public void index(T item) %7B%0A        // TODO send to SQS in order to handle it asynchronously%0A        try %7B%0A            domain.uploadDocuments(new UploadDocumentsRequest().withDocuments(new ByteArrayInputStream(%0A                    new ObjectMapper().writeValueAsString(item).getBytes(StandardCharsets.UTF_8))));%0A        %7D catch (JsonProcessingException e) %7B%0A            // TODO log indexer errors%0A            e.printStackTrace();%0A        %7D%0A    %7D%0A%0A    public Object search(String query) %7B%0A        return domain.search(new SearchRequest().withQuery(query));%0A    %7D%0A%0A    @PostConstruct%0A    protected void postConstruct() %7B%0A        DescribeDomainsRequest describeDomainsRequest = new DescribeDomainsRequest().withDomainNames(getDomainName());%0A        List%3CDomainStatus%3E list = amazonCloudSearch.describeDomains(describeDomainsRequest).getDomainStatusList();%0A%0A        if (list.isEmpty()) %7B%0A            throw new InternalServerErrorException(%22Could not find CloudSearch domain: %22 + getDomainName());%0A        %7D%0A        %0A        ServiceEndpoint searchService = list.get(0).getSearchService();%0A        %0A        if (searchService.getEndpoint() == null) %7B%0A            throw new InternalServerErrorException(%22Could not find CloudSearch domain: %22 + getDomainName());%0A        %7D%0A        %0A        domain = new AmazonCloudSearchDomainClient();%0A        domain.setEndpoint(searchService.getEndpoint());%0A    %7D%0A%0A    protected String getDomainName() %7B%0A        return getPrefix() + getType().getAnnotation(DynamoDBTable.class).tableName();%0A    %7D%0A    %0A    protected String getPrefix() %7B%0A        return Optional.ofNullable(env.get(%22MOSQUITO_REPORT_CLOUDSEARCH_DOMAIN_PREFIX%22)).orElse(%22localhost%22) + %22-%22;%0A    %7D%0A%7D%0A

Create base indexer