/**
  * Description of the Method
  *
  * @param context Description of the Parameter
  * @return Description of the Return Value
  */
 public String executeCommandAdd(ActionContext context) {
   Exception errorMessage = null;
   // Load project
   Connection db = null;
   try {
     FileFolder thisFolder = (FileFolder) context.getFormBean();
     thisFolder.setParentId(context.getRequest().getParameter("parentId"));
     db = getConnection(context);
     // Load the ticket and the organization
     Ticket thisTicket = addTicket(context, db);
     int ticketId = thisTicket.getId();
     // Check access permission to organization record
     if (!isRecordAccessPermitted(context, db, thisTicket.getOrgId())) {
       return ("PermissionError");
     }
     // Build array of folder trails
     ProjectManagementFileFolders.buildHierarchy(db, context);
     context.getRequest().setAttribute("fileFolder", thisFolder);
   } catch (Exception e) {
     errorMessage = e;
   } finally {
     this.freeConnection(context, db);
   }
   if (errorMessage == null) {
     return ("AddOK");
   } else {
     context.getRequest().setAttribute("Error", errorMessage);
     return ("SystemError");
   }
 }
 /**
  * Insert or update a Survey
  *
  * @param context Description of the Parameter
  * @return Description of the Return Value
  */
 public String executeCommandInsert(ActionContext context) {
   if (!(hasPermission(context, "campaign-campaigns-surveys-add")
       || hasPermission(context, "campaign-campaigns-surveys-edit"))) {
     return ("PermissionError");
   }
   boolean recordInserted = false;
   int recordsModified = -1;
   boolean isValid = false;
   Connection db = null;
   try {
     Survey newSurvey = (Survey) context.getFormBean();
     int surveyId = -1;
     if (context.getRequest().getParameter("id") != null
         && !(context.getRequest().getParameter("id").equals(""))) {
       surveyId = Integer.parseInt(context.getRequest().getParameter("id"));
     }
     if (surveyId == -1) {
       newSurvey.setEnteredBy(getUserId(context));
     }
     newSurvey.setModifiedBy(getUserId(context));
     newSurvey.setRequestItems(context.getRequest());
     db = this.getConnection(context);
     isValid = this.validateObject(context, db, newSurvey);
     Iterator iter = (Iterator) newSurvey.getQuestions().iterator();
     while (iter.hasNext()) {
       SurveyQuestion question = (SurveyQuestion) iter.next();
       isValid = this.validateObject(context, db, question) && isValid;
     }
     if (isValid) {
       if (surveyId == -1) {
         recordInserted = newSurvey.insert(db);
       } else {
         recordsModified = newSurvey.update(db);
       }
     }
     if ((recordInserted || recordsModified > 0) && isValid) {
       context.getRequest().setAttribute("SurveyDetails", newSurvey);
     } else {
       if (System.getProperty("DEBUG") != null) {
         System.out.println("CampaignManagerSurvey-> Insert Errors " + newSurvey.getErrors());
       }
       processErrors(context, newSurvey.getErrors());
     }
     context.getRequest().setAttribute("systemStatus", this.getSystemStatus(context));
   } catch (Exception errorMessage) {
     errorMessage.printStackTrace();
     context.getRequest().setAttribute("Error", errorMessage);
     return ("SystemError");
   } finally {
     this.freeConnection(context, db);
   }
   if ((recordInserted || recordsModified > 0) && isValid) {
     return ("InsertOK");
   } else if (recordsModified == 0 && isValid) {
     context.getRequest().setAttribute("Error", NOT_UPDATED_MESSAGE);
     return ("UserError");
   } else {
     return (executeCommandAdd(context));
   }
 }
 /**
  * Description of the Method
  *
  * @param context Description of the Parameter
  * @return Description of the Return Value
  */
 public String executeCommandModify(ActionContext context) {
   Exception errorMessage = null;
   boolean recordDeleted = false;
   // TODO: Add some permissions to get here!
   // Modify the itemId, and the folderId will be the location to return to
   String itemId = (String) context.getRequest().getParameter("id");
   String folderId = (String) context.getRequest().getParameter("folderId");
   Connection db = null;
   try {
     db = getConnection(context);
     // Load the ticket and the organization
     Ticket thisTicket = addTicket(context, db);
     int ticketId = thisTicket.getId();
     // Check access permission to organization record
     if (!isRecordAccessPermitted(context, db, thisTicket.getOrgId())) {
       return ("PermissionError");
     }
     // Load the file folder to be modified
     FileFolder thisFolder = (FileFolder) context.getFormBean();
     thisFolder.setId(Integer.parseInt(itemId));
     thisFolder.queryRecord(db, Integer.parseInt(itemId));
   } catch (Exception e) {
     errorMessage = e;
   } finally {
     this.freeConnection(context, db);
   }
   if (errorMessage == null) {
     return (executeCommandAdd(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 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 executeCommandDefault(ActionContext context) {
   if (getUserId(context) < 0) {
     return "PermissionError";
   }
   // setMaximized(context);
   IteamSearchBean search = (IteamSearchBean) context.getFormBean();
   PagedListInfo searchBeanInfo = this.getPagedListInfo(context, "searchBeanInfo");
   searchBeanInfo.setLink("ProjectManagementSearch.do?command=Default");
   Connection db = null;
   try {
     search.parseQuery();
     if (!search.isValid()) {
       return "SearchResultsERROR";
     }
     // Get the shared searcher
     IndexSearcher searcher = SearchUtils.getSharedSearcher(context, this.getDirectory(context));
     db = getConnection(context);
     String queryString = null;
     if (search.getScope() != SearchBean.THIS) {
       search.setProjectId(-1);
     }
     // Check for project access and get acceptable query string
     if (search.getProjectId() > -1) {
       Project thisProject = loadProject(db, search.getProjectId(), context);
       context.getRequest().setAttribute("Project", thisProject);
       queryString =
           "("
               + IteamSearchQuery.buildIteamSearchQuery(
                   search, db, getUserId(context), search.getProjectId())
               + ") AND ("
               + search.getParsedQuery()
               + ")";
     } else {
       queryString =
           "("
               + IteamSearchQuery.buildIteamSearchQuery(search, db, getUserId(context), -1)
               + ") AND ("
               + search.getParsedQuery()
               + ")";
     }
     // Execute the query and build search results
     SearchUtils.buildSearchResults(context, queryString, searcher, searchBeanInfo);
   } catch (Exception e) {
     context.getRequest().setAttribute("Error", e);
     return "SearchResultsERROR";
   } finally {
     freeConnection(context, db);
   }
   return "SearchResultsOK";
 }
 /**
  * Description of the Method
  *
  * @param context Description of the Parameter
  * @return Description of the Return Value
  */
 public String executeCommandSave(ActionContext context) {
   Connection db = null;
   int resultCount = 0;
   boolean recordInserted = false;
   boolean isValid = false;
   try {
     db = this.getConnection(context);
     // Load the ticket and the organization
     Ticket thisTicket = addTicket(context, db);
     int ticketId = thisTicket.getId();
     // Check access permission to organization record
     if (!isRecordAccessPermitted(context, db, thisTicket.getOrgId())) {
       return ("PermissionError");
     }
     // Insert or update the folder
     FileFolder thisFolder = (FileFolder) context.getFormBean();
     boolean newFolder = (thisFolder.getId() == -1);
     if (newFolder) {
       thisFolder.setEnteredBy(getUserId(context));
     }
     thisFolder.setModifiedBy(getUserId(context));
     thisFolder.setLinkModuleId(Constants.DOCUMENTS_TICKETS);
     thisFolder.setLinkItemId(ticketId);
     if (newFolder) {
       isValid = this.validateObject(context, db, thisFolder);
       if (isValid) {
         recordInserted = thisFolder.insert(db);
       }
     } else {
       isValid = this.validateObject(context, db, thisFolder);
       if (isValid) {
         resultCount = thisFolder.update(db);
       }
     }
     // Build array of folder trails
     ProjectManagementFileFolders.buildHierarchy(db, context);
   } catch (Exception e) {
     context.getRequest().setAttribute("Error", e);
     return ("SystemError");
   } finally {
     this.freeConnection(context, db);
   }
   if (recordInserted) {
     return ("InsertOK");
   } else if (resultCount == 1) {
     return ("UpdateOK");
   }
   return (executeCommandAdd(context));
 }
  /**
   * Description of the Method
   *
   * @param context Description of the Parameter
   * @return Description of the Return Value
   */
  public String executeCommandAdd(ActionContext context) {
    if (!(hasPermission(context, "campaign-campaigns-surveys-add")
        || hasPermission(context, "campaign-campaigns-surveys-edit"))) {
      return ("PermissionError");
    }
    try {
      int pg = 0;
      int updateResult = 0;
      Exception errorMessage = null;
      Connection db = null;
      if (context.getRequest().getParameter("pg") != null) {
        pg = Integer.parseInt(context.getRequest().getParameter("pg"));
      }

      try {
        db = this.getConnection(context);
        CustomForm thisForm = getDynamicForm(context, "survey");
        thisForm.setSelectedTabId(pg);
        Survey thisSurvey = (Survey) context.getFormBean();
        thisSurvey.setRequestItems(context.getRequest());
        thisForm.setContext(context);
        updateResult = thisForm.populate(db, thisSurvey);
        context.getRequest().setAttribute("Survey", thisSurvey);
        context.getRequest().setAttribute("CustomFormInfo", thisForm);
        context.getRequest().setAttribute("systemStatus", this.getSystemStatus(context));
      } catch (Exception e) {
        errorMessage = e;
      } finally {
        this.freeConnection(context, db);
      }
      String submenu = context.getRequest().getParameter("submenu");
      if (submenu == null) {
        submenu = (String) context.getRequest().getAttribute("submenu");
      }
      if (submenu == null) {
        submenu = "ManageSurveys";
      }

      context.getRequest().setAttribute("submenu", submenu);
      addModuleBean(context, submenu, "Add Survey");

      return ("AddOK");
    } catch (Exception errorMessage) {
      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, "admin-sysconfig-customlistviews-add")) {
      return ("PermissionError");
    }
    addModuleBean(context, "Configuration", "Custom List Views");
    Connection db = null;
    CustomListView newCustomView = null;
    boolean isValid = true;
    boolean recordInserted = false;
    CustomListView customView = (CustomListView) context.getFormBean();

    String moduleId = context.getRequest().getParameter("moduleId");
    try {
      db = getConnection(context);
      // build the module details
      PermissionCategory permCat = new PermissionCategory(db, Integer.parseInt(moduleId));
      context.getRequest().setAttribute("permissionCategory", permCat);

      isValid = this.validateObject(context, db, customView);
      if (isValid) {
        recordInserted = customView.insert(db);
      }

      if (recordInserted) {
        newCustomView = new CustomListView(db, customView.getId());
        context.getRequest().setAttribute("customListView", newCustomView);
      }
    } catch (Exception e) {
      e.printStackTrace(System.out);
      context.getRequest().setAttribute("Error", e);
      return ("SystemError");
    } finally {
      this.freeConnection(context, db);
    }
    if (recordInserted && isValid) {
      return ("InsertOK");
    }
    return (executeCommandAdd(context));
  }
  /**
   * 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 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 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 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));
  }