/** Create a new Issue by information entered in view and save into database */
  public void addIssue() {

    // Remove Control Characters
    newIssue.setSubject(newIssue.getSubject().replaceAll("\\p{Cntrl}", ""));
    newIssue.setDescription(newIssue.getDescription().replaceAll("\\p{Cntrl}", ""));
    newIssue.setNote(newIssue.getNote().replaceAll("\\p{Cntrl}", ""));

    newIssue.setParent(selectedIssue);
    newIssue.setSprint(selectedIssue.getSprint());
    newIssue.setStatus(
        statusService.findStatusStartBySprintId(selectedIssue.getSprint().getSprintId()));
    newIssue.setEstimate("D0T0");
    newIssue.setRemain("D0T0");
    newIssue.setType("Task");
    newIssue.setPointFormat("1");
    newIssue.setPriority("MUST");
    newIssue.setOldId(null);
    newIssue.setCreatedDate(new Date());
    issueService.saveIssue(newIssue);

    // Update status userstory
    UserStory userStory = userStoryService.findUserStoryByIssue(newIssue);
    UserStory.StatusType status = userStoryService.findStatusOfUserStory(userStory);
    userStory.setStatus(status);
    userStoryService.update(userStory);

    resetForm();
    RequestContext.getCurrentInstance().addCallbackParam("save", true);
    loadIssues();
  }
 public void updateUserStoryWhenUpdateIssue() {
   if (this.issue.getIssueId() != null) {
     updateIssue();
     UserStory userStoryOfIssue = this.issue.getUserStory();
     if (userStoryOfIssue != null) {
       // Update status userstory
       boolean updateStatusUs = false;
       UserStory.StatusType status =
           userStoryService.findStatusOfUserStoryBySprintAndUserStory(
               userStoryOfIssue, this.sprint);
       if (userStoryOfIssue.getStatus().compareTo(status) != 0) {
         updateStatusUs = true;
       }
       userStoryOfIssue.setPriority(PriorityType.valueOf(issue.getPriority()));
       userStoryOfIssue.setStatus(status);
       userStoryOfIssue.setName(this.issue.getSubject());
       userStoryOfIssue.setDescription(this.issue.getDescription());
       userStoryOfIssue.setNote(this.issue.getNote());
       if (userStoryService.update(userStoryOfIssue)) {
         userStoryService.updateAllIssueOfUserStoryHaveTheSameContent(userStoryOfIssue);
         // update kanbanissue status belong to userstory
         if (!sprintService.isPastSprint(issue.getSprint()) && updateStatusUs) {
           kanbanIssueService.updateAllKanbanIssueByUserStoryStatusOfTeam(
               userStoryOfIssue, sprint.getTeam());
         }
       }
     }
   }
   // Reset issueList and issue value
   resetIssueForm();
   resetIssueValue();
   resetAttachmentList();
   refreshIssueListTable();
 }
 /** Set status Destroy VOID for current user story */
 public void destroyVoid() {
   UserStory usTemp = userStoryService.findUserStoryById(selectedUserStoryId);
   usTemp.setStatus(usTemp.getPreviousStatus());
   userStoryService.update(usTemp);
   setValueForUsList();
   issueService.destroyVoidAllUnexpiredIssuesWhenDestroyVoidUserStory(usTemp.getUserStoryId());
   kanbanIssueService.destroyVoidAllKanbanIssueWhenDestroyVoidUserStory(usTemp.getUserStoryId());
 }
 public void setUsList(List<UserStory> usList) {
   try {
     for (UserStory us : usList) {
       userStoryService.update(us);
     }
     this.userStoryList = usList;
   } catch (Exception e) {
   }
 }
  /**
   * update user story on menu backlog (sprint backlog)
   *
   * @param userStoryId
   * @param name
   * @param value
   */
  public void updatePBLOnBacklogPage() {
    try {
      RequestContext requestContext = RequestContext.getCurrentInstance();
      String id = JSFUtils.getRequestParameter("Id");
      String name = JSFUtils.getRequestParameter("name");
      String description = JSFUtils.getRequestParameter("description");
      String value = JSFUtils.getRequestParameter("value");
      String risk = JSFUtils.getRequestParameter("risk");
      UserStory current = userStoryService.findUserStoryById(Long.parseLong(id.trim()));
      if (current == null) {
        requestContext.addCallbackParam("userStoryId", id);
        requestContext.addCallbackParam("error", EXCEPTION);
        return;
      }

      if (name.equals("")) {
        requestContext.addCallbackParam("userStoryId", id);
        requestContext.addCallbackParam("error", EMPTY_ERROR);
        return;
      }

      if ((userStoryService.checkExistUserStory(
                  Utils.standardizeString(name), current.getProject().getProjectId())
              != null)
          && !(current.getName().equals(Utils.standardizeString(name)))) {
        requestContext.addCallbackParam("userStoryId", id);
        requestContext.addCallbackParam("error", DUPPLICATE_ERROR);
        return;
      }
      if (Integer.parseInt(value) > MAX_BUSSINESS_VALUE || Integer.parseInt(value) < 0) {
        requestContext.addCallbackParam("userStoryId", id);
        requestContext.addCallbackParam("error", VALUE_ERROR);
        return;
      }
      if (Integer.parseInt(risk) > MAX_RISK_VALUE || Integer.parseInt(risk) < 0) {
        requestContext.addCallbackParam("userStoryId", id);
        requestContext.addCallbackParam("error", RISK_ERROR);
        return;
      }
      current.setName(name.replaceAll("\\p{Cntrl}", ""));
      current.setValue(Integer.parseInt(value));
      current.setRisk(Integer.parseInt(risk));
      current.setDescription(description.replaceAll("\\p{Cntrl}", ""));
      if (userStoryService.update(current)) {
        userStoryService.updateAllIssueOfUserStoryHaveTheSameContent(current);
      }
      requestContext.execute("PrimeFaces.ab({source:'',update:'form2'});");
    } catch (Exception e) {

    }
  }
  /** Set status VOID for current user story */
  public void setVoid() {
    try {
      UserStory usTemp = userStoryService.findUserStoryById(selectedUserStoryId);
      usTemp.setPreviousStatus(usTemp.getStatus());
      usTemp.setStatus(UserStory.StatusType.VOID);
      //		kanbanIssueService.deleteAllKanbanIssueByUserStoryID(usTemp.getUserStoryId());
      issueService.setVoidAllUnexpiredIssuesWhenSetUserStoryVoid(usTemp.getUserStoryId());
      kanbanIssueService.setVoidAllKanbanIssueWhenSetVoidUserStory(usTemp.getUserStoryId());
      userStoryService.update(usTemp);

      setValueForUsList();
      JSFUtils.resloveMethodExpression(
          "#{homeBean.setDefaultHistoryList}", Void.class, new Class<?>[0], new Object[0]);
    } catch (Exception e) {
      LOGGER.error(e);
    }
  }
  public void updateUserStory() {
    RequestContext context = RequestContext.getCurrentInstance();
    if (userStory == null) {
      context.addCallbackParam("userStoryId", userStory.getUserStoryId());
      context.addCallbackParam("error", EXCEPTION);
      return;
    }

    if (userStory.getName().equals("")) {
      context.addCallbackParam("userStoryId", userStory.getUserStoryId());
      context.addCallbackParam("error", EMPTY_ERROR);
      return;
    }

    if (userStoryService.checkExistUserStory(
                Utils.standardizeString(userStory.getName()), getProjectId())
            != null
        && !(userStory.getName().equals(Utils.standardizeString(userStory.getName())))) {
      context.addCallbackParam("userStoryId", userStory.getUserStoryId());
      context.addCallbackParam("error", DUPPLICATE_ERROR);
      return;
    }

    if (userStory.getValue() > MAX_BUSSINESS_VALUE || userStory.getValue() < 0) {
      context.addCallbackParam("userStoryId", userStory.getUserStoryId());
      context.addCallbackParam("error", VALUE_ERROR);
      return;
    }

    userStory.setName(userStory.getName().replaceAll("\\p{Cntrl}", ""));
    userStory.setDescription(userStory.getDescription().replaceAll("\\p{Cntrl}", ""));

    if (userStoryService.update(userStory)) {
      userStoryService.updateAllIssueOfUserStoryHaveTheSameContent(userStory);
      setValueForUsList();
      context.addCallbackParam("error", NO_ERROR);
      context.addCallbackParam("userStoryId", userStory.getUserStoryId());
      JSFUtils.resloveMethodExpression(
          "#{homeBean.setDefaultHistoryList}", Void.class, new Class<?>[0], new Object[0]);
    } else {
      context.addCallbackParam("userStoryId", userStory.getUserStoryId());
      context.addCallbackParam("error", EXCEPTION);
    }
  }
  @SuppressWarnings("static-access")
  public void updateIssue() {
    try {
      // String typeFormatPoint = ""; TODO Knight check to use or delete
      // typeFormatPoint = this.issueService.checkingPointFormat(estimatePoint); TODO Knight check
      // to use or delete

      String remainPoint = this.issue.getRemain();
      String estimatePoint = this.issue.getEstimate();
      estimatePoint = this.issueService.checkingEstimatePoint(estimatePoint);
      remainPoint = this.issueService.checkingRemainPoint(remainPoint);

      if ((this.issue.getStatus() != null)
          && (this.issue.getStatus().getType() == Status.StatusType.START)) {
        remainPoint = estimatePoint;
      }

      issueService.updatePointRemain(remainPoint, this.issue.getIssueId());
      this.issue.setRemain(remainPoint);
      this.issue.setEstimate(estimatePoint);
      this.issue.setPointFormat("1"); // alway set default format is DT

      // Remove Control Characters
      this.issue.setSubject(this.issue.getSubject().replaceAll("\\p{Cntrl}", ""));
      this.issue.setNote(this.issue.getNote().replaceAll("\\p{Cntrl}", ""));
      this.issue.setDescription(this.issue.getDescription().trim().replaceAll("\\p{Cntrl}", ""));
      this.issueService.updateIssue(this.issue);
      // check last sprint of team
      Sprint lastSprint = sprintService.findLastSprintByTeamId(teamId);

      if (lastSprint != null
          && lastSprint.getSprintId().compareTo(issue.getSprint().getSprintId()) == 0) {
        // update kanban issue if exist
        kanbanIssueService.updateKanbanIssueByIssueId(issue.getIssueId());
      }

      if (this.issue.getParent() != null) {
        issueService.updateStatusOfIssueParent(this.issue.getParent());
        // check update status us when sprint is valid time
        if (!sprintService.isPastSprint(issue.getSprint())) {
          // Update status userstory
          UserStory userStory = userStoryService.findUserStoryByIssue(this.issue);
          UserStory.StatusType status =
              userStoryService.findStatusOfUserStoryBySprintAndUserStory(userStory, this.sprint);

          if (userStory.getStatus().compareTo(status) != 0) {
            userStory.setStatus(status);
            userStoryService.update(userStory);
            // update kanbanissue status belong to userstory
            kanbanIssueService.updateAllKanbanIssueByUserStoryStatusOfTeam(
                userStory, sprint.getTeam());
          }
        }
      }

      updateRemainPointPerDay(this.issue);

      JSFUtils.addCallBackParam("edited", true);
      JSFUtils.addSuccessMessage("msgs", this.utils.getMessage("myagile.UpdateSuccess", null));
    } catch (Exception e) {
      JSFUtils.addWarningMessage("msgs", this.utils.getMessage("myagile.UpdateUnsuccess", null));
    }
  }