Skip to content

Commit 4674ea8

Browse files
committed
Clean up code
1 parent b34d987 commit 4674ea8

17 files changed

+240
-176
lines changed

backend/src/main/java/ch/puzzle/okr/mapper/ObjectiveMapper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public ObjectiveMapper(TeamBusinessService teamBusinessService, QuarterBusinessS
1919
this.quarterBusinessService = quarterBusinessService;
2020
}
2121

22+
// TODO: Adjust Unit Tests of ObjectiveMapper after merge of multitenancy-main
23+
2224
public ObjectiveDto toDto(Objective objective) {
2325
return new ObjectiveDto(objective.getId(), objective.getVersion(), objective.getTitle(),
2426
objective.getTeam().getId(), objective.getQuarter().getId(), objective.getQuarter().getLabel(),

backend/src/main/java/ch/puzzle/okr/service/business/AlignmentBusinessService.java

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -56,36 +56,49 @@ public void updateEntity(Long objectiveId, Objective objective) {
5656
Alignment savedAlignment = alignmentPersistenceService.findByAlignedObjectiveId(objectiveId);
5757

5858
if (savedAlignment == null) {
59-
Alignment alignment = buildAlignmentModel(objective, 0);
60-
alignmentValidationService.validateOnCreate(alignment);
61-
alignmentPersistenceService.save(alignment);
59+
createEntity(objective);
60+
} else {
61+
handleExistingAlignment(objective, savedAlignment);
62+
}
63+
}
64+
65+
private void handleExistingAlignment(Objective objective, Alignment savedAlignment) {
66+
if (objective.getAlignedEntityId() == null) {
67+
validateAndDeleteAlignmentById(savedAlignment.getId());
6268
} else {
63-
if (objective.getAlignedEntityId() == null) {
64-
alignmentValidationService.validateOnDelete(savedAlignment.getId());
65-
alignmentPersistenceService.deleteById(savedAlignment.getId());
66-
} else {
67-
Alignment alignment = buildAlignmentModel(objective, savedAlignment.getVersion());
68-
69-
alignment.setId(savedAlignment.getId());
70-
alignmentValidationService.validateOnUpdate(savedAlignment.getId(), alignment);
71-
if (isAlignmentTypeChange(alignment, savedAlignment)) {
72-
alignmentPersistenceService.recreateEntity(savedAlignment.getId(), alignment);
73-
} else {
74-
alignmentPersistenceService.save(alignment);
75-
}
76-
}
69+
validateAndUpdateAlignment(objective, savedAlignment);
70+
}
71+
}
72+
73+
private void validateAndUpdateAlignment(Objective objective, Alignment savedAlignment) {
74+
Alignment alignment = buildAlignmentModel(objective, savedAlignment.getVersion());
75+
76+
alignment.setId(savedAlignment.getId());
77+
alignmentValidationService.validateOnUpdate(savedAlignment.getId(), alignment);
78+
updateAlignment(savedAlignment, alignment);
79+
}
80+
81+
private void updateAlignment(Alignment savedAlignment, Alignment alignment) {
82+
if (isAlignmentTypeChange(alignment, savedAlignment)) {
83+
alignmentPersistenceService.recreateEntity(savedAlignment.getId(), alignment);
84+
} else {
85+
alignmentPersistenceService.save(alignment);
7786
}
7887
}
7988

8089
public Alignment buildAlignmentModel(Objective alignedObjective, int version) {
8190
if (alignedObjective.getAlignedEntityId().startsWith("O")) {
82-
Objective targetObjective = objectivePersistenceService
83-
.findById(Long.valueOf(alignedObjective.getAlignedEntityId().replace("O", "")));
84-
return ObjectiveAlignment.Builder.builder().withAlignedObjective(alignedObjective)
85-
.withTargetObjective(targetObjective).withVersion(version).build();
91+
Long entityId = Long.valueOf(alignedObjective.getAlignedEntityId().replace("O", ""));
92+
93+
Objective targetObjective = objectivePersistenceService.findById(entityId);
94+
return ObjectiveAlignment.Builder.builder() //
95+
.withAlignedObjective(alignedObjective) //
96+
.withTargetObjective(targetObjective) //
97+
.withVersion(version).build();
8698
} else if (alignedObjective.getAlignedEntityId().startsWith("K")) {
87-
KeyResult targetKeyResult = keyResultPersistenceService
88-
.findById(Long.valueOf(alignedObjective.getAlignedEntityId().replace("K", "")));
99+
Long entityId = Long.valueOf(alignedObjective.getAlignedEntityId().replace("K", ""));
100+
101+
KeyResult targetKeyResult = keyResultPersistenceService.findById(entityId);
89102
return KeyResultAlignment.Builder.builder().withAlignedObjective(alignedObjective)
90103
.withTargetKeyResult(targetKeyResult).withVersion(version).build();
91104
} else {
@@ -99,9 +112,10 @@ public boolean isAlignmentTypeChange(Alignment alignment, Alignment savedAlignme
99112
|| (alignment instanceof KeyResultAlignment && savedAlignment instanceof ObjectiveAlignment);
100113
}
101114

102-
public void updateKeyResultIdOnIdChange(Long oldId, KeyResult keyResult) {
103-
List<KeyResultAlignment> alignments = alignmentPersistenceService.findByKeyResultAlignmentId(oldId);
104-
alignments.forEach(alignment -> {
115+
public void updateKeyResultIdOnIdChange(Long oldKeyResultId, KeyResult keyResult) {
116+
List<KeyResultAlignment> keyResultAlignmentList = alignmentPersistenceService
117+
.findByKeyResultAlignmentId(oldKeyResultId);
118+
keyResultAlignmentList.forEach(alignment -> {
105119
alignment.setAlignmentTarget(keyResult);
106120
alignmentValidationService.validateOnUpdate(alignment.getId(), alignment);
107121
alignmentPersistenceService.save(alignment);
@@ -111,21 +125,23 @@ public void updateKeyResultIdOnIdChange(Long oldId, KeyResult keyResult) {
111125
public void deleteAlignmentByObjectiveId(Long objectiveId) {
112126
Alignment alignment = alignmentPersistenceService.findByAlignedObjectiveId(objectiveId);
113127
if (alignment != null) {
114-
alignmentValidationService.validateOnDelete(alignment.getId());
115-
alignmentPersistenceService.deleteById(alignment.getId());
128+
validateAndDeleteAlignmentById(alignment.getId());
116129
}
117-
List<ObjectiveAlignment> alignmentList = alignmentPersistenceService.findByObjectiveAlignmentId(objectiveId);
118-
alignmentList.forEach(objectiveAlignment -> {
119-
alignmentValidationService.validateOnDelete(objectiveAlignment.getId());
120-
alignmentPersistenceService.deleteById(objectiveAlignment.getId());
121-
});
130+
List<ObjectiveAlignment> objectiveAlignmentList = alignmentPersistenceService
131+
.findByObjectiveAlignmentId(objectiveId);
132+
objectiveAlignmentList
133+
.forEach(objectiveAlignment -> validateAndDeleteAlignmentById(objectiveAlignment.getId()));
122134
}
123135

124136
public void deleteAlignmentByKeyResultId(Long keyResultId) {
125-
List<KeyResultAlignment> alignmentList = alignmentPersistenceService.findByKeyResultAlignmentId(keyResultId);
126-
alignmentList.forEach(keyResultAlignment -> {
127-
alignmentValidationService.validateOnDelete(keyResultAlignment.getId());
128-
alignmentPersistenceService.deleteById(keyResultAlignment.getId());
129-
});
137+
List<KeyResultAlignment> keyResultAlignmentList = alignmentPersistenceService
138+
.findByKeyResultAlignmentId(keyResultId);
139+
keyResultAlignmentList
140+
.forEach(keyResultAlignment -> validateAndDeleteAlignmentById(keyResultAlignment.getId()));
141+
}
142+
143+
private void validateAndDeleteAlignmentById(Long alignmentId) {
144+
alignmentValidationService.validateOnDelete(alignmentId);
145+
alignmentPersistenceService.deleteById(alignmentId);
130146
}
131147
}

backend/src/main/java/ch/puzzle/okr/service/business/ObjectiveBusinessService.java

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,37 +57,45 @@ public List<AlignmentDto> getAlignmentPossibilities(Long quarterId) {
5757
List<Objective> objectivesByQuarter = objectivePersistenceService.findObjectiveByQuarterId(quarterId);
5858
List<AlignmentDto> alignmentDtoList = new ArrayList<>();
5959

60-
List<Team> teamList = objectivesByQuarter.stream().map(Objective::getTeam).distinct()
61-
.sorted(Comparator.comparing(Team::getName)).toList();
60+
List<Team> teamList = objectivesByQuarter.stream() //
61+
.map(Objective::getTeam) //
62+
.distinct() //
63+
.sorted(Comparator.comparing(Team::getName)) //
64+
.toList();
6265

6366
teamList.forEach(team -> {
6467
List<Objective> filteredObjectiveList = objectivesByQuarter.stream()
65-
.filter(objective -> objective.getTeam().equals(team)).toList().stream()
68+
.filter(objective -> objective.getTeam().equals(team))
6669
.sorted(Comparator.comparing(Objective::getTitle)).toList();
6770

68-
List<AlignmentObjectDto> alignmentObjectDtos = new ArrayList<>();
71+
List<AlignmentObjectDto> alignmentObjectDtoList = generateAlignmentObjects(filteredObjectiveList);
6972

70-
filteredObjectiveList.forEach(objective -> {
71-
AlignmentObjectDto objectiveDto = new AlignmentObjectDto(objective.getId(),
72-
"O - " + objective.getTitle(), "objective");
73-
alignmentObjectDtos.add(objectiveDto);
74-
75-
List<KeyResult> keyResults = keyResultBusinessService.getAllKeyResultsByObjective(objective.getId())
76-
.stream().sorted(Comparator.comparing(KeyResult::getTitle)).toList();
77-
78-
keyResults.forEach(keyResult -> {
79-
AlignmentObjectDto keyResultDto = new AlignmentObjectDto(keyResult.getId(),
80-
"KR - " + keyResult.getTitle(), "keyResult");
81-
alignmentObjectDtos.add(keyResultDto);
82-
});
83-
});
84-
AlignmentDto alignmentDto = new AlignmentDto(team.getId(), team.getName(), alignmentObjectDtos);
73+
AlignmentDto alignmentDto = new AlignmentDto(team.getId(), team.getName(), alignmentObjectDtoList);
8574
alignmentDtoList.add(alignmentDto);
8675
});
8776

8877
return alignmentDtoList;
8978
}
9079

80+
private List<AlignmentObjectDto> generateAlignmentObjects(List<Objective> filteredObjectiveList) {
81+
List<AlignmentObjectDto> alignmentObjectDtoList = new ArrayList<>();
82+
filteredObjectiveList.forEach(objective -> {
83+
AlignmentObjectDto objectiveDto = new AlignmentObjectDto(objective.getId(), "O - " + objective.getTitle(),
84+
"objective");
85+
alignmentObjectDtoList.add(objectiveDto);
86+
87+
List<KeyResult> keyResultList = keyResultBusinessService.getAllKeyResultsByObjective(objective.getId())
88+
.stream().sorted(Comparator.comparing(KeyResult::getTitle)).toList();
89+
90+
keyResultList.forEach(keyResult -> {
91+
AlignmentObjectDto keyResultDto = new AlignmentObjectDto(keyResult.getId(),
92+
"KR - " + keyResult.getTitle(), "keyResult");
93+
alignmentObjectDtoList.add(keyResultDto);
94+
});
95+
});
96+
return alignmentObjectDtoList;
97+
}
98+
9199
public List<Objective> getEntitiesByTeamId(Long id) {
92100
validator.validateOnGet(id);
93101

backend/src/main/java/ch/puzzle/okr/service/persistence/AlignmentPersistenceService.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import ch.puzzle.okr.models.alignment.ObjectiveAlignment;
66
import ch.puzzle.okr.repository.AlignmentRepository;
77
import jakarta.transaction.Transactional;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
810
import org.springframework.stereotype.Service;
911

1012
import java.util.List;
@@ -13,6 +15,7 @@
1315

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

1720
protected AlignmentPersistenceService(AlignmentRepository repository) {
1821
super(repository);
@@ -25,11 +28,11 @@ public String getModelName() {
2528

2629
@Transactional
2730
public Alignment recreateEntity(Long id, Alignment alignment) {
28-
System.out.println(alignment.toString());
29-
System.out.println("*".repeat(30));
31+
logger.debug("Delete and create new Alignment in order to prevent duplicates in case of changed Type");
32+
logger.debug("{}", alignment);
3033
// delete entity in order to prevent duplicates in case of changed keyResultType
3134
deleteById(id);
32-
System.out.printf("reached delete entity with %d", id);
35+
logger.debug("Reached delete entity with id {}", id);
3336
return save(alignment);
3437
}
3538

backend/src/main/java/ch/puzzle/okr/service/persistence/KeyResultPersistenceService.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import ch.puzzle.okr.models.keyresult.KeyResult;
44
import ch.puzzle.okr.repository.KeyResultRepository;
55
import jakarta.transaction.Transactional;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
68
import org.springframework.stereotype.Service;
79

810
import java.util.List;
@@ -11,6 +13,7 @@
1113

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

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

2831
@Transactional
2932
public KeyResult recreateEntity(Long id, KeyResult keyResult) {
30-
System.out.println(keyResult.toString());
31-
System.out.println("*".repeat(30));
33+
logger.debug("Delete KeyResult in order to prevent duplicates in case of changed keyResultType");
34+
logger.debug("{}", keyResult);
3235
// delete entity in order to prevent duplicates in case of changed keyResultType
3336
deleteById(id);
34-
System.out.printf("reached delete entity with %d", id);
37+
logger.debug("Reached delete entity with id {}", id);
3538
return save(keyResult);
3639
}
3740

backend/src/main/java/ch/puzzle/okr/service/validation/AlignmentValidationService.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,24 @@ public AlignmentValidationService(AlignmentPersistenceService alignmentPersisten
3333
public void validateOnCreate(Alignment model) {
3434
throwExceptionWhenModelIsNull(model);
3535
throwExceptionWhenIdIsNotNull(model.getId());
36-
throwExceptionWhenAlignedObjectIsNull(model);
36+
throwExceptionWhenAlignmentObjectIsNull(model);
3737
throwExceptionWhenAlignedIdIsSameAsTargetId(model);
3838
throwExceptionWhenAlignmentIsInSameTeam(model);
39-
throwExceptionWhenAlignedObjectiveAlreadyExists(model);
39+
throwExceptionWhenAlignmentWithAlignedObjectiveAlreadyExists(model);
4040
validate(model);
4141
}
4242

4343
@Override
4444
public void validateOnUpdate(Long id, Alignment model) {
4545
throwExceptionWhenModelIsNull(model);
4646
throwExceptionWhenIdIsNull(model.getId());
47-
throwExceptionWhenAlignedObjectIsNull(model);
47+
throwExceptionWhenAlignmentObjectIsNull(model);
4848
throwExceptionWhenAlignedIdIsSameAsTargetId(model);
4949
throwExceptionWhenAlignmentIsInSameTeam(model);
5050
validate(model);
5151
}
5252

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

7070
private void throwExceptionWhenAlignmentIsInSameTeam(Alignment model) {
71-
Team alignedTeam = teamPersistenceService.findById(model.getAlignedObjective().getTeam().getId());
72-
Team targetTeam = null;
71+
Team alignedObjectiveTeam = teamPersistenceService.findById(model.getAlignedObjective().getTeam().getId());
72+
Team targetObjectTeam = null;
7373

7474
if (model instanceof ObjectiveAlignment objectiveAlignment) {
75-
targetTeam = teamPersistenceService.findById(objectiveAlignment.getAlignmentTarget().getTeam().getId());
75+
targetObjectTeam = teamPersistenceService
76+
.findById(objectiveAlignment.getAlignmentTarget().getTeam().getId());
7677
} else if (model instanceof KeyResultAlignment keyResultAlignment) {
77-
targetTeam = teamPersistenceService
78+
targetObjectTeam = teamPersistenceService
7879
.findById(keyResultAlignment.getAlignmentTarget().getObjective().getTeam().getId());
7980
}
8081

81-
if (alignedTeam.equals(targetTeam)) {
82+
if (alignedObjectiveTeam.equals(targetObjectTeam)) {
8283
throw new OkrResponseStatusException(HttpStatus.BAD_REQUEST, ErrorKey.NOT_LINK_IN_SAME_TEAM,
83-
List.of("teamId", targetTeam.getId()));
84+
List.of("teamId", targetObjectTeam.getId()));
8485
}
8586
}
8687

@@ -94,7 +95,7 @@ private void throwExceptionWhenAlignedIdIsSameAsTargetId(Alignment model) {
9495
}
9596
}
9697

97-
private void throwExceptionWhenAlignedObjectiveAlreadyExists(Alignment model) {
98+
private void throwExceptionWhenAlignmentWithAlignedObjectiveAlreadyExists(Alignment model) {
9899
if (this.alignmentPersistenceService.findByAlignedObjectiveId(model.getAlignedObjective().getId()) != null) {
99100
throw new OkrResponseStatusException(HttpStatus.BAD_REQUEST, ErrorKey.ALIGNMENT_ALREADY_EXISTS,
100101
List.of("alignedObjectiveId", model.getAlignedObjective().getId()));

0 commit comments

Comments
 (0)