|
|
|
|
|
|
|
|
import org.springframework.http.HttpHeaders; |
|
|
import org.springframework.http.HttpHeaders; |
|
|
import org.springframework.http.ResponseEntity; |
|
|
import org.springframework.http.ResponseEntity; |
|
|
import org.springframework.web.bind.annotation.*; |
|
|
import org.springframework.web.bind.annotation.*; |
|
|
|
|
|
import vn.azteam.tpf.domain.TBCode; |
|
|
import vn.azteam.tpf.domain.TBCodeStatusEnum; |
|
|
import vn.azteam.tpf.domain.TBCodeStatusEnum; |
|
|
import vn.azteam.tpf.service.*; |
|
|
import vn.azteam.tpf.service.*; |
|
|
import vn.azteam.tpf.service.dto.*; |
|
|
import vn.azteam.tpf.service.dto.*; |
|
|
|
|
|
|
|
|
import vn.azteam.tpf.web.rest.util.PaginationUtil; |
|
|
import vn.azteam.tpf.web.rest.util.PaginationUtil; |
|
|
import vn.azteam.tpf.web.rest.util.RandomStringUtil; |
|
|
import vn.azteam.tpf.web.rest.util.RandomStringUtil; |
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
|
import java.net.URI; |
|
|
import java.net.URI; |
|
|
import java.net.URISyntaxException; |
|
|
import java.net.URISyntaxException; |
|
|
import java.time.Instant; |
|
|
import java.time.Instant; |
|
|
|
|
|
|
|
|
tBCodeDTO.setCreatedDate(Instant.now()); |
|
|
tBCodeDTO.setCreatedDate(Instant.now()); |
|
|
tBCodeDTO.setCreatedById(currentUser.getUserId()); |
|
|
tBCodeDTO.setCreatedById(currentUser.getUserId()); |
|
|
tBCodeDTO.setStatus(TBCodeStatusEnum.NEW); |
|
|
tBCodeDTO.setStatus(TBCodeStatusEnum.NEW); |
|
|
if(tBCodeCreationDTO.getTbProductLabelId() != null) { |
|
|
|
|
|
|
|
|
if (tBCodeCreationDTO.getTbProductLabelId() != null) { |
|
|
tBCodeDTO.setTbProductLabelId(tBCodeCreationDTO.getTbProductLabelId()); |
|
|
tBCodeDTO.setTbProductLabelId(tBCodeCreationDTO.getTbProductLabelId()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Boolean hasViolationException = false; |
|
|
Boolean hasViolationException = false; |
|
|
do { |
|
|
do { |
|
|
try { |
|
|
try { |
|
|
tBCodeDTO.setCode(RandomStringUtil.generateRandomStringFromUUID(8)); |
|
|
|
|
|
|
|
|
tBCodeDTO.setCode("AZTRACE_" + RandomStringUtil.generateRandomStringFromUUID(8)); |
|
|
result = tBCodeService.save(tBCodeDTO); |
|
|
result = tBCodeService.save(tBCodeDTO); |
|
|
} catch (org.springframework.dao.DataIntegrityViolationException ex) { |
|
|
} catch (org.springframework.dao.DataIntegrityViolationException ex) { |
|
|
if (ex.getMessage().contains("ux_tb_code_code")) { |
|
|
if (ex.getMessage().contains("ux_tb_code_code")) { |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 1 api tạo hoạt động cho code |
|
|
// 1 api tạo hoạt động cho code |
|
|
|
|
|
// @PostMapping("/tb-codes/create/activity") |
|
|
|
|
|
// @Timed |
|
|
|
|
|
// public ResponseEntity<TBActivityDTO> createActivityForTBCode(@RequestBody ActivityCodeCreationDTO |
|
|
|
|
|
// activityCodeCreationDTO) |
|
|
|
|
|
// throws URISyntaxException { |
|
|
|
|
|
// |
|
|
|
|
|
// log.debug("REST request to save TBActivityDTO : {}", activityCodeCreationDTO); |
|
|
|
|
|
// //Set created by to current user login |
|
|
|
|
|
// UserDTO currentUser = userService.getCurrentUserDTO().get(); |
|
|
|
|
|
// if (activityCodeCreationDTO.getTbCropId() == null) { |
|
|
|
|
|
// throw new BadRequestAlertException("1047", ENTITY_NAME, "1047"); |
|
|
|
|
|
// } |
|
|
|
|
|
// if (activityCodeCreationDTO.getActivityTypeId() == null) { |
|
|
|
|
|
// throw new BadRequestAlertException("1047", ENTITY_NAME, "1047"); |
|
|
|
|
|
// } |
|
|
|
|
|
// if (activityCodeCreationDTO.getExecuteDate() == null) { |
|
|
|
|
|
// throw new BadRequestAlertException("1047", ENTITY_NAME, "1047"); |
|
|
|
|
|
// } |
|
|
|
|
|
// |
|
|
|
|
|
// Optional<TBCropDTO> tBCropDTO = tBCropService.findOne(activityCodeCreationDTO.getTbCropId()); |
|
|
|
|
|
// if (!tBCropDTO.isPresent()) { |
|
|
|
|
|
// throw new BadRequestAlertException("1019", ENTITY_NAME, "1019"); |
|
|
|
|
|
// } |
|
|
|
|
|
// |
|
|
|
|
|
// Optional<TBActivityTypeDTO> tbActivityTypeDTO = tBActivityTypeService.findOne(activityCodeCreationDTO.getActivityTypeId()); |
|
|
|
|
|
// if (!tbActivityTypeDTO.isPresent()) { |
|
|
|
|
|
// throw new BadRequestAlertException("1019", ENTITY_NAME, "1019"); |
|
|
|
|
|
// } |
|
|
|
|
|
// |
|
|
|
|
|
// TBActivityDTO activityDTO = new TBActivityDTO(); |
|
|
|
|
|
// activityDTO.setCreatedById(currentUser.getId()); |
|
|
|
|
|
// activityDTO.setActivityTypeId(activityCodeCreationDTO.getActivityTypeId()); |
|
|
|
|
|
// activityDTO.setDescription(activityCodeCreationDTO.getDescription()); |
|
|
|
|
|
// activityDTO.setExecuteDate(activityCodeCreationDTO.getExecuteDate()); |
|
|
|
|
|
// activityDTO.setCropId(activityCodeCreationDTO.getTbCropId()); |
|
|
|
|
|
// activityDTO.setCreatedDate(Instant.now()); |
|
|
|
|
|
// TBActivityDTO result = tBActivityService.save(activityDTO); |
|
|
|
|
|
// return ResponseEntity.created(new URI("/tb-codes/create/activity" + result.getId())) |
|
|
|
|
|
// .headers(HeaderUtil.createEntityCreationAlert("TBActivity", result.getId().toString())) |
|
|
|
|
|
// .body(result); |
|
|
|
|
|
// } |
|
|
|
|
|
|
|
|
@PostMapping("/tb-codes/create/activity") |
|
|
@PostMapping("/tb-codes/create/activity") |
|
|
@Timed |
|
|
@Timed |
|
|
public ResponseEntity<TBActivityDTO> createActivityForTBCode(@RequestBody ActivityCodeCreationDTO |
|
|
public ResponseEntity<TBActivityDTO> createActivityForTBCode(@RequestBody ActivityCodeCreationDTO |
|
|
|
|
|
|
|
|
log.debug("REST request to save TBActivityDTO : {}", activityCodeCreationDTO); |
|
|
log.debug("REST request to save TBActivityDTO : {}", activityCodeCreationDTO); |
|
|
//Set created by to current user login |
|
|
//Set created by to current user login |
|
|
UserDTO currentUser = userService.getCurrentUserDTO().get(); |
|
|
UserDTO currentUser = userService.getCurrentUserDTO().get(); |
|
|
if (activityCodeCreationDTO.getTbCropId() == null) { |
|
|
|
|
|
|
|
|
if (StringUtils.isBlank(activityCodeCreationDTO.getCode())) { |
|
|
throw new BadRequestAlertException("1047", ENTITY_NAME, "1047"); |
|
|
throw new BadRequestAlertException("1047", ENTITY_NAME, "1047"); |
|
|
} |
|
|
} |
|
|
if (activityCodeCreationDTO.getActivityTypeId() == null) { |
|
|
if (activityCodeCreationDTO.getActivityTypeId() == null) { |
|
|
|
|
|
|
|
|
throw new BadRequestAlertException("1047", ENTITY_NAME, "1047"); |
|
|
throw new BadRequestAlertException("1047", ENTITY_NAME, "1047"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
Optional<TBCropDTO> tBCropDTO = tBCropService.findOne(activityCodeCreationDTO.getTbCropId()); |
|
|
|
|
|
|
|
|
Optional<TBCode> tbCode = tBCodeService.findByCode(activityCodeCreationDTO.getCode()); |
|
|
|
|
|
|
|
|
|
|
|
if(!tbCode.isPresent()) { |
|
|
|
|
|
throw new BadRequestAlertException("1047", ENTITY_NAME, "1047"); |
|
|
|
|
|
} else { |
|
|
|
|
|
if(!tbCode.get().getStatus().equals(TBCodeStatusEnum.ACTIVE) |
|
|
|
|
|
|| !tbCode.get().getStatus().equals(TBCodeStatusEnum.NEW)) { |
|
|
|
|
|
throw new BadRequestAlertException("1047", "TB Code is not active", "1047"); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Long tbCropId = tbCode.get().getTbCropId(); |
|
|
|
|
|
Optional<TBCropDTO> tBCropDTO = tBCropService.findOne(tbCropId); |
|
|
if (!tBCropDTO.isPresent()) { |
|
|
if (!tBCropDTO.isPresent()) { |
|
|
throw new BadRequestAlertException("1019", ENTITY_NAME, "1019"); |
|
|
throw new BadRequestAlertException("1019", ENTITY_NAME, "1019"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
activityDTO.setActivityTypeId(activityCodeCreationDTO.getActivityTypeId()); |
|
|
activityDTO.setActivityTypeId(activityCodeCreationDTO.getActivityTypeId()); |
|
|
activityDTO.setDescription(activityCodeCreationDTO.getDescription()); |
|
|
activityDTO.setDescription(activityCodeCreationDTO.getDescription()); |
|
|
activityDTO.setExecuteDate(activityCodeCreationDTO.getExecuteDate()); |
|
|
activityDTO.setExecuteDate(activityCodeCreationDTO.getExecuteDate()); |
|
|
activityDTO.setCropId(activityCodeCreationDTO.getTbCropId()); |
|
|
|
|
|
|
|
|
activityDTO.setCropId(tbCropId); |
|
|
activityDTO.setCreatedDate(Instant.now()); |
|
|
activityDTO.setCreatedDate(Instant.now()); |
|
|
TBActivityDTO result = tBActivityService.save(activityDTO); |
|
|
TBActivityDTO result = tBActivityService.save(activityDTO); |
|
|
return ResponseEntity.created(new URI("/tb-codes/create/activity" + result.getId())) |
|
|
return ResponseEntity.created(new URI("/tb-codes/create/activity" + result.getId())) |