Mam kilka obiektów DTO, które dziedziczą po jednym obiekcie
public class MovieRequest {}
public class OtherTitle extends MovieRequest {}
public class ReleaseDate extends MovieRequest {}
Dzięki temu mogłem sobie stworzyć generyczny obiekt DTO kontrybucji
public class ContributionNew<T extends MovieRequest> {}
Obiekt DTO kontrybucji jest przesyłany do metod
void createOtherTitleContribution(
final ContributionNew<OtherTitle> contribution,
...
);
void updateOtherTitleContribution(
final ContributionUpdate<OtherTitle> contribution,
...
);
void createReleaseDateContribution(
final ContributionNew<ReleaseDate> contribution,
...
);
void updateReleaseDateContribution(
final ContributionUpdate<ReleaseDate> contribution,
...
);
Tutaj są tylko 4, dla tytułów i dla dat premier. Obecnym sposobem wyjdzie mi około ponad 30 metod.
createBoxOfficeContribution
itd. Przykładowa metoda aktualizacji np.
updateOtherTitleContribution
wygląda tak.
@Override
public void updateOtherTitleContribution(
ContributionUpdate<OtherTitle> contribution,
Long contributionId,
Long userId
) throws ResourceNotFoundException {
log.info("Called with contribution {}, contributionId {}, userId {}",
contribution, contributionId, userId);
final UserEntity user = this.findUser(userId);
final ContributionEntity contributionEntity = this.findContribution(contributionId, EditStatus.WAITING, user, MovieField.OTHER_TITLE);
this.validIds(contributionEntity.getIdsToAdd(), contribution.getElementsToAdd().keySet());
this.validIds(contributionEntity.getIdsToUpdate().keySet(), contribution.getElementsToUpdate().keySet());
this.validIds(contributionEntity.getIdsToDelete(), contribution.getIdsToDelete());
this.cleanUpIdsToAdd(contributionEntity.getIdsToAdd(), contribution.getElementsToAdd().keySet(), contributionEntity.getMovie().getOtherTitles());
this.cleanUpIdsToUpdate(contributionEntity.getIdsToUpdate(), contribution.getElementsToUpdate().keySet());
this.cleanUpIdsToDelete(contributionEntity.getIdsToDelete(), contribution.getIdsToDelete());
contribution.getElementsToAdd().forEach((key, value) -> {
this.moviePersistenceService.updateOtherTitle(value, key, contributionEntity.getMovie());
});
contribution.getElementsToUpdate().forEach((key, value) -> {
this.moviePersistenceService.updateOtherTitle(value, key, contributionEntity.getMovie());
});
contribution.getNewElementsToAdd()
.forEach(otherTitle -> {
final Long id = this.moviePersistenceService.createOtherTitle(otherTitle, contributionEntity.getMovie(), user);
contributionEntity.getIdsToAdd().add(id);
});
contributionEntity.setSources(contribution.getSources());
Optional.ofNullable(contribution.getComment()).ifPresent(contributionEntity::setUserComment);
}
Jak widać unikalne są tylko fragmenty
this.moviePersistenceService.updateOtherTitle(...)
w metodzie
updateReleaseDateContribution
będzie to
this.moviePersistenceService.updateReleaseDate(...)
Jak miałbym teraz powielać te metody, to wyjdzie kilkadziesiąt metod i kilka tysięcy linijek kodu.
Nie mam pomysłu jak to można zoptymalizować.
Np. jakaś metoda typu
void updateContribution(
final ContributionUpdate<tutaj nie wiem co dać> contribution,
...
);
i np. w metodzie jakieś warunki. No nie wiem. Nie mam pojęcia jak to zrobić. Może Wy macie jakiś pomysł?