/**
   * Description of the Method
   *
   * @param context Description of the Parameter
   * @return Description of the Return Value
   */
  public String executeCommandView(ActionContext context) {
    if (!(hasPermission(context, "sales-leads-action-plans-view"))) {
      return ("PermissionError");
    }
    Exception errorMessage = null;
    Connection db = null;
    try {
      db = this.getConnection(context);
      User user = this.getUser(context, this.getUserId(context));
      String contactId = context.getRequest().getParameter("contactId");
      if (contactId == null) {
        contactId = context.getRequest().getParameter("contactId");
      }
      boolean popup =
          (context.getRequest().getParameter("popup") != null
              && "true".equals(context.getRequest().getParameter("popup")));
      Contact ContactDetails = new Contact(db, Integer.parseInt(contactId));
      if (!isRecordAccessPermitted(context, ContactDetails)) {
        return ("PermissionError");
      }
      context.getRequest().setAttribute("ContactDetails", ContactDetails);

      PagedListInfo planWorkListInfo =
          this.getPagedListInfo(context, "accountActionPlanWorkListInfo");
      planWorkListInfo.setLink(
          "SalesActionPlans.do?command=View&contactId=" + contactId + (popup ? "&popup=true" : ""));
      if (!planWorkListInfo.hasListFilters()) {
        planWorkListInfo.addFilter(1, "myhierarchy");
        planWorkListInfo.addFilter(2, "true");
      }

      ActionPlanWorkList planWorkList = new ActionPlanWorkList();
      planWorkList.setPagedListInfo(planWorkListInfo);
      planWorkList.setSiteId(ContactDetails.getSiteId());
      if (user.getSiteId() == -1) {
        planWorkList.setIncludeAllSites(true);
      }

      planWorkList.setLinkModuleId(ActionPlan.getMapIdGivenConstantId(db, ActionPlan.LEADS));
      planWorkList.setLinkItemId(contactId);

      if ("my".equals(planWorkListInfo.getFilterValue("listFilter1"))) {
        planWorkList.setOwner(this.getUserId(context));
      } else if ("mymanaged".equals(planWorkListInfo.getFilterValue("listFilter1"))) {
        planWorkList.setManager(this.getUserId(context));
      } else if ("mywaiting".equals(planWorkListInfo.getFilterValue("listFilter1"))) {
        planWorkList.setOwner(this.getUserId(context));
        planWorkList.setManager(this.getUserId(context));
        planWorkList.setCurrentStepOwner(this.getUserId(context));
        planWorkList.setAllMyPlans(true);
      } else if ("myhierarchy".equals(planWorkListInfo.getFilterValue("listFilter1"))) {
        planWorkList.setOwnerRange(this.getUserRange(context));
      }

      if ("all".equals(planWorkListInfo.getFilterValue("listFilter2"))) {
        planWorkList.setEnabled(Constants.UNDEFINED);
      } else if ("true".equals(planWorkListInfo.getFilterValue("listFilter2"))) {
        planWorkList.setEnabled(Constants.TRUE);
      } else if ("false".equals(planWorkListInfo.getFilterValue("listFilter2"))) {
        planWorkList.setEnabled(Constants.FALSE);
      }

      planWorkList.setBuildPhaseWork(true);
      planWorkList.setBuildStepWork(true);
      planWorkList.setBuildLinkedObject(true);
      planWorkList.buildList(db);
      context.getRequest().setAttribute("actionPlanWorkList", planWorkList);
    } catch (Exception e) {
      errorMessage = e;
    } finally {
      this.freeConnection(context, db);
    }
    if (errorMessage == null) {
      return getReturn(context, "View");
    } else {
      context.getRequest().setAttribute("Error", errorMessage);
      return ("SystemError");
    }
  }
  /**
   * Description of the Method
   *
   * @param context Description of the Parameter
   * @return Description of the Return Value
   */
  public String executeCommandInsert(ActionContext context) {
    if (!(hasPermission(context, "sales-leads-action-plans-add"))) {
      return ("PermissionError");
    }
    Connection db = null;
    User assigned = null;
    User manager = null;
    Contact assignedContact = null;
    Contact managerContact = null;
    Contact ContactDetails = null;
    ActionPlanWork planWork = (ActionPlanWork) context.getFormBean();
    String actionPlanIdString = context.getRequest().getParameter("actionPlan");
    if (actionPlanIdString != null
        && !"".equals(actionPlanIdString.trim())
        && !"null".equals(actionPlanIdString.trim())) {
      planWork.setActionPlanId(actionPlanIdString);
    }
    try {
      db = this.getConnection(context);

      String contactId = context.getRequest().getParameter("contactId");

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

      boolean isValid = this.validateObject(context, db, planWork);
      boolean recordStatus = false;
      if (isValid) {
        ActionPlan actionPlan = new ActionPlan();
        actionPlan.setBuildPhases(true);
        actionPlan.setBuildSteps(true);
        actionPlan.setLinkObjectId(ActionPlan.getMapIdGivenConstantId(db, ActionPlan.LEADS));
        actionPlan.queryRecord(db, planWork.getActionPlanId());

        planWork.setLinkModuleId(ActionPlan.getMapIdGivenConstantId(db, ActionPlan.LEADS));
        planWork.setLinkItemId(ContactDetails.getId());
        planWork.setEnteredBy(this.getUserId(context));
        planWork.setModifiedBy(this.getUserId(context));
        planWork.insert(db, actionPlan);
        this.processInsertHook(context, planWork);
        context.getRequest().setAttribute("actionPlanId", String.valueOf(planWork.getId()));
        assigned = this.getUser(context, planWork.getAssignedTo());
        assignedContact = new Contact(db, assigned.getContactId());
        manager = this.getUser(context, planWork.getManagerId());
        managerContact = new Contact(db, manager.getContactId());
      }
    } catch (Exception e) {
      context.getRequest().setAttribute("Error", e);
      e.printStackTrace(System.out);
      return ("SystemError");
    } finally {
      this.freeConnection(context, db);
    }

    // Process Emails
    if (planWork.getId() > -1) {
      try {
        String templateFile =
            getDbNamePath(context) + "templates_" + getUserLanguage(context) + ".xml";
        if (!FileUtils.fileExists(templateFile)) {
          templateFile = getDbNamePath(context) + "templates_en_US.xml";
        }
        // Send an email to the Action Plan "owner" & Action Plan "manager"
        planWork.sendEmail(
            context, assignedContact, managerContact, ContactDetails.getNameLast(), templateFile);
      } catch (Exception e) {
        context.getRequest().setAttribute("Error", e);
        return ("SystemError");
      }
    } else {
      return executeCommandAdd(context);
    }
    return (executeCommandDetails(context));
  }