diff --git a/src/main/java/org/isf/accounting/rest/BillController.java b/src/main/java/org/isf/accounting/rest/BillController.java index 9a413c995..4b1835b51 100644 --- a/src/main/java/org/isf/accounting/rest/BillController.java +++ b/src/main/java/org/isf/accounting/rest/BillController.java @@ -1,13 +1,7 @@ package org.isf.accounting.rest; -import static org.isf.shared.mapper.OHModelMapper.getObjectMapper; - -import java.util.ArrayList; -import java.util.Date; -import java.util.GregorianCalendar; -import java.util.List; -import java.util.stream.Collectors; - +import io.swagger.annotations.Api; +import io.swagger.annotations.Authorization; import org.isf.accounting.dto.BillDTO; import org.isf.accounting.dto.BillItemsDTO; import org.isf.accounting.dto.BillPaymentsDTO; @@ -16,7 +10,6 @@ import org.isf.accounting.model.Bill; import org.isf.accounting.model.BillItems; import org.isf.accounting.model.BillPayments; -import org.isf.patient.dto.PatientDTO; import org.isf.patient.manager.PatientBrowserManager; import org.isf.patient.model.Patient; import org.isf.priceslist.manager.PriceListManager; @@ -25,6 +18,7 @@ import org.isf.utils.exception.OHServiceException; import org.isf.utils.exception.model.OHExceptionMessage; import org.isf.utils.exception.model.OHSeverityLevel; +import org.modelmapper.ModelMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -32,17 +26,13 @@ import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.PutMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; -import io.swagger.annotations.Api; -import io.swagger.annotations.Authorization; +import java.util.ArrayList; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.List; +import java.util.stream.Collectors; @RestController @@ -58,6 +48,9 @@ public class BillController { @Autowired protected PatientBrowserManager patientManager; + @Autowired + protected ModelMapper modelMapper; + private final Logger logger = LoggerFactory.getLogger(BillController.class); /** @@ -71,7 +64,7 @@ ResponseEntity newBill(@RequestBody FullBillDTO newBillDto) throws logger.info("Create Bill " + newBillDto.toString()); - Bill bill = getObjectMapper().map(newBillDto.getBill(), Bill.class); + Bill bill = modelMapper.map(newBillDto.getBill(), Bill.class); Patient pat = patientManager.getPatient(bill.getPatName()); @@ -94,9 +87,9 @@ ResponseEntity newBill(@RequestBody FullBillDTO newBillDto) throws throw new OHAPIException(new OHExceptionMessage(null, "Price list not found!", OHSeverityLevel.ERROR)); } - ArrayList billItems = new ArrayList(newBillDto.getBillItems().stream().map(item -> getObjectMapper().map(item, BillItems.class)).collect(Collectors.toList())); + ArrayList billItems = new ArrayList(newBillDto.getBillItems().stream().map(item -> modelMapper.map(item, BillItems.class)).collect(Collectors.toList())); - ArrayList billPayments = new ArrayList( newBillDto.getBillPayments().stream().map(item -> getObjectMapper().map(item, BillPayments.class)).collect(Collectors.toList())); + ArrayList billPayments = new ArrayList( newBillDto.getBillPayments().stream().map(item -> modelMapper.map(item, BillPayments.class)).collect(Collectors.toList())); boolean isCreated = billManager.newBill(bill, billItems, billPayments); @@ -116,7 +109,7 @@ ResponseEntity newBill(@RequestBody FullBillDTO newBillDto) throws ResponseEntity updateBill(@PathVariable Integer id, @RequestBody FullBillDTO odBillDto) throws OHServiceException { logger.info("updated Bill " + odBillDto.toString()); - Bill bill = getObjectMapper().map(odBillDto.getBill(), Bill.class); + Bill bill = modelMapper.map(odBillDto.getBill(), Bill.class); bill.setId(id); @@ -145,9 +138,9 @@ ResponseEntity updateBill(@PathVariable Integer id, @RequestBody Fu throw new OHAPIException(new OHExceptionMessage(null, "Price list not found!", OHSeverityLevel.ERROR)); } - ArrayList billItems = new ArrayList(odBillDto.getBillItems().stream().map(item -> getObjectMapper().map(item, BillItems.class)).collect(Collectors.toList())); + ArrayList billItems = new ArrayList(odBillDto.getBillItems().stream().map(item -> modelMapper.map(item, BillItems.class)).collect(Collectors.toList())); - ArrayList billPayments = new ArrayList( odBillDto.getBillPayments().stream().map(item -> getObjectMapper().map(item, BillPayments.class)).collect(Collectors.toList())); + ArrayList billPayments = new ArrayList( odBillDto.getBillPayments().stream().map(item -> modelMapper.map(item, BillPayments.class)).collect(Collectors.toList())); boolean isUpdated = billManager.updateBill(bill, billItems, billPayments); @@ -191,7 +184,7 @@ public ResponseEntity> searchBills(@RequestParam(value="datefrom") bills = billManager.getBills(datefrom, dateto, pat); } - billDTOS = bills.stream().map(bil-> getObjectMapper().map(bil, BillDTO.class)).collect(Collectors.toList()); + billDTOS = bills.stream().map(bil-> modelMapper.map(bil, BillDTO.class)).collect(Collectors.toList()); if(billDTOS.size() == 0){ return ResponseEntity.status(HttpStatus.NO_CONTENT).body(billDTOS); @@ -233,7 +226,7 @@ public ResponseEntity> searchBillsPayments( payments = billManager.getPayments(datefrom, dateto, pat); } - paymentsDTOS = payments.stream().map(pay-> getObjectMapper().map(pay, BillPaymentsDTO.class)).collect(Collectors.toList()); + paymentsDTOS = payments.stream().map(pay-> modelMapper.map(pay, BillPaymentsDTO.class)).collect(Collectors.toList()); if(paymentsDTOS.size() == 0){ return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null); @@ -254,7 +247,7 @@ public ResponseEntity> getPaymentsByBillId(@PathVariable(v ArrayList bills = billManager.getPayments(id); - List paymentsDTOS = bills.stream().map(pay-> getObjectMapper().map(pay, BillPaymentsDTO.class)).collect(Collectors.toList()); + List paymentsDTOS = bills.stream().map(pay-> modelMapper.map(pay, BillPaymentsDTO.class)).collect(Collectors.toList()); if(paymentsDTOS.size() == 0){ return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null); @@ -275,7 +268,7 @@ public ResponseEntity> getItems(@PathVariable(value="bill_id" ArrayList items = billManager.getItems(id); - List itemsDTOS = items.stream().map(it-> getObjectMapper().map(it, BillItemsDTO.class)).collect(Collectors.toList()); + List itemsDTOS = items.stream().map(it-> modelMapper.map(it, BillItemsDTO.class)).collect(Collectors.toList()); if(itemsDTOS.size() == 0){ return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null); @@ -296,7 +289,7 @@ public ResponseEntity getBill(@PathVariable Integer id) throws OHServic Bill bill = billManager.getBill(id); - BillDTO billDTO = getObjectMapper().map(bill, BillDTO.class); + BillDTO billDTO = modelMapper.map(bill, BillDTO.class); if(billDTO == null){ return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null); @@ -317,7 +310,7 @@ public ResponseEntity> getPendingBillsAffiliate(@RequestParam(valu List bills = billManager.getPendingBillsAffiliate(code); - List billDTOS = bills.stream().map(bill-> getObjectMapper().map(bill, BillDTO.class)).collect(Collectors.toList()); + List billDTOS = bills.stream().map(bill-> modelMapper.map(bill, BillDTO.class)).collect(Collectors.toList()); if(billDTOS.size() == 0){ return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null); @@ -338,7 +331,7 @@ public ResponseEntity> getPendingBills(@RequestParam(value="patien List bills = billManager.getPendingBills(code); - List billDTOS = bills.stream().map(bill-> getObjectMapper().map(bill, BillDTO.class)).collect(Collectors.toList()); + List billDTOS = bills.stream().map(bill-> modelMapper.map(bill, BillDTO.class)).collect(Collectors.toList()); if(billDTOS.size() == 0){ @@ -367,13 +360,13 @@ public ResponseEntity> searchBills(@RequestParam(value="datefrom") GregorianCalendar dateto = new GregorianCalendar(); dateto.setTime(dateTo); - BillItems billItem = getObjectMapper().map(billItemDTO, BillItems.class); + BillItems billItem = modelMapper.map(billItemDTO, BillItems.class); logger.info("Get Bills datefrom:" + datefrom + " dateTo:" + dateto + " Bill ITEM ID: "+billItem.getId()); ArrayList bills = billManager.getBills(datefrom, dateto, billItem); - List billDTOS = bills.stream().map(bil-> getObjectMapper().map(bil, BillDTO.class)).collect(Collectors.toList()); + List billDTOS = bills.stream().map(bil-> modelMapper.map(bil, BillDTO.class)).collect(Collectors.toList()); if(billDTOS.size() == 0){ return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null); @@ -395,7 +388,7 @@ public ResponseEntity> getDistinctItems() throws OHServiceExc ArrayList items = billManager.getDistinctItems(); - List itemsDTOS = items.stream().map(it-> getObjectMapper().map(it, BillItemsDTO.class)).collect(Collectors.toList()); + List itemsDTOS = items.stream().map(it-> modelMapper.map(it, BillItemsDTO.class)).collect(Collectors.toList()); if(itemsDTOS.size() == 0){ return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null); @@ -429,11 +422,11 @@ public ResponseEntity deleteBill(@PathVariable Integer id) throws OHServiceExcep @PostMapping(value = "/bills/search/by/payments", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity> searchBillsByPayments(@RequestBody List paymentsDTO) throws OHServiceException { - ArrayList billPayments = new ArrayList(paymentsDTO.stream().map(pay-> getObjectMapper().map(pay, BillPayments.class)).collect(Collectors.toList())); + ArrayList billPayments = new ArrayList(paymentsDTO.stream().map(pay-> modelMapper.map(pay, BillPayments.class)).collect(Collectors.toList())); List bills = billManager.getBills(billPayments); - ListbillDTOS = bills.stream().map(bil-> getObjectMapper().map(bil, BillDTO.class)).collect(Collectors.toList()); + ListbillDTOS = bills.stream().map(bil-> modelMapper.map(bil, BillDTO.class)).collect(Collectors.toList()); if(billDTOS.size() == 0){ return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null); diff --git a/src/main/java/org/isf/admission/rest/AdmissionController.java b/src/main/java/org/isf/admission/rest/AdmissionController.java index aefa4c69e..79d835376 100644 --- a/src/main/java/org/isf/admission/rest/AdmissionController.java +++ b/src/main/java/org/isf/admission/rest/AdmissionController.java @@ -1,13 +1,6 @@ package org.isf.admission.rest; -import static org.isf.shared.mapper.OHModelMapper.getObjectMapper; - -import java.util.GregorianCalendar; -import java.util.List; -import java.util.stream.Collectors; - -import javax.validation.Valid; - +import io.swagger.annotations.Api; import org.isf.admission.dto.AdmissionDTO; import org.isf.admission.dto.AdmittedPatientDTO; import org.isf.admission.manager.AdmissionBrowserManager; @@ -32,6 +25,7 @@ import org.isf.utils.exception.model.OHSeverityLevel; import org.isf.ward.manager.WardBrowserManager; import org.isf.ward.model.Ward; +import org.modelmapper.ModelMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -39,16 +33,12 @@ import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.util.StringUtils; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.PutMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; -import io.swagger.annotations.Api; +import javax.validation.Valid; +import java.util.GregorianCalendar; +import java.util.List; +import java.util.stream.Collectors; @RestController @Api(value = "/admissions", produces = "application/vnd.ohapi.app-v1+json") @@ -78,6 +68,9 @@ public class AdmissionController { @Autowired private DeliveryResultTypeBrowserManager dlvrrestTypeManager; + @Autowired + protected ModelMapper modelMapper; + private final Logger logger = LoggerFactory.getLogger(AdmissionController.class); public AdmissionController(AdmissionBrowserManager admissionManager) { @@ -99,7 +92,7 @@ public ResponseEntity getAdmissions(@PathVariable int id) throws O return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null); } System.out.println("admissiontype code" + admission.getAdmType().getCode()); - return ResponseEntity.ok(getObjectMapper().map(admission, AdmissionDTO.class)); + return ResponseEntity.ok(modelMapper.map(admission, AdmissionDTO.class)); } /** @@ -122,7 +115,7 @@ public ResponseEntity getCurrentAdmission(@RequestParam("patientco if (admission == null) { return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null); } - return ResponseEntity.ok(getObjectMapper().map(admission, AdmissionDTO.class)); + return ResponseEntity.ok(modelMapper.map(admission, AdmissionDTO.class)); } /** @@ -143,7 +136,7 @@ public ResponseEntity> getAdmittedPatients( List Amittedpatients = admissionManager .getAdmittedPatients(admissionRange, dischargeRange, searchTerms).stream() - .map(admPt -> getObjectMapper().map(admPt, AdmittedPatientDTO.class)).collect(Collectors.toList()); + .map(admPt -> modelMapper.map(admPt, AdmittedPatientDTO.class)).collect(Collectors.toList()); if (Amittedpatients.size() == 0) { return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null); @@ -167,7 +160,7 @@ public ResponseEntity getPatientAdmissions(@RequestParam("patientc throw new OHAPIException(new OHExceptionMessage(null, "Patient not found!", OHSeverityLevel.ERROR), HttpStatus.INTERNAL_SERVER_ERROR); List admissionsDTOs = admissionManager.getAdmissions(patient).stream() - .map(adm -> getObjectMapper().map(adm, AdmissionDTO.class)).collect(Collectors.toList()); + .map(adm -> modelMapper.map(adm, AdmissionDTO.class)).collect(Collectors.toList()); if (admissionsDTOs.size() == 0) { return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null); @@ -240,7 +233,7 @@ public ResponseEntity deleteAdmissionType(@PathVariable int id) throws ResponseEntity newAdmissions(@Valid @RequestBody AdmissionDTO newAdmissionDTO) throws OHServiceException { - Admission newAdmission = getObjectMapper().map(newAdmissionDTO, Admission.class); + Admission newAdmission = modelMapper.map(newAdmissionDTO, Admission.class); if (newAdmissionDTO.getWard() != null && newAdmissionDTO.getWard().getCode() != null && !newAdmissionDTO.getWard().getCode().trim().isEmpty()) { @@ -407,7 +400,7 @@ ResponseEntity updateAdmissions(@RequestBody AdmissionDTO updAdmissionD if (old == null) { throw new OHAPIException(new OHExceptionMessage(null, "Admission not found!", OHSeverityLevel.ERROR)); } - Admission updAdmission = getObjectMapper().map(updAdmissionDTO, Admission.class); + Admission updAdmission = modelMapper.map(updAdmissionDTO, Admission.class); if (updAdmissionDTO.getWard() != null && updAdmissionDTO.getWard().getCode() != null && !updAdmissionDTO.getWard().getCode().trim().isEmpty()) { diff --git a/src/main/java/org/isf/admtype/dto/AdmissionTypeDTO.java b/src/main/java/org/isf/admtype/dto/AdmissionTypeDTO.java index 9e4be1785..d476b3d06 100644 --- a/src/main/java/org/isf/admtype/dto/AdmissionTypeDTO.java +++ b/src/main/java/org/isf/admtype/dto/AdmissionTypeDTO.java @@ -11,11 +11,11 @@ */ public class AdmissionTypeDTO { - + @NotNull @ApiModelProperty(notes = "code of the admission type", example="A", position = 1) private String code; - + @NotNull @ApiModelProperty(notes = "description of the admission type", example="AMBULANCE", position = 2) private String description; diff --git a/src/main/java/org/isf/admtype/rest/AdmissionTypeController.java b/src/main/java/org/isf/admtype/rest/AdmissionTypeController.java index a476e5200..2c9c3aa55 100644 --- a/src/main/java/org/isf/admtype/rest/AdmissionTypeController.java +++ b/src/main/java/org/isf/admtype/rest/AdmissionTypeController.java @@ -1,10 +1,6 @@ package org.isf.admtype.rest; -import java.util.List; -import java.util.stream.Collectors; - -import static org.isf.shared.mapper.OHModelMapper.getObjectMapper; - +import io.swagger.annotations.Api; import org.isf.admtype.dto.AdmissionTypeDTO; import org.isf.admtype.manager.AdmissionTypeBrowserManager; import org.isf.admtype.model.AdmissionType; @@ -12,21 +8,17 @@ import org.isf.utils.exception.OHServiceException; import org.isf.utils.exception.model.OHExceptionMessage; import org.isf.utils.exception.model.OHSeverityLevel; +import org.modelmapper.ModelMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.PutMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; -import io.swagger.annotations.Api; +import java.util.List; +import java.util.stream.Collectors; @RestController @Api(value = "/admissiontypes", produces = MediaType.APPLICATION_JSON_VALUE) @@ -35,6 +27,9 @@ public class AdmissionTypeController { @Autowired protected AdmissionTypeBrowserManager admtManager; + @Autowired + protected ModelMapper modelMapper; + private final Logger logger = LoggerFactory.getLogger(AdmissionTypeController.class); public AdmissionTypeController(AdmissionTypeBrowserManager admtManager) { @@ -51,7 +46,7 @@ public AdmissionTypeController(AdmissionTypeBrowserManager admtManager) { ResponseEntity newAdmissionType(@RequestBody AdmissionTypeDTO admissionTypeDTO) throws OHServiceException { String code = admissionTypeDTO.getCode(); logger.info("Create Admission Type " + code); - boolean isCreated = admtManager.newAdmissionType(getObjectMapper().map(admissionTypeDTO, AdmissionType.class)); + boolean isCreated = admtManager.newAdmissionType(modelMapper.map(admissionTypeDTO, AdmissionType.class)); AdmissionType admtCreated = null; List admtFounds = admtManager.getAdmissionType().stream().filter(ad -> ad.getCode().equals(code)) .collect(Collectors.toList()); @@ -75,7 +70,7 @@ ResponseEntity newAdmissionType(@RequestBody AdmissionTypeDTO admissionT ResponseEntity updateAdmissionTypet(@RequestBody AdmissionTypeDTO admissionTypeDTO) throws OHServiceException { logger.info("Update admissiontypes code:" + admissionTypeDTO.getCode()); - AdmissionType admt = getObjectMapper().map(admissionTypeDTO, AdmissionType.class); + AdmissionType admt = modelMapper.map(admissionTypeDTO, AdmissionType.class); if (!admtManager.codeControl(admt.getCode())) throw new OHAPIException(new OHExceptionMessage(null, "Admission Type not found!", OHSeverityLevel.ERROR)); boolean isUpdated = admtManager.updateAdmissionType(admt); @@ -96,7 +91,7 @@ public ResponseEntity> getAdmissionTypes() throws OHServi logger.info("Get all Admission Types "); List admissionTypes = admtManager.getAdmissionType(); List admissionTypeDTOs = admissionTypes.stream() - .map(admType -> getObjectMapper().map(admType, AdmissionTypeDTO.class)).collect(Collectors.toList()); + .map(admType -> modelMapper.map(admType, AdmissionTypeDTO.class)).collect(Collectors.toList()); if (admissionTypeDTOs.size() == 0) { return ResponseEntity.status(HttpStatus.NO_CONTENT).body(admissionTypeDTOs); } else { diff --git a/src/main/java/org/isf/agetype/rest/AgeTypeController.java b/src/main/java/org/isf/agetype/rest/AgeTypeController.java index 29db9dca2..c3049e28f 100644 --- a/src/main/java/org/isf/agetype/rest/AgeTypeController.java +++ b/src/main/java/org/isf/agetype/rest/AgeTypeController.java @@ -1,13 +1,7 @@ package org.isf.agetype.rest; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - -import javax.validation.Valid; - +import io.swagger.annotations.Api; +import io.swagger.annotations.Authorization; import org.isf.agetype.dto.AgeTypeDTO; import org.isf.agetype.manager.AgeTypeBrowserManager; import org.isf.agetype.model.AgeType; @@ -15,23 +9,21 @@ import org.isf.utils.exception.OHServiceException; import org.isf.utils.exception.model.OHExceptionMessage; import org.isf.utils.exception.model.OHSeverityLevel; +import org.modelmapper.ModelMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PutMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; -import io.swagger.annotations.Api; -import io.swagger.annotations.Authorization; - -import static org.isf.shared.mapper.OHModelMapper.getObjectMapper; +import javax.validation.Valid; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; @RestController @Api(value="/agetypes",produces = MediaType.APPLICATION_JSON_VALUE, authorizations = {@Authorization(value="basicAuth")}) @@ -39,6 +31,9 @@ public class AgeTypeController { @Autowired private AgeTypeBrowserManager ageTypeManager; + + @Autowired + protected ModelMapper modelMapper; private final Logger logger = LoggerFactory.getLogger(AgeTypeController.class); @@ -55,7 +50,7 @@ public AgeTypeController(AgeTypeBrowserManager ageTypeManager) { public ResponseEntity> getAllAgeTypes() throws OHServiceException { logger.info("Get age types"); List results = ageTypeManager.getAgeType(); - List parsedResults = results.stream().map(item -> getObjectMapper().map(item, AgeTypeDTO.class)).collect(Collectors.toList()); + List parsedResults = results.stream().map(item -> modelMapper.map(item, AgeTypeDTO.class)).collect(Collectors.toList()); if(parsedResults.size() > 0){ return ResponseEntity.ok(parsedResults); }else{ @@ -76,7 +71,7 @@ ResponseEntity updateAgeType(@Valid @RequestBody AgeTypeDTO ageTypeD throw new OHAPIException(new OHExceptionMessage(null, "The age type is not valid!", OHSeverityLevel.ERROR)); } logger.info("Update age type"); - AgeType ageType = getObjectMapper().map(ageTypeDTO, AgeType.class); + AgeType ageType = modelMapper.map(ageTypeDTO, AgeType.class); ArrayList ageTypes = new ArrayList(); ageTypes.add(ageType); if(ageTypeManager.updateAgeType(ageTypes)) { diff --git a/src/main/java/org/isf/config/SpringFoxConfig.java b/src/main/java/org/isf/config/SpringFoxConfig.java index 809bd1188..16be2bb64 100644 --- a/src/main/java/org/isf/config/SpringFoxConfig.java +++ b/src/main/java/org/isf/config/SpringFoxConfig.java @@ -35,6 +35,8 @@ private SecurityContext actuatorSecurityContext() { return SecurityContext.builder() .securityReferences(Arrays.asList(basicAuthReference())) .forPaths(PathSelectors.ant("/patients/**")) + //.forPaths(PathSelectors.ant("/diseases/**")) + //.forPaths(PathSelectors.ant("/opds/**")) .build(); } diff --git a/src/main/java/org/isf/disctype/rest/DischargeTypeController.java b/src/main/java/org/isf/disctype/rest/DischargeTypeController.java index 36b42523a..f7db1d43d 100644 --- a/src/main/java/org/isf/disctype/rest/DischargeTypeController.java +++ b/src/main/java/org/isf/disctype/rest/DischargeTypeController.java @@ -1,10 +1,6 @@ package org.isf.disctype.rest; -import java.util.List; -import java.util.stream.Collectors; - -import static org.isf.shared.mapper.OHModelMapper.getObjectMapper; - +import io.swagger.annotations.Api; import org.isf.disctype.dto.DischargeTypeDTO; import org.isf.disctype.manager.DischargeTypeBrowserManager; import org.isf.disctype.model.DischargeType; @@ -12,21 +8,17 @@ import org.isf.utils.exception.OHServiceException; import org.isf.utils.exception.model.OHExceptionMessage; import org.isf.utils.exception.model.OHSeverityLevel; +import org.modelmapper.ModelMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.PutMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; -import io.swagger.annotations.Api; +import java.util.List; +import java.util.stream.Collectors; @RestController @Api(value = "/dischargetype", produces = MediaType.APPLICATION_JSON_VALUE) @@ -35,6 +27,9 @@ public class DischargeTypeController { @Autowired protected DischargeTypeBrowserManager discTypeManager; + @Autowired + protected ModelMapper modelMapper; + private final Logger logger = LoggerFactory.getLogger(DischargeTypeController.class); public DischargeTypeController(DischargeTypeBrowserManager discTypeManager) { @@ -51,7 +46,7 @@ public DischargeTypeController(DischargeTypeBrowserManager discTypeManager) { ResponseEntity newDischargeType(@RequestBody DischargeTypeDTO dischTypeDTO) throws OHServiceException { String code = dischTypeDTO.getCode(); logger.info("Create discharge type " + code); - boolean isCreated = discTypeManager.newDischargeType(getObjectMapper().map(dischTypeDTO, DischargeType.class)); + boolean isCreated = discTypeManager.newDischargeType(modelMapper.map(dischTypeDTO, DischargeType.class)); DischargeType dischTypeCreated = null; List dischTypeFounds = discTypeManager.getDischargeType().stream().filter(ad -> ad.getCode().equals(code)) .collect(Collectors.toList()); @@ -73,7 +68,7 @@ ResponseEntity newDischargeType(@RequestBody DischargeTypeDTO dischTypeD @PutMapping(value = "/dischargetypes", produces = MediaType.APPLICATION_JSON_VALUE) ResponseEntity updateDischargeTypet(@RequestBody DischargeTypeDTO dischTypeDTO) throws OHServiceException { logger.info("Update dischargetypes code:" + dischTypeDTO.getCode()); - DischargeType dischType = getObjectMapper().map(dischTypeDTO, DischargeType.class); + DischargeType dischType = modelMapper.map(dischTypeDTO, DischargeType.class); if(!discTypeManager.codeControl(dischTypeDTO.getCode())) throw new OHAPIException( new OHExceptionMessage(null, "discharge type not found!", OHSeverityLevel.ERROR)); @@ -94,7 +89,7 @@ public ResponseEntity> getDischargeTypes() throws OHServi logger.info("Get all discharge types "); List dischTypes = discTypeManager.getDischargeType(); List dischTypeDTOs = dischTypes.stream() - .map(dischType -> getObjectMapper().map(dischType, DischargeTypeDTO.class)).collect(Collectors.toList()); + .map(dischType -> modelMapper.map(dischType, DischargeTypeDTO.class)).collect(Collectors.toList()); if (dischTypeDTOs.size() == 0) { return ResponseEntity.status(HttpStatus.NO_CONTENT).body(dischTypeDTOs); } else { diff --git a/src/main/java/org/isf/disease/rest/DiseaseController.java b/src/main/java/org/isf/disease/rest/DiseaseController.java index 688c73d0e..69f815854 100644 --- a/src/main/java/org/isf/disease/rest/DiseaseController.java +++ b/src/main/java/org/isf/disease/rest/DiseaseController.java @@ -1,287 +1,169 @@ package org.isf.disease.rest; -import static org.isf.shared.mapper.OHModelMapper.getObjectMapper; - -import java.util.HashMap; +import java.util.ArrayList; import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - -import javax.validation.Valid; import org.isf.disease.dto.DiseaseDTO; import org.isf.disease.manager.DiseaseBrowserManager; import org.isf.disease.model.Disease; -import org.isf.shared.exceptions.OHAPIException; +import org.isf.shared.rest.OHApiAbstractController; import org.isf.utils.exception.OHServiceException; -import org.isf.utils.exception.model.OHExceptionMessage; -import org.isf.utils.exception.model.OHSeverityLevel; +import org.modelmapper.ModelMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.PutMapping; -import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import io.swagger.annotations.Api; import io.swagger.annotations.Authorization; @RestController -@Api(value="/diseases",produces = MediaType.APPLICATION_JSON_VALUE, authorizations = {@Authorization(value="basicAuth")}) -public class DiseaseController { - @Autowired - protected DiseaseBrowserManager diseaseManager; +@Api(value = "/diseases", produces = MediaType.APPLICATION_JSON_VALUE, authorizations = {@Authorization(value = "basicAuth")}) +public class DiseaseController extends OHApiAbstractController { + + @Autowired + protected DiseaseBrowserManager diseaseManager; + + private final Logger logger = LoggerFactory.getLogger(DiseaseController.class); - private final Logger logger = LoggerFactory.getLogger(DiseaseController.class); + @Autowired + protected ModelMapper modelMapper; - public DiseaseController(DiseaseBrowserManager diseaseManager) { - this.diseaseManager = diseaseManager; - } - - /** - * Gets all the stored {@link Disease} with ODP flag true. - * @return the stored diseases with ODP flag true. - * @throws OHServiceException - */ - @GetMapping(value = "/diseases/opd", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity> getDiseasesOpd() throws OHServiceException { - logger.info("Get opd diseases"); - List diseases = diseaseManager.getDiseaseOpd(); - if(diseases != null) { - return computeResponse(diseases); - } else { - throw new OHAPIException(new OHExceptionMessage(null, "error while getting opd diseases", OHSeverityLevel.ERROR), HttpStatus.INTERNAL_SERVER_ERROR); - } - } - - /** - * Gets all the stored {@link Disease} with the specified typecode and flag ODP true. - * @param typeCode - the filter typecode. - * @return the retrieved diseases. - * @throws OHServiceException - */ - @GetMapping(value = "/diseases/opd/{typecode}", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity> getDiseasesOpdByCode(@PathVariable("typecode") String typeCode) throws OHServiceException { - logger.info("Get opd diseases by type code"); - List diseases = diseaseManager.getDiseaseOpd(typeCode); - if(diseases != null) { - return computeResponse(diseases); - } else { - throw new OHAPIException(new OHExceptionMessage(null, "error while getting opd diseases", OHSeverityLevel.ERROR), HttpStatus.INTERNAL_SERVER_ERROR); - } - } - - /** - * Gets all the stored {@link Disease} with IPD_OUT flag true. - * @return the stored disease with IPD flag true. - * @throws OHServiceException - */ - @GetMapping(value = "/diseases/ipd/out", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity> getDiseasesIpdOut() throws OHServiceException { - logger.info("Get ipd out diseases"); - List diseases = diseaseManager.getDiseaseIpdOut(); - if(diseases != null) { - return computeResponse(diseases); - } else { - throw new OHAPIException(new OHExceptionMessage(null, "error while getting ipd out diseases", OHSeverityLevel.ERROR), HttpStatus.INTERNAL_SERVER_ERROR); - } - } - - /** - * Gets all the stored {@link Disease} with the specified typecode and the flag IPD_OUT true. - * @param typeCode - the filter typecode. - * @return the retrieved diseases. - * @throws OHServiceException - */ - @GetMapping(value = "/diseases/ipd/out/{typecode}", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity> getDiseasesIpdOutByCode(@PathVariable("typecode") String typeCode) throws OHServiceException { - logger.info("Get ipd out diseases by type code"); - List diseases = diseaseManager.getDiseaseIpdOut(typeCode); - if(diseases != null) { - return computeResponse(diseases); - } else { - throw new OHAPIException(new OHExceptionMessage(null, "error while getting ipd out diseases", OHSeverityLevel.ERROR), HttpStatus.INTERNAL_SERVER_ERROR); - } - } - - /** - * Gets all the stored {@link Disease} with IPD_IN flag true. - * @return the stored disease with IPD flag true. - * @throws OHServiceException - */ - @GetMapping(value = "/diseases/ipd/in", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity> getDiseasesIpdIn() throws OHServiceException { - logger.info("Get ipd-in diseases"); - List diseases = diseaseManager.getDiseaseIpdIn(); - if(diseases != null) { - return computeResponse(diseases); - } else { - throw new OHAPIException(new OHExceptionMessage(null, "error while getting ipd-in diseases", OHSeverityLevel.ERROR), HttpStatus.INTERNAL_SERVER_ERROR); - } - } - - /** - * Gets all the stored {@link Disease} with the specified typecode and the flag IPD_IN true. - * @param typeCode - the filter typecode. - * @return the retrieved diseases. - * @throws OHServiceException - */ - @GetMapping(value = "/diseases/ipd/in/{typecode}", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity> getDiseasesIpdInByCode(@PathVariable("typecode") String typeCode) throws OHServiceException { - logger.info("Get ipd-in diseases by type code"); - List diseases = diseaseManager.getDiseaseIpdIn(typeCode); - if(diseases != null) { - return computeResponse(diseases); - } else { - throw new OHAPIException(new OHExceptionMessage(null, "error while getting ipd-in diseases", OHSeverityLevel.ERROR), HttpStatus.INTERNAL_SERVER_ERROR); - } - } - - /** - * Gets both OPD and IPDs diseases. - * @return the stored diseases. - * @throws OHServiceException - */ - @GetMapping(value = "/diseases/both", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity> getDiseases() throws OHServiceException { - logger.info("Get both ipd and opd diseases"); - List diseases = diseaseManager.getDisease(); - if(diseases != null) { - return computeResponse(diseases); - } else { - throw new OHAPIException(new OHExceptionMessage(null, "error while getting diseases", OHSeverityLevel.ERROR), HttpStatus.INTERNAL_SERVER_ERROR); - } - } - - /** - * Retrieves all OPD and IPDs {@link Disease} with the specified typecode. - * @param typeCode - the filter typecode. - * @return all the diseases with the specified typecode. - * @throws OHServiceException - */ - @GetMapping(value = "/diseases/both/{typecode}", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity> getDiseases(@PathVariable("typecode") String typeCode) throws OHServiceException { - logger.info("Get both ipd and opd diseases by type code"); - List diseases = diseaseManager.getDisease(typeCode); - if(diseases != null) { - return computeResponse(diseases); - } else { - throw new OHAPIException(new OHExceptionMessage(null, "error while getting diseases by type code", OHSeverityLevel.ERROR), HttpStatus.INTERNAL_SERVER_ERROR); - } - } - - /** - * Returns all diseases, deleted ones also - * @return the stored diseases. - * @throws OHServiceException - */ - @GetMapping(value = "/diseases/all", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity> getAllDiseases() throws OHServiceException { - logger.info("Get all diseases, deleted ones too"); - List diseases = diseaseManager.getDiseaseAll(); - if(diseases != null) { - return computeResponse(diseases); - } else { - throw new OHAPIException(new OHExceptionMessage(null, "error while getting all diseases", OHSeverityLevel.ERROR), HttpStatus.INTERNAL_SERVER_ERROR); - } - } - - /** - * Gets a {@link Disease} with the specified code. - * @param code - the disease code. - * @return the found disease. - * @throws OHServiceException - */ - @GetMapping(value = "/diseases/{code}", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity getDiseaseByCode(@PathVariable("code") int code) throws OHServiceException { - logger.info("Get disease by code"); - Disease disease = diseaseManager.getDiseaseByCode(code); - if(disease != null) { - return ResponseEntity.ok(getObjectMapper().map(disease, DiseaseDTO.class)); - } else { - throw new OHAPIException(new OHExceptionMessage(null, "no disease found with the specified code", OHSeverityLevel.ERROR), HttpStatus.INTERNAL_SERVER_ERROR); - } - } - - /** - * Stores the specified {@link Disease}. - * @param diseaseDTO - the disease to store. - * @return the stored disease - * @throws OHServiceException - */ - @PostMapping(value="/diseases", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity newDisease(@Valid @RequestBody DiseaseDTO diseaseDTO) throws OHServiceException { - Disease disease = getObjectMapper().map(diseaseDTO, Disease.class); - if(diseaseManager.codeControl(disease.getCode())) { - throw new OHAPIException(new OHExceptionMessage(null, "duplicated disease code", OHSeverityLevel.ERROR), HttpStatus.INTERNAL_SERVER_ERROR); - } else if(diseaseManager.descriptionControl(disease.getDescription(), disease.getType().getCode())) { - throw new OHAPIException(new OHExceptionMessage(null, "duplicated disease description for the same disease type", OHSeverityLevel.ERROR), HttpStatus.INTERNAL_SERVER_ERROR); - } - - if(diseaseManager.newDisease(disease)) { - return ResponseEntity.status(HttpStatus.CREATED).body(diseaseDTO); + public DiseaseController(DiseaseBrowserManager diseaseManager, ModelMapper modelMapper) { + super(modelMapper); + this.diseaseManager = diseaseManager; + } + + @GetMapping(value = "/diseases/all", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity> getDiseaseAll() throws OHServiceException { + logger.info("getDiseaseAll"); + ArrayList diseaseOpd = diseaseManager.getDiseaseAll(); + List diseaseDTOS = toDTOList(diseaseOpd); + if (diseaseDTOS.size() == 0) { + return ResponseEntity.status(HttpStatus.NO_CONTENT).body(diseaseDTOS); } else { - throw new OHAPIException(new OHExceptionMessage(null, "disease is not created!", OHSeverityLevel.ERROR), HttpStatus.INTERNAL_SERVER_ERROR); + return ResponseEntity.ok(diseaseDTOS); } - } - - /** - * Updates the specified {@link Disease}. - * @param diseaseDTO - the disease to update. - * @return the updated disease - * @throws OHServiceException - */ - @PutMapping(value="/diseases", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity updateDisease(@Valid @RequestBody DiseaseDTO diseaseDTO) throws OHServiceException { - Disease disease = getObjectMapper().map(diseaseDTO, Disease.class); - if(!diseaseManager.codeControl(disease.getCode())) { - throw new OHAPIException(new OHExceptionMessage(null, "disease not found", OHSeverityLevel.ERROR), HttpStatus.INTERNAL_SERVER_ERROR); - } - - if(diseaseManager.updateDisease(disease)) { - return ResponseEntity.ok(diseaseDTO); + } + + @GetMapping(value = "/diseases/opd", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity> getDiseaseOpd() throws OHServiceException { + logger.info("getDiseaseOpd"); + ArrayList diseases = diseaseManager.getDiseaseOpd(); + List diseaseDTOS = toDTOList(diseases); + if (diseaseDTOS.size() == 0) { + return ResponseEntity.status(HttpStatus.NO_CONTENT).body(diseaseDTOS); } else { - throw new OHAPIException(new OHExceptionMessage(null, "disease is not updated!", OHSeverityLevel.ERROR), HttpStatus.INTERNAL_SERVER_ERROR); + return ResponseEntity.ok(diseaseDTOS); } - } - - /** - * Mark as deleted the specified {@link Disease}. - * @param code - the code of the disease to mark delete. - * @return true if the disease has been marked, false otherwise. - * @throws OHServiceException - */ - @DeleteMapping(value = "/diseases/{code}", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity> deleteDisease(@PathVariable("code") int code) throws OHServiceException { - Disease disease = diseaseManager.getDiseaseByCode(code); - if(disease != null) { - Map result = new HashMap(); - result.put("deleted", diseaseManager.deleteDisease(disease)); - return ResponseEntity.ok(result); - } else { - throw new OHAPIException(new OHExceptionMessage(null, - "no disease found with the specified code", - OHSeverityLevel.ERROR), HttpStatus.INTERNAL_SERVER_ERROR); - } - } - - private ResponseEntity> computeResponse(List diseases) { - List diseasesDTO = diseases - .stream() - .map(item -> getObjectMapper().map(item, DiseaseDTO.class)) - .collect(Collectors.toList()); - if(diseasesDTO.size() == 0){ - return ResponseEntity.status(HttpStatus.NO_CONTENT).body(diseasesDTO); - }else{ - return ResponseEntity.ok(diseasesDTO); + } + + @GetMapping(value = "/diseases/opd/{typecode}", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity> getDiseaseOpdByTypeCode(@PathVariable String typecode) throws OHServiceException { + logger.info("getDiseaseOpdByTypeCode"); + ArrayList diseaseOpd = diseaseManager.getDiseaseOpd(typecode); + List diseaseDTOS = toDTOList(diseaseOpd); + if (diseaseDTOS.size() == 0) { + return ResponseEntity.status(HttpStatus.NO_CONTENT).body(diseaseDTOS); + } else { + return ResponseEntity.ok(diseaseDTOS); } - } + } + + @GetMapping(value = "/diseases/ipdOut", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity> getDiseaseIpdOut() throws OHServiceException { + logger.info("getDiseaseIpdOut"); + ArrayList diseases = diseaseManager.getDiseaseIpdOut(); + List diseaseDTOS = toDTOList(diseases); + if (diseaseDTOS.size() == 0) { + return ResponseEntity.status(HttpStatus.NO_CONTENT).body(diseaseDTOS); + } else { + return ResponseEntity.ok(diseaseDTOS); + } + } + + @GetMapping(value = "/diseases/ipdOut/{typecode}", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity> getDiseaseIpdOutByTypeCode(@PathVariable String typecode) throws OHServiceException { + logger.info(String.format("getDiseaseIpdOutByTypeCode typecode [%s]", typecode)); + ArrayList diseases = diseaseManager.getDiseaseIpdOut(typecode); + List diseaseDTOS = toDTOList(diseases); + if (diseaseDTOS.size() == 0) { + return ResponseEntity.status(HttpStatus.NO_CONTENT).body(diseaseDTOS); + } else { + return ResponseEntity.ok(diseaseDTOS); + } + } + + @GetMapping(value = "/diseases/ipdIn", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity> getDiseaseIpdIn() throws OHServiceException { + logger.info("getDiseaseIpdIn"); + ArrayList diseases = diseaseManager.getDiseaseIpdIn(); + List diseaseDTOS = toDTOList(diseases); + if (diseaseDTOS.size() == 0) { + return ResponseEntity.status(HttpStatus.NO_CONTENT).body(diseaseDTOS); + } else { + return ResponseEntity.ok(diseaseDTOS); + } + } + + @GetMapping(value = "/diseases/ipdIn/{typecode}", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity> getDiseaseIpdInByTypeCode(@PathVariable String typecode) throws OHServiceException { + logger.info(String.format("getDiseaseIpdInByTypeCode typecode [%s]", typecode)); + ArrayList diseases = diseaseManager.getDiseaseIpdIn(typecode); + List diseaseDTOS = toDTOList(diseases); + if (diseaseDTOS.size() == 0) { + return ResponseEntity.status(HttpStatus.NO_CONTENT).body(diseaseDTOS); + } else { + return ResponseEntity.ok(diseaseDTOS); + } + } + + @GetMapping(value = "/diseases", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity> getDisease() throws OHServiceException { + logger.info("getDisease"); + ArrayList diseases = diseaseManager.getDisease(); + List diseaseDTOS = toDTOList(diseases); + if (diseaseDTOS.size() == 0) { + return ResponseEntity.status(HttpStatus.NO_CONTENT).body(diseaseDTOS); + } else { + return ResponseEntity.ok(diseaseDTOS); + } + } + + @GetMapping(value = "/diseases/{typecode}", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity> getDiseaseByTypeCode(@PathVariable String typecode) throws OHServiceException { + logger.info(String.format("getDiseaseByTypeCode typecode [%s]", typecode)); + ArrayList diseases = diseaseManager.getDisease(typecode); + List diseaseDTOS = toDTOList(diseases); + if (diseaseDTOS.size() == 0) { + return ResponseEntity.status(HttpStatus.NO_CONTENT).body(diseaseDTOS); + } else { + return ResponseEntity.ok(diseaseDTOS); + } + } + + @GetMapping(value = "/diseases/disease/{code}", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity getDiseaseByCode(@PathVariable int code) throws OHServiceException { + logger.info(String.format("getDiseaseByCode typecode [%d]", code)); + Disease disease = diseaseManager.getDiseaseByCode(code); + if (disease == null) { + return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null); + } + return ResponseEntity.ok(toDTO(disease)); + } + + @Override + protected Class getDTOClass() { + return DiseaseDTO.class; + } + @Override + protected Class getModelClass() { + return Disease.class; + } } diff --git a/src/main/java/org/isf/distype/rest/DiseaseTypeController.java b/src/main/java/org/isf/distype/rest/DiseaseTypeController.java index 83e7c4697..bf9215f8f 100644 --- a/src/main/java/org/isf/distype/rest/DiseaseTypeController.java +++ b/src/main/java/org/isf/distype/rest/DiseaseTypeController.java @@ -1,13 +1,7 @@ package org.isf.distype.rest; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.stream.Collectors; - -import javax.validation.Valid; - +import io.swagger.annotations.Api; +import io.swagger.annotations.Authorization; import org.isf.distype.dto.DiseaseTypeDTO; import org.isf.distype.manager.DiseaseTypeBrowserManager; import org.isf.distype.model.DiseaseType; @@ -15,30 +9,30 @@ import org.isf.utils.exception.OHServiceException; import org.isf.utils.exception.model.OHExceptionMessage; import org.isf.utils.exception.model.OHSeverityLevel; +import org.modelmapper.ModelMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.PutMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; -import io.swagger.annotations.Api; -import io.swagger.annotations.Authorization; - -import static org.isf.shared.mapper.OHModelMapper.getObjectMapper; +import javax.validation.Valid; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.stream.Collectors; @RestController @Api(value="/diseasetypes",produces = MediaType.APPLICATION_JSON_VALUE, authorizations = {@Authorization(value="basicAuth")}) public class DiseaseTypeController { @Autowired private DiseaseTypeBrowserManager diseaseTypeManager; + + @Autowired + protected ModelMapper modelMapper; private final Logger logger = LoggerFactory.getLogger(DiseaseTypeController.class); @@ -55,7 +49,7 @@ public DiseaseTypeController(DiseaseTypeBrowserManager diseaseTypeManager) { public ResponseEntity> getAllDiseaseTypes() throws OHServiceException { List results = diseaseTypeManager.getDiseaseType(); List parsedResults= results.stream() - .map(item -> getObjectMapper().map(item, DiseaseTypeDTO.class)) + .map(item -> modelMapper.map(item, DiseaseTypeDTO.class)) .collect(Collectors.toList()); if(parsedResults.size() > 0){ return ResponseEntity.ok(parsedResults); @@ -72,7 +66,7 @@ public ResponseEntity> getAllDiseaseTypes() throws OHServic */ @PostMapping(value = "/diseasetypes", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity newDiseaseType(@Valid @RequestBody DiseaseTypeDTO diseaseTypeDTO) throws OHServiceException { - DiseaseType diseaseType = getObjectMapper().map(diseaseTypeDTO, DiseaseType.class); + DiseaseType diseaseType = modelMapper.map(diseaseTypeDTO, DiseaseType.class); logger.info("creating new disease type with code `"+ diseaseType.getCode() + "`"); if(diseaseTypeManager.codeControl(diseaseType.getCode())) { throw new OHAPIException(new OHExceptionMessage(null, @@ -97,7 +91,7 @@ public ResponseEntity newDiseaseType(@Valid @RequestBody Disease */ @PutMapping(value = "/diseasetypes", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity updateDiseaseType(@Valid @RequestBody DiseaseTypeDTO diseaseTypeDTO) throws OHServiceException { - DiseaseType diseaseType = getObjectMapper().map(diseaseTypeDTO, DiseaseType.class); + DiseaseType diseaseType = modelMapper.map(diseaseTypeDTO, DiseaseType.class); if(!diseaseTypeManager.codeControl(diseaseType.getCode())) { throw new OHAPIException(new OHExceptionMessage(null, "disease type not found!", diff --git a/src/main/java/org/isf/dlvrrestype/rest/DeliveryResultTypeController.java b/src/main/java/org/isf/dlvrrestype/rest/DeliveryResultTypeController.java index 40f410539..abed41388 100644 --- a/src/main/java/org/isf/dlvrrestype/rest/DeliveryResultTypeController.java +++ b/src/main/java/org/isf/dlvrrestype/rest/DeliveryResultTypeController.java @@ -1,10 +1,6 @@ package org.isf.dlvrrestype.rest; -import java.util.List; -import java.util.stream.Collectors; - -import static org.isf.shared.mapper.OHModelMapper.getObjectMapper; - +import io.swagger.annotations.Api; import org.isf.dlvrrestype.dto.DeliveryResultTypeDTO; import org.isf.dlvrrestype.manager.DeliveryResultTypeBrowserManager; import org.isf.dlvrrestype.model.DeliveryResultType; @@ -12,21 +8,17 @@ import org.isf.utils.exception.OHServiceException; import org.isf.utils.exception.model.OHExceptionMessage; import org.isf.utils.exception.model.OHSeverityLevel; +import org.modelmapper.ModelMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.PutMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; -import io.swagger.annotations.Api; +import java.util.List; +import java.util.stream.Collectors; @RestController @Api(value = "/deliveryresulttype", produces = MediaType.APPLICATION_JSON_VALUE) @@ -35,6 +27,9 @@ public class DeliveryResultTypeController { @Autowired protected DeliveryResultTypeBrowserManager dlvrrestManager; + @Autowired + protected ModelMapper modelMapper; + private final Logger logger = LoggerFactory.getLogger(DeliveryResultTypeController.class); public DeliveryResultTypeController(DeliveryResultTypeBrowserManager dlvrrestManager) { @@ -53,7 +48,7 @@ ResponseEntity newDeliveryResultType(@RequestBody DeliveryResultTypeDTO String code = dlvrrestTypeDTO.getCode(); logger.info("Create Delivery result type " + code); boolean isCreated = dlvrrestManager - .newDeliveryResultType(getObjectMapper().map(dlvrrestTypeDTO, DeliveryResultType.class)); + .newDeliveryResultType(modelMapper.map(dlvrrestTypeDTO, DeliveryResultType.class)); DeliveryResultType dlvrrestTypeCreated = null; List dlvrrestTypeFounds = dlvrrestManager.getDeliveryResultType().stream() .filter(ad -> ad.getCode().equals(code)).collect(Collectors.toList()); @@ -77,7 +72,7 @@ ResponseEntity newDeliveryResultType(@RequestBody DeliveryResultTypeDTO ResponseEntity updateDeliveryResultTypet(@RequestBody DeliveryResultTypeDTO dlvrrestTypeDTO) throws OHServiceException { logger.info("Update deliveryresulttypes code:" + dlvrrestTypeDTO.getCode()); - DeliveryResultType dlvrrestType = getObjectMapper().map(dlvrrestTypeDTO, DeliveryResultType.class); + DeliveryResultType dlvrrestType = modelMapper.map(dlvrrestTypeDTO, DeliveryResultType.class); if (!dlvrrestManager.codeControl(dlvrrestType.getCode())) throw new OHAPIException( new OHExceptionMessage(null, "Delivery result type not found!", OHSeverityLevel.ERROR)); @@ -99,7 +94,7 @@ public ResponseEntity> getDeliveryResultTypes() thro logger.info("Get all Delivery result types "); List dlvrrestissionTypes = dlvrrestManager.getDeliveryResultType(); List dlvrrestTypeDTOs = dlvrrestissionTypes.stream() - .map(dlvrrestType -> getObjectMapper().map(dlvrrestType, DeliveryResultTypeDTO.class)) + .map(dlvrrestType -> modelMapper.map(dlvrrestType, DeliveryResultTypeDTO.class)) .collect(Collectors.toList()); if (dlvrrestTypeDTOs.size() == 0) { return ResponseEntity.status(HttpStatus.NO_CONTENT).body(dlvrrestTypeDTOs); diff --git a/src/main/java/org/isf/dlvrtype/rest/DeliveryTypeController.java b/src/main/java/org/isf/dlvrtype/rest/DeliveryTypeController.java index c60677db3..101258b7c 100644 --- a/src/main/java/org/isf/dlvrtype/rest/DeliveryTypeController.java +++ b/src/main/java/org/isf/dlvrtype/rest/DeliveryTypeController.java @@ -1,10 +1,6 @@ package org.isf.dlvrtype.rest; -import java.util.List; -import java.util.stream.Collectors; - -import static org.isf.shared.mapper.OHModelMapper.getObjectMapper; - +import io.swagger.annotations.Api; import org.isf.dlvrtype.dto.DeliveryTypeDTO; import org.isf.dlvrtype.manager.DeliveryTypeBrowserManager; import org.isf.dlvrtype.model.DeliveryType; @@ -12,21 +8,17 @@ import org.isf.utils.exception.OHServiceException; import org.isf.utils.exception.model.OHExceptionMessage; import org.isf.utils.exception.model.OHSeverityLevel; +import org.modelmapper.ModelMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.PutMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; -import io.swagger.annotations.Api; +import java.util.List; +import java.util.stream.Collectors; @RestController @Api(value = "/deliverytype", produces = MediaType.APPLICATION_JSON_VALUE) @@ -35,6 +27,9 @@ public class DeliveryTypeController { @Autowired protected DeliveryTypeBrowserManager dlvrtypeManager; + @Autowired + protected ModelMapper modelMapper; + private final Logger logger = LoggerFactory.getLogger(DeliveryTypeController.class); public DeliveryTypeController(DeliveryTypeBrowserManager dlvrtypeManager) { @@ -51,7 +46,7 @@ public DeliveryTypeController(DeliveryTypeBrowserManager dlvrtypeManager) { ResponseEntity newDeliveryType(@RequestBody DeliveryTypeDTO dlvrTypeDTO) throws OHServiceException { String code = dlvrTypeDTO.getCode(); logger.info("Create Delivery type " + code); - boolean isCreated = dlvrtypeManager.newDeliveryType(getObjectMapper().map(dlvrTypeDTO, DeliveryType.class)); + boolean isCreated = dlvrtypeManager.newDeliveryType(modelMapper.map(dlvrTypeDTO, DeliveryType.class)); DeliveryType dlvrTypeCreated = null; List dlvrTypeFounds = dlvrtypeManager.getDeliveryType().stream().filter(ad -> ad.getCode().equals(code)) .collect(Collectors.toList()); @@ -73,7 +68,7 @@ ResponseEntity newDeliveryType(@RequestBody DeliveryTypeDTO dlvrTypeDTO) @PutMapping(value = "/deliverytypes/{code}", produces = MediaType.APPLICATION_JSON_VALUE) ResponseEntity updateDeliveryTypet(@RequestBody DeliveryTypeDTO dlvrTypeDTO) throws OHServiceException { logger.info("Update deliverytypes code:" + dlvrTypeDTO.getCode()); - DeliveryType dlvrType = getObjectMapper().map(dlvrTypeDTO, DeliveryType.class); + DeliveryType dlvrType = modelMapper.map(dlvrTypeDTO, DeliveryType.class); if(!dlvrtypeManager.codeControl(dlvrType.getCode())) throw new OHAPIException( new OHExceptionMessage(null, "Delivery type not found!", OHSeverityLevel.ERROR)); @@ -94,7 +89,7 @@ public ResponseEntity> getDeliveryTypes() throws OHService logger.info("Get all Delivery types "); List dlvrTypes = dlvrtypeManager.getDeliveryType(); List dlvrTypeDTOs = dlvrTypes.stream() - .map(dlvrType -> getObjectMapper().map(dlvrType, DeliveryTypeDTO.class)).collect(Collectors.toList()); + .map(dlvrType -> modelMapper.map(dlvrType, DeliveryTypeDTO.class)).collect(Collectors.toList()); if (dlvrTypeDTOs.size() == 0) { return ResponseEntity.status(HttpStatus.NO_CONTENT).body(dlvrTypeDTOs); } else { diff --git a/src/main/java/org/isf/opd/dto/OpdDTO.java b/src/main/java/org/isf/opd/dto/OpdDTO.java new file mode 100644 index 000000000..67e72fa53 --- /dev/null +++ b/src/main/java/org/isf/opd/dto/OpdDTO.java @@ -0,0 +1,197 @@ +package org.isf.opd.dto; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import org.isf.disease.dto.DiseaseDTO; +import org.isf.patient.dto.PatientDTO; + +import java.util.Date; +import java.util.GregorianCalendar; + +@ApiModel(description = "Class representing a opd") +public class OpdDTO { + + private Integer code; + + @ApiModelProperty(notes = "Date", example = "1979-05-01", position = 1) + private Date date; + + @ApiModelProperty(notes = "Visit Date", example = "1979-05-01", position = 2) + private GregorianCalendar visitDate; + + @ApiModelProperty(notes = "PatientDTO", position = 3) + private PatientDTO patient; + + @ApiModelProperty(notes = "Age", example = "40", position = 4) + private int age; + + @ApiModelProperty(notes = "Sex", allowableValues = "F,M", example = "M", position = 5) + private char sex; + + @ApiModelProperty(notes = "Note", example = "Bla bla", position = 6) + private String note; //ADDED: Alex + + @ApiModelProperty(notes = "Prog year", example = "2020", position = 7) + private int prog_year; + + @ApiModelProperty(notes = "Disease", position = 8) + private DiseaseDTO disease; + + @ApiModelProperty(notes = "Disease2", position = 9) + private DiseaseDTO disease2; + + @ApiModelProperty(notes = "Disease3", position = 10) + private DiseaseDTO disease3; + + @ApiModelProperty(notes = "New patient", allowableValues = "n,R", example = "R", position = 11) + private char newPatient; //n=NEW R=REATTENDANCE + + @ApiModelProperty(notes = "Referral from", allowableValues = "R,null", example = "R", position = 12) + private String referralFrom; //R=referral from another unit; null=no referral from + + @ApiModelProperty(notes = "Referral to", allowableValues = "R,null", example = "R", position = 13) + private String referralTo; //R=referral to another unit; null=no referral to + + @ApiModelProperty(notes = "User ID", example = "test", position = 14) + private String userID; + + private int lock; + + private volatile int hashCode = 0; + + public Integer getCode() { + return code; + } + + public void setCode(Integer code) { + this.code = code; + } + + public Date getDate() { + return date; + } + + public void setDate(Date date) { + this.date = date; + } + + public GregorianCalendar getVisitDate() { + return visitDate; + } + + public void setVisitDate(GregorianCalendar visitDate) { + this.visitDate = visitDate; + } + + public PatientDTO getPatient() { + return patient; + } + + public void setPatient(PatientDTO patient) { + this.patient = patient; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public char getSex() { + return sex; + } + + public void setSex(char sex) { + this.sex = sex; + } + + public String getNote() { + return note; + } + + public void setNote(String note) { + this.note = note; + } + + public int getProg_year() { + return prog_year; + } + + public void setProg_year(int prog_year) { + this.prog_year = prog_year; + } + + public DiseaseDTO getDisease() { + return disease; + } + + public void setDisease(DiseaseDTO disease) { + this.disease = disease; + } + + public DiseaseDTO getDisease2() { + return disease2; + } + + public void setDisease2(DiseaseDTO disease2) { + this.disease2 = disease2; + } + + public DiseaseDTO getDisease3() { + return disease3; + } + + public void setDisease3(DiseaseDTO disease3) { + this.disease3 = disease3; + } + + public char getNewPatient() { + return newPatient; + } + + public void setNewPatient(char newPatient) { + this.newPatient = newPatient; + } + + public String getReferralFrom() { + return referralFrom; + } + + public void setReferralFrom(String referralFrom) { + this.referralFrom = referralFrom; + } + + public String getReferralTo() { + return referralTo; + } + + public void setReferralTo(String referralTo) { + this.referralTo = referralTo; + } + + public String getUserID() { + return userID; + } + + public void setUserID(String userID) { + this.userID = userID; + } + + public int getLock() { + return lock; + } + + public void setLock(int lock) { + this.lock = lock; + } + + public int getHashCode() { + return hashCode; + } + + public void setHashCode(int hashCode) { + this.hashCode = hashCode; + } +} diff --git a/src/main/java/org/isf/opd/rest/OpdController.java b/src/main/java/org/isf/opd/rest/OpdController.java new file mode 100644 index 000000000..8246fccc9 --- /dev/null +++ b/src/main/java/org/isf/opd/rest/OpdController.java @@ -0,0 +1,101 @@ +package org.isf.opd.rest; + +import java.util.ArrayList; +import java.util.Calendar; +import java.util.GregorianCalendar; +import java.util.List; + +import org.isf.opd.dto.OpdDTO; +import org.isf.opd.manager.OpdBrowserManager; +import org.isf.opd.model.Opd; +import org.isf.shared.rest.OHApiAbstractController; +import org.isf.utils.exception.OHServiceException; +import org.modelmapper.ModelMapper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import io.swagger.annotations.Api; +import io.swagger.annotations.Authorization; + +@RestController +@Api(value = "/opds", produces = MediaType.APPLICATION_JSON_VALUE, authorizations = {@Authorization(value = "basicAuth")}) +public class OpdController extends OHApiAbstractController { + private static final String DEFAULT_PAGE_SIZE = "80"; + + private final Logger logger = LoggerFactory.getLogger(OpdController.class); + + @Autowired + protected OpdBrowserManager opdManager; + + public OpdController(OpdBrowserManager opdManager, @Autowired ModelMapper modelMapper) { + super(modelMapper); + this.opdManager = opdManager; + } + + @PostMapping(value = "/opds", produces = MediaType.APPLICATION_JSON_VALUE) + ResponseEntity newOpd(@RequestBody OpdDTO newOpd) throws OHServiceException { + logger.info(String.format("Create opd for patient [%d]", newOpd.getPatient().getCode())); + Boolean createOpd = opdManager.newOpd(toModel(newOpd)); + return ResponseEntity.status(HttpStatus.CREATED).body(createOpd); + } + + @GetMapping(value = "/opds/{patientcode}", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity> getOpds(@PathVariable Integer patientcode) throws OHServiceException { + logger.info(String.format("Get opds by patientcode [%d]", patientcode)); + ArrayList opds = opdManager.getOpdList(patientcode); + List opdDTOS = toDTOList(opds); + if (opdDTOS.size() == 0) { + return ResponseEntity.status(HttpStatus.NO_CONTENT).body(opdDTOS); + } else { + return ResponseEntity.ok(opdDTOS); + } + } + + @GetMapping(value = "/opds/search", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity> searchOpds( + @RequestParam(value = "diseaseTypeCode", required = false, defaultValue = "") String diseaseTypeCode, + @RequestParam(value = "diseaseCode", required = false, defaultValue = "") String diseaseCode, + @RequestParam(value = "dateFrom", required = false) GregorianCalendar dateFrom, + @RequestParam(value = "dateTo", required = false) GregorianCalendar dateTo, + @RequestParam(value = "ageFrom", required = false, defaultValue = "0") Integer ageFrom, + @RequestParam(value = "ageTo", required = false, defaultValue = "100") Integer ageTo, + @RequestParam(value = "sex", required = false, defaultValue = "A") char sex, + @RequestParam(value = "newPatient", required = false, defaultValue = "R") char newPatient) throws OHServiceException { + logger.info(String.format("Search opds by diseaseTypeCode [%s], diseaseCode [%s], dateFrom [%s], dateTo [%s], ageFrom [%d], ageTo [%d], sex [%c], newPatient [%c]" + , diseaseTypeCode, diseaseCode, dateFrom, dateTo, ageFrom, ageTo, sex, newPatient)); + if (dateFrom == null) { + dateFrom = new GregorianCalendar(); + dateFrom.add(Calendar.DAY_OF_MONTH, - 7); + } + if (dateTo == null) { + dateTo = new GregorianCalendar(); + } + ArrayList opds = opdManager.getOpd(diseaseTypeCode, diseaseCode, dateFrom, dateTo, ageFrom, ageTo, sex, newPatient); + List opdDTOS = toDTOList(opds); + if (opdDTOS.size() == 0) { + return ResponseEntity.status(HttpStatus.NO_CONTENT).body(opdDTOS); + } else { + return ResponseEntity.ok(opdDTOS); + } + } + + @Override + protected Class getDTOClass() { + return OpdDTO.class; + } + + @Override + protected Class getModelClass() { + return Opd.class; + } +} diff --git a/src/main/java/org/isf/operation/dto/OperationDTO.java b/src/main/java/org/isf/operation/dto/OperationDTO.java index 0e031f4f6..7b22da634 100644 --- a/src/main/java/org/isf/operation/dto/OperationDTO.java +++ b/src/main/java/org/isf/operation/dto/OperationDTO.java @@ -1,62 +1,69 @@ package org.isf.operation.dto; -import javax.validation.constraints.NotNull; - +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; import org.isf.opetype.dto.OperationTypeDTO; +import javax.validation.constraints.NotNull; + +@ApiModel(description = "Class representing an operation") public class OperationDTO { - private String code; + @ApiModelProperty(notes = "Code", example = "", position = 1) + private String code; - @NotNull - private String description; + @NotNull + @ApiModelProperty(notes = "Description", example = "", position = 2) + private String description; - @NotNull - private OperationTypeDTO type; + @NotNull + @ApiModelProperty(notes = "Operation type", position = 3) + private OperationTypeDTO type; - @NotNull - private Integer major; + @NotNull + @ApiModelProperty(notes = "Major", position = 4) + private Integer major; - private Integer lock; + private Integer lock; - public String getCode() { - return code; - } + public String getCode() { + return code; + } - public void setCode(String code) { - this.code = code; - } + public void setCode(String code) { + this.code = code; + } - public String getDescription() { - return description; - } + public String getDescription() { + return description; + } - public void setDescription(String description) { - this.description = description; - } + public void setDescription(String description) { + this.description = description; + } - public OperationTypeDTO getType() { - return type; - } + public OperationTypeDTO getOperationType() { + return type; + } - public void setType(OperationTypeDTO type) { - this.type = type; - } + public void setOperationType(OperationTypeDTO type) { + this.type = type; + } - public Integer getMajor() { - return major; - } + public Integer getMajor() { + return major; + } - public void setMajor(Integer major) { - this.major = major; - } + public void setMajor(Integer major) { + this.major = major; + } - public Integer getLock() { - return lock; - } + public int getLock() { + return lock; + } - public void setLock(Integer lock) { - this.lock = lock; - } + public void setLock(int lock) { + this.lock = lock; + } } diff --git a/src/main/java/org/isf/operation/rest/OperationController.java b/src/main/java/org/isf/operation/rest/OperationController.java new file mode 100644 index 000000000..f29e7837e --- /dev/null +++ b/src/main/java/org/isf/operation/rest/OperationController.java @@ -0,0 +1,165 @@ +package org.isf.operation.rest; + +import java.util.List; + +import org.isf.operation.dto.OperationDTO; +import org.isf.operation.manager.OperationBrowserManager; +import org.isf.operation.model.Operation; +import org.isf.opetype.model.OperationType; +import org.isf.shared.rest.OHApiAbstractController; +import org.isf.utils.exception.OHServiceException; +import org.modelmapper.ModelMapper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PatchMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +import io.swagger.annotations.Api; +import io.swagger.annotations.Authorization; + +@RestController +@Api(value = "/operations", produces = MediaType.APPLICATION_JSON_VALUE, authorizations = {@Authorization(value = "basicAuth")}) +public class OperationController extends OHApiAbstractController { + + @Autowired + protected OperationBrowserManager operationBrowserManager; + + private final Logger logger = LoggerFactory.getLogger(OperationController.class); + + public OperationController(OperationBrowserManager operationBrowserManager, @Autowired ModelMapper modelMapper) { + super(modelMapper); + this.operationBrowserManager = operationBrowserManager; + } + + /** + * create a new {@link OperationDTO} + * + * @param newOperation + * @return + * @throws OHServiceException + */ + @PostMapping(value = "/operations", produces = MediaType.APPLICATION_JSON_VALUE) + ResponseEntity newOperation(@RequestBody OperationDTO newOperation) throws OHServiceException { + logger.info(String.format("newOperation [%s]", newOperation.getCode())); + Boolean createOperation = operationBrowserManager.newOperation(toModel(newOperation)); + return ResponseEntity.status(HttpStatus.CREATED).body(createOperation); + } + + /** + * return the list of {@link OperationDTO}s + * + * @return the list of {@link OperationDTO}s. It could be empty or null. + * @throws OHServiceException + */ + @GetMapping(value = "/operations", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity> getOperation() throws OHServiceException { + logger.info(String.format("getOperation")); + return ResponseEntity.ok().body(toDTOList(operationBrowserManager.getOperation())); + } + + /** + * return the {@link OperationDTO} with the specified code + * + * @param code + * @return the {@link OperationDTO} by code. + * @throws OHServiceException + */ + @GetMapping(value = "/operations/operation/{code}", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity getOperationByCode(String code) throws OHServiceException { + logger.info(String.format("getOperationByCode [%s]", code)); + return ResponseEntity.ok().body(toDTO(operationBrowserManager.getOperationByCode(code))); + } + + /** + * return the {@link Operation}s whose type matches specified string + * + * @param typeDescription - a type description + * @return the list of {@link Operation}s. It could be empty or null. + * @throws OHServiceException + */ + @GetMapping(value = "/operations/{typeDescription}", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity> getOperation(String typeDescription) throws OHServiceException { + logger.info(String.format("getOperation by typeDescription [%s]", typeDescription)); + return ResponseEntity.ok().body(toDTOList(operationBrowserManager.getOperation(typeDescription))); + } + + /** + * updates an {@link Operation} in the DB + * + * @param operation - the {@link OperationDTO} to update + * @return true if the item has been updated. false other + * @throws OHServiceException + */ + @PatchMapping(value = "/operations", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity updateOperation(@RequestBody OperationDTO operation) throws OHServiceException { + logger.info(String.format("updateOperation code [%s]", operation.getCode())); + // the user has confirmed he wants to overwrite the record + return operationBrowserManager.updateOperation(toModel(operation)) + ? ResponseEntity.status(HttpStatus.NO_CONTENT).body(Boolean.TRUE) + : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Boolean.FALSE); + } + + /** + * Delete a {@link Operation} in the DB + * + * @param operation - the {@link OperationDTO} to delete + * @return true if the item has been updated, false otherwise. + * @throws OHServiceException + */ + @DeleteMapping(value="/operations", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity deleteOperation(@RequestBody OperationDTO operation) throws OHServiceException { + logger.info(String.format("deleteOperation code [%s]", operation.getCode())); + return operationBrowserManager.deleteOperation(toModel(operation)) + ? ResponseEntity.status(HttpStatus.NO_CONTENT).body(Boolean.TRUE) + : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Boolean.FALSE); + } + + /** + * checks if an {@link Operation} code has already been use + * + * @param code - the code + * @return true if the code is already in use, false otherwise. + * @throws OHServiceException + */ + @GetMapping(value = "/operations/check/{code}", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity codeControl(String code) throws OHServiceException { + logger.info(String.format("codeControl [%s]", code)); + return operationBrowserManager.codeControl(code) + ? ResponseEntity.status(HttpStatus.NO_CONTENT).body(Boolean.TRUE) + : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Boolean.FALSE); + } + + /** + * checks if an {@link Operation} description has already been used within the specified {@link OperationType} + * + * @param description - the {@link Operation} description + * @param typeCode - the {@link OperationType} code + * @return true if the description is already in use, false otherwise. + * @throws OHServiceException + */ + @GetMapping(value = "/operations/descriptionControl/{description}/{typeCode}", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity descriptionControl(String description, String typeCode) throws OHServiceException { + logger.info(String.format("descriptionControl description [%s] typeCode [%s]", description, typeCode)); + return operationBrowserManager.descriptionControl(description,typeCode) + ? ResponseEntity.status(HttpStatus.NO_CONTENT).body(Boolean.TRUE) + : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Boolean.FALSE); + } + + @Override + protected Class getDTOClass() { + return OperationDTO.class; + } + + @Override + protected Class getModelClass() { + return Operation.class; + } +} diff --git a/src/main/java/org/isf/opetype/dto/OperationTypeDTO.java b/src/main/java/org/isf/opetype/dto/OperationTypeDTO.java index bf392284a..4ab08c96e 100644 --- a/src/main/java/org/isf/opetype/dto/OperationTypeDTO.java +++ b/src/main/java/org/isf/opetype/dto/OperationTypeDTO.java @@ -1,31 +1,33 @@ package org.isf.opetype.dto; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + import javax.validation.constraints.NotNull; +@ApiModel(description = "Class representing an operation type") public class OperationTypeDTO { - - private String code; - - @NotNull - private String description; + @ApiModelProperty(notes = "Code", example = "", position = 1) + private String code; - public String getCode() { - return code; - } + @NotNull + @ApiModelProperty(notes = "Description", example = "", position = 2) + private String description; - public void setCode(String code) { - this.code = code; - } + public String getCode() { + return code; + } - public String getDescription() { - return description; - } + public void setCode(String code) { + this.code = code; + } - public void setDescription(String description) { - this.description = description; - } - - + public String getDescription() { + return description; + } + public void setDescription(String description) { + this.description = description; + } } diff --git a/src/main/java/org/isf/opetype/rest/OperationTypeController.java b/src/main/java/org/isf/opetype/rest/OperationTypeController.java new file mode 100644 index 000000000..99123c5ec --- /dev/null +++ b/src/main/java/org/isf/opetype/rest/OperationTypeController.java @@ -0,0 +1,127 @@ +package org.isf.opetype.rest; + +import java.util.List; + +import org.isf.opetype.dto.OperationTypeDTO; +import org.isf.opetype.manager.OperationTypeBrowserManager; +import org.isf.opetype.model.OperationType; +import org.isf.shared.rest.OHApiAbstractController; +import org.isf.utils.exception.OHServiceException; +import org.modelmapper.ModelMapper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PatchMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +import io.swagger.annotations.Api; +import io.swagger.annotations.Authorization; + +@RestController +@Api(value = "/opetypes", produces = MediaType.APPLICATION_JSON_VALUE, authorizations = {@Authorization(value = "basicAuth")}) +public class OperationTypeController extends OHApiAbstractController { + + @Autowired + private OperationTypeBrowserManager operationTypeBrowserManager; + + private final Logger logger = LoggerFactory.getLogger(OperationTypeController.class); + + @Autowired + protected ModelMapper modelMapper; + + public OperationTypeController(OperationTypeBrowserManager operationTypeBrowserManager, ModelMapper modelMapper) { + super(modelMapper); + this.operationTypeBrowserManager = operationTypeBrowserManager; + } + + /** + * return the list of {@link OperationType}s + * + * @return the list of {@link OperationType}s. It could be empty or null. + * @throws OHServiceException + */ + @GetMapping(value = "/opetypes", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity> getOperationType() throws OHServiceException { + logger.info(String.format("getOperationType")); + List operationTypeList = operationTypeBrowserManager.getOperationType(); + return ResponseEntity.status(HttpStatus.FOUND).body(toDTOList(operationTypeList)); + } + + /** + * insert an {@link OperationType} in the DB + * + * @param operationType - the {@link OperationType} to insert + * @return true if the {@link OperationType} has been inserted, false otherwise. + * @throws OHServiceException + */ + @PostMapping( value = "/opetypes", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity newOperationType(@RequestBody OperationTypeDTO operationType) throws OHServiceException { + logger.info(String.format("newOperationType [%s]", operationType.getCode())); + return operationTypeBrowserManager.newOperationType(toModel(operationType)) + ? ResponseEntity.status(HttpStatus.CREATED).body(Boolean.TRUE) + : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Boolean.FALSE); + } + + /** + * update an {@link OperationType} + * + * @param operationType - the {@link OperationType} to update + * @return true if the {@link OperationType} has been updated, false otherwise. + * @throws OHServiceException + */ + @PatchMapping( value="/opetypes", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity updateOperationType(@RequestBody OperationTypeDTO operationType) throws OHServiceException { + logger.info(String.format("updateOperationType [%s]", operationType.getCode())); + return operationTypeBrowserManager.updateOperationType(toModel(operationType)) + ? ResponseEntity.status(HttpStatus.NO_CONTENT).body(Boolean.TRUE) + : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Boolean.FALSE); + } + + /** + * delete an {@link OperationType} + * + * @param operationType - the {@link OperationType} to delete + * @return true if the {@link OperationType} has been delete, false otherwise. + * @throws OHServiceException + */ + @DeleteMapping(value="/opetypes", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity deleteOperationType(@RequestBody OperationTypeDTO operationType) throws OHServiceException { + logger.info(String.format("deleteOperationType [%s]", operationType.getCode())); + return operationTypeBrowserManager.deleteOperationType(toModel(operationType)) + ? ResponseEntity.status(HttpStatus.NO_CONTENT).body(Boolean.TRUE) + : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Boolean.FALSE); + } + + /** + * checks if an {@link OperationType} code has already been used + * + * @param code - the code + * @return true if the code is already in use, false otherwise. + * @throws OHServiceException + */ + @GetMapping(value="/opetypes/check/{code}", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity codeControl(@PathVariable String code) throws OHServiceException { + logger.info(String.format("OperationType code [%s]", code)); + return operationTypeBrowserManager.codeControl(code) + ? ResponseEntity.status(HttpStatus.NO_CONTENT).body(Boolean.TRUE) + : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Boolean.FALSE); + } + + @Override + protected Class getDTOClass() { + return OperationTypeDTO.class; + } + + @Override + protected Class getModelClass() { + return OperationType.class; + } +} diff --git a/src/main/java/org/isf/patient/dto/PatientDTO.java b/src/main/java/org/isf/patient/dto/PatientDTO.java index 5e0234d64..5eb8ce913 100644 --- a/src/main/java/org/isf/patient/dto/PatientDTO.java +++ b/src/main/java/org/isf/patient/dto/PatientDTO.java @@ -2,7 +2,6 @@ import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; -import org.eclipse.jdt.core.dom.NullLiteral; import javax.validation.constraints.NotNull; import java.util.Date; diff --git a/src/main/java/org/isf/patient/rest/PatientController.java b/src/main/java/org/isf/patient/rest/PatientController.java index 429932576..dda33d095 100644 --- a/src/main/java/org/isf/patient/rest/PatientController.java +++ b/src/main/java/org/isf/patient/rest/PatientController.java @@ -6,9 +6,11 @@ import org.isf.patient.manager.PatientBrowserManager; import org.isf.patient.model.Patient; import org.isf.shared.exceptions.OHAPIException; +import org.isf.shared.rest.OHApiAbstractController; import org.isf.utils.exception.OHServiceException; import org.isf.utils.exception.model.OHExceptionMessage; import org.isf.utils.exception.model.OHSeverityLevel; +import org.modelmapper.ModelMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -16,112 +18,103 @@ import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.util.StringUtils; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.PutMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.List; -import java.util.stream.Collectors; - -import static org.isf.shared.mapper.OHModelMapper.getObjectMapper; @RestController -@Api(value="/patients",produces = MediaType.APPLICATION_JSON_VALUE, authorizations = {@Authorization(value="basicAuth")}) -public class PatientController { - - protected static final String DEFAULT_PAGE_SIZE = "80"; +@Api(value = "/patients", produces = MediaType.APPLICATION_JSON_VALUE, authorizations = {@Authorization(value = "basicAuth")}) +public class PatientController extends OHApiAbstractController { + protected static final String DEFAULT_PAGE_SIZE = "80"; - @Autowired - protected PatientBrowserManager patientManager; + @Autowired + protected PatientBrowserManager patientManager; - private final Logger logger = LoggerFactory.getLogger(PatientController.class); + private final Logger logger = LoggerFactory.getLogger(PatientController.class); - public PatientController(PatientBrowserManager patientManager) { - this.patientManager = patientManager; - } + public PatientController(PatientBrowserManager patientManager, @Autowired ModelMapper modelMapper) { + super(modelMapper); + this.patientManager = patientManager; + } /** * Create new Patient + * * @param newPatient * @return * @throws OHServiceException */ - @PostMapping(value = "/patients", produces = MediaType.APPLICATION_JSON_VALUE) + @PostMapping(value = "/patients", produces = MediaType.APPLICATION_JSON_VALUE) ResponseEntity newPatient(@RequestBody PatientDTO newPatient) throws OHServiceException { String name = StringUtils.isEmpty(newPatient.getName()) ? newPatient.getFirstName() + " " + newPatient.getSecondName() : newPatient.getName(); - logger.info("Create patient " + name); - boolean isCreated = patientManager.newPatient(getObjectMapper().map(newPatient, Patient.class)); + logger.info("Create patient " + name); + boolean isCreated = patientManager.newPatient(toModel(newPatient)); Patient patient = patientManager.getPatient(name); - if(!isCreated || patient == null){ + if (!isCreated || patient == null) { throw new OHAPIException(new OHExceptionMessage(null, "Patient is not created!", OHSeverityLevel.ERROR)); } return ResponseEntity.status(HttpStatus.CREATED).body(patient.getCode()); - } + } - @PutMapping(value = "/patients/{code}", produces = MediaType.APPLICATION_JSON_VALUE) + @PutMapping(value = "/patients/{code}", produces = MediaType.APPLICATION_JSON_VALUE) ResponseEntity updatePatient(@PathVariable int code, @RequestBody PatientDTO updatePatient) throws OHServiceException { - logger.info("Update patient code:" + code); - Patient patient = getObjectMapper().map(updatePatient, Patient.class); + logger.info("Update patient code:" + code); + Patient patient = toModel(updatePatient); patient.setCode(code); boolean isUpdated = patientManager.updatePatient(patient); - if(!isUpdated){ + if (!isUpdated) { throw new OHAPIException(new OHExceptionMessage(null, "Patient is not updated!", OHSeverityLevel.ERROR)); } return ResponseEntity.ok(patient.getCode()); - } - - @GetMapping(value = "/patients", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity> getPatients( - @RequestParam(value="page", required=false, defaultValue="0") Integer page, - @RequestParam(value="size", required=false, defaultValue=DEFAULT_PAGE_SIZE) Integer size) throws OHServiceException { - logger.info("Get patients page:" + page + " size:" + size); - ArrayList patients = patientManager.getPatient(page, size); - List patientDTOS = patients.stream().map(it -> getObjectMapper().map(it, PatientDTO.class)).collect(Collectors.toList()); - if(patientDTOS.size() == 0){ + } + + @GetMapping(value = "/patients", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity> getPatients( + @RequestParam(value = "page", required = false, defaultValue = "0") Integer page, + @RequestParam(value = "size", required = false, defaultValue = DEFAULT_PAGE_SIZE) Integer size) throws OHServiceException { + logger.info("Get patients page:" + page + " size:" + size); + ArrayList patients = patientManager.getPatient(page, size); + List patientDTOS = toDTOList(patients); + if (patientDTOS.size() == 0) { return ResponseEntity.status(HttpStatus.NO_CONTENT).body(patientDTOS); - }else{ + } else { return ResponseEntity.ok(patientDTOS); } - } - - @GetMapping(value = "/patients/{code}", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity getPatient(@PathVariable Integer code) throws OHServiceException { - logger.info("Get patient code:" + code); - Patient patient = patientManager.getPatient(code); - if (patient == null) { - return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null); - } - return ResponseEntity.ok(getObjectMapper().map(patient, PatientDTO.class)); - } - - - @GetMapping(value = "/patients/search", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity searchPatient( - @RequestParam(value="name", defaultValue="") String name, - @RequestParam(value="code", required=false) Integer code) throws OHServiceException { - logger.info("Search patient name:" + name + " code:" + code); - Patient patient = null; - if(code != null) { + } + + @GetMapping(value = "/patients/{code}", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity getPatient(@PathVariable Integer code) throws OHServiceException { + logger.info("Get patient code:" + code); + Patient patient = patientManager.getPatient(code); + if (patient == null) { + return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null); + } + return ResponseEntity.ok(toDTO(patient)); + } + + + @GetMapping(value = "/patients/search", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity searchPatient( + @RequestParam(value = "name", defaultValue = "") String name, + @RequestParam(value = "code", required = false) Integer code) throws OHServiceException { + logger.info("Search patient name:" + name + " code:" + code); + Patient patient = null; + if (code != null) { patient = patientManager.getPatient(code); - }else if (!name.equals("")) { + } else if (!name.equals("")) { patient = patientManager.getPatient(name); - } + } if (patient == null) { return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null); } - return ResponseEntity.ok(getObjectMapper().map(patient, PatientDTO.class)); - } + return ResponseEntity.ok(toDTO(patient)); + } - @DeleteMapping(value = "/patients/{code}", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity deletePatient(@PathVariable int code) throws OHServiceException { - logger.info("Delete patient code:" + code); + @DeleteMapping(value = "/patients/{code}", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity deletePatient(@PathVariable int code) throws OHServiceException { + logger.info("Delete patient code:" + code); Patient patient = patientManager.getPatient(code); boolean isDeleted = false; if (patient != null) { @@ -134,4 +127,14 @@ public ResponseEntity deletePatient(@PathVariable int code) throws OHServiceExce } return (ResponseEntity) ResponseEntity.ok(isDeleted); } + + @Override + protected Class getDTOClass() { + return PatientDTO.class; + } + + @Override + protected Class getModelClass() { + return Patient.class; + } } diff --git a/src/main/java/org/isf/patvac/dto/PatientVaccineDTO.java b/src/main/java/org/isf/patvac/dto/PatientVaccineDTO.java new file mode 100644 index 000000000..3404b52cb --- /dev/null +++ b/src/main/java/org/isf/patvac/dto/PatientVaccineDTO.java @@ -0,0 +1,68 @@ +package org.isf.patvac.dto; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import org.isf.patient.dto.PatientDTO; + +import java.util.Date; + +@ApiModel(description = "Class representing a patient vaccine") +public class PatientVaccineDTO { + + @ApiModelProperty(notes = "Code", example="", position = 1) + private int code; + + @ApiModelProperty(notes = "Progr", example="", position = 2) + private int progr; + + @ApiModelProperty(notes = "Date", example = "1979-05-01", position = 3) + private Date vaccineDate; + + @ApiModelProperty(notes = "Patient", position = 4) + private PatientDTO patient; + + // TODO + // private VaccineDTO vaccine; + + private int lock; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public int getProgr() { + return progr; + } + + public void setProgr(int progr) { + this.progr = progr; + } + + public Date getVaccineDate() { + return vaccineDate; + } + + public void setVaccineDate(Date vaccineDate) { + this.vaccineDate = vaccineDate; + } + + public PatientDTO getPatient() { + return patient; + } + + public void setPatient(PatientDTO patient) { + this.patient = patient; + } + + public int getLock() { + return lock; + } + + public void setLock(int lock) { + this.lock = lock; + } +} diff --git a/src/main/java/org/isf/patvac/rest/PatientVaccineController.java b/src/main/java/org/isf/patvac/rest/PatientVaccineController.java new file mode 100644 index 000000000..27d01ab83 --- /dev/null +++ b/src/main/java/org/isf/patvac/rest/PatientVaccineController.java @@ -0,0 +1,159 @@ +package org.isf.patvac.rest; + +import java.text.SimpleDateFormat; +import java.util.GregorianCalendar; +import java.util.List; + +import org.isf.patvac.dto.PatientVaccineDTO; +import org.isf.patvac.manager.PatVacManager; +import org.isf.patvac.model.PatientVaccine; +import org.isf.shared.rest.OHApiAbstractController; +import org.isf.utils.exception.OHServiceException; +import org.modelmapper.ModelMapper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PatchMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.Authorization; + +@RestController +@Api(value = "/patvac", produces = MediaType.APPLICATION_JSON_VALUE, authorizations = {@Authorization(value = "basicAuth")}) +public class PatientVaccineController extends OHApiAbstractController { + + @Autowired + private PatVacManager patVacManager; + + private final Logger logger = LoggerFactory.getLogger(PatientVaccineController.class); + + @Autowired + protected ModelMapper modelMapper; + + public PatientVaccineController(PatVacManager patVacManager, ModelMapper modelMapper) { + super(modelMapper); + this.patVacManager = patVacManager; + } + + /** + * returns all {@link PatientVaccine}s of today or one week ago + * + * @param minusOneWeek - if true return the last week + * @return the list of {@link PatientVaccine}s + * @throws OHServiceException + */ + @ApiOperation(value = "returns all PatientVaccines of today or one week ago") + @GetMapping(value="/patvac/latest", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity> getPatientVaccine(boolean minusOneWeek) throws OHServiceException { + logger.info(String.format("getPatientVaccine minusOneWeek [%b]", minusOneWeek)); + List patientVaccineDTOS = toDTOList(patVacManager.getPatientVaccine(minusOneWeek)); + return ResponseEntity.status(HttpStatus.FOUND).body(toDTOList(patientVaccineDTOS)); + } + + /** + * returns all {@link PatientVaccine}s within dateFrom and + * dateTo + * + * @param vaccineTypeCode + * @param vaccineCode + * @param dateFrom + * @param dateTo + * @param sex + * @param ageFrom + * @param ageTo + * @return the list of {@link PatientVaccine}s + * @throws OHServiceException + */ + @GetMapping(value="/patvac", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity> getPatientVaccine(@RequestParam String vaccineTypeCode, + @RequestParam String vaccineCode, + @RequestParam GregorianCalendar dateFrom, + @RequestParam GregorianCalendar dateTo, + @RequestParam char sex, + @RequestParam int ageFrom, + @RequestParam int ageTo) throws OHServiceException { + SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy"); + logger.info(String.format("getPatientVaccine vaccineTypeCode [%s] vaccineCode [%s] dateFrom [%s] dateTo [%s] sex [%c] ageFrom [%d] ageTo [%d]", vaccineTypeCode, vaccineCode, sdf.format(dateFrom.getTime()), sdf.format(dateTo.getTime()), sex, ageFrom, ageTo)); + List patientVaccineDTOS = toDTOList(patVacManager.getPatientVaccine(vaccineTypeCode, vaccineCode, dateFrom, dateTo, sex, ageFrom, ageTo)); + return ResponseEntity.status(HttpStatus.FOUND).body(toDTOList(patientVaccineDTOS)); + } + + /** + * inserts a {@link PatientVaccine} in the DB + * + * @param patVac - the {@link PatientVaccine} to insert + * @return true if the item has been inserted, false otherwise + * @throws OHServiceException + */ + @PostMapping(value = "/patvac", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity newPatientVaccine(@RequestBody PatientVaccineDTO patVac) throws OHServiceException { + logger.info(String.format("newPatientVaccine code [%d]", patVac.getCode())); + return patVacManager.newPatientVaccine(toModel(patVac)) + ? ResponseEntity.status(HttpStatus.CREATED).body(Boolean.TRUE) + : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Boolean.FALSE); + } + + /** + * updates a {@link PatientVaccine} + * + * @param patVac - the {@link PatientVaccine} to update + * @return true if the item has been updated, false otherwise + * @throws OHServiceException + */ + @PatchMapping(value = "/patvac", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity updatePatientVaccine(@RequestBody PatientVaccineDTO patVac) throws OHServiceException { + logger.info(String.format("updatePatientVaccine code [%d]", patVac.getCode())); + return patVacManager.updatePatientVaccine(toModel(patVac)) + ? ResponseEntity.status(HttpStatus.NO_CONTENT).body(Boolean.TRUE) + : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Boolean.FALSE); + } + + /** + * deletes a {@link PatientVaccine} + * + * @param patVac - the {@link PatientVaccine} to delete + * @return true if the item has been deleted, false otherwise + * @throws OHServiceException + */ + @DeleteMapping(value = "/patvac", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity deletePatientVaccine(@RequestBody PatientVaccineDTO patVac) throws OHServiceException { + logger.info(String.format("deletePatientVaccine code [%d]", patVac.getCode())); + return patVacManager.deletePatientVaccine(toModel(patVac)) + ? ResponseEntity.status(HttpStatus.NO_CONTENT).body(Boolean.TRUE) + : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Boolean.FALSE); + } + + /** + * Returns the max progressive number within specified year or within current year if 0. + * + * @param year + * @return int - the progressive number in the year + * @throws OHServiceException + */ + @ApiOperation(value="Returns the max progressive number within specified year or within current year if 0.") + @GetMapping(value = "/patvac/prog-year", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity getProgYear(@RequestParam int year) throws OHServiceException { + logger.info(String.format("getProgYear year [%d]", year)); + return ResponseEntity.status(HttpStatus.NO_CONTENT).body(patVacManager.getProgYear(year)); + } + + @Override + protected Class getDTOClass() { + return PatientVaccineDTO.class; + } + + @Override + protected Class getModelClass() { + return PatientVaccine.class; + } +} diff --git a/src/main/java/org/isf/pregtreattype/dto/PregnantTreatmentTypeDTO.java b/src/main/java/org/isf/pregtreattype/dto/PregnantTreatmentTypeDTO.java index 87f68b0b0..bb25cdd03 100644 --- a/src/main/java/org/isf/pregtreattype/dto/PregnantTreatmentTypeDTO.java +++ b/src/main/java/org/isf/pregtreattype/dto/PregnantTreatmentTypeDTO.java @@ -1,28 +1,34 @@ package org.isf.pregtreattype.dto; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + import javax.validation.constraints.NotNull; +@ApiModel(description = "Class representing pregnant treatment type") public class PregnantTreatmentTypeDTO { - private String code; + @ApiModelProperty(notes = "Code", example = "", position = 1) + private String code; - @NotNull - private String description; + @NotNull + @ApiModelProperty(notes = "Description", example = "", position = 2) + private String description; - public String getCode() { - return code; - } + public String getCode() { + return code; + } - public void setCode(String code) { - this.code = code; - } + public void setCode(String code) { + this.code = code; + } - public String getDescription() { - return description; - } + public String getDescription() { + return description; + } - public void setDescription(String description) { - this.description = description; - } + public void setDescription(String description) { + this.description = description; + } } diff --git a/src/main/java/org/isf/pregtreattype/rest/PregnantTreatmentTypeController.java b/src/main/java/org/isf/pregtreattype/rest/PregnantTreatmentTypeController.java new file mode 100644 index 000000000..6d5f818c8 --- /dev/null +++ b/src/main/java/org/isf/pregtreattype/rest/PregnantTreatmentTypeController.java @@ -0,0 +1,126 @@ +package org.isf.pregtreattype.rest; + +import java.util.List; + +import org.isf.pregtreattype.dto.PregnantTreatmentTypeDTO; +import org.isf.pregtreattype.manager.PregnantTreatmentTypeBrowserManager; +import org.isf.pregtreattype.model.PregnantTreatmentType; +import org.isf.shared.rest.OHApiAbstractController; +import org.isf.utils.exception.OHServiceException; +import org.modelmapper.ModelMapper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PatchMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +import io.swagger.annotations.Api; +import io.swagger.annotations.Authorization; + +@RestController +@Api(value = "/pregtreattype", produces = MediaType.APPLICATION_JSON_VALUE, authorizations = {@Authorization(value = "basicAuth")}) +public class PregnantTreatmentTypeController extends OHApiAbstractController { + + @Autowired + private PregnantTreatmentTypeBrowserManager manager; + + private final Logger logger = LoggerFactory.getLogger(PregnantTreatmentTypeController.class); + + @Autowired + protected ModelMapper modelMapper; + + public PregnantTreatmentTypeController(PregnantTreatmentTypeBrowserManager manager, ModelMapper modelMapper) { + super(modelMapper); + this.manager = manager; + } + + /** + * return the list of {@link PregnantTreatmentType}s + * + * @return the list of {@link PregnantTreatmentType}s + * @throws OHServiceException + */ + @GetMapping(value = "/pregtreattype", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity> getPregnantTreatmentType() throws OHServiceException { + logger.info(String.format("getPregnantTreatmentType")); + return ResponseEntity.status(HttpStatus.FOUND).body(toDTOList(manager.getPregnantTreatmentType())); + } + + /** + * insert a {@link PregnantTreatmentType} in the DB + * + * @param pregnantTreatmentType - the {@link PregnantTreatmentType} to insert + * @return true if the item has been inserted, false otherwise + * @throws OHServiceException + */ + @PostMapping(value = "/pregtreattype", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity newPregnantTreatmentType(@RequestBody PregnantTreatmentTypeDTO pregnantTreatmentType) throws OHServiceException { + logger.info(String.format("newPregnantTreatmentType code [%s]"), pregnantTreatmentType.getCode()); + return manager.newPregnantTreatmentType(toModel(pregnantTreatmentType)) + ? ResponseEntity.status(HttpStatus.CREATED).body(Boolean.TRUE) + : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Boolean.FALSE); + } + + /** + * update a {@link PregnantTreatmentType} in the DB + * + * @param pregnantTreatmentType - the {@link PregnantTreatmentType} to update + * @return true if the item has been updated, false otherwise + * @throws OHServiceException + */ + @PatchMapping(value = "/pregtreattype", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity updatePregnantTreatmentType(@RequestBody PregnantTreatmentTypeDTO pregnantTreatmentType) throws OHServiceException { + logger.info(String.format("updatePregnantTreatmentType code [%s]"), pregnantTreatmentType.getCode()); + return manager.updatePregnantTreatmentType(toModel(pregnantTreatmentType)) + ? ResponseEntity.status(HttpStatus.NO_CONTENT).body(Boolean.TRUE) + : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Boolean.FALSE); + } + + /** + * delete a {@link PregnantTreatmentType} in the DB + * + * @param pregnantTreatmentType - the {@link PregnantTreatmentType} to delete + * @return true if the item has been deleted, false otherwise + * @throws OHServiceException + */ + @DeleteMapping(value = "/pregtreattype", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity deletePregnantTreatmentType(@RequestBody PregnantTreatmentTypeDTO pregnantTreatmentType) throws OHServiceException { + logger.info(String.format("deletePregnantTreatmentType code [%s]"), pregnantTreatmentType.getCode()); + return manager.deletePregnantTreatmentType(toModel(pregnantTreatmentType)) + ? ResponseEntity.status(HttpStatus.NO_CONTENT).body(Boolean.TRUE) + : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Boolean.FALSE); + } + + /** + * check if the code is already in use + * + * @param code - the code + * @return true if the code is already in use, false otherwise + * @throws OHServiceException + */ + @GetMapping(value = "/pregtreattype/check/{code}", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity codeControl(@PathVariable String code) throws OHServiceException { + logger.info(String.format("codeControl code [%s]"), code); + return manager.codeControl(code) + ? ResponseEntity.status(HttpStatus.NO_CONTENT).body(Boolean.TRUE) + : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Boolean.FALSE); + } + + @Override + protected Class getDTOClass() { + return PregnantTreatmentTypeDTO.class; + } + + @Override + protected Class getModelClass() { + return PregnantTreatmentType.class; + } +} diff --git a/src/main/java/org/isf/pricelist/dto/ListDTO.java b/src/main/java/org/isf/pricelist/dto/ListDTO.java new file mode 100644 index 000000000..2e3c67f56 --- /dev/null +++ b/src/main/java/org/isf/pricelist/dto/ListDTO.java @@ -0,0 +1,65 @@ +package org.isf.pricelist.dto; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +@ApiModel(description = "Class representing list") +public class ListDTO { + + @ApiModelProperty(notes = "Id", example = "", position = 1) + private int id; + + @ApiModelProperty(notes = "Code", example = "", position = 2) + private String code; + + @ApiModelProperty(notes = "Name", example = "", position = 3) + private String name; + + @ApiModelProperty(notes = "Description", example = "", position = 4) + private String description; + + @ApiModelProperty(notes = "Currency", example = "", position = 5) + private String currency; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getCurrency() { + return currency; + } + + public void setCurrency(String currency) { + this.currency = currency; + } + + +} diff --git a/src/main/java/org/isf/pricelist/dto/PriceDTO.java b/src/main/java/org/isf/pricelist/dto/PriceDTO.java new file mode 100644 index 000000000..f1415829d --- /dev/null +++ b/src/main/java/org/isf/pricelist/dto/PriceDTO.java @@ -0,0 +1,92 @@ +package org.isf.pricelist.dto; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import org.isf.priceslist.model.PriceList; + +@ApiModel(description = "Class representing price") +public class PriceDTO { + + @ApiModelProperty(notes = "Id", example = "", position = 1) + private int id; + + @ApiModelProperty(notes = "PriceList", example = "", position = 2) + private PriceList list; + + @ApiModelProperty(notes = "Group", example = "", position = 3) + private String group; + + @ApiModelProperty(notes = "Item", example = "", position = 4) + private String item; + + @ApiModelProperty(notes = "Description", example = "", position = 5) + private String description; + + @ApiModelProperty(notes = "Price", example = "", position = 6) + private Double price; + + @ApiModelProperty(notes = "Editable", example = "", position = 7) + private boolean editable; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public PriceList getList() { + return list; + } + + public void setList(PriceList list) { + this.list = list; + } + + public String getGroup() { + return group; + } + + public void setGroup(String group) { + this.group = group; + } + + public String getItem() { + return item; + } + + public void setItem(String item) { + this.item = item; + } + + public String getDesc() { + return description; + } + + public void setDesc(String desc) { + this.description = desc; + } + + public Double getPrice() { + return price; + } + + public void setPrice(Double price) { + this.price = price; + } + + public boolean isPrice() { + return item.compareTo("") != 0; + } + + public boolean isEditable() { + return editable; + } + + public void setEditable(boolean editable) { + this.editable = editable; + } + + +} diff --git a/src/main/java/org/isf/pricelist/dto/PriceListDTO.java b/src/main/java/org/isf/pricelist/dto/PriceListDTO.java new file mode 100644 index 000000000..0ee0d1ab3 --- /dev/null +++ b/src/main/java/org/isf/pricelist/dto/PriceListDTO.java @@ -0,0 +1,63 @@ +package org.isf.pricelist.dto; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +@ApiModel(description = "Class representing price list") +public class PriceListDTO { + + @ApiModelProperty(notes = "Id", example = "", position = 1) + private int id; + + @ApiModelProperty(notes = "Code", example = "", position = 2) + private String code; + + @ApiModelProperty(notes = "Notes", example = "", position = 3) + private String name; + + @ApiModelProperty(notes = "Description", example = "", position = 4) + private String description; + + @ApiModelProperty(notes = "Currency", example = "", position = 5) + private String currency; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getCurrency() { + return currency; + } + + public void setCurrency(String currency) { + this.currency = currency; + } +} diff --git a/src/main/java/org/isf/pricelist/rest/PriceListController.java b/src/main/java/org/isf/pricelist/rest/PriceListController.java new file mode 100644 index 000000000..94c51a252 --- /dev/null +++ b/src/main/java/org/isf/pricelist/rest/PriceListController.java @@ -0,0 +1,189 @@ +package org.isf.pricelist.rest; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.stream.Collectors; + +import org.isf.pricelist.dto.PriceDTO; +import org.isf.pricelist.dto.PriceListDTO; +import org.isf.priceslist.manager.PriceListManager; +import org.isf.priceslist.model.Price; +import org.isf.priceslist.model.PriceList; +import org.isf.shared.rest.OHApiAbstractController; +import org.isf.utils.exception.OHServiceException; +import org.isf.utils.exception.model.OHExceptionMessage; +import org.isf.utils.exception.model.OHSeverityLevel; +import org.modelmapper.ModelMapper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PatchMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import io.swagger.annotations.Api; +import io.swagger.annotations.Authorization; + +@RestController +@Api(value = "/pricelist", produces = MediaType.APPLICATION_JSON_VALUE, authorizations = {@Authorization(value = "basicAuth")}) +public class PriceListController extends OHApiAbstractController { + + @Autowired + private PriceListManager manager; + + private final Logger logger = LoggerFactory.getLogger(PriceListController.class); + + @Autowired + protected ModelMapper modelMapper; + + public PriceListController(PriceListManager manager, ModelMapper modelMapper) { + super(modelMapper); + this.manager = manager; + } + + /** + * return the list of {@link List}s in the DB + * @return the list of {@link List}s + * @throws OHServiceException + */ + @GetMapping (value = "/pricelist", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity> getLists() throws OHServiceException { + logger.info(String.format("getLists")); + return ResponseEntity.ok().body(toDTOList(manager.getLists())); + } + + /** + * return the list of {@link Price}s in the DB + * @return the list of {@link Price}s + * @throws OHServiceException + */ + @GetMapping(value = "/pricelist/price", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity> getPrices() throws OHServiceException { + logger.info(String.format("getPrices")); + return ResponseEntity.ok().body(manager.getPrices().stream().map(it -> modelMapper.map(it, PriceDTO.class)).collect(Collectors.toList())); + } + + /** + * updates all {@link Price}s in the specified {@link List} + * @param //priceListDTO - the {@link List} + * @param //priceDTOList - the list of {@link Price}s + * @return true if the list has been replaced, false otherwise + * @throws OHServiceException + */ + @PatchMapping(value = "/pricelist/{priceListCode}/price", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity updatePrices(@PathVariable String priceListCode, @RequestBody List priceDTOList) throws OHServiceException { + logger.info(String.format("updatePrices priceListCode [%s] priceDTOList size [%d]"), priceListCode, priceDTOList.size()); + List prices = priceDTOList.stream().map(it -> modelMapper.map(it, Price.class)).collect(Collectors.toList()); + return manager.updatePrices(getListById(priceListCode), new ArrayList(prices)) + ? ResponseEntity.status(HttpStatus.NO_CONTENT).body(Boolean.TRUE) + : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Boolean.FALSE); + } + + + /** + * Get a {@link PriceList} for the code provided + * TODO: move this logic to the manager in the core library + * + * @param priceListCode - the code to identify the {@link PriceList} + * @return the {@link PriceList} + * @throws OHServiceException + */ + private PriceList getListById(String priceListCode) throws OHServiceException { + logger.info(String.format("getListById priceListCode [%s]"), priceListCode); + ArrayList priceLists = manager.getLists(); + for (Iterator priceListIterator = priceLists.iterator(); priceListIterator.hasNext(); ) { + PriceList priceList = priceListIterator.next(); + if (priceList.getCode().equals(priceListCode)) { + return priceList; + } + } + throw new OHServiceException(new OHExceptionMessage(")\"Price list not found\"", ")\"Price list not found for the code provided\"", OHSeverityLevel.ERROR)); + } + + /** + * insert a new {@link List} in the DB + * + * @param priceListDTO - the {@link List} + * @return true if the list has been inserted, false otherwise + * @throws OHServiceException + */ + @PostMapping(value = "/pricelist", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity newList(@RequestBody PriceListDTO priceListDTO) throws OHServiceException { + logger.info(String.format("newList code [%s]"), priceListDTO.getCode()); + return manager.newList(toModel(priceListDTO)) + ? ResponseEntity.status(HttpStatus.CREATED).body(Boolean.TRUE) + : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Boolean.FALSE); + } + + /** + * update a {@link List} in the DB + * + * @param priceListDTO - the {@link List} to update + * @return true if the list has been updated, false otherwise + * @throws OHServiceException + */ + @PatchMapping(value = "/pricelist", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity updateList(@RequestBody PriceListDTO priceListDTO) throws OHServiceException { + logger.info(String.format("updateList code [%s]"), priceListDTO.getCode()); + return manager.updateList(toModel(priceListDTO)) + ? ResponseEntity.status(HttpStatus.NO_CONTENT).body(Boolean.TRUE) + : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Boolean.FALSE); + } + + /** + * delete a {@link List} in the DB + * + * @param priceListDTO - the {@link List} to delete + * @return true if the list has been deleted, false otherwise + * @throws OHServiceException + */ + @DeleteMapping(value = "/pricelist", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity deleteList(@RequestBody PriceListDTO priceListDTO) throws OHServiceException { + logger.info(String.format("deleteList code [%s]"), priceListDTO.getCode()); + return manager.deleteList(toModel(priceListDTO)) + ? ResponseEntity.status(HttpStatus.NO_CONTENT).body(Boolean.TRUE) + : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Boolean.FALSE); + } + + /** + * duplicate {@link //list} multiplying by factor and rounding by step + * + * @param priceListDTO - the {@link //list} to be duplicated + * @param factor - the multiplying factor + * @param step - the rounding step + * @return true if the list has been duplicated, false otherwise + * @throws OHServiceException + */ + @PostMapping(value = "/pricelist/copy/{factor}/{step}", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity copyList(@RequestBody PriceListDTO priceListDTO, @RequestParam(value = "factor", defaultValue = "1.") double factor, @RequestParam(value = "factor", defaultValue = "0.") double step) throws OHServiceException { + logger.info(String.format("copyList code [%s] factor[%.,2f] step[%.,2f]"), priceListDTO.getCode(), factor, step); + return manager.copyList(toModel(priceListDTO), factor, step) + ? ResponseEntity.status(HttpStatus.NO_CONTENT).body(Boolean.TRUE) + : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Boolean.FALSE); + } + + // TODO: is it useful in a web context? + // @PostMapping(value = "/pricelist/convert") + // public List convertPrice(@RequestBody Map> pricesJson) throws OHServiceException { + // return null; + // } + + @Override + protected Class getDTOClass() { + return PriceListDTO.class; + } + + @Override + protected Class getModelClass() { + return PriceList.class; + } +} \ No newline at end of file diff --git a/src/main/java/org/isf/priceothers/dto/PricesOthersDTO.java b/src/main/java/org/isf/priceothers/dto/PricesOthersDTO.java new file mode 100644 index 000000000..80165bc62 --- /dev/null +++ b/src/main/java/org/isf/priceothers/dto/PricesOthersDTO.java @@ -0,0 +1,96 @@ +package org.isf.priceothers.dto; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +@ApiModel(description = "Class representing a price others") +public class PricesOthersDTO { + + @ApiModelProperty(notes = "Id", example="", position = 1) + private int id; + + @ApiModelProperty(notes = "Code", example="", position = 2) + private String code; + + @ApiModelProperty(notes = "Description", example="", position = 3) + private String description; + + @ApiModelProperty(notes = "OpdInclude", example="", position = 4) + private boolean opdInclude; + + @ApiModelProperty(notes = "IpdInclude", example="", position = 5) + private boolean ipdInclude; + + @ApiModelProperty(notes = "Daily", example="", position = 6) + private boolean daily; + + @ApiModelProperty(notes = "Discharge", example="", position = 7) + private boolean discharge; + + @ApiModelProperty(notes = "Undefined", example="", position = 8) + private boolean undefined; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public boolean isOpdInclude() { + return opdInclude; + } + + public void setOpdInclude(boolean opdInclude) { + this.opdInclude = opdInclude; + } + + public boolean isIpdInclude() { + return ipdInclude; + } + + public void setIpdInclude(boolean ipdInclude) { + this.ipdInclude = ipdInclude; + } + + public boolean isDaily() { + return daily; + } + + public void setDaily(boolean daily) { + this.daily = daily; + } + + public boolean isDischarge() { + return discharge; + } + + public void setDischarge(boolean discharge) { + this.discharge = discharge; + } + + public boolean isUndefined() { + return undefined; + } + + public void setUndefined(boolean undefined) { + this.undefined = undefined; + } +} \ No newline at end of file diff --git a/src/main/java/org/isf/priceothers/rest/PricesOthersController.java b/src/main/java/org/isf/priceothers/rest/PricesOthersController.java new file mode 100644 index 000000000..265fb14d7 --- /dev/null +++ b/src/main/java/org/isf/priceothers/rest/PricesOthersController.java @@ -0,0 +1,108 @@ +package org.isf.priceothers.rest; + +import java.util.List; + +import org.isf.priceothers.dto.PricesOthersDTO; +import org.isf.pricesothers.manager.PricesOthersManager; +import org.isf.pricesothers.model.PricesOthers; +import org.isf.shared.rest.OHApiAbstractController; +import org.isf.utils.exception.OHServiceException; +import org.modelmapper.ModelMapper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RestController; + +import io.swagger.annotations.Api; +import io.swagger.annotations.Authorization; + +@RestController +@Api(value = "/priceothers", produces = MediaType.APPLICATION_JSON_VALUE, authorizations = {@Authorization(value = "basicAuth")}) +public class PricesOthersController extends OHApiAbstractController { + + @Autowired + private PricesOthersManager manager; + + private final Logger logger = LoggerFactory.getLogger(PricesOthersController.class); + + @Autowired + protected ModelMapper modelMapper; + + public PricesOthersController(PricesOthersManager manager, ModelMapper modelMapper) { + super(modelMapper); + this.manager = manager; + } + + /** + * return the list of {@link PricesOthersDTO}s in the DB + * + * @return the list of {@link PricesOthersDTO}s + * @throws OHServiceException + */ + @GetMapping(value = "/priceothers", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity> getOthers() throws OHServiceException { + logger.info(String.format("getOthers")); + return ResponseEntity.ok().body(toDTOList(manager.getOthers())); + } + + /** + * insert a new {@link PricesOthersDTO} in the DB + * + * @param pricesOthersDTO - the {@link PricesOthersDTO} to insert + * @return true if the list has been inserted, false otherwise + * @throws OHServiceException + */ + @PostMapping(value = "/priceothers", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity newOther(PricesOthersDTO pricesOthersDTO) throws OHServiceException { + logger.info(String.format("newOther [%s]", pricesOthersDTO.getCode())); + // TODO: to better follow REST conventions we need an URI to use as Location header value on created. Check: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/ResponseEntity.html + return manager.newOther(toModel(pricesOthersDTO)) + ? ResponseEntity.status(HttpStatus.CREATED).body(Boolean.TRUE) + : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Boolean.FALSE); + } + + /** + * delete a {@link PricesOthersDTO} in the DB + * + * @param pricesOthersDTO - the {@link PricesOthersDTO} to delete + * @return true if the list has been deleted, false otherwise + * @throws OHServiceException + */ + @DeleteMapping(value = "/priceothers", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity deleteOther(PricesOthersDTO pricesOthersDTO) throws OHServiceException { + logger.info(String.format("deleteOther [%s]", pricesOthersDTO.getCode())); + return manager.deleteOther(toModel(pricesOthersDTO)) + ? ResponseEntity.status(HttpStatus.NO_CONTENT).body(Boolean.TRUE) + : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Boolean.FALSE); + } + + /** + * update a {@link PricesOthersDTO} in the DB + * + * @param pricesOthersDTO - the {@link PricesOthersDTO} to update + * @return true if the list has been updated, false otherwise + * @throws OHServiceException + */ + public ResponseEntity updateOther(PricesOthersDTO pricesOthersDTO) throws OHServiceException { + logger.info(String.format("updateOther [%s]", pricesOthersDTO.getCode())); + return manager.updateOther(toModel(pricesOthersDTO)) + ? ResponseEntity.status(HttpStatus.NO_CONTENT).body(Boolean.TRUE) + : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Boolean.FALSE); + } + + @Override + protected Class getDTOClass() { + return PricesOthersDTO.class; + } + + @Override + protected Class getModelClass() { + return PricesOthers.class; + } +} \ No newline at end of file diff --git a/src/main/java/org/isf/shared/mapper/OHModelMapper.java b/src/main/java/org/isf/shared/mapper/OHModelMapper.java deleted file mode 100644 index e0d0e7078..000000000 --- a/src/main/java/org/isf/shared/mapper/OHModelMapper.java +++ /dev/null @@ -1,23 +0,0 @@ -package org.isf.shared.mapper; - -import org.isf.shared.mapper.converter.BlobToByteArrayConverter; -import org.isf.shared.mapper.converter.ByteArrayToBlobConverter; -import org.modelmapper.ModelMapper; - -/** - * @author akashytsa - */ -public class OHModelMapper { - - private static ModelMapper modelMapper; - - private static ModelMapper getInstance() { - modelMapper = new ModelMapper(); - modelMapper.addConverter(new BlobToByteArrayConverter()); - modelMapper.addConverter(new ByteArrayToBlobConverter()); - return modelMapper; - } - public static ModelMapper getObjectMapper() { - return modelMapper == null ? getInstance() : modelMapper; - } -} \ No newline at end of file diff --git a/src/main/java/org/isf/shared/mapper/converter/BlobToByteArrayConverter.java b/src/main/java/org/isf/shared/mapper/converter/BlobToByteArrayConverter.java index 7f8037b52..bdf018eed 100644 --- a/src/main/java/org/isf/shared/mapper/converter/BlobToByteArrayConverter.java +++ b/src/main/java/org/isf/shared/mapper/converter/BlobToByteArrayConverter.java @@ -3,6 +3,7 @@ import org.modelmapper.AbstractConverter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; import java.sql.Blob; import java.sql.SQLException; @@ -11,6 +12,7 @@ * @author akashytsa * */ +@Component public class BlobToByteArrayConverter extends AbstractConverter { private final Logger logger = LoggerFactory.getLogger(BlobToByteArrayConverter.class); diff --git a/src/main/java/org/isf/shared/mapper/converter/ByteArrayToBlobConverter.java b/src/main/java/org/isf/shared/mapper/converter/ByteArrayToBlobConverter.java index 2f31eed1d..e6d40e648 100644 --- a/src/main/java/org/isf/shared/mapper/converter/ByteArrayToBlobConverter.java +++ b/src/main/java/org/isf/shared/mapper/converter/ByteArrayToBlobConverter.java @@ -3,6 +3,7 @@ import org.modelmapper.AbstractConverter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; import javax.sql.rowset.serial.SerialBlob; import java.sql.Blob; @@ -12,6 +13,7 @@ * @author akashytsa * */ +@Component public class ByteArrayToBlobConverter extends AbstractConverter { private final Logger logger = LoggerFactory.getLogger(ByteArrayToBlobConverter.class); diff --git a/src/main/java/org/isf/shared/mapper/converter/ModelMapperConfig.java b/src/main/java/org/isf/shared/mapper/converter/ModelMapperConfig.java new file mode 100644 index 000000000..04e31cb15 --- /dev/null +++ b/src/main/java/org/isf/shared/mapper/converter/ModelMapperConfig.java @@ -0,0 +1,25 @@ +package org.isf.shared.mapper.converter; + +import org.modelmapper.ModelMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class ModelMapperConfig { + + @Autowired + protected BlobToByteArrayConverter blobToByteArrayConverter; + + @Autowired + protected ByteArrayToBlobConverter byteArrayToBlobConverter; + + @Bean + public ModelMapper modelMapper() { + ModelMapper modelMapper = new ModelMapper(); + modelMapper.addConverter(blobToByteArrayConverter); + modelMapper.addConverter(byteArrayToBlobConverter); + return modelMapper; + } + +} diff --git a/src/main/java/org/isf/shared/responsebodyadvice/DTO.java b/src/main/java/org/isf/shared/responsebodyadvice/DTO.java deleted file mode 100644 index b991f5677..000000000 --- a/src/main/java/org/isf/shared/responsebodyadvice/DTO.java +++ /dev/null @@ -1,14 +0,0 @@ -package org.isf.shared.responsebodyadvice; - - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; -@Deprecated -@Target({ElementType.METHOD}) -@Retention(RetentionPolicy.RUNTIME) -public @interface DTO { - Class value(); -} - diff --git a/src/main/java/org/isf/shared/responsebodyadvice/DtoMapperResponseBodyAdvice.java b/src/main/java/org/isf/shared/responsebodyadvice/DtoMapperResponseBodyAdvice.java deleted file mode 100644 index d310c35a4..000000000 --- a/src/main/java/org/isf/shared/responsebodyadvice/DtoMapperResponseBodyAdvice.java +++ /dev/null @@ -1,52 +0,0 @@ -//package org.isf.shared.responsebodyadvice; -// -//import java.util.Collection; -// -//import org.modelmapper.ModelMapper; -//import org.springframework.beans.factory.annotation.Autowired; -//import org.springframework.core.MethodParameter; -//import org.springframework.data.domain.Page; -//import org.springframework.http.MediaType; -//import org.springframework.http.converter.HttpMessageConverter; -//import org.springframework.http.converter.json.MappingJacksonValue; -//import org.springframework.http.server.ServerHttpRequest; -//import org.springframework.http.server.ServerHttpResponse; -//import org.springframework.util.Assert; -//import org.springframework.web.bind.annotation.ControllerAdvice; -//import org.springframework.web.servlet.mvc.method.annotation.AbstractMappingJacksonResponseBodyAdvice; -//@Deprecated -//@ControllerAdvice -//public class DtoMapperResponseBodyAdvice extends AbstractMappingJacksonResponseBodyAdvice { -// -// //@Autowired -// //private ModelMapper modelMapper; -// private static final ModelMapper modelMapper = new ModelMapper(); -// -// @Override -// protected void beforeBodyWriteInternal(MappingJacksonValue bodyContainer, MediaType contentType, -// MethodParameter returnType, ServerHttpRequest request, ServerHttpResponse response) { -// // TODO Auto-generated method stub -// DTO ann = returnType.getMethodAnnotation(DTO.class); -// Assert.state(ann != null, "No Dto annotation"); -// -// Class dtoType = ann.value(); -// Object value = bodyContainer.getValue(); -// Object returnValue; -// -// if (value instanceof Page) { -// returnValue = ((Page) value).map(it -> modelMapper.map(it, dtoType)); -// } else if (value instanceof Collection) { -// returnValue = ((Collection) value).stream().map(it -> modelMapper.map(it, dtoType)); -// } else { -// returnValue = modelMapper.map(value, dtoType); -// } -// bodyContainer.setValue(returnValue); -// } -// -// @Override -// public boolean supports(MethodParameter returnType, Class> converterType) { -// // TODO Auto-generated method stub -// return super.supports(returnType, converterType) && returnType.hasMethodAnnotation(DTO.class); -// } -// -//} diff --git a/src/main/java/org/isf/shared/rest/OHApiAbstractController.java b/src/main/java/org/isf/shared/rest/OHApiAbstractController.java new file mode 100644 index 000000000..cc9c63e25 --- /dev/null +++ b/src/main/java/org/isf/shared/rest/OHApiAbstractController.java @@ -0,0 +1,32 @@ +package org.isf.shared.rest; + +import java.util.List; +import java.util.stream.Collectors; + +import org.modelmapper.ModelMapper; + +public abstract class OHApiAbstractController { + + protected ModelMapper modelMapper; + + protected OHApiAbstractController(ModelMapper modelMapper){ + this.modelMapper = modelMapper; + } + + protected T toDTO(S model) { + return modelMapper.map(model, getDTOClass()); + } + + protected S toModel(T dto) { + return modelMapper.map(dto, getModelClass()); + } + + protected List toDTOList(List list) { + return (List) list.stream().map(it -> modelMapper.map(it, getDTOClass())).collect(Collectors.toList()); + } + + abstract protected Class getDTOClass(); + + abstract protected Class getModelClass(); + +} diff --git a/src/main/java/org/isf/vaccine/dto/VaccineDTO.java b/src/main/java/org/isf/vaccine/dto/VaccineDTO.java index cce6abd22..c65d3d422 100644 --- a/src/main/java/org/isf/vaccine/dto/VaccineDTO.java +++ b/src/main/java/org/isf/vaccine/dto/VaccineDTO.java @@ -4,12 +4,11 @@ import io.swagger.annotations.ApiModelProperty; import org.isf.vactype.dto.VaccineTypeDTO; import org.isf.vactype.model.VaccineType; +import org.modelmapper.ModelMapper; +import org.springframework.beans.factory.annotation.Autowired; -import javax.persistence.*; import javax.validation.constraints.NotNull; -import static org.isf.shared.mapper.OHModelMapper.getObjectMapper; - @ApiModel(description = "Class representing a vaccine") public class VaccineDTO { @@ -25,6 +24,9 @@ public class VaccineDTO { @ApiModelProperty(notes = "Type of the vaccine", position = 3) private VaccineTypeDTO vaccineType; + @Autowired + protected ModelMapper modelMapper; + public String getCode() { return code; } @@ -60,7 +62,7 @@ public void setVaccineType(VaccineTypeDTO vaccineType) { } public VaccineType getVaccineTypeFromDTO(VaccineTypeDTO vaccineTypeDTO){ - return getObjectMapper().map(vaccineTypeDTO, VaccineType.class); + return modelMapper.map(vaccineTypeDTO, VaccineType.class); } public VaccineDTO vaccineType(VaccineTypeDTO vaccineType) { diff --git a/src/main/java/org/isf/vaccine/rest/VaccineController.java b/src/main/java/org/isf/vaccine/rest/VaccineController.java index 7576c8852..bbb4bbf16 100644 --- a/src/main/java/org/isf/vaccine/rest/VaccineController.java +++ b/src/main/java/org/isf/vaccine/rest/VaccineController.java @@ -10,6 +10,7 @@ import org.isf.vaccine.dto.VaccineDTO; import org.isf.vaccine.manager.VaccineBrowserManager; import org.isf.vaccine.model.Vaccine; +import org.modelmapper.ModelMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -22,8 +23,6 @@ import java.util.List; import java.util.stream.Collectors; -import static org.isf.shared.mapper.OHModelMapper.getObjectMapper; - @RestController @Api(value = "/vaccines", produces = MediaType.APPLICATION_JSON_VALUE, authorizations = {@Authorization(value = "basicAuth")}) public class VaccineController { @@ -33,6 +32,9 @@ public class VaccineController { @Autowired protected VaccineBrowserManager vaccineManager; + @Autowired + protected ModelMapper modelMapper; + public VaccineController(VaccineBrowserManager vaccineManager) { this.vaccineManager = vaccineManager; } @@ -47,7 +49,7 @@ public VaccineController(VaccineBrowserManager vaccineManager) { public ResponseEntity> getVaccines() throws OHServiceException { logger.info("Get vaccines"); ArrayList vaccines = vaccineManager.getVaccine(); - List listVaccines = vaccines.stream().map(it -> getObjectMapper().map(it, VaccineDTO.class)).collect(Collectors.toList()); + List listVaccines = vaccines.stream().map(it -> modelMapper.map(it, VaccineDTO.class)).collect(Collectors.toList()); if (listVaccines.size() == 0) { return ResponseEntity.status(HttpStatus.NO_CONTENT).body(listVaccines); } else { @@ -66,7 +68,7 @@ public ResponseEntity> getVaccines() throws OHServiceException public ResponseEntity> getVaccinesByVaccineTypeCode(@PathVariable String vaccineTypeCode) throws OHServiceException { logger.info("Get vaccine by code:" + vaccineTypeCode); ArrayList vaccines = vaccineManager.getVaccine(vaccineTypeCode); - List listVaccines = vaccines.stream().map(it -> getObjectMapper().map(it, VaccineDTO.class)).collect(Collectors.toList()); + List listVaccines = vaccines.stream().map(it -> modelMapper.map(it, VaccineDTO.class)).collect(Collectors.toList()); if (listVaccines.size() == 0) { return ResponseEntity.status(HttpStatus.NO_CONTENT).body(listVaccines); } else { @@ -86,7 +88,7 @@ public ResponseEntity newVaccine(@RequestBody VaccineDTO newVaccine) throws OHSe logger.info("Create vaccine: " + newVaccine.toString()); boolean isCreated; try { - isCreated = vaccineManager.newVaccine(getObjectMapper().map(newVaccine, Vaccine.class)); + isCreated = vaccineManager.newVaccine(modelMapper.map(newVaccine, Vaccine.class)); } catch (OHDataIntegrityViolationException e) { throw new OHAPIException(new OHExceptionMessage(null, "Vaccine type already present!", OHSeverityLevel.ERROR)); } @@ -106,7 +108,7 @@ public ResponseEntity newVaccine(@RequestBody VaccineDTO newVaccine) throws OHSe @PutMapping(value = "/vaccines/", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity updateVaccine(@RequestBody VaccineDTO updateVaccine) throws OHServiceException { logger.info("Update vaccine: " + updateVaccine.toString()); - boolean isUpdated = vaccineManager.updateVaccine(getObjectMapper().map(updateVaccine, Vaccine.class)); + boolean isUpdated = vaccineManager.updateVaccine(modelMapper.map(updateVaccine, Vaccine.class)); if (!isUpdated) { throw new OHAPIException(new OHExceptionMessage(null, "Vaccine is not updated!", OHSeverityLevel.ERROR)); } @@ -124,7 +126,7 @@ public ResponseEntity updateVaccine(@RequestBody VaccineDTO updateVaccine) throw @DeleteMapping(value = "/vaccines/{code}", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity deleteVaccine(@RequestBody VaccineDTO vaccineToDelete) throws OHServiceException { logger.info("Delete vaccine: " + vaccineToDelete.toString()); - boolean isDeleted = vaccineManager.deleteVaccine(getObjectMapper().map(vaccineToDelete, Vaccine.class)); + boolean isDeleted = vaccineManager.deleteVaccine(modelMapper.map(vaccineToDelete, Vaccine.class)); if (!isDeleted) { throw new OHAPIException(new OHExceptionMessage(null, "Vaccine is not deleted!", OHSeverityLevel.ERROR)); } diff --git a/src/main/java/org/isf/vactype/rest/VaccineTypeController.java b/src/main/java/org/isf/vactype/rest/VaccineTypeController.java index d0e855c00..49ada6f83 100644 --- a/src/main/java/org/isf/vactype/rest/VaccineTypeController.java +++ b/src/main/java/org/isf/vactype/rest/VaccineTypeController.java @@ -10,6 +10,7 @@ import org.isf.vactype.dto.VaccineTypeDTO; import org.isf.vactype.manager.VaccineTypeBrowserManager; import org.isf.vactype.model.VaccineType; +import org.modelmapper.ModelMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -22,8 +23,6 @@ import java.util.List; import java.util.stream.Collectors; -import static org.isf.shared.mapper.OHModelMapper.getObjectMapper; - @RestController @Api(value = "/vaccinetype", produces = MediaType.APPLICATION_JSON_VALUE, authorizations = {@Authorization(value = "basicAuth")}) public class VaccineTypeController { @@ -33,6 +32,9 @@ public class VaccineTypeController { @Autowired protected VaccineTypeBrowserManager vaccineTypeManager; + @Autowired + protected ModelMapper modelMapper; + public VaccineTypeController(VaccineTypeBrowserManager vaccineTypeManager) { this.vaccineTypeManager = vaccineTypeManager; } @@ -47,7 +49,7 @@ public VaccineTypeController(VaccineTypeBrowserManager vaccineTypeManager) { public ResponseEntity> getVaccineType() throws OHServiceException { logger.info("Get vaccines type"); ArrayList vaccinesTypes = vaccineTypeManager.getVaccineType(); - List listVaccines = vaccinesTypes.stream().map(it -> getObjectMapper().map(it, VaccineTypeDTO.class)).collect(Collectors.toList()); + List listVaccines = vaccinesTypes.stream().map(it -> modelMapper.map(it, VaccineTypeDTO.class)).collect(Collectors.toList()); if (listVaccines.size() == 0) { return ResponseEntity.status(HttpStatus.NO_CONTENT).body(listVaccines); } else { @@ -67,7 +69,7 @@ public ResponseEntity newVaccineType(@RequestBody VaccineTypeDTO newVaccineType) logger.info("Create vaccine type: " + newVaccineType.toString()); boolean isCreated; try { - isCreated = vaccineTypeManager.newVaccineType(getObjectMapper().map(newVaccineType, VaccineType.class)); + isCreated = vaccineTypeManager.newVaccineType(modelMapper.map(newVaccineType, VaccineType.class)); }catch(OHDataIntegrityViolationException e){ throw new OHAPIException(new OHExceptionMessage(null, "Vaccine type already present!", OHSeverityLevel.ERROR)); } @@ -87,7 +89,7 @@ public ResponseEntity newVaccineType(@RequestBody VaccineTypeDTO newVaccineType) @PutMapping(value = "/vaccinetype/", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity updateVaccineType(@RequestBody VaccineTypeDTO updateVaccineType) throws OHServiceException { logger.info("Update vaccine type: " + updateVaccineType.toString()); - boolean isUpdated = vaccineTypeManager.updateVaccineType(getObjectMapper().map(updateVaccineType, VaccineType.class)); + boolean isUpdated = vaccineTypeManager.updateVaccineType(modelMapper.map(updateVaccineType, VaccineType.class)); if (!isUpdated) { throw new OHAPIException(new OHExceptionMessage(null, "Vaccine type is not updated!", OHSeverityLevel.ERROR)); } @@ -105,7 +107,7 @@ public ResponseEntity updateVaccineType(@RequestBody VaccineTypeDTO updateVaccin @DeleteMapping(value = "/vaccinetype/{code}", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity deleteVaccineType(@RequestBody VaccineTypeDTO vaccineTypeToDelete) throws OHServiceException { logger.info("Delete vaccine type: " + vaccineTypeToDelete.toString()); - boolean isDeleted = vaccineTypeManager.deleteVaccineType(getObjectMapper().map(vaccineTypeToDelete, VaccineType.class)); + boolean isDeleted = vaccineTypeManager.deleteVaccineType(modelMapper.map(vaccineTypeToDelete, VaccineType.class)); if (!isDeleted) { throw new OHAPIException(new OHExceptionMessage(null, "Vaccine type is not deleted!", OHSeverityLevel.ERROR)); } diff --git a/src/main/java/org/isf/visits/rest/VisitsController.java b/src/main/java/org/isf/visits/rest/VisitsController.java index fa7990c15..bdb7fce44 100644 --- a/src/main/java/org/isf/visits/rest/VisitsController.java +++ b/src/main/java/org/isf/visits/rest/VisitsController.java @@ -9,6 +9,7 @@ import org.isf.visits.dto.VisitDTO; import org.isf.visits.manager.VisitManager; import org.isf.visits.model.Visit; +import org.modelmapper.ModelMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -21,8 +22,6 @@ import java.util.List; import java.util.stream.Collectors; -import static org.isf.shared.mapper.OHModelMapper.getObjectMapper; - @RestController @Api(value = "/visit", produces = MediaType.APPLICATION_JSON_VALUE, authorizations = {@Authorization(value = "basicAuth")}) public class VisitsController { @@ -32,6 +31,9 @@ public class VisitsController { @Autowired protected VisitManager visitManager; + @Autowired + protected ModelMapper modelMapper; + public VisitsController(VisitManager visitManager) { this.visitManager = visitManager; } @@ -47,7 +49,7 @@ public VisitsController(VisitManager visitManager) { public ResponseEntity> getVisit(@PathVariable int patID) throws OHServiceException { logger.info("Get visit related to patId: " + patID); ArrayList visit = visitManager.getVisits(patID); - List listVisit = visit.stream().map(it -> getObjectMapper().map(it, VisitDTO.class)).collect(Collectors.toList()); + List listVisit = visit.stream().map(it -> modelMapper.map(it, VisitDTO.class)).collect(Collectors.toList()); if (listVisit.size() == 0) { return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null); } else { @@ -65,7 +67,7 @@ public ResponseEntity> getVisit(@PathVariable int patID) throws O @PostMapping(value = "/visit", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity newVisit(@RequestBody VisitDTO newVisit) throws OHServiceException { logger.info("Create Visit: " + newVisit); - int visitId = visitManager.newVisit(getObjectMapper().map(newVisit, Visit.class)); + int visitId = visitManager.newVisit(modelMapper.map(newVisit, Visit.class)); return ResponseEntity.status(HttpStatus.CREATED).body(newVisit.getVisitID()); } @@ -81,7 +83,7 @@ public ResponseEntity newVisits(@RequestBody List newVisits) throws OH logger.info("Create Visits"); ArrayList listVisits = new ArrayList(); for (VisitDTO visit : newVisits) { - listVisits.add(getObjectMapper().map(visit, Visit.class)); + listVisits.add(modelMapper.map(visit, Visit.class)); } boolean areCreated = visitManager.newVisits(listVisits); if (!areCreated) { diff --git a/src/main/java/org/isf/ward/rest/WardController.java b/src/main/java/org/isf/ward/rest/WardController.java index 25a1410eb..57a8991ba 100644 --- a/src/main/java/org/isf/ward/rest/WardController.java +++ b/src/main/java/org/isf/ward/rest/WardController.java @@ -2,13 +2,18 @@ import io.swagger.annotations.Api; import io.swagger.annotations.Authorization; + +import org.isf.disease.dto.DiseaseDTO; +import org.isf.disease.model.Disease; import org.isf.shared.exceptions.OHAPIException; +import org.isf.shared.rest.OHApiAbstractController; import org.isf.utils.exception.OHServiceException; import org.isf.utils.exception.model.OHExceptionMessage; import org.isf.utils.exception.model.OHSeverityLevel; import org.isf.ward.dto.WardDTO; import org.isf.ward.manager.WardBrowserManager; import org.isf.ward.model.Ward; +import org.modelmapper.ModelMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -21,18 +26,20 @@ import java.util.List; import java.util.stream.Collectors; -import static org.isf.shared.mapper.OHModelMapper.getObjectMapper; - @RestController @Api(value = "/wards", produces = MediaType.APPLICATION_JSON_VALUE, authorizations = {@Authorization(value = "basicAuth")}) -public class WardController { +public class WardController extends OHApiAbstractController { private final Logger logger = LoggerFactory.getLogger(WardController.class); @Autowired protected WardBrowserManager wardManager; - public WardController(WardBrowserManager wardManager) { + @Autowired + protected ModelMapper modelMapper; + + public WardController(WardBrowserManager wardManager, ModelMapper modelMapper) { + super(modelMapper); this.wardManager = wardManager; } @@ -46,7 +53,7 @@ public WardController(WardBrowserManager wardManager) { public ResponseEntity> getWards() throws OHServiceException { logger.info("Get wards"); ArrayList wards = wardManager.getWards(); - List listWard = wards.stream().map(ward -> getObjectMapper().map(ward, WardDTO.class)).collect(Collectors.toList()); + List listWard = wards.stream().map(ward -> modelMapper.map(ward, WardDTO.class)).collect(Collectors.toList()); if (listWard.size() == 0) { return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null); } else { @@ -64,7 +71,7 @@ public ResponseEntity> getWards() throws OHServiceException { public ResponseEntity> getWardsNoMaternity() throws OHServiceException { logger.info("Get wards no maternity"); ArrayList wards = wardManager.getWardsNoMaternity(); - List listWard = wards.stream().map(ward -> getObjectMapper().map(ward, WardDTO.class)).collect(Collectors.toList()); + List listWard = wards.stream().map(ward -> modelMapper.map(ward, WardDTO.class)).collect(Collectors.toList()); if (listWard.size() == 0) { return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null); } else { @@ -82,7 +89,7 @@ public ResponseEntity> getWardsNoMaternity() throws OHServiceExcep @GetMapping(value = "/wards/occupation", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity getCurrentOccupation(@RequestBody WardDTO wardDto) throws OHServiceException { logger.info("Get current occupation ward code: " + wardDto.getCode()); - Ward ward = getObjectMapper().map(wardDto, Ward.class); + Ward ward = modelMapper.map(wardDto, Ward.class); Integer numberOfPatients = wardManager.getCurrentOccupation(ward); if (numberOfPatients == -1) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null); @@ -101,7 +108,7 @@ public ResponseEntity getCurrentOccupation(@RequestBody WardDTO wardDto @PostMapping(value = "/wards", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity newWard(@RequestBody WardDTO newWard) throws OHServiceException { logger.info("Create Ward: " + newWard); - boolean isCreated = wardManager.newWard(getObjectMapper().map(newWard, Ward.class)); + boolean isCreated = wardManager.newWard(modelMapper.map(newWard, Ward.class)); if (!isCreated) { throw new OHAPIException(new OHExceptionMessage(null, "Ward is not created!", OHSeverityLevel.ERROR)); } @@ -118,7 +125,7 @@ public ResponseEntity newWard(@RequestBody WardDTO newWard) throws OHServiceExce @PutMapping(value = "/wards", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity updateWard(@RequestBody WardDTO updateWard) throws OHServiceException { logger.info("Update ward with code: " + updateWard.getCode()); - boolean isUpdated = wardManager.updateWard(getObjectMapper().map(updateWard, Ward.class)); + boolean isUpdated = wardManager.updateWard(modelMapper.map(updateWard, Ward.class)); if (!isUpdated) { throw new OHAPIException(new OHExceptionMessage(null, "Ward is not updated!", OHSeverityLevel.ERROR)); } @@ -136,7 +143,7 @@ public ResponseEntity updateWard(@RequestBody WardDTO updateWard) throws OHServi @DeleteMapping(value = "/wards", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity deleteWard(@RequestBody WardDTO wardToDelete) throws OHServiceException { logger.info("Delete Ward with code: " + wardToDelete.getCode()); - boolean isDeleted = wardManager.deleteWard(getObjectMapper().map(wardToDelete, Ward.class)); + boolean isDeleted = wardManager.deleteWard(modelMapper.map(wardToDelete, Ward.class)); if (!isDeleted) { throw new OHAPIException(new OHExceptionMessage(null, "Wards is not deleted!", OHSeverityLevel.ERROR)); } @@ -172,4 +179,14 @@ public ResponseEntity checkWardMaternityCode(@PathVariable Boolean crea return ResponseEntity.ok(check); } + @Override + protected Class getDTOClass() { + return WardDTO.class; + } + + @Override + protected Class getModelClass() { + return Ward.class; + } + } diff --git a/src/test/java/org/isf/patient/rest/PatientControllerTest.java b/src/test/java/org/isf/patient/rest/PatientControllerTest.java index 6d2705a6d..5b584fe86 100644 --- a/src/test/java/org/isf/patient/rest/PatientControllerTest.java +++ b/src/test/java/org/isf/patient/rest/PatientControllerTest.java @@ -1,42 +1,19 @@ package org.isf.patient.rest; -import static org.hamcrest.CoreMatchers.anyOf; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.instanceOf; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.hamcrest.CoreMatchers.nullValue; -import static org.hamcrest.Matchers.containsString; -import static org.isf.shared.mapper.OHModelMapper.getObjectMapper; -import static org.junit.Assert.assertThat; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.when; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.log; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; -import java.util.stream.Collectors; -import java.util.stream.IntStream; - +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import org.isf.patient.dto.PatientDTO; import org.isf.patient.manager.PatientBrowserManager; import org.isf.patient.model.Patient; import org.isf.patient.test.TestPatient; import org.isf.shared.exceptions.OHAPIException; import org.isf.shared.exceptions.OHResponseEntityExceptionHandler; -import org.isf.shared.mapper.OHModelMapper; import org.isf.utils.exception.OHException; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import org.modelmapper.ModelMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.MediaType; @@ -46,8 +23,22 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.HttpMediaTypeNotSupportedException; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import static org.hamcrest.CoreMatchers.*; +import static org.hamcrest.Matchers.containsString; +import static org.junit.Assert.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.log; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; /** @@ -62,12 +53,14 @@ public class PatientControllerTest { private PatientBrowserManager patientBrowserManagerMock; private MockMvc mockMvc; + + ModelMapper modelMapper = new ModelMapper(); @Before public void setup() { MockitoAnnotations.initMocks(this); this.mockMvc = MockMvcBuilders - .standaloneSetup(new PatientController(patientBrowserManagerMock)) + .standaloneSetup(new PatientController(patientBrowserManagerMock, modelMapper)) .setControllerAdvice(new OHResponseEntityExceptionHandler()) .build(); } @@ -300,7 +293,7 @@ public void whet_get_patients_non_parameters_then_return_list_of_PatientDTO_page ArrayList patientList = PatientHelper.setupPatientList(expectedPageSize); - List expectedPatienDTOList = patientList.stream().map(it -> getObjectMapper().map(it, PatientDTO.class)).collect(Collectors.toList()); + List expectedPatienDTOList = patientList.stream().map(it -> new ModelMapper().map(it, PatientDTO.class)).collect(Collectors.toList()); when(patientBrowserManagerMock.getPatient(any(Integer.class),any(Integer.class))) @@ -560,7 +553,7 @@ public static ArrayList setupPatientList(int size) { static class PatientDTOHelper{ public static PatientDTO setup() throws OHException{ Patient patient = PatientHelper.setupPatient(); - return OHModelMapper.getObjectMapper().map(patient, PatientDTO.class); + return new ModelMapper().map(patient, PatientDTO.class); } public static String asJsonString(PatientDTO patientDTO){ diff --git a/src/test/java/org/isf/shared/rest/OHApiAbstractControllerTest.java b/src/test/java/org/isf/shared/rest/OHApiAbstractControllerTest.java new file mode 100644 index 000000000..ce5f60c23 --- /dev/null +++ b/src/test/java/org/isf/shared/rest/OHApiAbstractControllerTest.java @@ -0,0 +1,81 @@ +package org.isf.shared.rest; + +import static org.junit.Assert.*; + +import java.util.Arrays; +import java.util.List; + +import org.junit.Test; +import org.modelmapper.ModelMapper; + +public class OHApiAbstractControllerTest { + + ModelMapper modelMapper = new ModelMapper(); + + private OHApiAbstractController target; + + @Test + public void testOHApiAbstractController() { + target = createInstance(modelMapper); + assertNotNull(target); + } + + private OHApiAbstractController createInstance(ModelMapper modelMapper) { + return new OHApiAbstractController(modelMapper) { + + @Override + protected Class getDTOClass() { + return Object.class; + } + + @Override + protected Class getModelClass() { + return Object.class; + } + + }; + } + + @Test + public void testToDTO() { + target = createInstance(modelMapper); + Object expected = new Object(); + Object dto = target.toDTO(expected); + assertNotNull(dto); + assertEquals(expected, dto); + } + + @Test + public void testToModel() { + target = createInstance(modelMapper); + Object expected = new Object(); + Object model = target.toModel(expected); + assertNotNull(model); + assertEquals(expected, model); + } + + @Test + public void testToDTOList() { + List expectedList = Arrays.asList(new Object(),new Object()); + target = createInstance(modelMapper); + List actualList = target.toDTOList(expectedList); + assertEquals(expectedList, actualList); + } + + @Test + public void testGetDTOClass() { + target = createInstance(modelMapper); + Class actualDTOClass = target.getModelClass(); + assertNotNull(actualDTOClass); + assertEquals(Object.class, actualDTOClass); + } + + @Test + public void testGetModelClass() { + target = createInstance(modelMapper); + Class actualModelClass = target.getModelClass(); + assertNotNull(actualModelClass); + assertEquals(Object.class, actualModelClass); + } + +}