Skip to content

Commit

Permalink
Clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
lkleisa committed Jun 6, 2024
1 parent b34d987 commit 4674ea8
Show file tree
Hide file tree
Showing 17 changed files with 240 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public ObjectiveMapper(TeamBusinessService teamBusinessService, QuarterBusinessS
this.quarterBusinessService = quarterBusinessService;
}

// TODO: Adjust Unit Tests of ObjectiveMapper after merge of multitenancy-main

public ObjectiveDto toDto(Objective objective) {
return new ObjectiveDto(objective.getId(), objective.getVersion(), objective.getTitle(),
objective.getTeam().getId(), objective.getQuarter().getId(), objective.getQuarter().getLabel(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,36 +56,49 @@ public void updateEntity(Long objectiveId, Objective objective) {
Alignment savedAlignment = alignmentPersistenceService.findByAlignedObjectiveId(objectiveId);

if (savedAlignment == null) {
Alignment alignment = buildAlignmentModel(objective, 0);
alignmentValidationService.validateOnCreate(alignment);
alignmentPersistenceService.save(alignment);
createEntity(objective);
} else {
handleExistingAlignment(objective, savedAlignment);
}
}

private void handleExistingAlignment(Objective objective, Alignment savedAlignment) {
if (objective.getAlignedEntityId() == null) {
validateAndDeleteAlignmentById(savedAlignment.getId());
} else {
if (objective.getAlignedEntityId() == null) {
alignmentValidationService.validateOnDelete(savedAlignment.getId());
alignmentPersistenceService.deleteById(savedAlignment.getId());
} else {
Alignment alignment = buildAlignmentModel(objective, savedAlignment.getVersion());

alignment.setId(savedAlignment.getId());
alignmentValidationService.validateOnUpdate(savedAlignment.getId(), alignment);
if (isAlignmentTypeChange(alignment, savedAlignment)) {
alignmentPersistenceService.recreateEntity(savedAlignment.getId(), alignment);
} else {
alignmentPersistenceService.save(alignment);
}
}
validateAndUpdateAlignment(objective, savedAlignment);
}
}

private void validateAndUpdateAlignment(Objective objective, Alignment savedAlignment) {
Alignment alignment = buildAlignmentModel(objective, savedAlignment.getVersion());

alignment.setId(savedAlignment.getId());
alignmentValidationService.validateOnUpdate(savedAlignment.getId(), alignment);
updateAlignment(savedAlignment, alignment);
}

private void updateAlignment(Alignment savedAlignment, Alignment alignment) {
if (isAlignmentTypeChange(alignment, savedAlignment)) {
alignmentPersistenceService.recreateEntity(savedAlignment.getId(), alignment);
} else {
alignmentPersistenceService.save(alignment);
}
}

public Alignment buildAlignmentModel(Objective alignedObjective, int version) {
if (alignedObjective.getAlignedEntityId().startsWith("O")) {
Objective targetObjective = objectivePersistenceService
.findById(Long.valueOf(alignedObjective.getAlignedEntityId().replace("O", "")));
return ObjectiveAlignment.Builder.builder().withAlignedObjective(alignedObjective)
.withTargetObjective(targetObjective).withVersion(version).build();
Long entityId = Long.valueOf(alignedObjective.getAlignedEntityId().replace("O", ""));

Objective targetObjective = objectivePersistenceService.findById(entityId);
return ObjectiveAlignment.Builder.builder() //
.withAlignedObjective(alignedObjective) //
.withTargetObjective(targetObjective) //
.withVersion(version).build();
} else if (alignedObjective.getAlignedEntityId().startsWith("K")) {
KeyResult targetKeyResult = keyResultPersistenceService
.findById(Long.valueOf(alignedObjective.getAlignedEntityId().replace("K", "")));
Long entityId = Long.valueOf(alignedObjective.getAlignedEntityId().replace("K", ""));

KeyResult targetKeyResult = keyResultPersistenceService.findById(entityId);
return KeyResultAlignment.Builder.builder().withAlignedObjective(alignedObjective)
.withTargetKeyResult(targetKeyResult).withVersion(version).build();
} else {
Expand All @@ -99,9 +112,10 @@ public boolean isAlignmentTypeChange(Alignment alignment, Alignment savedAlignme
|| (alignment instanceof KeyResultAlignment && savedAlignment instanceof ObjectiveAlignment);
}

public void updateKeyResultIdOnIdChange(Long oldId, KeyResult keyResult) {
List<KeyResultAlignment> alignments = alignmentPersistenceService.findByKeyResultAlignmentId(oldId);
alignments.forEach(alignment -> {
public void updateKeyResultIdOnIdChange(Long oldKeyResultId, KeyResult keyResult) {
List<KeyResultAlignment> keyResultAlignmentList = alignmentPersistenceService
.findByKeyResultAlignmentId(oldKeyResultId);
keyResultAlignmentList.forEach(alignment -> {
alignment.setAlignmentTarget(keyResult);
alignmentValidationService.validateOnUpdate(alignment.getId(), alignment);
alignmentPersistenceService.save(alignment);
Expand All @@ -111,21 +125,23 @@ public void updateKeyResultIdOnIdChange(Long oldId, KeyResult keyResult) {
public void deleteAlignmentByObjectiveId(Long objectiveId) {
Alignment alignment = alignmentPersistenceService.findByAlignedObjectiveId(objectiveId);
if (alignment != null) {
alignmentValidationService.validateOnDelete(alignment.getId());
alignmentPersistenceService.deleteById(alignment.getId());
validateAndDeleteAlignmentById(alignment.getId());
}
List<ObjectiveAlignment> alignmentList = alignmentPersistenceService.findByObjectiveAlignmentId(objectiveId);
alignmentList.forEach(objectiveAlignment -> {
alignmentValidationService.validateOnDelete(objectiveAlignment.getId());
alignmentPersistenceService.deleteById(objectiveAlignment.getId());
});
List<ObjectiveAlignment> objectiveAlignmentList = alignmentPersistenceService
.findByObjectiveAlignmentId(objectiveId);
objectiveAlignmentList
.forEach(objectiveAlignment -> validateAndDeleteAlignmentById(objectiveAlignment.getId()));
}

public void deleteAlignmentByKeyResultId(Long keyResultId) {
List<KeyResultAlignment> alignmentList = alignmentPersistenceService.findByKeyResultAlignmentId(keyResultId);
alignmentList.forEach(keyResultAlignment -> {
alignmentValidationService.validateOnDelete(keyResultAlignment.getId());
alignmentPersistenceService.deleteById(keyResultAlignment.getId());
});
List<KeyResultAlignment> keyResultAlignmentList = alignmentPersistenceService
.findByKeyResultAlignmentId(keyResultId);
keyResultAlignmentList
.forEach(keyResultAlignment -> validateAndDeleteAlignmentById(keyResultAlignment.getId()));
}

private void validateAndDeleteAlignmentById(Long alignmentId) {
alignmentValidationService.validateOnDelete(alignmentId);
alignmentPersistenceService.deleteById(alignmentId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,37 +57,45 @@ public List<AlignmentDto> getAlignmentPossibilities(Long quarterId) {
List<Objective> objectivesByQuarter = objectivePersistenceService.findObjectiveByQuarterId(quarterId);
List<AlignmentDto> alignmentDtoList = new ArrayList<>();

List<Team> teamList = objectivesByQuarter.stream().map(Objective::getTeam).distinct()
.sorted(Comparator.comparing(Team::getName)).toList();
List<Team> teamList = objectivesByQuarter.stream() //
.map(Objective::getTeam) //
.distinct() //
.sorted(Comparator.comparing(Team::getName)) //
.toList();

teamList.forEach(team -> {
List<Objective> filteredObjectiveList = objectivesByQuarter.stream()
.filter(objective -> objective.getTeam().equals(team)).toList().stream()
.filter(objective -> objective.getTeam().equals(team))
.sorted(Comparator.comparing(Objective::getTitle)).toList();

List<AlignmentObjectDto> alignmentObjectDtos = new ArrayList<>();
List<AlignmentObjectDto> alignmentObjectDtoList = generateAlignmentObjects(filteredObjectiveList);

filteredObjectiveList.forEach(objective -> {
AlignmentObjectDto objectiveDto = new AlignmentObjectDto(objective.getId(),
"O - " + objective.getTitle(), "objective");
alignmentObjectDtos.add(objectiveDto);

List<KeyResult> keyResults = keyResultBusinessService.getAllKeyResultsByObjective(objective.getId())
.stream().sorted(Comparator.comparing(KeyResult::getTitle)).toList();

keyResults.forEach(keyResult -> {
AlignmentObjectDto keyResultDto = new AlignmentObjectDto(keyResult.getId(),
"KR - " + keyResult.getTitle(), "keyResult");
alignmentObjectDtos.add(keyResultDto);
});
});
AlignmentDto alignmentDto = new AlignmentDto(team.getId(), team.getName(), alignmentObjectDtos);
AlignmentDto alignmentDto = new AlignmentDto(team.getId(), team.getName(), alignmentObjectDtoList);
alignmentDtoList.add(alignmentDto);
});

return alignmentDtoList;
}

private List<AlignmentObjectDto> generateAlignmentObjects(List<Objective> filteredObjectiveList) {
List<AlignmentObjectDto> alignmentObjectDtoList = new ArrayList<>();
filteredObjectiveList.forEach(objective -> {
AlignmentObjectDto objectiveDto = new AlignmentObjectDto(objective.getId(), "O - " + objective.getTitle(),
"objective");
alignmentObjectDtoList.add(objectiveDto);

List<KeyResult> keyResultList = keyResultBusinessService.getAllKeyResultsByObjective(objective.getId())
.stream().sorted(Comparator.comparing(KeyResult::getTitle)).toList();

keyResultList.forEach(keyResult -> {
AlignmentObjectDto keyResultDto = new AlignmentObjectDto(keyResult.getId(),
"KR - " + keyResult.getTitle(), "keyResult");
alignmentObjectDtoList.add(keyResultDto);
});
});
return alignmentObjectDtoList;
}

public List<Objective> getEntitiesByTeamId(Long id) {
validator.validateOnGet(id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import ch.puzzle.okr.models.alignment.ObjectiveAlignment;
import ch.puzzle.okr.repository.AlignmentRepository;
import jakarta.transaction.Transactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
Expand All @@ -13,6 +15,7 @@

@Service
public class AlignmentPersistenceService extends PersistenceBase<Alignment, Long, AlignmentRepository> {
private static final Logger logger = LoggerFactory.getLogger(AlignmentPersistenceService.class);

protected AlignmentPersistenceService(AlignmentRepository repository) {
super(repository);
Expand All @@ -25,11 +28,11 @@ public String getModelName() {

@Transactional
public Alignment recreateEntity(Long id, Alignment alignment) {
System.out.println(alignment.toString());
System.out.println("*".repeat(30));
logger.debug("Delete and create new Alignment in order to prevent duplicates in case of changed Type");
logger.debug("{}", alignment);
// delete entity in order to prevent duplicates in case of changed keyResultType
deleteById(id);
System.out.printf("reached delete entity with %d", id);
logger.debug("Reached delete entity with id {}", id);
return save(alignment);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import ch.puzzle.okr.models.keyresult.KeyResult;
import ch.puzzle.okr.repository.KeyResultRepository;
import jakarta.transaction.Transactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
Expand All @@ -11,6 +13,7 @@

@Service
public class KeyResultPersistenceService extends PersistenceBase<KeyResult, Long, KeyResultRepository> {
private static final Logger logger = LoggerFactory.getLogger(KeyResultPersistenceService.class);

protected KeyResultPersistenceService(KeyResultRepository repository) {
super(repository);
Expand All @@ -27,11 +30,11 @@ public List<KeyResult> getKeyResultsByObjective(Long objectiveId) {

@Transactional
public KeyResult recreateEntity(Long id, KeyResult keyResult) {
System.out.println(keyResult.toString());
System.out.println("*".repeat(30));
logger.debug("Delete KeyResult in order to prevent duplicates in case of changed keyResultType");
logger.debug("{}", keyResult);
// delete entity in order to prevent duplicates in case of changed keyResultType
deleteById(id);
System.out.printf("reached delete entity with %d", id);
logger.debug("Reached delete entity with id {}", id);
return save(keyResult);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,24 @@ public AlignmentValidationService(AlignmentPersistenceService alignmentPersisten
public void validateOnCreate(Alignment model) {
throwExceptionWhenModelIsNull(model);
throwExceptionWhenIdIsNotNull(model.getId());
throwExceptionWhenAlignedObjectIsNull(model);
throwExceptionWhenAlignmentObjectIsNull(model);
throwExceptionWhenAlignedIdIsSameAsTargetId(model);
throwExceptionWhenAlignmentIsInSameTeam(model);
throwExceptionWhenAlignedObjectiveAlreadyExists(model);
throwExceptionWhenAlignmentWithAlignedObjectiveAlreadyExists(model);
validate(model);
}

@Override
public void validateOnUpdate(Long id, Alignment model) {
throwExceptionWhenModelIsNull(model);
throwExceptionWhenIdIsNull(model.getId());
throwExceptionWhenAlignedObjectIsNull(model);
throwExceptionWhenAlignmentObjectIsNull(model);
throwExceptionWhenAlignedIdIsSameAsTargetId(model);
throwExceptionWhenAlignmentIsInSameTeam(model);
validate(model);
}

private void throwExceptionWhenAlignedObjectIsNull(Alignment model) {
private void throwExceptionWhenAlignmentObjectIsNull(Alignment model) {
if (model.getAlignedObjective() == null) {
throw new OkrResponseStatusException(HttpStatus.BAD_REQUEST, ErrorKey.ATTRIBUTE_NULL,
List.of("alignedObjectiveId"));
Expand All @@ -68,19 +68,20 @@ private void throwExceptionWhenAlignedObjectIsNull(Alignment model) {
}

private void throwExceptionWhenAlignmentIsInSameTeam(Alignment model) {
Team alignedTeam = teamPersistenceService.findById(model.getAlignedObjective().getTeam().getId());
Team targetTeam = null;
Team alignedObjectiveTeam = teamPersistenceService.findById(model.getAlignedObjective().getTeam().getId());
Team targetObjectTeam = null;

if (model instanceof ObjectiveAlignment objectiveAlignment) {
targetTeam = teamPersistenceService.findById(objectiveAlignment.getAlignmentTarget().getTeam().getId());
targetObjectTeam = teamPersistenceService
.findById(objectiveAlignment.getAlignmentTarget().getTeam().getId());
} else if (model instanceof KeyResultAlignment keyResultAlignment) {
targetTeam = teamPersistenceService
targetObjectTeam = teamPersistenceService
.findById(keyResultAlignment.getAlignmentTarget().getObjective().getTeam().getId());
}

if (alignedTeam.equals(targetTeam)) {
if (alignedObjectiveTeam.equals(targetObjectTeam)) {
throw new OkrResponseStatusException(HttpStatus.BAD_REQUEST, ErrorKey.NOT_LINK_IN_SAME_TEAM,
List.of("teamId", targetTeam.getId()));
List.of("teamId", targetObjectTeam.getId()));
}
}

Expand All @@ -94,7 +95,7 @@ private void throwExceptionWhenAlignedIdIsSameAsTargetId(Alignment model) {
}
}

private void throwExceptionWhenAlignedObjectiveAlreadyExists(Alignment model) {
private void throwExceptionWhenAlignmentWithAlignedObjectiveAlreadyExists(Alignment model) {
if (this.alignmentPersistenceService.findByAlignedObjectiveId(model.getAlignedObjective().getId()) != null) {
throw new OkrResponseStatusException(HttpStatus.BAD_REQUEST, ErrorKey.ALIGNMENT_ALREADY_EXISTS,
List.of("alignedObjectiveId", model.getAlignedObjective().getId()));
Expand Down
Loading

0 comments on commit 4674ea8

Please sign in to comment.