now can only add payload for enrolment request log auto generate requestID and always have the imei and ip in request payload for every request

publicendpointService
parent 2255ffe2f9
commit e7c633e10d

@ -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<String> errors = ex.getBindingResult()
.getAllErrors()
.stream()
.map(error -> ((FieldError) error).getField() + ": " + error.getDefaultMessage())
.collect(Collectors.toList());
return new ResponseEntity<>(errors, HttpStatus.BAD_REQUEST);
}
}

@ -18,42 +18,50 @@
*/ */
package com.example.Public.Configuration.controller; 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.model.DeviceConfig;
import com.example.Public.Configuration.response.Response; import com.example.Public.Configuration.response.Response;
import com.example.Public.Configuration.service.impl.DeviceConfigServiceImpl; 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.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.List;
@RestController @RestController
@RequestMapping("/device-config") @RequestMapping("/device-config")
@Validated
public class DeviceConfigController { public class DeviceConfigController {
@Autowired @Autowired
private DeviceConfigServiceImpl deviceConfigService; private DeviceConfigServiceImpl deviceConfigService;
@PostMapping("/create") @PostMapping("/")
public ResponseEntity<?> createDeviceConfigProfile(@RequestBody DeviceConfig deviceConfig) { public ResponseEntity<?> createDeviceConfigProfile(@Valid @RequestBody DeviceConfigDto deviceConfig) {
DeviceConfig newDeviceConfig = deviceConfigService.getByConfigId(deviceConfig.getConfigId()); Gson ConfigProfile = new Gson();
if (newDeviceConfig == null) { String profileJson = ConfigProfile.toJson(deviceConfig.getConfigProfile());
deviceConfigService.saveConfigProfile(deviceConfig); 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(), return new ResponseEntity<>(new Response(HttpStatus.CREATED.value(),
"Device Config created successfully"), HttpStatus.CREATED); "Device Config created successfully"), HttpStatus.CREATED);
} }
return new ResponseEntity<>("Device already exists", HttpStatus.BAD_REQUEST);
}
@GetMapping("/configs") @GetMapping("/")
public List<DeviceConfig> getConfigsDevices() { public List<DeviceConfig> getConfigsDevices() {
return deviceConfigService.getAllDeviceConfigs(); return deviceConfigService.getAllDeviceConfigs();
} }
@GetMapping("/{configId}/ones") @GetMapping("/{configId}")
public ResponseEntity<?> fetchConfigByConfigId(@PathVariable int configId) { public ResponseEntity<?> fetchConfigByConfigId(@PathVariable int configId) {
DeviceConfig deviceConfig1 = deviceConfigService.getByConfigId(configId); DeviceConfig deviceConfig1 = deviceConfigService.getConfigProfileByConfigId(configId);
if (deviceConfig1 == null) { if (deviceConfig1 == null) {
return new ResponseEntity<>(new Response(HttpStatus.NOT_FOUND.value(), return new ResponseEntity<>(new Response(HttpStatus.NOT_FOUND.value(),
"No Device found for Config ID: +configId"), HttpStatus.NOT_FOUND); "No Device found for Config ID: +configId"), HttpStatus.NOT_FOUND);
@ -63,7 +71,7 @@ public class DeviceConfigController {
@PutMapping("/{configId}") @PutMapping("/{configId}")
public ResponseEntity<?> updateDeviceDetails(@PathVariable int configId, @RequestBody DeviceConfig deviceConfig) { public ResponseEntity<?> updateDeviceDetails(@PathVariable int configId, @RequestBody DeviceConfig deviceConfig) {
DeviceConfig updateConfig = deviceConfigService.getByConfigId(configId); DeviceConfig updateConfig = deviceConfigService.getConfigProfileByConfigId(configId);
if (updateConfig == null) { if (updateConfig == null) {
deviceConfigService.UpdateConfigDetails(deviceConfig); deviceConfigService.UpdateConfigDetails(deviceConfig);
return new ResponseEntity<>(new Response(HttpStatus.NOT_FOUND.value(), return new ResponseEntity<>(new Response(HttpStatus.NOT_FOUND.value(),
@ -75,7 +83,7 @@ public class DeviceConfigController {
@DeleteMapping("/{configId}") @DeleteMapping("/{configId}")
public ResponseEntity<?> deleteConfigConfigId(@PathVariable int configId) { public ResponseEntity<?> deleteConfigConfigId(@PathVariable int configId) {
DeviceConfig deleteConfig = deviceConfigService.getByConfigId(configId); DeviceConfig deleteConfig = deviceConfigService.getConfigProfileByConfigId(configId);
if (deleteConfig == null) { if (deleteConfig == null) {
deviceConfigService.deleteConfigProfile(configId); deviceConfigService.deleteConfigProfile(configId);
return new ResponseEntity<>(new Response(HttpStatus.NOT_FOUND.value(), return new ResponseEntity<>(new Response(HttpStatus.NOT_FOUND.value(),

@ -35,7 +35,7 @@ public class DeviceController {
@Autowired @Autowired
private DeviceServiceImpl deviceService; private DeviceServiceImpl deviceService;
@PostMapping("/create") @PostMapping("/")
public ResponseEntity<?> createDevice(@RequestBody Device device) { public ResponseEntity<?> createDevice(@RequestBody Device device) {
Device deviceExist = deviceService.getDeviceById(device.getDeviceId()); Device deviceExist = deviceService.getDeviceById(device.getDeviceId());
@ -47,20 +47,20 @@ public class DeviceController {
return new ResponseEntity<String>("Device already exists", HttpStatus.BAD_REQUEST); return new ResponseEntity<String>("Device already exists", HttpStatus.BAD_REQUEST);
} }
@GetMapping("/devices") @GetMapping("/")
public List<Device> getDevicesByDeviceId() { public List<Device> getDevicesByDeviceId() {
return deviceService.getAllDevices(); return deviceService.getAllDevices();
} }
@GetMapping("/{deviceId}/ones") @GetMapping("/{deviceId}")
public ResponseEntity<?> fetchDeviceByDeviceId(@PathVariable int deviceId) { public ResponseEntity<?> fetchDeviceByDeviceId(@PathVariable int deviceId) {
Device device = deviceService.getDeviceById(deviceId); Device deviceProfile = deviceService.getDeviceById(deviceId);
if (device == null) { if (deviceProfile == null) {
return new ResponseEntity<>(new Response(HttpStatus.NOT_FOUND.value() 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}") @PutMapping("/{deviceId}")

@ -23,9 +23,11 @@ import com.example.Public.Configuration.model.EnrolmentRequestLog;
import com.example.Public.Configuration.response.Response; import com.example.Public.Configuration.response.Response;
import com.example.Public.Configuration.service.impl.EnrolmentRequestServiceImpl; import com.example.Public.Configuration.service.impl.EnrolmentRequestServiceImpl;
import com.google.gson.Gson; import com.google.gson.Gson;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.time.Instant; import java.time.Instant;
@ -33,36 +35,32 @@ import java.util.List;
@RestController @RestController
@RequestMapping("/enrolment-request") @RequestMapping("/enrolment-request")
@Validated
public class EnrolmentRequestController { public class EnrolmentRequestController {
@Autowired // This annotation is used to inject the DeviceService object into the DeviceController class. @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(); Gson payload = new Gson();
String ENROLMENT_REQUEST = payload.toJson(enrolmentRequestLog.getEnrolmentRequest()); String ENROLMENT_REQUEST = payload.toJson(enrolmentRequestLog);
enrolmentRequestLog.setResponseTime(String.valueOf(Instant.now().toEpochMilli()));
EnrolmentRequestLog deviceEnrolmentRequest = enrolmentRequestService.getEnrolmentRequestByRequestId
(enrolmentRequestLog.getRequestId());
if (deviceEnrolmentRequest == null) {
EnrolmentRequestLog enrolmentRequest = new EnrolmentRequestLog(); EnrolmentRequestLog enrolmentRequest = new EnrolmentRequestLog();
enrolmentRequest.setRequestId(enrolmentRequestLog.getRequestId());
enrolmentRequest.setEnrolmentRequest(ENROLMENT_REQUEST); enrolmentRequest.setEnrolmentRequest(ENROLMENT_REQUEST);
enrolmentRequest.setResponseTime(Long.parseLong(enrolmentRequestLog.getResponseTime())); enrolmentRequest.setReceivedAtTime(Instant.now().toEpochMilli());
enrolmentRequestService.saveEnrolmentRequest(enrolmentRequest); enrolmentRequestService.saveEnrolmentRequest(enrolmentRequest);
return new ResponseEntity<>(new Response(HttpStatus.CREATED.value(), return new ResponseEntity<>(new Response(HttpStatus.CREATED.value(),
"Device added successfully"), HttpStatus.CREATED); "Device added successfully"), HttpStatus.OK);
}
return new ResponseEntity<>("Device already exists", HttpStatus.BAD_REQUEST);
} }
@GetMapping("/requests") @GetMapping("/")
public List<EnrolmentRequestLog> getDevicesByRequests() { public List<EnrolmentRequestLog> getDevicesByRequests() {
return enrolmentRequestService.getAllEnrolmentRequests(); return enrolmentRequestService.getAllEnrolmentRequests();
} }
@GetMapping("/{requestId}/ones") @GetMapping("/{requestId}")
public ResponseEntity<?> fetchEnrolmentRequest(@PathVariable int requestId) { public ResponseEntity<?> fetchEnrolmentRequest(@PathVariable int requestId) {
EnrolmentRequestLog deviceEnrolment = enrolmentRequestService.getEnrolmentRequestByRequestId(requestId); EnrolmentRequestLog deviceEnrolment = enrolmentRequestService.getEnrolmentRequestByRequestId(requestId);
if (deviceEnrolment == null) { if (deviceEnrolment == null) {

@ -19,8 +19,11 @@
package com.example.Public.Configuration.dao; package com.example.Public.Configuration.dao;
import com.example.Public.Configuration.model.Device; import com.example.Public.Configuration.model.Device;
import jakarta.validation.constraints.NotNull;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
public interface DeviceDao extends JpaRepository<Device, Integer> { public interface DeviceDao extends JpaRepository<Device, Integer> {
// Device findByImei(@NotNull Long imei);
// Device findByDeviceId(int deviceId);
} }

@ -1,16 +1,17 @@
package com.example.Public.Configuration.dto; package com.example.Public.Configuration.dto;
import com.example.Public.Configuration.model.DeviceConfig; import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
@Data @Data
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
public class ConfigProfileDTO { public class DeviceConfigDto {
private int configId; private int configId;
private DeviceConfig deviceConfig; private ConfigProfile configProfile;
private String configContent; private String configContent;
@ -18,7 +19,10 @@ public class ConfigProfileDTO {
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
public static class ConfigProfile { public static class ConfigProfile {
@NotBlank(message = "IMEI cannot be blank")
private String phn_no_rx_1; private String phn_no_rx_1;
private String phn_no_rx_2; private String phn_no_rx_2;
private String phn_no_tx_1; private String phn_no_tx_1;
private String gprs_apn; private String gprs_apn;

@ -1,29 +1,20 @@
package com.example.Public.Configuration.dto; package com.example.Public.Configuration.dto;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.NonNull;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class EnrolmentRequestDTO {
private int requestId;
private EnrolmentRequest enrolmentRequest;
private String responseTime;
@Data @Data
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
public static class EnrolmentRequest { public class EnrolmentRequestDTO {
@NotBlank(message = "IMEI cannot be blank")
@NotNull
private String imei; private String imei;
@NotBlank(message = "IP Address cannot be blank")
private String ipAddress; private String ipAddress;
private int subTypeId; private String subTypeId;
private String smsPIN; private String smsPIN;
private int mfwVersion; private int mfwVersion;
private int rssi; private int rssi;
@ -33,5 +24,6 @@ public class EnrolmentRequestDTO {
private String lac; private String lac;
private int sysTick; private int sysTick;
private String meFwVersion; private String meFwVersion;
} }
}

@ -18,11 +18,10 @@
*/ */
package com.example.Public.Configuration.model; package com.example.Public.Configuration.model;
import com.example.Public.Configuration.dto.DeviceConfigDto;
import jakarta.persistence.*; import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor; import lombok.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date; import java.util.Date;
@ -31,12 +30,13 @@ import java.util.Date;
@Table(name = "device_profile") @Table(name = "device_profile")
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@Getter
@Setter
public class Device { public class Device {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "device_id") @Column(name = "device_id")
private int deviceId; private int deviceId;
@ -53,8 +53,14 @@ public class Device {
@Column(name = "device_organization") @Column(name = "device_organization")
private String device_organization; private String device_organization;
@Column(name = "config_id") // @ManyToOne
private String configId; // @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
} }

@ -20,16 +20,17 @@ package com.example.Public.Configuration.model;
import jakarta.persistence.*; import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor; import lombok.*;
import lombok.Data;
import lombok.NoArgsConstructor;
@Entity @Entity
@Data @Data
@Table(name = "device_config-profile") @Table(name = "device_config-profile")
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@Getter
@Setter
public class DeviceConfig { public class DeviceConfig {
@Id @Id
@ -38,10 +39,16 @@ public class DeviceConfig {
private int configId; private int configId;
@NotNull @NotNull
@NotBlank(message = "configProfile cannot be blank")
@Column(name = "config_profile") @Column(name = "config_profile")
private String configProfile; private String configProfile;
@Column(name = "config_content") @Column(name = "config_content")
private String configContent; private String configContent;
// One config profile is related to one device
@OneToOne(mappedBy = "DeviceConfig")
private Device device;
} }

@ -19,10 +19,13 @@
package com.example.Public.Configuration.model; package com.example.Public.Configuration.model;
import jakarta.persistence.*; import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import java.awt.*;
@Entity @Entity
@Data @Data
@ -40,10 +43,11 @@ public class EnrolmentRequestLog {
private String EnrolmentRequest; private String EnrolmentRequest;
@Column(name = "response_time") @Column(name = "response_time")
private long responseTime; private long receivedAtTime;
@Column(name = "author") // // One enrolment log can have multiple devices
private String author; // @OneToMany(mappedBy = "EnrolmentRequestLog")
// private List devices;
} }

@ -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 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 return deviceConfigDao.findByConfigId(configId); // use to get the device details by id
} }

@ -39,8 +39,8 @@ public class DeviceServiceImpl {
return deviceDao.findAll(); // use to command using return deviceDao.findAll(); // use to command using
} }
public Device getDeviceById(int device_id) { public Device getDeviceById(int deviceId) {
return deviceDao.findById(device_id).orElse(null); // use to get the device details by id 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 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 if (updatedDevice != null) { // device details are present in the database then update the device details
updatedDevice.setDeviceId(device.getDeviceId()); updatedDevice.setDeviceId(device.getDeviceId());
updatedDevice.setDevice_model(device.getDevice_model()); updatedDevice.setDevice_model(device.getDevice_model());
updatedDevice.setConfigId(device.getConfigId());
deviceDao.save(updatedDevice); deviceDao.save(updatedDevice);
} }
} }
public void deleteDevice(int device_id) {//used to delete the device details from the database public void deleteDevice(int deviceId) {//used to delete the device details from the database
deviceDao.deleteById(device_id);//delete the device details by id deviceDao.deleteById(deviceId);//delete the device details by id
} }
} }

@ -52,7 +52,7 @@ public class EnrolmentRequestServiceImpl {
if (updatedEnrolmentRequest != null) { // device details are present in the database then update the device details if (updatedEnrolmentRequest != null) { // device details are present in the database then update the device details
updatedEnrolmentRequest.setRequestId(Integer.parseInt(enrolmentRequestLog.getEnrolmentRequest())); updatedEnrolmentRequest.setRequestId(Integer.parseInt(enrolmentRequestLog.getEnrolmentRequest()));
updatedEnrolmentRequest.setResponseTime(enrolmentRequestLog.getResponseTime()); updatedEnrolmentRequest.setReceivedAtTime(enrolmentRequestLog.getReceivedAtTime());
enrolmentRequestLogDao.save(updatedEnrolmentRequest); enrolmentRequestLogDao.save(updatedEnrolmentRequest);
} }
} }

Loading…
Cancel
Save