I have a SQL Table In which I want to apply some filters and pagination, I want to filter it on the minRequestTime, maxRequestTime, and the channel, I set the default date to today’s date (when the user will not provide it from the frontend, it will set automatically to today’s date), now the problem is when I don’t provide the date from postman the filters do not work properly,
I am using JPA Projection because of aliases in my sql query.
My Entity
@Entity
@Table(name = "reporting_general")
@Data
public class ReportingGeneral implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int id;
@Column(name = "transaction_name")
public String transactionName;
public String username;
@Column(name = "contact_number")
public String contactNumber;
public String segment;
@Column(name = "user_type")
public String userType;
@Column(name = "primary_key")
public String primaryKey;
public String channel;
@Column(name = "response_code")
public String ResponseCode;
@Column(name = "request_time")
public Date requestTime;
@Column(name = "response_time")
public Date responseTime;
}
Interface I’ve used for JPA Projection
public interface ActiveAccountReport {
String getUserName();
String getContactNumber();
String getPrimaryKey();
LocalDateTime getMinRequestTime();
LocalDateTime getMaxRequestTime();
String getSuccess();
String getFailed();
String getTotalHits();
String getChannel();
}
Class used for RequestBody
@NoArgsConstructor
@AllArgsConstructor
@Data
@Slf4j
public class ActiveAccountRequest {
@Column(name = "startDate")
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime startDate = LocalDateTime.now();
@Column(name = "endDate")
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime endDate = LocalDateTime.now();
private List<String> channel ;
}
My Repository
@Repository
public interface ReportingGenRepo extends JpaRepository<ReportingGeneral, Integer> {
@Query(value = "SELECT REPORTING_GENERAL.USERNAME AS userName, " +
"ANY_VALUE (REPORTING_GENERAL.CONTACT_NUMBER ) AS contactNumber,ANY_VALUE (REPORTING_GENERAL.PRIMARY_KEY) AS primaryKey," +
"MIN( REQUEST_TIME ) AS minRequestTime ,MAX( REQUEST_TIME ) AS maxRequestTime, " +
"COUNT(IF ( RESPONSE_CODE = '1', 1, NULL )) AS success,COUNT(IF " +
"( RESPONSE_CODE != '1', 1, NULL )) AS failed,COUNT(*) AS totalHits,CHANNEL as channel" +
" FROM REPORTING_GENERAL WHERE " +
" REPORTING_GENERAL.ID > 0 AND CHANNEL IN ?3 AND (REPORTING_GENERAL.REQUEST_TIME BETWEEN ?1 AND ?2)" +
"GROUP BY channel, username",
countQuery = "SELECT count(*) FROM REPORTING_GENERAL WHERE " +
" REPORTING_GENERAL.ID > 0 AND CHANNEL IN ?3 AND (REPORTING_GENERAL.REQUEST_TIME BETWEEN ?1 AND ?2)" +
"GROUP BY channel, username ", nativeQuery = true)
public Page<ActiveAccountReport> getActiveAccountReportFilters(
LocalDateTime startDate,
LocalDateTime endDate,
List<String> channel,
Pageable pageable);
@Query(value = "SELECT REPORTING_GENERAL.USERNAME AS userName, " +
"ANY_VALUE (REPORTING_GENERAL.CONTACT_NUMBER ) AS contactNumber,ANY_VALUE (REPORTING_GENERAL.PRIMARY_KEY) AS primaryKey," +
"MIN( REQUEST_TIME ) AS minRequestTime ,MAX( REQUEST_TIME ) AS maxRequestTime, " +
"COUNT(IF ( RESPONSE_CODE = '1', 1, NULL )) AS success,COUNT(IF " +
"( RESPONSE_CODE != '1', 1, NULL )) AS failed,COUNT(*) AS totalHits,CHANNEL as channel" +
" FROM REPORTING_GENERAL WHERE " +
" REPORTING_GENERAL.ID > 0 " +
"GROUP BY channel, username",
countQuery = "SELECT count(*) FROM REPORTING_GENERAL WHERE " +
" REPORTING_GENERAL.ID > 0 " +
"GROUP BY channel, username ", nativeQuery = true)
public Page<ActiveAccountReport> getActiveAccountReport(Pageable pageable);
}
My Service
@Service
public class ReportingGenService {
@Autowired
private ReportingGenRepo reportingGenRepo;
public Page<ActiveAccountReport> paginatedActiveAccountReports(ActiveAccountRequest activeAccountRequest,
Integer page, Integer size) {
Page<ActiveAccountReport> activeAccountReports = null;
Pageable pageable = PageRequest.of(page, size);
if (activeAccountRequest.getStartDate() != null &&
activeAccountRequest.getEndDate() != null &&
activeAccountRequest.getChannel().size() > 0) {
activeAccountReports = reportingGenRepo.getActiveAccountReportFilters(activeAccountRequest.getStartDate(),
activeAccountRequest.getEndDate(), activeAccountRequest.getChannel(), pageable);
}
else {
activeAccountReports = reportingGenRepo.getActiveAccountReport(pageable);
}
return activeAccountReports;
}
}
My Controller
@RestController
@RequestMapping("/repo")
public class ReportingGenController {
@Autowired
private ReportingGenService reportingGenService;
@GetMapping("/get")
public Page<ActiveAccountReport> findAll(
@RequestBody(required = false) ActiveAccountRequest activeAccountRequest,
@RequestParam(value = "page",defaultValue = "0") Integer page,
@RequestParam(value = "size",defaultValue = "5") Integer size){
return reportingGenService.paginatedActiveAccountReports(activeAccountRequest,page,size);
}
}