package in.gov.chennaicorporation.gccoffice.school.Controller; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import in.gov.chennaicorporation.gccoffice.school.Service.DropdownsService; @RestController @RequestMapping("/gcc/api/gccschool/dropdown") public class DropdownsController { @Autowired private DropdownsService dropdownsService; @Autowired public DropdownsController(DropdownsService dropdownsService) { this.dropdownsService = dropdownsService; } // To Get Court Dropdown Data @GetMapping("/getCourtData") public ResponseEntity> getCourtData() { try { List> data = dropdownsService.getCourtData(); Map response = new HashMap<>(); if (data.size() != 0) { response.put("status", "OK"); response.put("data", data); } else { response.put("status", "Failed"); response.put("data", "No Data Found"); } return ResponseEntity.ok(response); } catch (Exception e) { // Log the exception details for debugging e.printStackTrace(); // Provide a user-friendly error message return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(Map.of("status", "Error", "message", "Error getting data: " + e.getMessage())); } } // To Get Equipmengt Dropdown Data @GetMapping("/getEquipmentData") public ResponseEntity> getEquipmentData() { try { List> data = dropdownsService.getEquipmentData(); Map response = new HashMap<>(); if (data.size() != 0) { response.put("status", "OK"); response.put("data", data); } else { response.put("status", "Failed"); response.put("data", "No Data Found"); } return ResponseEntity.ok(response); } catch (Exception e) { // Log the exception details for debugging e.printStackTrace(); // Provide a user-friendly error message return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(Map.of("status", "Error", "message", "Error getting data: " + e.getMessage())); } } // To Get Building Material Dropdown Data @GetMapping("/getBuildingMaterial") public ResponseEntity> getBuildingMaterial() { try { List> data = dropdownsService.getBuildingMaterial(); Map response = new HashMap<>(); if (data.size() != 0) { response.put("status", "OK"); response.put("data", data); } else { response.put("status", "Failed"); response.put("data", "No Data Found"); } return ResponseEntity.ok(response); } catch (Exception e) { // Log the exception details for debugging e.printStackTrace(); // Provide a user-friendly error message return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(Map.of("status", "Error", "message", "Error getting data: " + e.getMessage())); } } // To Get Roof Material Dropdown Data @GetMapping("/getRoofMaterial") public ResponseEntity> getRoofMaterial() { try { List> data = dropdownsService.getRoofMaterial(); Map response = new HashMap<>(); if (data.size() != 0) { response.put("status", "OK"); response.put("data", data); } else { response.put("status", "Failed"); response.put("data", "No Data Found"); } return ResponseEntity.ok(response); } catch (Exception e) { // Log the exception details for debugging e.printStackTrace(); // Provide a user-friendly error message return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(Map.of("status", "Error", "message", "Error getting data: " + e.getMessage())); } } // To Get Water Source Dropdown Data @GetMapping("/getWaterSource") public ResponseEntity> getWaterSource() { try { List> data = dropdownsService.getWaterSource(); Map response = new HashMap<>(); if (data.size() != 0) { response.put("status", "OK"); response.put("data", data); } else { response.put("status", "Failed"); response.put("data", "No Data Found"); } return ResponseEntity.ok(response); } catch (Exception e) { // Log the exception details for debugging e.printStackTrace(); // Provide a user-friendly error message return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(Map.of("status", "Error", "message", "Error getting data: " + e.getMessage())); } } // To Get Water Storage Dropdown Data @GetMapping("/getWaterStorage") public ResponseEntity> getWaterStorage() { try { List> data = dropdownsService.getWaterStorage(); Map response = new HashMap<>(); if (data.size() != 0) { response.put("status", "OK"); response.put("data", data); } else { response.put("status", "Failed"); response.put("data", "No Data Found"); } return ResponseEntity.ok(response); } catch (Exception e) { // Log the exception details for debugging e.printStackTrace(); // Provide a user-friendly error message return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(Map.of("status", "Error", "message", "Error getting data: " + e.getMessage())); } } // To Get Rooms Dropdown Data @GetMapping("/getRooms") public ResponseEntity> getRooms() { try { List> data = dropdownsService.getRooms(); Map response = new HashMap<>(); if (data.size() != 0) { response.put("status", "OK"); response.put("data", data); } else { response.put("status", "Failed"); response.put("data", "No Data Found"); } return ResponseEntity.ok(response); } catch (Exception e) { // Log the exception details for debugging e.printStackTrace(); // Provide a user-friendly error message return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(Map.of("status", "Error", "message", "Error getting data: " + e.getMessage())); } } @GetMapping("/getFloorsByBuilding") public ResponseEntity> getFloorsByBuilding(@RequestParam("building_id") int buildingId) { try { List> data = dropdownsService.getFloorsByBuilding(buildingId); Map response = new HashMap<>(); if (!data.isEmpty()) { response.put("status", "OK"); response.put("data", data); } else { response.put("status", "Failed"); response.put("data", "No floors found for the selected building."); } return ResponseEntity.ok(response); } catch (Exception e) { e.printStackTrace(); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(Map.of("status", "Error", "message", "Failed to fetch floors.")); } } @GetMapping("/getBuildingsBySchoolId") public ResponseEntity> getBuildingsBySchoolId(@RequestParam int school_id) { try { List> data = dropdownsService.getBuildingsBySchoolId(school_id); Map response = new HashMap<>(); if (!data.isEmpty()) { response.put("status", "OK"); response.put("data", data); } else { response.put("status", "Failed"); response.put("data", "No buildings found for the selected school."); } return ResponseEntity.ok(response); } catch (Exception e) { e.printStackTrace(); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(Map.of("status", "Error", "message", "Failed to fetch buildings.")); } } @GetMapping("/getDropdownValues") public List> getDropdownValues(@RequestParam String user_id) { List> resultList = new ArrayList<>(); Map response = new HashMap<>(); String schoolId = dropdownsService.getSchoolId(user_id); if (schoolId == null || schoolId.trim().isEmpty() || schoolId.equalsIgnoreCase("error")) { response.put("Message", "Failure"); response.put("Data", List.of()); resultList.add(response); return resultList; } List> udiseData = dropdownsService.getUdiseNumbersBySchoolId(schoolId); if (!udiseData.isEmpty()) { List udiseList = udiseData.stream().map(m -> (String) m.get("udise")).collect(Collectors.toList()); response.put("Message", "Success"); response.put("Data", udiseList); } else { response.put("Message", "No Data Found"); response.put("Data", List.of()); } resultList.add(response); return resultList; } @GetMapping("/getCategoryValues") public List> getCategoryValues(@RequestParam String user_id) { List> resultList = new ArrayList<>(); Map response = new HashMap<>(); String schoolId = dropdownsService.getSchoolId(user_id); if (schoolId == null || schoolId.trim().isEmpty() || schoolId.equalsIgnoreCase("error")) { response.put("Message", "Failure"); response.put("Data", List.of()); resultList.add(response); return resultList; } List> categoryData = dropdownsService.getCategoryBySchoolId(schoolId); if (!categoryData.isEmpty()) { List categoryList = categoryData.stream().map(m -> (String) m.get("category")) .collect(Collectors.toList()); response.put("Message", "Success"); response.put("Data", categoryList); } else { response.put("Message", "No Data Found"); response.put("Data", List.of()); } resultList.add(response); return resultList; } @GetMapping("/getZoneValues") public List> getZoneValues(@RequestParam String user_id) { List> resultList = new ArrayList<>(); Map response = new HashMap<>(); String schoolId = dropdownsService.getSchoolId(user_id); if (schoolId == null || schoolId.trim().isEmpty() || schoolId.equalsIgnoreCase("error")) { response.put("Message", "Failure"); response.put("Data", List.of()); resultList.add(response); return resultList; } List> zoneData = dropdownsService.getZoneBySchoolId(schoolId); if (!zoneData.isEmpty()) { List zoneList = zoneData.stream().map(m -> (String) m.get("zone")).collect(Collectors.toList()); response.put("Message", "Success"); response.put("Data", zoneList); } else { response.put("Message", "No Data Found"); response.put("Data", List.of()); } resultList.add(response); return resultList; } @GetMapping("/getwardValues") public List> getWardValues(@RequestParam String user_id) { List> resultList = new ArrayList<>(); Map response = new HashMap<>(); String schoolId = dropdownsService.getSchoolId(user_id); if (schoolId == null || schoolId.trim().isEmpty() || schoolId.equalsIgnoreCase("error")) { response.put("Message", "Failure"); response.put("Data", List.of()); resultList.add(response); return resultList; } List> wardData = dropdownsService.getWardBySchoolId(schoolId); if (!wardData.isEmpty()) { List wardList = wardData.stream().map(m -> (String) m.get("division")).collect(Collectors.toList()); response.put("Message", "Success"); response.put("Data", wardList); } else { response.put("Message", "No Data Found"); response.put("Data", List.of()); } resultList.add(response); return resultList; } @GetMapping("/getAllDropdownValues") public ResponseEntity getAllDropdownValues(@RequestParam String user_id, @RequestParam String type) { Map response = new HashMap<>(); List dropdownList = Collections.emptyList(); // First get school_id(s) for this user String schoolId = dropdownsService.getSchoolId(user_id); if (schoolId.equals("error")) { response.put("Message", "Failed to fetch school ID"); response.put("Data", dropdownList); return ResponseEntity.ok(Collections.singletonList(response)); } try { List> rawData; switch (type.toLowerCase()) { case "udise": rawData = dropdownsService.getUdiseNumbersBySchoolId(schoolId); dropdownList = rawData.stream().map(map -> String.valueOf(map.get("udise"))) .collect(Collectors.toList()); break; case "category": rawData = dropdownsService.getCategoryBySchoolId(schoolId); dropdownList = rawData.stream().map(map -> String.valueOf(map.get("category"))).distinct() .collect(Collectors.toList()); break; case "zone": rawData = dropdownsService.getZoneBySchoolId(schoolId); dropdownList = rawData.stream().map(map -> String.valueOf(map.get("zone"))).distinct() .collect(Collectors.toList()); break; case "ward": rawData = dropdownsService.getWardBySchoolId(schoolId); dropdownList = rawData.stream().map(map -> String.valueOf(map.get("division"))).distinct() .collect(Collectors.toList()); break; default: response.put("Message", "Invalid dropdown type"); response.put("Data", dropdownList); return ResponseEntity.badRequest().body(Collections.singletonList(response)); } response.put("Message", "Success"); response.put("Data", dropdownList); return ResponseEntity.ok(Collections.singletonList(response)); } catch (Exception e) { e.printStackTrace(); response.put("Message", "Error while processing dropdown values"); response.put("Data", dropdownList); return ResponseEntity.ok(Collections.singletonList(response)); } } // To Get Asset Types Data @GetMapping("/getAssetType") public ResponseEntity> getAssetType() { try { List> data = dropdownsService.getAssetType(); Map response = new HashMap<>(); if (data.size() != 0) { response.put("status", "OK"); response.put("data", data); } else { response.put("status", "Failed"); response.put("data", "No Data Found"); } return ResponseEntity.ok(response); } catch (Exception e) { // Log the exception details for debugging e.printStackTrace(); // Provide a user-friendly error message return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(Map.of("status", "Error", "message", "Error getting data: " + e.getMessage())); } } // To Get Sub Asset Types Data @GetMapping("/getSubAssetType") public ResponseEntity> getSubAssetType(int assetid) { try { List> data = dropdownsService.getSubAssetType(assetid); Map response = new HashMap<>(); if (data.size() != 0) { response.put("status", "OK"); response.put("data", data); } else { response.put("status", "Failed"); response.put("data", "No Data Found"); } return ResponseEntity.ok(response); } catch (Exception e) { e.printStackTrace(); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(Map.of("status", "Error", "message", "Error getting data: " + e.getMessage())); } } @GetMapping("/getSplSubAssetType") public ResponseEntity> getSplSubAssetType(int school_id, int assetid) { try { List assets = dropdownsService.getAssetList(school_id, assetid); List> data = dropdownsService.getSplSubAssetType(assets, assetid); Map response = new HashMap<>(); if (data.size() != 0) { response.put("status", "OK"); response.put("data", data); } else { response.put("status", "Failed"); response.put("data", "No Data Found"); } return ResponseEntity.ok(response); } catch (Exception e) { e.printStackTrace(); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(Map.of("status", "Error", "message", "Error getting data: " + e.getMessage())); } } }