Source:
public ResponseEntity saveOrUpdate(@RequestParam Long applicationId, @Valid @RequestBody StateHistoryViewRepresentation stateHistoryViewRepresentation, BindingResult bindingResult, Locale locale) { StateHistoryDTO stateHistoryDTO = null; if (bindingResult.hasErrors()) { return fieldErrorResponseComposer.composeResponse(bindingResult); } else { stateHistoryDTO = StateHistoryDTO.builder() .id(stateHistoryViewRepresentation.getId()) .candidateId(stateHistoryViewRepresentation.getCandidateId()) .description(stateHistoryViewRepresentation.getDescription()) .result(stateHistoryViewRepresentation.getResult()) .offeredMoney(stateHistoryViewRepresentation.getOfferedMoney()) .claim(stateHistoryViewRepresentation.getClaim()) .feedbackDate(stateHistoryViewRepresentation.getFeedbackDate() != null ? stateHistoryViewRepresentation.getFeedbackDate() : null) .dayOfStart(stateHistoryViewRepresentation.getDayOfStart()) .dateOfEnter(stateHistoryViewRepresentation.getDateOfEnter()) .creationDate(null) .stateDTO(StateDTO.builder().id(stateHistoryViewRepresentation.getStateId()) .name(stateHistoryViewRepresentation.getStateName()).build()) .recommendation(stateHistoryViewRepresentation.getRecommendation() != null ? stateHistoryViewRepresentation.getRecommendation() == 1 : null) .reviewerName(stateHistoryViewRepresentation.getReviewerName()) .recommendedPositionLevel(stateHistoryViewRepresentation.getRecommendedPositionLevel()) .build(); } statesHistoryService.saveStateHistory(stateHistoryDTO, applicationId); return new ResponseEntity<>(Collections.singletonMap("applicationId", applicationId), HttpStatus.OK); }

Target:
public ResponseEntity saveOrUpdate(@RequestParam Long applicationId, @Valid @RequestBody StateHistoryViewRepresentation stateHistoryViewRepresentation, BindingResult bindingResult, Locale locale) { StateHistoryDTO stateHistoryDTO; String stateName = stateHistoryViewRepresentation.getStateName(); if (isNewState(stateName)) { validateNewState(stateHistoryViewRepresentation, bindingResult); } if (bindingResult.hasErrors()) { return fieldErrorResponseComposer.composeResponse(bindingResult); } else { stateHistoryDTO = StateHistoryDTO.builder() .id(stateHistoryViewRepresentation.getId()) .applicationDTO(applicationsService.getApplicationDtoById(applicationId)) .candidateId(stateHistoryViewRepresentation.getCandidateId()) .description(stateHistoryViewRepresentation.getDescription()) .result(stateHistoryViewRepresentation.getResult()) .offeredMoney(stateHistoryViewRepresentation.getOfferedMoney()) .claim(stateHistoryViewRepresentation.getClaim()) .feedbackDate(stateHistoryViewRepresentation.getFeedbackDate()) .dayOfStart(stateHistoryViewRepresentation.getDayOfStart()) .dateOfEnter(stateHistoryViewRepresentation.getDateOfEnter()) .creationDate(null) .stateDTO(StateDTO.builder().id(stateHistoryViewRepresentation.getStateId()) .name(stateHistoryViewRepresentation.getStateName()).build()) .recommendation(stateHistoryViewRepresentation.getRecommendation() != null ? stateHistoryViewRepresentation.getRecommendation() == 1 : null) .reviewerName(stateHistoryViewRepresentation.getReviewerName()) .recommendedPositionLevel(stateHistoryViewRepresentation.getRecommendedPositionLevel()) .build(); if (isNewState(stateName)) { ChannelDTO channelDTO = channelService.getChannelDtoByName(stateHistoryViewRepresentation.getChannelName()); PositionDTO positionDTO = positionService.getPositionDtoByName(stateHistoryViewRepresentation.getPositionName()); stateHistoryDTO.getApplicationDTO().setChannelId(channelDTO.getId()); stateHistoryDTO.getApplicationDTO().setPositionId(positionDTO.getId()); stateHistoryDTO.setChannel(channelDTO); stateHistoryDTO.setPosition(positionDTO); } statesHistoryService.saveStateHistory(stateHistoryDTO); return new ResponseEntity<>(Collections.singletonMap("applicationId", applicationId), HttpStatus.OK); } }