/**
   * Show Dependencies if any before deleting Survey
   *
   * @param context Description of the Parameter
   * @return Description of the Return Value
   */
  public String executeCommandConfirmDelete(ActionContext context) {
    Exception errorMessage = null;
    Connection db = null;
    Survey thisSurvey = null;

    HtmlDialog htmlDialog = new HtmlDialog();
    String id = null;

    if (!(hasPermission(context, "campaign-campaigns-surveys-delete"))) {
      return ("PermissionError");
    }

    if (context.getRequest().getParameter("id") != null) {
      id = context.getRequest().getParameter("id");
    }

    try {
      db = this.getConnection(context);
      SystemStatus systemStatus = this.getSystemStatus(context);
      thisSurvey = new Survey(db, id);
      if (!hasAuthority(context, thisSurvey.getEnteredBy())) {
        return ("PermissionError");
      }

      DependencyList dependencies = thisSurvey.processDependencies(db);
      dependencies.setSystemStatus(systemStatus);
      htmlDialog.addMessage(
          systemStatus.getLabel("confirmdelete.caution") + "\n" + dependencies.getHtmlString());
      htmlDialog.setTitle(systemStatus.getLabel("confirmdelete.title"));

      if (dependencies.size() == 0) {
        htmlDialog.setShowAndConfirm(false);
        htmlDialog.setDeleteUrl(
            "javascript:window.location.href='CampaignManagerSurvey.do?command=Delete&id="
                + id
                + "'");
      } else {
        htmlDialog.setHeader(systemStatus.getLabel("confirmdelete.surveyCampaignHeader"));
        htmlDialog.addButton(
            systemStatus.getLabel("button.ok"), "javascript:parent.window.close()");
      }

    } catch (Exception e) {
      errorMessage = e;
    } finally {
      this.freeConnection(context, db);
    }
    if (errorMessage == null) {
      context.getSession().setAttribute("Dialog", htmlDialog);
      return ("ConfirmDeleteOK");
    } 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 executeCommandModify(ActionContext context) {
   if (!(hasPermission(context, "tickets-tickets-tasks-edit"))) {
     return ("PermissionError");
   }
   Connection db = null;
   Task thisTask = null;
   String forward = context.getRequest().getParameter("forward");
   if (forward != null && !"".equals(forward.trim())) {
     context.getRequest().setAttribute("forward", forward);
   }
   String id = context.getRequest().getParameter("id");
   addModuleBean(context, "View Tickets", "Add Ticket");
   SystemStatus systemStatus = this.getSystemStatus(context);
   String ticketId = context.getRequest().getParameter("ticketId");
   Ticket thisTicket = null;
   try {
     db = this.getConnection(context);
     thisTask = (Task) context.getFormBean();
     if (thisTask.getId() == -1) {
       thisTask = new Task(db, Integer.parseInt(id));
     }
     // Load the ticket
     if (ticketId != null && !"".equals(ticketId)) {
       thisTicket = new Ticket(db, Integer.parseInt(ticketId));
     } else {
       thisTicket = new Ticket(db, thisTask.getTicketId());
     }
     // Check access permission to organization record
     if (!isRecordAccessPermitted(context, db, thisTicket.getOrgId())) {
       return ("PermissionError");
     }
     thisTask.checkEnabledOwnerAccount(db);
     if (thisTask.getContactId() > -1) {
       thisTask.checkEnabledLinkAccount(db);
       Contact contact = new Contact(db, thisTask.getContactId());
       thisTask.setContactName(contact.getNameFull());
     }
     LookupList list = systemStatus.getLookupList(db, "lookup_ticket_task_category");
     list.addItem(-1, systemStatus.getLabel("calendar.none.4dashes", "-- None --"));
     context.getRequest().setAttribute("ticketTaskCategoryList", list);
   } catch (Exception e) {
     context.getRequest().setAttribute("Error", e);
     return ("SystemError");
   } finally {
     this.freeConnection(context, db);
   }
   context.getRequest().setAttribute("Task", thisTask);
   if (!hasAuthority(context, thisTask.getOwner())
       || !(hasPermission(context, "tickets-tickets-tasks-edit"))) {
     if (hasPermission(context, "tickets-tickets-tasks-view")) {
       return "TaskDetailsOK";
     }
     return ("PermissionError");
   }
   return "AddTaskOK";
 }
 /**
  * Description of the Method
  *
  * @param context Description of the Parameter
  * @return Description of the Return Value
  */
 public String executeCommandConfirmDelete(ActionContext context) {
   if (!(hasPermission(context, "tickets-tickets-tasks-delete"))) {
     return ("PermissionError");
   }
   Connection db = null;
   Task thisTask = null;
   HtmlDialog htmlDialog = new HtmlDialog();
   String id = context.getRequest().getParameter("id");
   String sourcePopup = context.getRequest().getParameter("sourcePopup");
   try {
     db = this.getConnection(context);
     SystemStatus systemStatus = this.getSystemStatus(context);
     thisTask = new Task(db, Integer.parseInt(id));
     htmlDialog.setTitle(systemStatus.getLabel("confirmdelete.title"));
     if (!hasAuthority(context, thisTask.getOwner())) {
       htmlDialog.setHeader(systemStatus.getLabel("confirmdelete.taskNotOwnerHeader"));
       htmlDialog.addButton(
           systemStatus.getLabel("button.ok"), "javascript:parent.window.close()");
     } else {
       DependencyList dependencies = thisTask.processDependencies(db);
       htmlDialog.addMessage(
           systemStatus.getLabel("confirmdelete.caution") + "\n" + dependencies.getHtmlString());
       if (dependencies.size() == 0) {
         htmlDialog.setShowAndConfirm(false);
         htmlDialog.setDeleteUrl(
             "javascript:window.location.href='TroubleTicketTasks.do?command=Delete&id="
                 + id
                 + "&sourcePopup="
                 + sourcePopup
                 + "'");
       } else {
         htmlDialog.setHeader(systemStatus.getLabel("confirmdelete.header"));
         htmlDialog.addButton(
             systemStatus.getLabel("button.delete"),
             "javascript:window.location.href='TroubleTicketTasks.do?command=Delete&id="
                 + id
                 + "&sourcePopup="
                 + sourcePopup
                 + "'");
         htmlDialog.addButton(
             systemStatus.getLabel("button.cancel"), "javascript:parent.window.close()");
       }
     }
   } catch (Exception e) {
     context.getRequest().setAttribute("Error", e);
     return ("SystemError");
   } finally {
     this.freeConnection(context, db);
   }
   context.getSession().setAttribute("Dialog", htmlDialog);
   return ("ConfirmDeleteOK");
 }
  /**
   * Description of the Method
   *
   * @param context Description of the Parameter
   * @return Description of the Return Value
   */
  public String executeCommandDelete(ActionContext context) {
    if (!(hasPermission(context, "campaign-campaigns-surveys-delete"))) {
      return ("PermissionError");
    }

    Exception errorMessage = null;
    boolean recordDeleted = false;
    Survey thisSurvey = null;
    Connection db = null;
    SystemStatus systemStatus = this.getSystemStatus(context);
    try {
      db = this.getConnection(context);
      thisSurvey = new Survey(db, Integer.parseInt(context.getRequest().getParameter("id")));
      if (!hasAuthority(context, thisSurvey.getEnteredBy())) {
        return ("PermissionError");
      }
      recordDeleted = thisSurvey.delete(db);
      if (!recordDeleted) {
        HashMap map = new HashMap();
        map.put("${thisSurvey.inactiveCount}", "" + thisSurvey.getInactiveCount());
        map.put(
            "${thisSurvey.campaign}",
            (thisSurvey.getInactiveCount() == 1 ? "campaign is" : "campaigns are"));
        map.put("${thisSurvey.use}", (thisSurvey.getInactiveCount() == 1 ? "uses" : "use"));
        Template template =
            new Template(systemStatus.getLabel("object.validation.actionError.canNotDeleteSurvey"));
        template.setParseElements(map);
        thisSurvey.getErrors().put("actionError", template.getParsedText());
      }
    } catch (Exception e) {
      errorMessage = e;
    } finally {
      this.freeConnection(context, db);
    }

    if (errorMessage == null) {
      if (recordDeleted) {
        context.getRequest().setAttribute("refreshUrl", "CampaignManagerSurvey.do?command=View");
        return ("DeleteOK");
      } else {
        processErrors(context, thisSurvey.getErrors());
        return (executeCommandView(context));
      }
    } 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 executeCommandAdd(ActionContext context) {
    if (!hasPermission(context, "tickets-tickets-tasks-add")) {
      return ("PermissionError");
    }
    Connection db = null;

    try {
      db = this.getConnection(context);
      // Load the ticket
      Ticket thisTicket =
          new Ticket(db, Integer.parseInt(context.getRequest().getParameter("ticketId")));
      // Check access permission to organization record
      if (!isRecordAccessPermitted(context, db, thisTicket.getOrgId())) {
        return ("PermissionError");
      }

      TicketTask thisTicketTask = (TicketTask) context.getFormBean();
      if (thisTicketTask.getEnteredBy() == -1) {
        thisTicketTask = new TicketTask();
        thisTicketTask.setTicketId(thisTicket.getId());
      }
      Task thisTask = thisTicketTask;
      context.getRequest().setAttribute("Task", thisTask);

      SystemStatus systemStatus = this.getSystemStatus(context);
      LookupList list = systemStatus.getLookupList(db, "lookup_ticket_task_category");
      list.addItem(-1, systemStatus.getLabel("calendar.none.4dashes", "-- None --"));
      context.getRequest().setAttribute("ticketTaskCategoryList", list);
    } catch (Exception e) {
      context.getRequest().setAttribute("Error", e);
      return ("SystemError");
    } finally {
      this.freeConnection(context, db);
    }

    addModuleBean(context, "View Tickets", "Add Ticket");
    return ("AddTaskOK");
  }
  /**
   * Description of the Method
   *
   * @param context Description of the Parameter
   * @return Description of the Return Value
   */
  public String executeCommandSave(ActionContext context) {
    String permission = "tickets-tickets-tasks-edit";
    Connection db = null;
    int resultCount = -1;
    boolean recordInserted = false;
    boolean isValid = false;
    String addAnother = (String) context.getRequest().getParameter("addAnother");
    if (addAnother != null && !"".equals(addAnother.trim())) {
      context.getRequest().setAttribute("addAnother", addAnother);
    }
    String forward = context.getRequest().getParameter("forward");
    if (forward != null && !"".equals(forward.trim())) {
      context.getRequest().setAttribute("forward", forward);
    }
    String ticketId = context.getRequest().getParameter("ticketId");
    TicketTask thisTask = (TicketTask) context.getFormBean();
    SystemStatus systemStatus = this.getSystemStatus(context);
    String action = (thisTask.getId() > 0 ? "modify" : "insert");
    if ("insert".equals(action)) {
      permission = "tickets-tickets-tasks-add";
    }
    if (!hasPermission(context, permission)) {
      return ("PermissionError");
    }
    thisTask.setModifiedBy(getUserId(context));
    try {
      db = this.getConnection(context);
      if ("insert".equals(action)) {
        // Load the ticket
        Ticket thisTicket = new Ticket(db, Integer.parseInt(ticketId));
        // Check access permission to organization record
        if (!isRecordAccessPermitted(context, db, thisTicket.getOrgId())) {
          return ("PermissionError");
        }
        if (forward != null || addAnother != null) {
          LookupList list = systemStatus.getLookupList(db, "lookup_ticket_task_category");
          list.addItem(-1, systemStatus.getLabel("calendar.none.4dashes", "-- None --"));
          context.getRequest().setAttribute("ticketTaskCategoryList", list);
        }
        thisTask.setEnteredBy(getUserId(context));
        thisTask.setTicketId(Integer.parseInt(ticketId));
        isValid = this.validateObject(context, db, thisTask);
        if (isValid) {
          recordInserted = thisTask.insert(db);
        }
        if (recordInserted) {
          this.processInsertHook(context, thisTask);
        }
      } else {
        Task oldTask = new Task(db, thisTask.getId());
        if (!hasAuthority(context, oldTask.getOwner())) {
          return ("PermissionError");
        }
        isValid = this.validateObject(context, db, thisTask);
        if (isValid) {
          resultCount = thisTask.update(db);
        }
        if (resultCount == 1) {
          this.processUpdateHook(context, oldTask, thisTask);
        }
      }
      if (!isValid || !(recordInserted || resultCount == 1)) {
        if (thisTask.getContactId() > -1) {
          thisTask.checkEnabledLinkAccount(db);
          Contact contact = new Contact(db, thisTask.getContactId());
          thisTask.setContactName(contact.getNameFull());
          context.getRequest().setAttribute("Task", thisTask);
        }
      }
    } catch (Exception e) {
      context.getRequest().setAttribute("Error", e);
      return ("SystemError");
    } finally {
      this.freeConnection(context, db);
    }
    if (recordInserted || resultCount == 1) {
      if ((resultCount == 1 || recordInserted)
          && forward != null
          && "details".equals(forward.trim())) {
        context.getRequest().setAttribute("id", String.valueOf(thisTask.getId()));
        context.getRequest().setAttribute("forward", forward);
        return executeCommandDetails(context);
      }
      if (recordInserted && addAnother != null && "true".equals(addAnother.trim())) {
        Task task = new Task();
        task.setTicketId(ticketId);
        context.getRequest().setAttribute("Task", task);
        return "AddTaskOK";
      }

      addModuleBean(context, "View Tickets", "Ticket Save OK");
      return ("SaveOK");
    }
    if ("insert".equals(action)) {
      return executeCommandAdd(context);
    } else {
      return executeCommandModify(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");
  }