public void setBacklogItemIterationGoal(BacklogItem item, IterationGoal iterationGoal) {
   if (iterationGoal != null && item.getBacklog() == iterationGoal.getIteration()) {
     if (item.getIterationGoal() != null) {
       item.getIterationGoal().getBacklogItems().remove(item);
     }
     item.setIterationGoal(iterationGoal);
     iterationGoal.getBacklogItems().add(item);
   } else {
     if (item.getIterationGoal() != null) {
       item.getIterationGoal().getBacklogItems().remove(item);
     }
     item.setIterationGoal(null);
   }
 }
  public BacklogItem storeBacklogItem(
      BacklogItem storable,
      Backlog backlog,
      BacklogItem dataItem,
      Set<User> responsibles,
      IterationGoal iterationGoal) {

    boolean historyUpdated = false;

    if (backlog == null) {
      throw new IllegalArgumentException("Backlog must not be null.");
    }
    if (dataItem == null) {
      throw new IllegalArgumentException("No data given.");
    }
    if (storable == null) {
      storable = new BacklogItem();
      storable.setCreatedDate(Calendar.getInstance().getTime());
      try {
        storable.setCreator(SecurityUtil.getLoggedUser()); // may fail if request is multithreaded
      } catch (Exception e) {
      } // however, saving item should not fail.
    }
    storable.setDescription(dataItem.getDescription());
    storable.setEffortLeft(dataItem.getEffortLeft());
    storable.setName(dataItem.getName());
    if (storable.getOriginalEstimate() == null) {
      if (dataItem.getOriginalEstimate() == null) {
        storable.setOriginalEstimate(dataItem.getEffortLeft());
      } else {
        storable.setOriginalEstimate(dataItem.getOriginalEstimate());
      }
    }
    storable.setPriority(dataItem.getPriority());
    storable.setState(dataItem.getState());

    if (dataItem.getState() == State.DONE) {
      storable.setEffortLeft(new AFTime(0));
    } else if (dataItem.getEffortLeft() == null) {
      storable.setEffortLeft(storable.getOriginalEstimate());
    }

    Backlog originalBacklog = storable.getBacklog();
    boolean isBeingMoved = false;

    if (storable.getBacklog() != null && storable.getBacklog() != backlog) {
      isBeingMoved = true;
      this.moveItemToBacklog(storable, backlog, false);
      historyUpdated = true;
    } else if (storable.getBacklog() == null) {
      storable.setBacklog(backlog);
    }

    storable.setResponsibles(responsibles);

    if (iterationGoal == null && isBeingMoved) {
      // Down stepping from Product/Project Story to Iteration Task
      boolean isTargetIteration = backlog instanceof fi.hut.soberit.agilefant.model.Iteration;
      boolean isSourceIteration =
          originalBacklog instanceof fi.hut.soberit.agilefant.model.Iteration;
      if (isTargetIteration && !isSourceIteration) {
        // Not using iterationGoalBusiness because of circular dependency in Spring.
        iterationGoal = new IterationGoal();
        iterationGoal.setName(storable.getName());
        iterationGoal.setIteration((Iteration) backlog);
        iterationGoalDAO.store(iterationGoal);
      }
    }

    this.setBacklogItemIterationGoal(storable, iterationGoal);
    BacklogItem persisted;

    if (storable.getId() == 0) {
      int persistedId = (Integer) backlogItemDAO.create(storable);
      persisted = backlogItemDAO.get(persistedId);
    } else {
      backlogItemDAO.store(storable);
      persisted = storable;
    }
    if (!historyUpdated) {
      historyBusiness.updateBacklogHistory(backlog.getId());
    }
    return persisted;
  }