/**
  * Description of the Method
  *
  * @param context Description of the Parameter
  * @return Description of the Return Value
  */
 public String executeCommandDelete(ActionContext context) {
   if (!(hasPermission(context, "sales-leads-action-plans-delete"))) {
     return ("PermissionError");
   }
   Connection db = null;
   try {
     db = this.getConnection(context);
     String planWorkId = context.getRequest().getParameter("actionPlanId");
     ActionPlanWork planWork = new ActionPlanWork(db, Integer.parseInt(planWorkId));
     planWork.setBuildLinkedObject(true);
     planWork.buildPhaseWork(db);
     planWork.buildLinkedObject(db);
     planWork.delete(db);
   } catch (Exception e) {
     context.getRequest().setAttribute("Error", e);
     return ("SystemError");
   } finally {
     this.freeConnection(context, db);
   }
   return (executeCommandView(context));
 }
  /**
   * Description of the Method
   *
   * @param context Description of the Parameter
   * @return Description of the Return Value
   */
  public String executeCommandUpdateStatus(ActionContext context) {
    if (!(hasPermission(context, "sales-leads-action-plans-edit"))) {
      return ("PermissionError");
    }
    Connection db = null;
    boolean status = false;
    ActionItemWork itemWork = null;
    ActionPhaseWork phaseWork = null;
    ActionPhaseWork nextPhaseWork = null;
    ActionItemWork nextItem = null;
    ActionPlanWork planWork = null;
    ActionItemWork oldItem = null;
    boolean sendEmailForAllNewSteps = false;
    boolean sendEmailForNextStep = false;
    try {
      db = this.getConnection(context);

      String itemId = context.getRequest().getParameter("stepId");
      String nextStepId = context.getRequest().getParameter("nextStepId");
      String statusId = context.getRequest().getParameter("statusId");

      if (itemId != null && Integer.parseInt(itemId) > 0) {
        itemWork = new ActionItemWork();
        itemWork.setBuildStep(true);
        itemWork.queryRecord(db, Integer.parseInt(itemId));
        phaseWork = new ActionPhaseWork(db, itemWork.getPhaseWorkId());
        planWork = new ActionPlanWork(db, phaseWork.getPlanWorkId());
        planWork.setBuildLinkedObject(true);
        planWork.buildLinkedObject(db);
        planWork.buildPhaseWork(db);
        itemWork.setPlanWork(planWork);
        oldItem = new ActionItemWork();
        oldItem.setPlanWork(planWork);
        oldItem.queryRecord(db, itemWork.getId());
        if (Integer.parseInt(statusId) == ActionPlanWork.COMPLETED) {
          itemWork.checkPreviousSteps(db, planWork);
        }
        if (statusId != null && Integer.parseInt(statusId) > 0) {
          itemWork.setStatusId(statusId);
          itemWork.setModifiedBy(this.getUserId(context));
        }

        if (nextStepId != null && Integer.parseInt(nextStepId) > 0) {
          nextItem = new ActionItemWork();
          nextItem.setBuildStep(true);
          nextItem.queryRecord(db, Integer.parseInt(nextStepId));
          nextItem.setPlanWork(planWork);
          if (nextItem.getPhaseWorkId() != itemWork.getPhaseWorkId()) {
            nextPhaseWork = new ActionPhaseWork(db, nextItem.getPhaseWorkId());
            nextPhaseWork.setPlanWork(planWork);
            nextPhaseWork.setBuildLinkedObject(true);
            nextPhaseWork.buildPhaseObject(db);
            nextPhaseWork.buildStepWork(db);
            if (nextPhaseWork != null
                && nextPhaseWork.getPhase().getRandom()
                && nextPhaseWork.noStepComplete()) {
              sendEmailForAllNewSteps = true;
            }
          }
          status = itemWork.updateStatus(db, nextItem);
          this.processUpdateHook(context, oldItem, itemWork);
          if (!sendEmailForAllNewSteps) {
            if (nextItem != null) {
              this.processUpdateHook(context, itemWork, nextItem);
            }
          }
        } else {
          status = itemWork.updateStatus(db, null);
          phaseWork.setBuildStepWork(true);
          phaseWork.setBuildPhase(true);
          phaseWork.queryRecord(db, itemWork.getPhaseWorkId());
          this.processUpdateHook(context, oldItem, itemWork);
          nextPhaseWork = phaseWork.getNextPhase(db);
          if (phaseWork.getPhase().getRandom()
              && phaseWork.allStepsComplete()
              && nextPhaseWork != null) {
            nextPhaseWork.setPlanWork(planWork);
            nextPhaseWork.setBuildLinkedObject(true);
            nextPhaseWork.buildStepWork(db);
            nextPhaseWork.buildPhaseObject(db);
            if (nextPhaseWork.getPhase().getRandom() && nextPhaseWork.noStepComplete()) {
              // random phase to random phase translation
              sendEmailForAllNewSteps = true;
            } else if (!nextPhaseWork.getPhase().getRandom() && nextPhaseWork.noStepComplete()) {
              // random phase to serial phase translation
              if (nextPhaseWork.getItemWorkList().size() > 0) {
                nextItem = (ActionItemWork) nextPhaseWork.getItemWorkList().get(0);
                nextItem.setPlanWork(planWork);
                nextItem.buildStep(db);
                nextItem.buildLinkedObject(db);
                this.processUpdateHook(context, itemWork, nextItem);
              }
            }
          }
        }
      }
      if (sendEmailForAllNewSteps) {
        if (nextPhaseWork != null && nextPhaseWork.getItemWorkList() != null) {
          Iterator iter = (Iterator) nextPhaseWork.getItemWorkList().iterator();
          while (iter.hasNext()) {
            nextItem = (ActionItemWork) iter.next();
            nextItem.setPlanWork(planWork);
            nextItem = new ActionItemWork(db, nextItem.getId());
            nextItem.buildStep(db);
            nextItem.buildLinkedObject(db);
            this.processUpdateHook(context, itemWork, nextItem);
          }
        }
      }
      String returnUrl = context.getRequest().getParameter("returnUrl");
      context.getRequest().setAttribute("refreshUrl", returnUrl);
    } catch (Exception e) {
      e.printStackTrace();
      context.getRequest().setAttribute("Error", e);
      return ("SystemError");
    } finally {
      this.freeConnection(context, db);
    }
    if ("true".equals(context.getRequest().getParameter("popup"))) {
      return "UpdateStatusOK";
    }
    return (executeCommandDetails(context));
  }