protected void processGetRequest(HttpServletRequest request, HttpServletResponse response) {

    if ((request == null) || (response == null)) {
      return;
    }

    response.setContentType("text/html");
    PrintWriter out = null;

    String name = request.getParameter("Name");
    boolean excludeNavbar =
        StringUtilities.isStringValueBooleanTrue(request.getParameter("ExcludeNavbar"));
    String notificationGroup_AlertAssociations =
        getNotificationGroup_AlertAssociations(name, excludeNavbar);

    try {
      StringBuilder htmlBuilder = new StringBuilder();

      StatsAggHtmlFramework statsAggHtmlFramework = new StatsAggHtmlFramework();
      String htmlHeader = statsAggHtmlFramework.createHtmlHeader("StatsAgg - " + PAGE_NAME, "");

      String htmlBody =
          statsAggHtmlFramework.createHtmlBody(
              "<div id=\"page-content-wrapper\">\n"
                  + "<!-- Keep all page content within the page-content inset div! -->\n"
                  + "  <div class=\"page-content inset statsagg_page_content_font\">\n"
                  + "    <div class=\"content-header\"> \n"
                  + "      <div class=\"pull-left content-header-h2-min-width-statsagg\"> <h2> "
                  + PAGE_NAME
                  + " </h2> </div>\n"
                  + "    </div> "
                  + "    <div class=\"statsagg_force_word_wrap\">"
                  + notificationGroup_AlertAssociations
                  + "    </div>\n"
                  + "  </div>\n"
                  + "</div>\n",
              excludeNavbar);

      htmlBuilder
          .append("<!DOCTYPE html>\n<html>\n")
          .append(htmlHeader)
          .append(htmlBody)
          .append("</html>");

      Document htmlDocument = Jsoup.parse(htmlBuilder.toString());
      String htmlFormatted = htmlDocument.toString();
      out = response.getWriter();
      out.println(htmlFormatted);
    } catch (Exception e) {
      logger.error(e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e));
    } finally {
      if (out != null) {
        out.close();
      }
    }
  }
  protected void processPostRequest(HttpServletRequest request, HttpServletResponse response) {

    if ((request == null) || (response == null)) {
      return;
    }

    String operation = request.getParameter("Operation");

    if ((operation != null) && operation.equals("Clone")) {
      String name = request.getParameter("Name");
      cloneNotificationGroup(name);
    }

    if ((operation != null) && operation.equals("Test")) {
      String name = request.getParameter("Name");
      testNotificationGroup(name);
    }

    if ((operation != null) && operation.equals("Remove")) {
      String name = Common.getObjectParameter(request, "Name");
      removeNotificationGroup(name);
    }

    StatsAggHtmlFramework.redirectAndGet(response, 303, "NotificationGroups");
  }
  private void cloneNotificationGroup(String notificationGroupName) {

    if (notificationGroupName == null) {
      return;
    }

    try {
      NotificationGroupsDao notificationGroupsDao = new NotificationGroupsDao(false);
      NotificationGroup notificationGroup =
          notificationGroupsDao.getNotificationGroupByName(notificationGroupName);
      List<NotificationGroup> allNotificationGroups =
          notificationGroupsDao.getAllDatabaseObjectsInTable();
      notificationGroupsDao.close();

      if ((notificationGroup != null) && (notificationGroup.getName() != null)) {
        Set<String> allNotificationGroupNames = new HashSet<>();
        for (NotificationGroup currentNotificationGroup : allNotificationGroups) {
          if (currentNotificationGroup.getName() != null)
            allNotificationGroupNames.add(currentNotificationGroup.getName());
        }

        NotificationGroup clonedNotificationGroup = NotificationGroup.copy(notificationGroup);
        clonedNotificationGroup.setId(-1);
        String clonedAlterName =
            StatsAggHtmlFramework.createCloneName(
                notificationGroup.getName(), allNotificationGroupNames);
        clonedNotificationGroup.setName(clonedAlterName);
        clonedNotificationGroup.setUppercaseName(clonedAlterName.toUpperCase());

        NotificationGroupsLogic notificationGroupsLogic = new NotificationGroupsLogic();
        notificationGroupsLogic.alterRecordInDatabase(clonedNotificationGroup);
      }
    } catch (Exception e) {
      logger.error(e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e));
    }
  }
  private String buildNotificationGroupsHtml() {

    StringBuilder html = new StringBuilder();

    StatsAggHtmlFramework statsAggHtmlFramework = new StatsAggHtmlFramework();
    String htmlHeader = statsAggHtmlFramework.createHtmlHeader("StatsAgg - " + PAGE_NAME, "");

    StringBuilder htmlBodyStringBuilder = new StringBuilder();
    htmlBodyStringBuilder.append(
        "<div id=\"page-content-wrapper\">\n"
            + "<!-- Keep all page content within the page-content inset div! -->\n"
            + "<div class=\"page-content inset statsagg_page_content_font\">\n"
            + "  <div class=\"content-header\"> \n"
            + "    <div class=\"pull-left content-header-h2-min-width-statsagg\"> <h2> "
            + PAGE_NAME
            + " </h2> </div>\n"
            + "    <div class=\"pull-right \">\n"
            + "     <a href=\"CreateNotificationGroup\" class=\"btn btn-primary statsagg_page_content_font\">Create New Notification Group <i class=\"fa fa-long-arrow-right\"></i></a> \n"
            + "    </div>"
            + "  </div>"
            + "  <table id=\"NotificationGroupsTable\" style=\"display:none\" class=\"table table-bordered table-hover \">\n"
            + "    <thead>\n"
            + "      <tr>\n"
            + "        <th>Notification Group Name</th>\n"
            + "        <th>Email addresses</th>\n"
            + "        <th>Operations</th>\n"
            + "      </tr>\n"
            + "    </thead>\n"
            + "    <tbody>\n");

    AlertsDao alertsDao = new AlertsDao();
    Set<Integer> notificationGroupIdsAssociatedWithAlerts =
        alertsDao.getAllDistinctNotificationGroupIds();
    NotificationGroupsDao notificationGroupsDao = new NotificationGroupsDao();
    List<NotificationGroup> notificationGroups =
        notificationGroupsDao.getAllDatabaseObjectsInTable();

    for (NotificationGroup notificationGroup : notificationGroups) {

      String notificationGroupDetails =
          "<a href=\"NotificationGroupDetails?Name="
              + StatsAggHtmlFramework.urlEncode(notificationGroup.getName())
              + "\">"
              + StatsAggHtmlFramework.htmlEncode(notificationGroup.getName())
              + "</a>";

      StringBuilder emailAddressesOutput = new StringBuilder();
      String[] emailAddresses = StringUtils.split(notificationGroup.getEmailAddresses(), ",");
      if ((emailAddresses != null) && (emailAddresses.length != 0)) {
        for (int i = 0; i < emailAddresses.length; i++) {
          String trimmedEmailAddress = emailAddresses[i].trim();
          emailAddressesOutput.append(trimmedEmailAddress);
          if ((i + 1) != emailAddresses.length) emailAddressesOutput.append(", ");
        }
      }

      String alter =
          "<a href=\"CreateNotificationGroup?Operation=Alter&amp;Name="
              + StatsAggHtmlFramework.urlEncode(notificationGroup.getName())
              + "\">alter</a>";

      List<KeyValue> cloneKeysAndValues = new ArrayList<>();
      cloneKeysAndValues.add(new KeyValue("Operation", "Clone"));
      cloneKeysAndValues.add(
          new KeyValue("Name", Encode.forHtmlAttribute(notificationGroup.getName())));
      String clone =
          StatsAggHtmlFramework.buildJavaScriptPostLink(
              "Clone_" + notificationGroup.getName(),
              "NotificationGroups",
              "clone",
              cloneKeysAndValues);

      List<KeyValue> testKeysAndValues = new ArrayList<>();
      testKeysAndValues.add(new KeyValue("Operation", "Test"));
      testKeysAndValues.add(
          new KeyValue("Name", Encode.forHtmlAttribute(notificationGroup.getName())));
      String test =
          StatsAggHtmlFramework.buildJavaScriptPostLink(
              "Test_" + notificationGroup.getName(),
              "NotificationGroups",
              "test",
              testKeysAndValues,
              true,
              "Are you sure you want to send a test email alert to \\'"
                  + Encode.forJavaScript(notificationGroup.getName())
                  + "\\'?");

      List<KeyValue> removeKeysAndValues = new ArrayList<>();
      removeKeysAndValues.add(new KeyValue("Operation", "Remove"));
      removeKeysAndValues.add(
          new KeyValue("Name", Encode.forHtmlAttribute(notificationGroup.getName())));
      String remove =
          StatsAggHtmlFramework.buildJavaScriptPostLink(
              "Remove_" + notificationGroup.getName(),
              "NotificationGroups",
              "remove",
              removeKeysAndValues,
              true,
              "Are you sure you want to remove this notification group?");

      htmlBodyStringBuilder
          .append("<tr>\n")
          .append("<td class=\"statsagg_force_word_break\">")
          .append(notificationGroupDetails)
          .append("</td>\n")
          .append("<td class=\"statsagg_force_word_break\">")
          .append(StatsAggHtmlFramework.htmlEncode(emailAddressesOutput.toString()))
          .append("</td>\n")
          .append("<td>")
          .append(alter)
          .append(", ")
          .append(clone)
          .append(", ")
          .append(test);

      if (notificationGroupIdsAssociatedWithAlerts == null)
        htmlBodyStringBuilder.append(", ").append(remove);
      else if (!notificationGroupIdsAssociatedWithAlerts.contains(notificationGroup.getId()))
        htmlBodyStringBuilder.append(", ").append(remove);

      htmlBodyStringBuilder.append("</td>\n").append("</tr>\n");
    }

    htmlBodyStringBuilder.append(
        ""
            + "</tbody>\n"
            + "<tfoot> \n"
            + "  <tr>\n"
            + "    <th></th>\n"
            + "    <th></th>\n"
            + "    <th></th>\n"
            + "  </tr>\n"
            + "</tfoot>"
            + "</table>\n"
            + "</div>\n"
            + "</div>\n");

    String htmlBody = (statsAggHtmlFramework.createHtmlBody(htmlBodyStringBuilder.toString()));

    html.append("" + "<!DOCTYPE html>\n" + "<html>\n")
        .append(htmlHeader)
        .append(htmlBody)
        .append("</html>");

    return html.toString();
  }
  protected String getNotificationGroup_AlertAssociations(
      String notificationGroupName, boolean excludeNavbar) {

    if (notificationGroupName == null) {
      return "<b>No notification group specified</b>";
    }

    NotificationGroupsDao notificationGroupsDao = new NotificationGroupsDao();
    NotificationGroup notificationGroup =
        notificationGroupsDao.getNotificationGroupByName(notificationGroupName);
    if (notificationGroup == null) return "<b>Notification group not found</b>";

    StringBuilder outputString = new StringBuilder();

    outputString
        .append("<b>Notification Group Name</b> = ")
        .append(StatsAggHtmlFramework.htmlEncode(notificationGroup.getName()))
        .append("<br>");

    Map<Integer, Set<Integer>> alertIdAssociationsByByNotificationGroupId = null;
    //        synchronized(GlobalVariables.notificationGroupIdAssociationsByAlertId) {
    //            alertIdAssociationsByByNotificationGroupId =
    // com.pearson.statsagg.alerts.NotificationGroups.getAlertIdAssociationsByNotificationGroupId(GlobalVariables.notificationGroupIdAssociationsByAlertId);
    //        }

    if (alertIdAssociationsByByNotificationGroupId == null) {
      outputString.append("<b>Total Alert Associations</b> = ").append("0");
      return outputString.toString();
    }

    Set<Integer> alertIdAssociations =
        alertIdAssociationsByByNotificationGroupId.get(notificationGroup.getId());
    if (alertIdAssociations == null) alertIdAssociations = new HashSet<>();

    int associationCount = alertIdAssociations.size();
    outputString
        .append("<b>Total Alert Associations</b> = ")
        .append(associationCount)
        .append("<br><br>");
    if (associationCount <= 0) return outputString.toString();

    List<String> alertNames = new ArrayList<>();

    AlertsDao alertsDao = new AlertsDao(false);
    for (Integer alertId : alertIdAssociations) {
      Alert alert = alertsDao.getAlert(alertId);
      if ((alert == null) || (alert.getName() == null)) continue;
      alertNames.add(alert.getName());
    }
    alertsDao.close();

    Collections.sort(alertNames);

    outputString.append("<b>Alert Associations...</b>").append("<br>");

    outputString.append("<ul>");
    for (String alertName : alertNames) {
      String alertDetailsUrl =
          "<a href=\"AlertDetails?"
              + "ExcludeNavbar="
              + excludeNavbar
              + "&amp;Name="
              + StatsAggHtmlFramework.urlEncode(alertName)
              + "\">"
              + StatsAggHtmlFramework.htmlEncode(alertName)
              + "</a>";

      outputString.append("<li>").append(alertDetailsUrl).append("</li>");
    }
    outputString.append("</ul>");

    return outputString.toString();
  }