diff --git a/src/main/java/com/example/Public/Configuration/config/GlobalExceptionHandler.java b/src/main/java/com/example/Public/Configuration/config/GlobalExceptionHandler.java new file mode 100644 index 0000000..85b3eef --- /dev/null +++ b/src/main/java/com/example/Public/Configuration/config/GlobalExceptionHandler.java @@ -0,0 +1,24 @@ +package com.example.Public.Configuration.config; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.validation.FieldError; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +import java.util.List; +import java.util.stream.Collectors; + +@RestControllerAdvice +public class GlobalExceptionHandler { + @ExceptionHandler(MethodArgumentNotValidException.class) + public ResponseEntity handleValidationExceptions(MethodArgumentNotValidException ex) { + List errors = ex.getBindingResult() + .getAllErrors() + .stream() + .map(error -> ((FieldError) error).getField() + ": " + error.getDefaultMessage()) + .collect(Collectors.toList()); + return new ResponseEntity<>(errors, HttpStatus.BAD_REQUEST); + } +} diff --git a/src/main/java/com/example/Public/Configuration/controller/DeviceConfigController.java b/src/main/java/com/example/Public/Configuration/controller/DeviceConfigController.java index ffff887..f17dad7 100644 --- a/src/main/java/com/example/Public/Configuration/controller/DeviceConfigController.java +++ b/src/main/java/com/example/Public/Configuration/controller/DeviceConfigController.java @@ -18,42 +18,50 @@ */ package com.example.Public.Configuration.controller; +import com.example.Public.Configuration.dto.DeviceConfigDto; import com.example.Public.Configuration.model.DeviceConfig; import com.example.Public.Configuration.response.Response; import com.example.Public.Configuration.service.impl.DeviceConfigServiceImpl; +import com.google.gson.Gson; +import jakarta.validation.Valid; +import jakarta.validation.Validation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/device-config") +@Validated public class DeviceConfigController { @Autowired private DeviceConfigServiceImpl deviceConfigService; - @PostMapping("/create") - public ResponseEntity createDeviceConfigProfile(@RequestBody DeviceConfig deviceConfig) { - DeviceConfig newDeviceConfig = deviceConfigService.getByConfigId(deviceConfig.getConfigId()); - if (newDeviceConfig == null) { - deviceConfigService.saveConfigProfile(deviceConfig); + @PostMapping("/") + public ResponseEntity createDeviceConfigProfile(@Valid @RequestBody DeviceConfigDto deviceConfig) { + Gson ConfigProfile = new Gson(); + String profileJson = ConfigProfile.toJson(deviceConfig.getConfigProfile()); + DeviceConfig newConfig = new DeviceConfig(); + newConfig.setConfigId(deviceConfig.getConfigId()); + newConfig.setConfigProfile(profileJson); + newConfig.setConfigContent(deviceConfig.getConfigContent()); + deviceConfigService.saveConfigProfile(newConfig); return new ResponseEntity<>(new Response(HttpStatus.CREATED.value(), "Device Config created successfully"), HttpStatus.CREATED); } - return new ResponseEntity<>("Device already exists", HttpStatus.BAD_REQUEST); - } - @GetMapping("/configs") + @GetMapping("/") public List getConfigsDevices() { return deviceConfigService.getAllDeviceConfigs(); } - @GetMapping("/{configId}/ones") + @GetMapping("/{configId}") public ResponseEntity fetchConfigByConfigId(@PathVariable int configId) { - DeviceConfig deviceConfig1 = deviceConfigService.getByConfigId(configId); + DeviceConfig deviceConfig1 = deviceConfigService.getConfigProfileByConfigId(configId); if (deviceConfig1 == null) { return new ResponseEntity<>(new Response(HttpStatus.NOT_FOUND.value(), "No Device found for Config ID: +configId"), HttpStatus.NOT_FOUND); @@ -63,7 +71,7 @@ public class DeviceConfigController { @PutMapping("/{configId}") public ResponseEntity updateDeviceDetails(@PathVariable int configId, @RequestBody DeviceConfig deviceConfig) { - DeviceConfig updateConfig = deviceConfigService.getByConfigId(configId); + DeviceConfig updateConfig = deviceConfigService.getConfigProfileByConfigId(configId); if (updateConfig == null) { deviceConfigService.UpdateConfigDetails(deviceConfig); return new ResponseEntity<>(new Response(HttpStatus.NOT_FOUND.value(), @@ -75,7 +83,7 @@ public class DeviceConfigController { @DeleteMapping("/{configId}") public ResponseEntity deleteConfigConfigId(@PathVariable int configId) { - DeviceConfig deleteConfig = deviceConfigService.getByConfigId(configId); + DeviceConfig deleteConfig = deviceConfigService.getConfigProfileByConfigId(configId); if (deleteConfig == null) { deviceConfigService.deleteConfigProfile(configId); return new ResponseEntity<>(new Response(HttpStatus.NOT_FOUND.value(), diff --git a/src/main/java/com/example/Public/Configuration/controller/DeviceController.java b/src/main/java/com/example/Public/Configuration/controller/DeviceController.java index 6c524fd..e24e0cd 100644 --- a/src/main/java/com/example/Public/Configuration/controller/DeviceController.java +++ b/src/main/java/com/example/Public/Configuration/controller/DeviceController.java @@ -35,7 +35,7 @@ public class DeviceController { @Autowired private DeviceServiceImpl deviceService; - @PostMapping("/create") + @PostMapping("/") public ResponseEntity createDevice(@RequestBody Device device) { Device deviceExist = deviceService.getDeviceById(device.getDeviceId()); @@ -47,20 +47,20 @@ public class DeviceController { return new ResponseEntity("Device already exists", HttpStatus.BAD_REQUEST); } - @GetMapping("/devices") + @GetMapping("/") public List getDevicesByDeviceId() { return deviceService.getAllDevices(); } - @GetMapping("/{deviceId}/ones") + @GetMapping("/{deviceId}") public ResponseEntity fetchDeviceByDeviceId(@PathVariable int deviceId) { - Device device = deviceService.getDeviceById(deviceId); - if (device == null) { + Device deviceProfile = deviceService.getDeviceById(deviceId); + if (deviceProfile == null) { return new ResponseEntity<>(new Response(HttpStatus.NOT_FOUND.value() - , "No Device found for ID: +id"), HttpStatus.NOT_FOUND); + , "No Device found for ID: +deviceId"), HttpStatus.NOT_FOUND); } - return new ResponseEntity<>(device, HttpStatus.OK); + return new ResponseEntity<>(deviceProfile, HttpStatus.OK); } @PutMapping("/{deviceId}") diff --git a/src/main/java/com/example/Public/Configuration/controller/EnrolmentRequestController.java b/src/main/java/com/example/Public/Configuration/controller/EnrolmentRequestController.java index 9e21d47..cd209d3 100644 --- a/src/main/java/com/example/Public/Configuration/controller/EnrolmentRequestController.java +++ b/src/main/java/com/example/Public/Configuration/controller/EnrolmentRequestController.java @@ -23,9 +23,11 @@ import com.example.Public.Configuration.model.EnrolmentRequestLog; import com.example.Public.Configuration.response.Response; import com.example.Public.Configuration.service.impl.EnrolmentRequestServiceImpl; import com.google.gson.Gson; +import jakarta.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import java.time.Instant; @@ -33,36 +35,32 @@ import java.util.List; @RestController @RequestMapping("/enrolment-request") +@Validated public class EnrolmentRequestController { @Autowired // This annotation is used to inject the DeviceService object into the DeviceController class. - private EnrolmentRequestServiceImpl enrolmentRequestService; // This object is used to perform CRUD operations on the Device table in the database. + private EnrolmentRequestServiceImpl enrolmentRequestService; + private EnrolmentRequestDTO enrolmentRequestDTO; // This object is used to perform CRUD operations on the Device table in the database. + + @PostMapping("/") // add a new device to the database. + public ResponseEntity createEnrolmentRequest(@Valid @RequestBody EnrolmentRequestDTO enrolmentRequestLog) { - @PostMapping("/create") // add a new device to the database. - public ResponseEntity createEnrolmentRequest(@RequestBody EnrolmentRequestDTO enrolmentRequestLog) { Gson payload = new Gson(); - String ENROLMENT_REQUEST = payload.toJson(enrolmentRequestLog.getEnrolmentRequest()); - enrolmentRequestLog.setResponseTime(String.valueOf(Instant.now().toEpochMilli())); - EnrolmentRequestLog deviceEnrolmentRequest = enrolmentRequestService.getEnrolmentRequestByRequestId - (enrolmentRequestLog.getRequestId()); - if (deviceEnrolmentRequest == null) { + String ENROLMENT_REQUEST = payload.toJson(enrolmentRequestLog); EnrolmentRequestLog enrolmentRequest = new EnrolmentRequestLog(); - enrolmentRequest.setRequestId(enrolmentRequestLog.getRequestId()); enrolmentRequest.setEnrolmentRequest(ENROLMENT_REQUEST); - enrolmentRequest.setResponseTime(Long.parseLong(enrolmentRequestLog.getResponseTime())); + enrolmentRequest.setReceivedAtTime(Instant.now().toEpochMilli()); enrolmentRequestService.saveEnrolmentRequest(enrolmentRequest); return new ResponseEntity<>(new Response(HttpStatus.CREATED.value(), - "Device added successfully"), HttpStatus.CREATED); - } - return new ResponseEntity<>("Device already exists", HttpStatus.BAD_REQUEST); + "Device added successfully"), HttpStatus.OK); } - @GetMapping("/requests") + @GetMapping("/") public List getDevicesByRequests() { return enrolmentRequestService.getAllEnrolmentRequests(); } - @GetMapping("/{requestId}/ones") + @GetMapping("/{requestId}") public ResponseEntity fetchEnrolmentRequest(@PathVariable int requestId) { EnrolmentRequestLog deviceEnrolment = enrolmentRequestService.getEnrolmentRequestByRequestId(requestId); if (deviceEnrolment == null) { diff --git a/src/main/java/com/example/Public/Configuration/dao/DeviceDao.java b/src/main/java/com/example/Public/Configuration/dao/DeviceDao.java index 16f601f..b868e37 100644 --- a/src/main/java/com/example/Public/Configuration/dao/DeviceDao.java +++ b/src/main/java/com/example/Public/Configuration/dao/DeviceDao.java @@ -19,8 +19,11 @@ package com.example.Public.Configuration.dao; import com.example.Public.Configuration.model.Device; +import jakarta.validation.constraints.NotNull; import org.springframework.data.jpa.repository.JpaRepository; public interface DeviceDao extends JpaRepository { + // Device findByImei(@NotNull Long imei); + // Device findByDeviceId(int deviceId); } diff --git a/src/main/java/com/example/Public/Configuration/dto/DeviceConfigDto.java b/src/main/java/com/example/Public/Configuration/dto/DeviceConfigDto.java index aefa3b3..8956348 100644 --- a/src/main/java/com/example/Public/Configuration/dto/DeviceConfigDto.java +++ b/src/main/java/com/example/Public/Configuration/dto/DeviceConfigDto.java @@ -1,16 +1,17 @@ package com.example.Public.Configuration.dto; -import com.example.Public.Configuration.model.DeviceConfig; +import jakarta.validation.constraints.NotBlank; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; + @Data @AllArgsConstructor @NoArgsConstructor -public class ConfigProfileDTO { +public class DeviceConfigDto { private int configId; - private DeviceConfig deviceConfig; + private ConfigProfile configProfile; private String configContent; @@ -18,7 +19,10 @@ public class ConfigProfileDTO { @AllArgsConstructor @NoArgsConstructor public static class ConfigProfile { + + @NotBlank(message = "IMEI cannot be blank") private String phn_no_rx_1; + private String phn_no_rx_2; private String phn_no_tx_1; private String gprs_apn; diff --git a/src/main/java/com/example/Public/Configuration/dto/EnrolmentRequestDTO.java b/src/main/java/com/example/Public/Configuration/dto/EnrolmentRequestDTO.java index 7ad8db8..3e9521a 100644 --- a/src/main/java/com/example/Public/Configuration/dto/EnrolmentRequestDTO.java +++ b/src/main/java/com/example/Public/Configuration/dto/EnrolmentRequestDTO.java @@ -1,37 +1,29 @@ package com.example.Public.Configuration.dto; -import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.NotBlank; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; -import lombok.NonNull; + @Data @AllArgsConstructor @NoArgsConstructor public class EnrolmentRequestDTO { - private int requestId; - private EnrolmentRequest enrolmentRequest; - private String responseTime; - - @Data - @AllArgsConstructor - @NoArgsConstructor - public static class EnrolmentRequest { + @NotBlank(message = "IMEI cannot be blank") + private String imei; + @NotBlank(message = "IP Address cannot be blank") + private String ipAddress; + private String subTypeId; + private String smsPIN; + private int mfwVersion; + private int rssi; + private String network; + private int comTypeId; + private String cellId; + private String lac; + private int sysTick; + private String meFwVersion; - @NotNull - private String imei; - - private String ipAddress; - private int subTypeId; - private String smsPIN; - private int mfwVersion; - private int rssi; - private String network; - private int comTypeId; - private String cellId; - private String lac; - private int sysTick; - private String meFwVersion; - } } + diff --git a/src/main/java/com/example/Public/Configuration/model/Device.java b/src/main/java/com/example/Public/Configuration/model/Device.java index 783468b..3bfcfb1 100644 --- a/src/main/java/com/example/Public/Configuration/model/Device.java +++ b/src/main/java/com/example/Public/Configuration/model/Device.java @@ -18,11 +18,10 @@ */ package com.example.Public.Configuration.model; +import com.example.Public.Configuration.dto.DeviceConfigDto; import jakarta.persistence.*; import jakarta.validation.constraints.NotNull; -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; +import lombok.*; import java.util.Date; @@ -31,12 +30,13 @@ import java.util.Date; @Table(name = "device_profile") @NoArgsConstructor @AllArgsConstructor +@Getter +@Setter public class Device { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name = "device_id") private int deviceId; @@ -53,8 +53,14 @@ public class Device { @Column(name = "device_organization") private String device_organization; - @Column(name = "config_id") - private String configId; +// @ManyToOne +// @JoinColumn(name = "enrolment_id", referencedColumnName = "enrolment_id") +// private EnrolmentRequestLog enrolmentRequestLog; // Foreign Key to EnrolmentLog + + @OneToOne + @JoinColumn(name = "config_id", referencedColumnName = "config_id") + private DeviceConfig DeviceConfig; // Foreign Key to ConfigProfile + } diff --git a/src/main/java/com/example/Public/Configuration/model/DeviceConfig.java b/src/main/java/com/example/Public/Configuration/model/DeviceConfig.java index dee9876..349d374 100644 --- a/src/main/java/com/example/Public/Configuration/model/DeviceConfig.java +++ b/src/main/java/com/example/Public/Configuration/model/DeviceConfig.java @@ -20,16 +20,17 @@ package com.example.Public.Configuration.model; import jakarta.persistence.*; +import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; +import lombok.*; @Entity @Data @Table(name = "device_config-profile") @NoArgsConstructor @AllArgsConstructor +@Getter +@Setter public class DeviceConfig { @Id @@ -38,10 +39,16 @@ public class DeviceConfig { private int configId; @NotNull + @NotBlank(message = "configProfile cannot be blank") @Column(name = "config_profile") private String configProfile; @Column(name = "config_content") private String configContent; + + + // One config profile is related to one device + @OneToOne(mappedBy = "DeviceConfig") + private Device device; } diff --git a/src/main/java/com/example/Public/Configuration/model/EnrolmentRequestLog.java b/src/main/java/com/example/Public/Configuration/model/EnrolmentRequestLog.java index 0031036..b74d8a9 100644 --- a/src/main/java/com/example/Public/Configuration/model/EnrolmentRequestLog.java +++ b/src/main/java/com/example/Public/Configuration/model/EnrolmentRequestLog.java @@ -19,10 +19,13 @@ package com.example.Public.Configuration.model; import jakarta.persistence.*; +import jakarta.validation.constraints.NotNull; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; +import java.awt.*; + @Entity @Data @@ -40,12 +43,13 @@ public class EnrolmentRequestLog { private String EnrolmentRequest; @Column(name = "response_time") - private long responseTime; + private long receivedAtTime; - @Column(name = "author") - private String author; +// // One enrolment log can have multiple devices +// @OneToMany(mappedBy = "EnrolmentRequestLog") +// private List devices; - } +} diff --git a/src/main/java/com/example/Public/Configuration/service/impl/DeviceConfigServiceImpl.java b/src/main/java/com/example/Public/Configuration/service/impl/DeviceConfigServiceImpl.java index 39c04fc..c6c2a2a 100644 --- a/src/main/java/com/example/Public/Configuration/service/impl/DeviceConfigServiceImpl.java +++ b/src/main/java/com/example/Public/Configuration/service/impl/DeviceConfigServiceImpl.java @@ -40,7 +40,7 @@ public class DeviceConfigServiceImpl { return deviceConfigDao.findAll(); // use to command using the deviceDao object to get all the device details from the database } - public DeviceConfig getByConfigId(int configId) { + public DeviceConfig getConfigProfileByConfigId(int configId) { return deviceConfigDao.findByConfigId(configId); // use to get the device details by id } diff --git a/src/main/java/com/example/Public/Configuration/service/impl/DeviceServiceImpl.java b/src/main/java/com/example/Public/Configuration/service/impl/DeviceServiceImpl.java index ae4ca05..c2cd124 100644 --- a/src/main/java/com/example/Public/Configuration/service/impl/DeviceServiceImpl.java +++ b/src/main/java/com/example/Public/Configuration/service/impl/DeviceServiceImpl.java @@ -39,8 +39,8 @@ public class DeviceServiceImpl { return deviceDao.findAll(); // use to command using } - public Device getDeviceById(int device_id) { - return deviceDao.findById(device_id).orElse(null); // use to get the device details by id + public Device getDeviceById(int deviceId) { + return deviceDao.findById(deviceId).orElse(null); // use to get the device details by id } public void UpdateDetailsById(Device device) { //used to update the device details in the database @@ -49,13 +49,12 @@ public class DeviceServiceImpl { if (updatedDevice != null) { // device details are present in the database then update the device details updatedDevice.setDeviceId(device.getDeviceId()); updatedDevice.setDevice_model(device.getDevice_model()); - updatedDevice.setConfigId(device.getConfigId()); deviceDao.save(updatedDevice); } } - public void deleteDevice(int device_id) {//used to delete the device details from the database - deviceDao.deleteById(device_id);//delete the device details by id + public void deleteDevice(int deviceId) {//used to delete the device details from the database + deviceDao.deleteById(deviceId);//delete the device details by id } } diff --git a/src/main/java/com/example/Public/Configuration/service/impl/EnrolmentRequestServiceImpl.java b/src/main/java/com/example/Public/Configuration/service/impl/EnrolmentRequestServiceImpl.java index d86124d..7c8b489 100644 --- a/src/main/java/com/example/Public/Configuration/service/impl/EnrolmentRequestServiceImpl.java +++ b/src/main/java/com/example/Public/Configuration/service/impl/EnrolmentRequestServiceImpl.java @@ -52,7 +52,7 @@ public class EnrolmentRequestServiceImpl { if (updatedEnrolmentRequest != null) { // device details are present in the database then update the device details updatedEnrolmentRequest.setRequestId(Integer.parseInt(enrolmentRequestLog.getEnrolmentRequest())); - updatedEnrolmentRequest.setResponseTime(enrolmentRequestLog.getResponseTime()); + updatedEnrolmentRequest.setReceivedAtTime(enrolmentRequestLog.getReceivedAtTime()); enrolmentRequestLogDao.save(updatedEnrolmentRequest); } }