diff --git a/pgr-services/src/main/java/org/egov/pgr/repository/rowmapper/PGRQueryBuilder.java b/pgr-services/src/main/java/org/egov/pgr/repository/rowmapper/PGRQueryBuilder.java index 14d9e1dda9..c522b65d3a 100644 --- a/pgr-services/src/main/java/org/egov/pgr/repository/rowmapper/PGRQueryBuilder.java +++ b/pgr-services/src/main/java/org/egov/pgr/repository/rowmapper/PGRQueryBuilder.java @@ -1,10 +1,12 @@ package org.egov.pgr.repository.rowmapper; import org.egov.pgr.web.models.RequestSearchCriteria; +import org.egov.tracer.model.CustomException; import org.springframework.stereotype.Repository; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; +import java.time.Instant; import java.util.Collection; import java.util.List; import java.util.Set; @@ -33,22 +35,31 @@ public String getPGRSearchQuery(RequestSearchCriteria criteria, List pre StringBuilder builder = new StringBuilder(QUERY); - if(criteria.getTenantId() != null) { - String tenantId = criteria.getTenantId(); - - String[] tenantIdChunks = tenantId.split("\\."); - - if (tenantIdChunks.length == 1) { + if(criteria.getIsPlainSearch() != null && criteria.getIsPlainSearch()){ + Set tenantIds = criteria.getTenantIds(); + if(!CollectionUtils.isEmpty(tenantIds)){ addClauseIfRequired(preparedStmtList, builder); - builder.append(" ser.tenantid LIKE ? "); - preparedStmtList.add(criteria.getTenantId() + '%'); - } else { - addClauseIfRequired(preparedStmtList, builder); - builder.append(" ser.tenantid=? "); - preparedStmtList.add(criteria.getTenantId()); + builder.append(" ser.tenantId IN (").append(createQuery(tenantIds)).append(")"); + addToPreparedStatement(preparedStmtList, tenantIds); + } + } + else { + if (criteria.getTenantId() != null) { + String tenantId = criteria.getTenantId(); + + String[] tenantIdChunks = tenantId.split("\\."); + + if (tenantIdChunks.length == 1) { + addClauseIfRequired(preparedStmtList, builder); + builder.append(" ser.tenantid LIKE ? "); + preparedStmtList.add(criteria.getTenantId() + '%'); + } else { + addClauseIfRequired(preparedStmtList, builder); + builder.append(" ser.tenantid=? "); + preparedStmtList.add(criteria.getTenantId()); + } } } - Set serviceCodes = criteria.getServiceCode(); if (!CollectionUtils.isEmpty(serviceCodes)) { addClauseIfRequired(preparedStmtList, builder); @@ -91,6 +102,26 @@ public String getPGRSearchQuery(RequestSearchCriteria criteria, List pre addToPreparedStatement(preparedStmtList, localities); } + if (criteria.getFromDate() != null) { + addClauseIfRequired(preparedStmtList, builder); + + //If user does not specify toDate, take today's date as toDate by default. + if (criteria.getToDate() == null) { + criteria.setToDate(Instant.now().toEpochMilli()); + } + + builder.append(" ser.createdtime BETWEEN ? AND ?"); + preparedStmtList.add(criteria.getFromDate()); + preparedStmtList.add(criteria.getToDate()); + + } else { + //if only toDate is provided as parameter without fromDate parameter, throw an exception. + if (criteria.getToDate() != null) { + throw new CustomException("INVALID_SEARCH", "Cannot specify to-Date without a from-Date"); + } + } + + addOrderByClause(builder, criteria); addLimitAndOffset(builder, criteria, preparedStmtList); diff --git a/pgr-services/src/main/java/org/egov/pgr/service/PGRService.java b/pgr-services/src/main/java/org/egov/pgr/service/PGRService.java index d714ae137c..982a1ac0d2 100644 --- a/pgr-services/src/main/java/org/egov/pgr/service/PGRService.java +++ b/pgr-services/src/main/java/org/egov/pgr/service/PGRService.java @@ -87,6 +87,8 @@ public List search(RequestInfo requestInfo, RequestSearchCriteri if(criteria.getMobileNumber()!=null && CollectionUtils.isEmpty(criteria.getUserIds())) return new ArrayList<>(); + criteria.setIsPlainSearch(false); + List serviceWrappers = repository.getServiceWrappers(criteria); if(CollectionUtils.isEmpty(serviceWrappers)) @@ -133,11 +135,49 @@ public ServiceRequest update(ServiceRequest request){ * @return */ public Integer count(RequestInfo requestInfo, RequestSearchCriteria criteria){ + criteria.setIsPlainSearch(false); Integer count = repository.getCount(criteria); return count; } + public List plainSearch(RequestInfo requestInfo, RequestSearchCriteria criteria) { + validator.validatePlainSearch(criteria); + + criteria.setIsPlainSearch(true); + + if(criteria.getLimit()==null) + criteria.setLimit(config.getDefaultLimit()); + + if(criteria.getOffset()==null) + criteria.setOffset(config.getDefaultOffset()); + + if(criteria.getLimit()!=null && criteria.getLimit() > config.getMaxLimit()) + criteria.setLimit(config.getMaxLimit()); + + List serviceWrappers = repository.getServiceWrappers(criteria); + + if(CollectionUtils.isEmpty(serviceWrappers)){ + return new ArrayList<>(); + } + userService.enrichUsers(serviceWrappers); + List enrichedServiceWrappers = workflowService.enrichWorkflow(requestInfo, serviceWrappers); + Map> sortedWrappers = new TreeMap<>(Collections.reverseOrder()); + for(ServiceWrapper svc : enrichedServiceWrappers){ + if(sortedWrappers.containsKey(svc.getService().getAuditDetails().getCreatedTime())){ + sortedWrappers.get(svc.getService().getAuditDetails().getCreatedTime()).add(svc); + }else{ + List serviceWrapperList = new ArrayList<>(); + serviceWrapperList.add(svc); + sortedWrappers.put(svc.getService().getAuditDetails().getCreatedTime(), serviceWrapperList); + } + } + List sortedServiceWrappers = new ArrayList<>(); + for(Long createdTimeDesc : sortedWrappers.keySet()){ + sortedServiceWrappers.addAll(sortedWrappers.get(createdTimeDesc)); + } + return sortedServiceWrappers; + } } diff --git a/pgr-services/src/main/java/org/egov/pgr/validator/ServiceRequestValidator.java b/pgr-services/src/main/java/org/egov/pgr/validator/ServiceRequestValidator.java index 070d4821aa..58281f2eb3 100644 --- a/pgr-services/src/main/java/org/egov/pgr/validator/ServiceRequestValidator.java +++ b/pgr-services/src/main/java/org/egov/pgr/validator/ServiceRequestValidator.java @@ -63,6 +63,7 @@ public void validateUpdate(ServiceRequest request, Object mdmsData){ validateDepartment(request, mdmsData); validateReOpen(request); RequestSearchCriteria criteria = RequestSearchCriteria.builder().ids(Collections.singleton(id)).build(); + criteria.setIsPlainSearch(false); List serviceWrappers = repository.getServiceWrappers(criteria); if(CollectionUtils.isEmpty(serviceWrappers)) @@ -276,5 +277,9 @@ private void validateSource(String source){ } - + public void validatePlainSearch(RequestSearchCriteria criteria) { + if(CollectionUtils.isEmpty(criteria.getTenantIds())){ + throw new CustomException("TENANT_ID_LIST_EMPTY", "Tenant ids not provided for searching."); + } + } } diff --git a/pgr-services/src/main/java/org/egov/pgr/web/controllers/RequestsApiController.java b/pgr-services/src/main/java/org/egov/pgr/web/controllers/RequestsApiController.java index 05d6560db8..09f34237b6 100644 --- a/pgr-services/src/main/java/org/egov/pgr/web/controllers/RequestsApiController.java +++ b/pgr-services/src/main/java/org/egov/pgr/web/controllers/RequestsApiController.java @@ -61,6 +61,15 @@ public ResponseEntity requestsSearchPost(@Valid @RequestBody Re } + @RequestMapping(value = "request/_plainsearch", method = RequestMethod.POST) + public ResponseEntity requestsPlainSearchPost(@Valid @RequestBody RequestInfoWrapper requestInfoWrapper, @Valid @ModelAttribute RequestSearchCriteria requestSearchCriteria) { + List serviceWrappers = pgrService.plainSearch(requestInfoWrapper.getRequestInfo(), requestSearchCriteria); + ResponseInfo responseInfo = responseInfoFactory.createResponseInfoFromRequestInfo(requestInfoWrapper.getRequestInfo(), true); + ServiceResponse response = ServiceResponse.builder().responseInfo(responseInfo).serviceWrappers(serviceWrappers).build(); + return new ResponseEntity<>(response, HttpStatus.OK); + + } + @RequestMapping(value="/request/_update", method = RequestMethod.POST) public ResponseEntity requestsUpdatePost(@Valid @RequestBody ServiceRequest request) throws IOException { ServiceRequest enrichedReq = pgrService.update(request); diff --git a/pgr-services/src/main/java/org/egov/pgr/web/models/RequestSearchCriteria.java b/pgr-services/src/main/java/org/egov/pgr/web/models/RequestSearchCriteria.java index 8d3a9334d7..59bc45beaa 100644 --- a/pgr-services/src/main/java/org/egov/pgr/web/models/RequestSearchCriteria.java +++ b/pgr-services/src/main/java/org/egov/pgr/web/models/RequestSearchCriteria.java @@ -19,6 +19,9 @@ public class RequestSearchCriteria { @JsonProperty("tenantId") private String tenantId; + @JsonProperty("tenantIds") + private Set tenantIds; + @JsonProperty("serviceCode") private Set serviceCode; @@ -43,6 +46,12 @@ public class RequestSearchCriteria { @JsonProperty("ids") private Set ids; + @JsonProperty("fromDate") + private Long fromDate; + + @JsonProperty("toDate") + private Long toDate; + @JsonProperty("limit") private Integer limit; @@ -52,6 +61,9 @@ public class RequestSearchCriteria { @JsonIgnore private Set userIds; + @JsonIgnore + private Boolean isPlainSearch; + public enum SortOrder { ASC,