I have 2 tables Header which has date and Lines which has employee and present. The Header and Lines table have one to many relation ship.
I am getting the listing as below,
[
{
"header": 1,
"date": "2022-09-17",
"line": [
{
"empInOutLineId": 1,
"employee": {
"employeeId": 43,
"employeeName": "test"
},
"present": true
},
{
"empInOutLineId": 15,
"employee": {
"employeeId": 40,
"employeeName": "test1",
"employeeNumber": "1051"
},
"present": true
}
]
},
{
"header": 2,
"date": "2022-09-18",
"line": [
{
"empInOutLineId": 16,
"employee": {
"employeeId": 43,
"employeeName": "test"
},
"present": true
},
{
"empInOutLineId": 17,
"employee": {
"employeeId": 40,
"employeeName": "test1",
"employeeNumber": "1051"
},
"present": false
}
]
}
]
But I want the listing to be like this,
[
{
"employeeName": "K Mangr",
"2022-09-01": true,
"2022-09-02": false,
"2022-09-03": true
},
{
"employeeName": "Divya",
"2022-09-01": true,
"2022-09-02": false,
"2022-09-03": true
}
]
How to achieve this in spring boot rest api?
This is my code for listing header and lines details based on month and year,
@GetMapping("/month-year/{date}")
public ResponseEntity listEmployeeInOutByMonthAndYear(@PathVariable("date") String date) {
System.out.println("month" + date.substring(5, 7));
System.out.println("year" + date.substring(0, 4));
List<EmployeeInOutHeaderResponseEntity> listNew = new ArrayList<EmployeeInOutHeaderResponseEntity>();
List<EmployeeInOutHeader> list = employeeInOutHeaderService.findAllByDateLike("%" + date.substring(0, 7) + "%");
System.out.println("list" + list.size());
if (list.isEmpty()) {
ObjectNode jsonObject = objectMapper.createObjectNode();
jsonObject.put("statusCode", res.setStatusCode(204));
jsonObject.put("message", res.setDescription("no data"));
return new ResponseEntity(jsonObject, HttpStatus.OK);
} else {
for (EmployeeInOutHeader employeeInOutHeader : list) {
if (employeeInOutHeader.getStatus() != null) {
if (!employeeInOutHeader.getStatus().getListTypeValueId().equals(4)) {
EmployeeInOutHeaderResponseEntity employeeInOutHeaderResponseEntity = new EmployeeInOutHeaderResponseEntity(
employeeInOutHeader, 0);
List<EmployeeInOutLine> employeeInOutLineList = new ArrayList<EmployeeInOutLine>();
if (!employeeInOutHeaderResponseEntity.getEmployeeInOutLine().isEmpty()) {
for (EmployeeInOutLine eLine : employeeInOutHeaderResponseEntity.getEmployeeInOutLine()) {
if (eLine.getStatus() != null) {
if (!eLine.getStatus().getListTypeValueId().equals(4)) {
EmployeeInOutLine employeeInOutLine = new EmployeeInOutLine(eLine, "");
if ((employeeInOutLine.getInTime() == null
&& employeeInOutLine.getOutTime() == null)
|| employeeInOutLine.getInTime() == null
|| employeeInOutLine.getOutTime() == null) {
employeeInOutLine.setPresent(false);
} else {
employeeInOutLine.setPresent(true);
}
if (employeeInOutLine.getEmployee() != null) {
Employee emp = new Employee(employeeInOutLine.getEmployee(), "", 0);
employeeInOutLine.setEmployee(emp);
}
if (employeeInOutLine.getStatus() != null) {
ListTypeValues lTypeValues = new ListTypeValues(
employeeInOutLine.getStatus().getListTypeValueId());
employeeInOutLine.setStatus(lTypeValues);
}
employeeInOutLineList.add(employeeInOutLine);
}
}
}
employeeInOutHeaderResponseEntity.setEmployeeInOutLine(employeeInOutLineList);
}
listNew.add(employeeInOutHeaderResponseEntity);
}
}
}
}
if (listNew.isEmpty()) {
ObjectNode jsonObject = objectMapper.createObjectNode();
jsonObject.put("statusCode", res.setStatusCode(204));
jsonObject.put("message", res.setDescription("no data"));
return new ResponseEntity(jsonObject, HttpStatus.OK);
} else {
return new ResponseEntity(listNew, HttpStatus.OK);
}
}
Thanks in Advance!