Skip to content

Commit

Permalink
Tenant reindex (#616)
Browse files Browse the repository at this point in the history
* [RAIN-1726] Added plainSearch API with date and multiple tenant wise reindexing (#612)

* Fixes search query builder (#613)
  • Loading branch information
shashwat-egov authored Jan 17, 2021
1 parent 8a4d6d3 commit cfabecd
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -33,22 +35,31 @@ public String getPGRSearchQuery(RequestSearchCriteria criteria, List<Object> 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<String> 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<String> serviceCodes = criteria.getServiceCode();
if (!CollectionUtils.isEmpty(serviceCodes)) {
addClauseIfRequired(preparedStmtList, builder);
Expand Down Expand Up @@ -91,6 +102,26 @@ public String getPGRSearchQuery(RequestSearchCriteria criteria, List<Object> 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);
Expand Down
40 changes: 40 additions & 0 deletions pgr-services/src/main/java/org/egov/pgr/service/PGRService.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public List<ServiceWrapper> search(RequestInfo requestInfo, RequestSearchCriteri
if(criteria.getMobileNumber()!=null && CollectionUtils.isEmpty(criteria.getUserIds()))
return new ArrayList<>();

criteria.setIsPlainSearch(false);

List<ServiceWrapper> serviceWrappers = repository.getServiceWrappers(criteria);

if(CollectionUtils.isEmpty(serviceWrappers))
Expand Down Expand Up @@ -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<ServiceWrapper> 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<ServiceWrapper> serviceWrappers = repository.getServiceWrappers(criteria);

if(CollectionUtils.isEmpty(serviceWrappers)){
return new ArrayList<>();
}

userService.enrichUsers(serviceWrappers);
List<ServiceWrapper> enrichedServiceWrappers = workflowService.enrichWorkflow(requestInfo, serviceWrappers);

Map<Long, List<ServiceWrapper>> 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<ServiceWrapper> serviceWrapperList = new ArrayList<>();
serviceWrapperList.add(svc);
sortedWrappers.put(svc.getService().getAuditDetails().getCreatedTime(), serviceWrapperList);
}
}
List<ServiceWrapper> sortedServiceWrappers = new ArrayList<>();
for(Long createdTimeDesc : sortedWrappers.keySet()){
sortedServiceWrappers.addAll(sortedWrappers.get(createdTimeDesc));
}
return sortedServiceWrappers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<ServiceWrapper> serviceWrappers = repository.getServiceWrappers(criteria);

if(CollectionUtils.isEmpty(serviceWrappers))
Expand Down Expand Up @@ -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.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ public ResponseEntity<ServiceResponse> requestsSearchPost(@Valid @RequestBody Re

}

@RequestMapping(value = "request/_plainsearch", method = RequestMethod.POST)
public ResponseEntity<ServiceResponse> requestsPlainSearchPost(@Valid @RequestBody RequestInfoWrapper requestInfoWrapper, @Valid @ModelAttribute RequestSearchCriteria requestSearchCriteria) {
List<ServiceWrapper> 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<ServiceResponse> requestsUpdatePost(@Valid @RequestBody ServiceRequest request) throws IOException {
ServiceRequest enrichedReq = pgrService.update(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public class RequestSearchCriteria {
@JsonProperty("tenantId")
private String tenantId;

@JsonProperty("tenantIds")
private Set<String> tenantIds;

@JsonProperty("serviceCode")
private Set<String> serviceCode;

Expand All @@ -43,6 +46,12 @@ public class RequestSearchCriteria {
@JsonProperty("ids")
private Set<String> ids;

@JsonProperty("fromDate")
private Long fromDate;

@JsonProperty("toDate")
private Long toDate;

@JsonProperty("limit")
private Integer limit;

Expand All @@ -52,6 +61,9 @@ public class RequestSearchCriteria {
@JsonIgnore
private Set<String> userIds;

@JsonIgnore
private Boolean isPlainSearch;


public enum SortOrder {
ASC,
Expand Down

0 comments on commit cfabecd

Please sign in to comment.