/**
   * Description of the Method
   *
   * @param context Description of the Parameter
   * @return Description of the Return Value
   */
  public String executeCommandAdd(ActionContext context) {
    if (!(hasPermission(context, "sales-leads-action-plans-add"))) {
      return ("PermissionError");
    }
    ActionPlanWork planWork = (ActionPlanWork) context.getFormBean();
    Connection db = null;
    try {
      db = this.getConnection(context);

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

      // prepare a list of action plans
      ActionPlanList actionPlanList = new ActionPlanList();
      actionPlanList.setIncludeOnlyApproved(Constants.TRUE);
      actionPlanList.setLinkObjectId(ActionPlan.getMapIdGivenConstantId(db, ActionPlan.LEADS));
      actionPlanList.setEnabled(Constants.TRUE);
      actionPlanList.setSiteId(ContactDetails.getSiteId());
      actionPlanList.buildList(db);
      context.getRequest().setAttribute("actionPlanList", actionPlanList);
      context.getRequest().setAttribute("actionPlanWork", planWork);
    } catch (Exception e) {
      e.printStackTrace();
      context.getRequest().setAttribute("Error", e);
      return ("SystemError");
    } finally {
      this.freeConnection(context, db);
    }
    return getReturn(context, "Add");
  }
  /**
   * 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 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");
  }
  /**
   * 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));
  }