diff --git a/pom.xml b/pom.xml index 77946dbc35c10e78938ea0703653736716f49816..ad09745bc80d45934de08825c575aadb9746138e 100644 --- a/pom.xml +++ b/pom.xml @@ -44,7 +44,12 @@ <artifactId>javax.mail</artifactId> <version>1.6.0</version> </dependency> - + <!-- https://mvnrepository.com/artifact/com.opencsv/opencsv --> + <dependency> + <groupId>com.opencsv</groupId> + <artifactId>opencsv</artifactId> + <version>5.7.1</version> + </dependency> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> <dependency> <groupId>org.apache.poi</groupId> diff --git a/src/main/java/com/tarento/formservice/controllers/FormsController.java b/src/main/java/com/tarento/formservice/controllers/FormsController.java index 2e6b9262f5ce89d5a6e70aecc041a6df29e7f9ba..ef400dd4da0bb4888c10ba874c6613328e450759 100644 --- a/src/main/java/com/tarento/formservice/controllers/FormsController.java +++ b/src/main/java/com/tarento/formservice/controllers/FormsController.java @@ -6,6 +6,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import com.tarento.formservice.model.*; import org.apache.commons.lang3.StringUtils; import org.apache.tomcat.util.codec.binary.Base64; import org.slf4j.Logger; @@ -24,16 +25,6 @@ import org.springframework.web.multipart.MultipartFile; import com.fasterxml.jackson.core.JsonProcessingException; import com.google.gson.Gson; -import com.tarento.formservice.model.AssignApplication; -import com.tarento.formservice.model.Consent; -import com.tarento.formservice.model.FormData; -import com.tarento.formservice.model.IncomingData; -import com.tarento.formservice.model.KeyValue; -import com.tarento.formservice.model.KeyValueList; -import com.tarento.formservice.model.SearchObject; -import com.tarento.formservice.model.SearchRequestDto; -import com.tarento.formservice.model.Status; -import com.tarento.formservice.model.UserInfo; import com.tarento.formservice.models.Form; import com.tarento.formservice.models.FormDetail; import com.tarento.formservice.service.FormsService; @@ -169,6 +160,22 @@ public class FormsController { return ResponseGenerator.failureResponse(Constants.ResponseMessages.ERROR_MESSAGE); } + @PostMapping(value = PathRoutes.FormServiceApi.GET_INSTITUTE_DATA, produces = MediaType.APPLICATION_JSON_VALUE) + public String getInstituteData( + @RequestHeader(value = Constants.Parameters.X_USER_INFO, required = false) String xUserInfo, + @RequestBody InstituteDownloadRequestDto instituteDownloadRequestDto) throws JsonProcessingException { + List<Map<String, Object>> responseData = new ArrayList<>(); + UserInfo userInfo = null; + if (StringUtils.isNotBlank(xUserInfo)) { + userInfo = new Gson().fromJson(xUserInfo, UserInfo.class); + } + responseData = formsService.getInstitutesData(userInfo, instituteDownloadRequestDto); + if (responseData != null) { + return ResponseGenerator.successResponse(responseData); + } + return ResponseGenerator.failureResponse(Constants.ResponseMessages.ERROR_MESSAGE); + } + @GetMapping(value = PathRoutes.FormServiceApi.GET_APPLICATIONS_STATUS_COUNT, produces = MediaType.APPLICATION_JSON_VALUE) public String getApplicationsStatusCount( @RequestHeader(value = Constants.Parameters.X_USER_INFO, required = false) String xUserInfo) diff --git a/src/main/java/com/tarento/formservice/model/InstituteCSVResponseDto.java b/src/main/java/com/tarento/formservice/model/InstituteCSVResponseDto.java new file mode 100644 index 0000000000000000000000000000000000000000..21991bc1d98c92bad65d2eb590f41990040d2c32 --- /dev/null +++ b/src/main/java/com/tarento/formservice/model/InstituteCSVResponseDto.java @@ -0,0 +1,11 @@ +package com.tarento.formservice.model; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class InstituteCSVResponseDto { + + private String data; +} diff --git a/src/main/java/com/tarento/formservice/model/InstituteDownloadRequestDto.java b/src/main/java/com/tarento/formservice/model/InstituteDownloadRequestDto.java new file mode 100644 index 0000000000000000000000000000000000000000..ecb7fc35d4925ce4370d51dad551795579850974 --- /dev/null +++ b/src/main/java/com/tarento/formservice/model/InstituteDownloadRequestDto.java @@ -0,0 +1,15 @@ +package com.tarento.formservice.model; + +import lombok.Getter; +import lombok.Setter; + +import java.util.List; + +@Getter +@Setter +public class InstituteDownloadRequestDto { + + private Integer page; + private Integer size; +} + diff --git a/src/main/java/com/tarento/formservice/repository/ElasticSearchRepository.java b/src/main/java/com/tarento/formservice/repository/ElasticSearchRepository.java index 43cbd793bc49ad553ec5b015322e669ca61ce594..9b2f7d0c563dddddd70be71f4aba9b2aac4571ee 100644 --- a/src/main/java/com/tarento/formservice/repository/ElasticSearchRepository.java +++ b/src/main/java/com/tarento/formservice/repository/ElasticSearchRepository.java @@ -25,6 +25,8 @@ import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback; import org.elasticsearch.client.RestHighLevelClient; +import org.elasticsearch.client.core.CountRequest; +import org.elasticsearch.client.core.CountResponse; import org.elasticsearch.common.xcontent.XContentType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -268,4 +270,15 @@ public class ElasticSearchRepository { } return response; } + + public CountResponse executeCountRequest(CountRequest countRequest) { + CountResponse response = null; + try { + response = client.count(countRequest, RequestOptions.DEFAULT); + } catch (IOException e) { + LOGGER.error(marker, "Encountered an error while connecting : ", e); + LOGGER.error(marker, "Error Message to report : {}", e.getMessage()); + } + return response; + } } diff --git a/src/main/java/com/tarento/formservice/service/FormsService.java b/src/main/java/com/tarento/formservice/service/FormsService.java index e87ef79e666fd34e6719e6cead21136887db5630..b1845da6fc9db449f24f3b2ea9e9a01a9755f112 100644 --- a/src/main/java/com/tarento/formservice/service/FormsService.java +++ b/src/main/java/com/tarento/formservice/service/FormsService.java @@ -5,17 +5,9 @@ import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentMap; +import com.tarento.formservice.model.*; import org.springframework.web.multipart.MultipartFile; -import com.tarento.formservice.model.AssignApplication; -import com.tarento.formservice.model.Consent; -import com.tarento.formservice.model.IncomingData; -import com.tarento.formservice.model.KeyValueList; -import com.tarento.formservice.model.ResponseData; -import com.tarento.formservice.model.SearchRequestDto; -import com.tarento.formservice.model.State; -import com.tarento.formservice.model.StateMatrix; -import com.tarento.formservice.model.UserInfo; import com.tarento.formservice.models.Form; import com.tarento.formservice.models.FormDetail; @@ -43,6 +35,8 @@ public interface FormsService { List<Map<String, Object>> getAllPlainForms(); + List<Map<String, Object>> getInstitutesData(UserInfo userInfo, InstituteDownloadRequestDto instituteDownloadRequestDto); + KeyValueList getApplicationsStatusCount(UserInfo userInfo); public Boolean saveFormSubmitv1(IncomingData incomingData, UserInfo userInfo, String action); diff --git a/src/main/java/com/tarento/formservice/service/impl/FormsServiceImpl.java b/src/main/java/com/tarento/formservice/service/impl/FormsServiceImpl.java index 3d667c798794048f6df45bbde3b7e8145a50ba47..6c7a3c5ad7f9e1bd9b9d4e9fbf0b3ba746fe53d0 100644 --- a/src/main/java/com/tarento/formservice/service/impl/FormsServiceImpl.java +++ b/src/main/java/com/tarento/formservice/service/impl/FormsServiceImpl.java @@ -13,10 +13,13 @@ import java.util.Random; import java.util.concurrent.ConcurrentMap; import java.util.stream.Collectors; +import com.tarento.formservice.model.*; import org.apache.commons.lang3.StringUtils; import org.elasticsearch.action.search.MultiSearchResponse; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.client.core.CountRequest; +import org.elasticsearch.client.core.CountResponse; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.RangeQueryBuilder; @@ -48,23 +51,6 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; import com.tarento.formservice.dao.FormsDao; -import com.tarento.formservice.model.AssignApplication; -import com.tarento.formservice.model.Assignee; -import com.tarento.formservice.model.Consent; -import com.tarento.formservice.model.IncomingData; -import com.tarento.formservice.model.KeyValue; -import com.tarento.formservice.model.KeyValueList; -import com.tarento.formservice.model.ResponseData; -import com.tarento.formservice.model.Result; -import com.tarento.formservice.model.Role; -import com.tarento.formservice.model.Roles; -import com.tarento.formservice.model.SearchObject; -import com.tarento.formservice.model.SearchRequestDto; -import com.tarento.formservice.model.State; -import com.tarento.formservice.model.StateMatrix; -import com.tarento.formservice.model.Status; -import com.tarento.formservice.model.UserInfo; -import com.tarento.formservice.model.WorkflowDto; import com.tarento.formservice.models.Field; import com.tarento.formservice.models.Form; import com.tarento.formservice.models.FormDetail; @@ -380,6 +366,65 @@ public class FormsServiceImpl implements FormsService { return null; } + @Override + public List<Map<String, Object>> getInstitutesData(UserInfo userInfo, InstituteDownloadRequestDto instituteDownloadRequestDto) { + try { + // count builder + CountRequest countRequest = new CountRequest(appConfig.getFormDataIndex()).query(QueryBuilders.matchAllQuery()); + CountResponse countResponse = elasticRepository.executeCountRequest(countRequest); + LOGGER.debug("count Response : {}", countResponse); + if(countResponse == null || countResponse.getCount() <= 0) { + // no data in index + return null; + } + // query builder + SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); + // setting size and range in query + setSizeAndRangeLimit(instituteDownloadRequestDto, searchSourceBuilder, countResponse); + // creating bool query + BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery(); + boolBuilder.must(QueryBuilders.matchAllQuery()); + searchSourceBuilder.query(boolBuilder).sort(Constants.TIMESTAMP, SortOrder.DESC); + // es call + SearchRequest searchRequest = new SearchRequest(appConfig.getFormDataIndex()).source(searchSourceBuilder); + LOGGER.info("Search Request : " + searchRequest); + return formsDao.searchResponse(searchRequest); + } catch (Exception e) { + LOGGER.error(String.format(Constants.EXCEPTION, "getApplications", e.getMessage())); + } + return null; + } + + private static void setSizeAndRangeLimit(InstituteDownloadRequestDto instituteDownloadRequestDto, + SearchSourceBuilder searchSourceBuilder, + CountResponse countResponse) { + // setting from limit if size is present in request + if(instituteDownloadRequestDto.getSize() != null && instituteDownloadRequestDto.getSize() > 0) { + searchSourceBuilder.size(instituteDownloadRequestDto.getSize()); + int fromRange = 0; + if(instituteDownloadRequestDto.getPage() == 0) { + fromRange = instituteDownloadRequestDto.getSize()*(instituteDownloadRequestDto.getPage()); + } else if(instituteDownloadRequestDto.getPage() > 0) { + fromRange = instituteDownloadRequestDto.getSize()*(instituteDownloadRequestDto.getPage()-1); + } + searchSourceBuilder.from(fromRange); + } else { + int count = Long.valueOf(countResponse.getCount()).intValue(); + searchSourceBuilder.size(count); + } + } + + public InstituteCSVResponseDto createCSVFile(List<Map<String, Object>> instituteData) { + if(instituteData == null || instituteData.isEmpty()) { + //return error + } + for(Map<String, Object> data : instituteData) { + System.out.print("data : "+data); + } + return null; + } + + /** * Add filters in search response */ diff --git a/src/main/java/com/tarento/formservice/utils/CloudStorage.java b/src/main/java/com/tarento/formservice/utils/CloudStorage.java index 3da215e492fb1ee26e360ee5bbe28c40e198abfa..e2efa7b67e418a03beeabed23726d8f6f831cf00 100644 --- a/src/main/java/com/tarento/formservice/utils/CloudStorage.java +++ b/src/main/java/com/tarento/formservice/utils/CloudStorage.java @@ -15,7 +15,7 @@ import org.sunbird.cloud.storage.factory.StorageServiceFactory; import scala.Option; -@Service + public class CloudStorage { public static final Logger LOGGER = LoggerFactory.getLogger(CloudStorage.class); diff --git a/src/main/java/com/tarento/formservice/utils/PathRoutes.java b/src/main/java/com/tarento/formservice/utils/PathRoutes.java index 162a218e2a4ed17487981fea171f6c1ead74c375..8a2d7278af6b15ec185fe0794f31a722efa1e21b 100644 --- a/src/main/java/com/tarento/formservice/utils/PathRoutes.java +++ b/src/main/java/com/tarento/formservice/utils/PathRoutes.java @@ -11,6 +11,7 @@ public interface PathRoutes { final String SAVE_FORM_SUBMIT_V1 = "/v1/saveFormSubmit"; final String SAVE_FORM_SUBMIT_BULK = "/saveFormSubmitBulk"; final String GET_ALL_APPLICATIONS = "/getAllApplications"; + final String GET_INSTITUTE_DATA = "/getInstituteData"; final String GET_APPLICATIONS_BY_ID = "/getApplicationsById"; final String GET_APPLICATIONS_STATUS_COUNT = "/getApplicationsStatusCount"; final String FILE_UPLOAD = "/fileUpload"; diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index af21aad3f3347be254878f3e0f3062d2280c69da..dcc76a8701e532dd519038284ce8c22d91cb2a76 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,14 +1,14 @@ server.port=8099 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver -spring.datasource.url=jdbc:mysql://localhost:3306/retail_user -spring.datasource.username=retail_user -spring.datasource.password=ReTail123T +spring.datasource.url=jdbc:mysql://159.223.143.139:3306/retail_user +spring.datasource.username=root +spring.datasource.password=Testing@123 #----------------------------- ELASTIC SEARCH CONFIGURATIONS ------------------------------# -services.esindexer.host=elastic.pulz.app +services.esindexer.host=159.223.143.139 services.esindexer.host.name=elastic.pulz.app -services.esindexer.host.port=443 +services.esindexer.host.port=9200 services.esindexer.username=elastic services.esindexer.password=PuAz@itAqwsaR34bYu # INDEX AND DOCUMENT TYPES