/**
  * 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));
  }
  /**
   * Description of the Method
   *
   * @param context Description of the Parameter
   * @return Description of the Return Value
   */
  public String executeCommandDetails(ActionContext context) {
    if (!(hasPermission(context, "sales-leads-action-plans-view"))) {
      return ("PermissionError");
    }
    Exception errorMessage = null;
    Connection db = null;
    Contact ContactDetails = null;
    try {
      db = this.getConnection(context);

      String contactId = context.getRequest().getParameter("contactId");
      if (contactId == null) {
        contactId = (String) context.getRequest().getAttribute("contactId");
      }
      ContactDetails = new Contact(db, Integer.parseInt(contactId));
      if (!isRecordAccessPermitted(context, ContactDetails)) {
        return ("PermissionError");
      }
      context.getRequest().setAttribute("ContactDetails", ContactDetails);

      String actionPlanId = context.getRequest().getParameter("actionPlanId");
      if (actionPlanId == null || "".equals(actionPlanId)) {
        actionPlanId = (String) context.getRequest().getAttribute("actionPlanId");
      }
      ActionPlanWork planWork = new ActionPlanWork();
      planWork.setBuildPhaseWork(true);
      planWork.setBuildGlobalPhases(Constants.FALSE);
      planWork.setBuildStepWork(true);
      planWork.setBuildLinkedObject(true);
      planWork.queryRecord(db, Integer.parseInt(actionPlanId));
      planWork.buildStepLinks();
      context.getRequest().setAttribute("actionPlanWork", planWork);

      // Build plan with just the global phases
      ActionPlanWork globalPlanWork = new ActionPlanWork();
      globalPlanWork.setBuildPhaseWork(true);
      globalPlanWork.setBuildGlobalPhases(Constants.TRUE);
      globalPlanWork.setBuildStepWork(true);
      globalPlanWork.setBuildLinkedObject(true);
      globalPlanWork.queryRecord(db, Integer.parseInt(actionPlanId));
      context.getRequest().setAttribute("globalActionPlanWork", globalPlanWork);

      SystemStatus thisSystem = this.getSystemStatus(context);
      LookupList ratingLookup = thisSystem.getLookupList(db, "lookup_contact_rating");
      ratingLookup.addItem(-1, thisSystem.getLabel("calendar.none.4dashes"));
      ratingLookup.setJsEvent("onChange=\"javascript:updateRating(this);\"");
      context.getRequest().setAttribute("ratingLookup", ratingLookup);
      context.getRequest().setAttribute("systemStatus", thisSystem);
      context
          .getRequest()
          .setAttribute(
              "objectName", ActionPlan.getDescriptionGivenConstantId(db, ActionPlan.LEADS));
      context.getRequest().setAttribute("constants", ActionPlan.buildConstants(db));
      String notAttached = context.getRequest().getParameter("notAttached");

      Organization orgDetails = new Organization();
      context.getRequest().setAttribute("orgDetails", orgDetails);

      if (notAttached != null && "true".equals(notAttached.trim())) {
        context
            .getRequest()
            .setAttribute(
                "actionWarning",
                thisSystem.getLabel("", "The recipient was not added to the active campaign"));
      }
    } catch (Exception e) {
      e.printStackTrace();
      context.getRequest().setAttribute("Error", e);
      return ("SystemError");
    } finally {
      this.freeConnection(context, db);
    }
    return getReturn(context, "Details");
  }