class ObjectiveService {

  @Transactional
  public void deleteObjectiveById(Long objectiveId, User user) {
    Objective referencedObjective = objectiveRepository.findByIdOrThrow(objectiveId);
    throwIfCycleForObjectiveIsClosed(referencedObjective);
    for (Objective subObjective : referencedObjective.getSubObjectives()) {
      subObjective.setParentObjective(null);
      objectiveRepository.save(subObjective);
      logger.info(
          "Removed parent objective from "
              + referencedObjective.getName()
              + "(id:"
              + referencedObjective.getId()
              + ").");
    }

    if (referencedObjective.getParentOkrUnit() != null) {
      for (Objective otherObjective : referencedObjective.getParentOkrUnit().getObjectives()) {
        if (otherObjective.getSequence() > referencedObjective.getSequence()) {
          otherObjective.setSequence(otherObjective.getSequence() - 1);
          objectiveRepository.save(otherObjective);
        }
      }
    }

    objectiveRepository.deleteById(objectiveId);
    activityService.createActivity(user, referencedObjective, Action.DELETED);
  }

  @Autowired public  ObjectiveService(
      ParentService parentService,
      ObjectiveRepository objectiveRepository,
      KeyResultRepository keyResultRepository,
      ActivityService activityService,
      EntityCrawlerService entityCrawlerService,
      ConfigurationService configurationService,
      @Qualifier("okrUnitServiceUsers") OkrUnitServiceUsers<OkrChildUnit> unitService);

}