<commit_before><%@page language="java"
        contentType="text/html"
        session="true"
        import="java.util.*,
        org.opennms.netmgt.config.*,
        org.opennms.netmgt.config.common.*,
        org.opennms.netmgt.config.poller.*,
        org.opennms.web.WebSecurityUtils,
        org.opennms.web.element.*,
        org.opennms.netmgt.EventConstants,
        org.opennms.netmgt.xml.event.Event,
        org.opennms.netmgt.utils.*,
        org.opennms.web.Util,
        java.net.*,
        java.io.*,
        java.text.NumberFormat,
        java.text.SimpleDateFormat
        "
%>
<%!//A singleton instance of a "Match-any" interface, which can be used for generic tests/removals etc.
	private static org.opennms.netmgt.config.poller.Interface matchAnyInterface;
	{
		matchAnyInterface = new org.opennms.netmgt.config.poller.Interface();
		matchAnyInterface.setAddress("match-any");
	}

	private static void addNode(Outage theOutage, int newNodeId) {
		try {
			org.opennms.netmgt.config.poller.Node newNode = new org.opennms.netmgt.config.poller.Node();
			newNode.setId(newNodeId);
			if (!theOutage.getNodeCollection().contains(newNode)) {
				theOutage.addNode(newNode);
				theOutage.removeInterface(matchAnyInterface); //Just arbitrarily try and remove it.  If it's not there, this will do nothing
			}
		} catch (NumberFormatException e) {
			//Just ignore it - we can't add the node, why should we care?
		} catch (IndexOutOfBoundsException ioob) {
			// same here
		}
	}
	
	private static void addInterface(Outage theOutage, org.opennms.netmgt.config.poller.Interface newInterface) {
		if (!theOutage.getInterfaceCollection().contains(newInterface)) {
			theOutage.addInterface(newInterface);
			try {
				theOutage.removeInterface(matchAnyInterface); //Just arbitrarily try and remove it.  If it's not there, this will do nothing
			} catch (IndexOutOfBoundsException ioob) {
				// ok if it fails
			}
		}
	}

	private static String getNumberSelectField(String name, int start, int end, int selected, int padding) {
		NumberFormat nf = NumberFormat.getInstance();
		nf.setMinimumIntegerDigits(padding);
		nf.setGroupingUsed(false);
		StringBuffer sb = new StringBuffer();
		sb.append("<select name=\"").append(name).append("\">");
		for (int i = start; i <= end; i++) {
			sb.append("<option value=\"").append(nf.format(i)).append("\"");
			if (i == selected) {
				sb.append(" selected");
			}
			sb.append(">").append(nf.format(i)).append("</option>");
		}
		sb.append("</select>");

		return sb.toString();
	}
	
	private static String getMonthSelectField(String name, int month) {
		StringBuffer sb = new StringBuffer();
		sb.append("<select name=\"").append(name).append("\">");
		SimpleDateFormat shortDF = new SimpleDateFormat("MMM", Locale.US);
		SimpleDateFormat longDF  = new SimpleDateFormat("MMMM");
		for (int mon = 0; mon < 12; mon++) {
			Date tempDate = new GregorianCalendar(0, mon, 1).getTime();
			sb.append("<option value=\"").append(shortDF.format(tempDate)).append("\" ");
			sb.append((month==(mon+1))?"selected":"");
			sb.append(">").append(longDF.format(tempDate)).append("</option>");
		}
		sb.append("</select>");
		
		return sb.toString();
	}
	
	public void sendOutagesChangedEvent() throws ServletException {
		Event event = new Event();
		event.setSource("Web UI");
		event.setUei(EventConstants.SCHEDOUTAGES_CHANGED_EVENT_UEI);
		try {
			event.setHost(InetAddress.getLocalHost().getHostName());
		} catch (UnknownHostException uhE) {
			event.setHost("unresolved.host");
		}

		event.setTime(EventConstants.formatToString(new java.util.Date()));
		try {
			Util.createEventProxy().send(event);
		} catch (Exception e) {
			throw new ServletException("Could not send event " + event.getUei(), e);
		}
	}
	
%>
<%
	NotifdConfigFactory.init(); //Must do this early on - if it fails, then just throw the exception to the web gui

	// @i18n
	final HashMap<String, String> shortDayNames = new HashMap<String, String>();
	shortDayNames.put("sunday", "Sunday");
	shortDayNames.put("monday", "Monday");
	shortDayNames.put("tuesday", "Tuesday");
	shortDayNames.put("wednesday", "Wednesday");
	shortDayNames.put("thursday", "Thursday");
	shortDayNames.put("friday", "Friday");
	shortDayNames.put("saturday", "Saturday");
	shortDayNames.put("1", "1st");
	shortDayNames.put("2", "2nd");
	shortDayNames.put("3", "3rd");
	shortDayNames.put("4", "4th");
	shortDayNames.put("5", "5th");
	shortDayNames.put("6", "6th");
	shortDayNames.put("7", "7th");
	shortDayNames.put("8", "8th");
	shortDayNames.put("9", "9th");
	shortDayNames.put("10", "10th");
	shortDayNames.put("11", "11th");
	shortDayNames.put("12", "12th");
	shortDayNames.put("13", "13th");
	shortDayNames.put("14", "14th");
	shortDayNames.put("15", "15th");
	shortDayNames.put("16", "16th");
	shortDayNames.put("17", "17th");
	shortDayNames.put("18", "18th");
	shortDayNames.put("19", "19th");
	shortDayNames.put("20", "20th");
	shortDayNames.put("21", "21st");
	shortDayNames.put("22", "22nd");
	shortDayNames.put("23", "23rd");
	shortDayNames.put("24", "24th");
	shortDayNames.put("25", "25th");
	shortDayNames.put("26", "26th");
	shortDayNames.put("27", "27th");
	shortDayNames.put("28", "28th");
	shortDayNames.put("29", "29th");
	shortDayNames.put("30", "30th");
	shortDayNames.put("31", "31st");

	GregorianCalendar today = new GregorianCalendar();
	final int date = today.get(Calendar.DATE);
	final int month = today.get(Calendar.MONTH) + 1;
	final int year = today.get(Calendar.YEAR);

	PollOutagesConfigFactory.init(); //Only init - do *not* reload
	PollOutagesConfigFactory pollFactory = PollOutagesConfigFactory.getInstance();
	Outage theOutage;

	if ("Cancel".equals(request.getParameter("cancelButton"))) {
		response.sendRedirect("index.jsp");
		return;
	}

	String nameParam = request.getParameter("name");
	if (nameParam != null) {
		//first time in - name is passed as a param.  Find the outage, copy it, and shove it in the session
		//Also keep a copy of the name, for later saving (replacing the original with the edited copy)
		Outage tempOutage = pollFactory.getOutage(nameParam);
		CharArrayWriter writer = new CharArrayWriter();
		tempOutage.marshal(writer);
		theOutage = (Outage) Outage.unmarshal(new CharArrayReader(writer.toCharArray()));
		request.getSession().setAttribute("opennms.editoutage", theOutage);
		request.getSession().setAttribute("opennms.editoutage.origname", nameParam);
	} else if ("true".equals(request.getParameter("addNew"))) {
		theOutage = new Outage();
		String nodes[] = request.getParameterValues("nodeID");
		String interfaces[] = request.getParameterValues("ipAddr");


		//Nuke whitespace - it messes with all sorts of things
		theOutage.setName(request.getParameter("newName").trim());
		
		request.getSession().setAttribute("opennms.editoutage", theOutage);
		request.getSession().removeAttribute("opennms.editoutage.origname");
		if (nodes != null) {
			for(int i = 0 ; i < nodes.length; i++ ) {
				int node = WebSecurityUtils.safeParseInt(nodes[i]);
				addNode(theOutage, node);
			}
		}
		if (interfaces != null) {
			for(int i = 0 ; i < interfaces.length; i++ ) {
				org.opennms.netmgt.config.poller.Interface newInterface = new org.opennms.netmgt.config.poller.Interface();
				// hope this has builtin safeParseStuff
				newInterface.setAddress(interfaces[i]);
				addInterface(theOutage, newInterface);
			}
		}
	} else {
		//Neither starting the edit, nor adding a new outage.  
		theOutage = (Outage) request.getSession().getAttribute("opennms.editoutage");
		if (theOutage == null) {
			//No name, and no outage in the session.  Give up
%>
<html>
<body>
<p>
Could not find an outage to edit because no outage name parameter was specified nor is any outage stored in the session. Cannot edit!
</p>
</body>
</html>
<%
	return;

		}
	}
	//Load the initial set of enabled outages from the external configurations
	// This will be overridden by a formSubmission to use the form values, but is necessary for the initial load of the page
	//It is more efficient to piggy back on this initial setup (creating the hashmaps) than doing it separately
	Set<String> enabledOutages = new HashSet<String>();

	// ******* Notification outages config *********
	Collection<String> notificationOutages = NotifdConfigFactory.getInstance().getConfiguration().getOutageCalendarCollection();
	if (notificationOutages.contains(theOutage.getName())) {
		enabledOutages.add("notifications");
	}

	// ******* Threshd outages config *********
	ThreshdConfigFactory.init();
	Map<org.opennms.netmgt.config.threshd.Package, List<String>> thresholdOutages = new HashMap<org.opennms.netmgt.config.threshd.Package, List<String>>();
	for (org.opennms.netmgt.config.threshd.Package thisPackage : ThreshdConfigFactory.getInstance().getConfiguration().getPackage()) {
		thresholdOutages.put(thisPackage, thisPackage.getOutageCalendarCollection());
		if (thisPackage.getOutageCalendarCollection().contains(theOutage.getName())) {
			enabledOutages.add("threshold-" + thisPackage.getName());
		}
	}

	// ******* Polling outages config *********
	PollerConfigFactory.init();
	Map<org.opennms.netmgt.config.poller.Package, List<String>> pollingOutages = new HashMap<org.opennms.netmgt.config.poller.Package, List<String>>();
	for (org.opennms.netmgt.config.poller.Package thisPackage : PollerConfigFactory.getInstance().getConfiguration().getPackage()) {
		pollingOutages.put(thisPackage, thisPackage.getOutageCalendarCollection());
		if (thisPackage.getOutageCalendarCollection().contains(theOutage.getName())) {
			enabledOutages.add("polling-" + thisPackage.getName());
		}
	}

	// ******* Collectd outages config *********
	CollectdConfigFactory.init();
	Map<org.opennms.netmgt.config.collectd.Package, List<String>> collectionOutages = new HashMap<org.opennms.netmgt.config.collectd.Package, List<String>>();
	for (CollectdPackage pkg : CollectdConfigFactory.getInstance().getCollectdConfig().getPackages()) {
		org.opennms.netmgt.config.collectd.Package thisPackage = pkg.getPackage();
		collectionOutages.put(thisPackage, thisPackage.getOutageCalendarCollection());
		if (thisPackage.getOutageCalendarCollection().contains(theOutage.getName())) {
			enabledOutages.add("collect-" + thisPackage.getName());
		}
	}

	if (request.getParameter("deleteOutageType") != null) {
		theOutage.setType(null);
		theOutage.removeAllTime();
	} else {
	    if (request.getParameter("outageType") != null) {
			theOutage.setType(request.getParameter("outageType"));
	    }
	}
	
	String isFormSubmission = request.getParameter("formSubmission");
	if ("true".equals(isFormSubmission)) {

		//Process the form submission - yeah, this should be a servlet, but this is a quick and dirty hack for now
		//It can be tidied up later -- of course, it's been what, almost 3 years?  "later" means 2.0 + rewrite  ;)
		//First, process any changes to the editable inputs

		//Process the notifications status.  NB: we keep an in-memory copy initially, and only save when the save button is clicked
		if ("on".equals(request.getParameter("notifications"))) {
			//Want to turn it on.
			enabledOutages.add("notifications");
		} else {
			//Want to turn off (missing, or set to something other than "on")
			enabledOutages.remove("notifications");
		}

		for (org.opennms.netmgt.config.poller.Package thisKey : pollingOutages.keySet()) {
			String name = "polling-" + thisKey.getName();
			System.out.println("Checking " + name);
			if ("on".equals(request.getParameter(name))) {
				System.out.println(" is on - adding to enabledOutages");
				enabledOutages.add(name);
			} else {
				enabledOutages.remove(name);
			}
		}

		for (org.opennms.netmgt.config.threshd.Package thisKey : thresholdOutages.keySet()) {
			String name = "threshold-" + thisKey.getName();
			System.out.println("Checking " + name);
			if ("on".equals(request.getParameter(name))) {
				enabledOutages.add(name);
			} else {
				enabledOutages.remove(name);
			}
		}

		for (org.opennms.netmgt.config.collectd.Package thisKey : collectionOutages.keySet()) {
			String name = "collect-" + thisKey.getName();
			System.out.println("Checking " + name);
			if ("on".equals(request.getParameter(name))) {
				enabledOutages.add(name);
			} else {
				enabledOutages.remove(name);
			}
		}

		//Now handle any buttons that were clicked.  There should be only one
		//If there is more than one, we use the first and ignore the rest.
		if (request.getParameter("saveButton") != null) {
			//Save was clicked - save 

			//Check if the outage is a new one, or an edited old one
			String origname = (String) request.getSession().getAttribute("opennms.editoutage.origname");
			if (origname == null) {
				//A new outage - just plonk it in place
				pollFactory.addOutage(theOutage);
			} else {
				//An edited outage - replace the old one
				pollFactory.replaceOutage(pollFactory.getOutage(origname), theOutage);
			}
			//Push the enabledOutages into the actual configuration of the various packages
			//Don't do until after we've successfully put the outage into the polloutages configuration (for coherency)
			if (enabledOutages.contains("notifications")) {
				if (!notificationOutages.contains(theOutage.getName())) {
					NotifdConfigFactory.getInstance().getConfiguration().addOutageCalendar(theOutage.getName());
				}
			} else {
				if (notificationOutages.contains(theOutage.getName())) {
					NotifdConfigFactory.getInstance().getConfiguration().removeOutageCalendar(theOutage.getName());
				}
			}

			for (org.opennms.netmgt.config.poller.Package thisKey : pollingOutages.keySet()) {
				Collection<String> pollingPackage = pollingOutages.get(thisKey);
				String name = "polling-" + thisKey.getName();
				if (enabledOutages.contains(name)) {
					if (!pollingPackage.contains(theOutage.getName())) {
						thisKey.addOutageCalendar(theOutage.getName());
					}
				} else {
					if (pollingPackage.contains(theOutage.getName())) {
						thisKey.removeOutageCalendar(theOutage.getName());
					}
				}
			}

			for (org.opennms.netmgt.config.threshd.Package thisKey : thresholdOutages.keySet()) {
				Collection<String> thresholdPackage = thresholdOutages.get(thisKey);
				String name = "threshold-" + thisKey.getName();
				if (enabledOutages.contains(name)) {
					if (!thresholdPackage.contains(theOutage.getName())) {
						thisKey.addOutageCalendar(theOutage.getName());
					}
				} else {
					if (thresholdPackage.contains(theOutage.getName())) {
						thisKey.removeOutageCalendar(theOutage.getName());
					}
				}
			}

			for (org.opennms.netmgt.config.collectd.Package thisKey : collectionOutages.keySet()) {
				Collection<String> collectPackage = collectionOutages.get(thisKey);
				String name = "collect-" + thisKey.getName();
				if (enabledOutages.contains(name)) {
					if (!collectPackage.contains(theOutage.getName())) {
						thisKey.addOutageCalendar(theOutage.getName());
					}
				} else {
					if (collectPackage.contains(theOutage.getName())) {
						thisKey.removeOutageCalendar(theOutage.getName());
					}
				}
			}

			//Save to disk	
			pollFactory.saveCurrent();
			NotifdConfigFactory.getInstance().saveCurrent();
			ThreshdConfigFactory.getInstance().saveCurrent();
			CollectdConfigFactory.getInstance().saveCurrent();
			PollerConfigFactory.getInstance().save();
			sendOutagesChangedEvent();

			//forward the request for proper display
			// RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher("/admin/sched-outages/index.jsp");
			response.sendRedirect("index.jsp");
			// dispatcher.forward(request, response);
			return;
		} else if (request.getParameter("addNodeButton") != null) {
			int newNodeId = WebSecurityUtils.safeParseInt(request.getParameter("newNode"));
			addNode(theOutage, newNodeId);
		} else if (request.getParameter("addInterfaceButton") != null) {
			org.opennms.netmgt.config.poller.Interface newInterface = new org.opennms.netmgt.config.poller.Interface();
			newInterface.setAddress(request.getParameter("newInterface"));
			addInterface(theOutage, newInterface);
		} else if (request.getParameter("matchAny") != null) {
			//To turn on matchAny, all normal nodes and interfaces are removed
			theOutage.removeAllInterface();
			theOutage.removeAllNode();
			theOutage.addInterface(matchAnyInterface);
		} else if (request.getParameter("addOutage") != null && theOutage.getType() != null) {
			if (theOutage.getType().equalsIgnoreCase("specific")) {
				Time newTime = new Time();

				StringBuffer timeBuffer = new StringBuffer(17);
				timeBuffer.append(request.getParameter("chooseStartDay"));
				timeBuffer.append("-");
				timeBuffer.append(request.getParameter("chooseStartMonth"));<commit_msg>Fixed the crashes in editoutage.jsp documented in bug 1225, comment 13.<commit_after><%@page language="java"
        contentType="text/html"
        session="true"
        import="java.util.*,
        org.opennms.netmgt.config.*,
        org.opennms.netmgt.config.common.*,
        org.opennms.netmgt.config.poller.*,
        org.opennms.web.WebSecurityUtils,
        org.opennms.web.element.*,
        org.opennms.netmgt.EventConstants,
        org.opennms.netmgt.xml.event.Event,
        org.opennms.netmgt.utils.*,
        org.opennms.web.Util,
        org.exolab.castor.xml.ValidationException,
        java.net.*,
        java.io.*,
        java.text.NumberFormat,
        java.text.SimpleDateFormat
        "
%>
<%!//A singleton instance of a "Match-any" interface, which can be used for generic tests/removals etc.
	private static org.opennms.netmgt.config.poller.Interface matchAnyInterface;
	{
		matchAnyInterface = new org.opennms.netmgt.config.poller.Interface();
		matchAnyInterface.setAddress("match-any");
	}

	private static void addNode(Outage theOutage, int newNodeId) throws ValidationException {
		try {
			org.opennms.netmgt.config.poller.Node newNode = new org.opennms.netmgt.config.poller.Node();
			newNode.setId(newNodeId);
			if (!theOutage.getNodeCollection().contains(newNode)) {
				newNode.validate();
				theOutage.addNode(newNode);
				theOutage.removeInterface(matchAnyInterface); //Just arbitrarily try and remove it.  If it's not there, this will do nothing
			}
		} catch (NumberFormatException e) {
			//Just ignore it - we can't add the node, why should we care?
		} catch (IndexOutOfBoundsException ioob) {
			// same here
		}
	}
	
	private static void addInterface(Outage theOutage, org.opennms.netmgt.config.poller.Interface newInterface) throws ValidationException {
		if (!theOutage.getInterfaceCollection().contains(newInterface)) {
			newInterface.validate();
			theOutage.addInterface(newInterface);
			try {
				theOutage.removeInterface(matchAnyInterface); //Just arbitrarily try and remove it.  If it's not there, this will do nothing
			} catch (IndexOutOfBoundsException ioob) {
				// ok if it fails
			}
		}
	}

	private static String getNumberSelectField(String name, int start, int end, int selected, int padding) {
		NumberFormat nf = NumberFormat.getInstance();
		nf.setMinimumIntegerDigits(padding);
		nf.setGroupingUsed(false);
		StringBuffer sb = new StringBuffer();
		sb.append("<select name=\"").append(name).append("\">");
		for (int i = start; i <= end; i++) {
			sb.append("<option value=\"").append(nf.format(i)).append("\"");
			if (i == selected) {
				sb.append(" selected");
			}
			sb.append(">").append(nf.format(i)).append("</option>");
		}
		sb.append("</select>");

		return sb.toString();
	}
	
	private static String getMonthSelectField(String name, int month) {
		StringBuffer sb = new StringBuffer();
		sb.append("<select name=\"").append(name).append("\">");
		SimpleDateFormat shortDF = new SimpleDateFormat("MMM", Locale.US);
		SimpleDateFormat longDF  = new SimpleDateFormat("MMMM");
		for (int mon = 0; mon < 12; mon++) {
			Date tempDate = new GregorianCalendar(0, mon, 1).getTime();
			sb.append("<option value=\"").append(shortDF.format(tempDate)).append("\" ");
			sb.append((month==(mon+1))?"selected":"");
			sb.append(">").append(longDF.format(tempDate)).append("</option>");
		}
		sb.append("</select>");
		
		return sb.toString();
	}
	
	public void sendOutagesChangedEvent() throws ServletException {
		Event event = new Event();
		event.setSource("Web UI");
		event.setUei(EventConstants.SCHEDOUTAGES_CHANGED_EVENT_UEI);
		try {
			event.setHost(InetAddress.getLocalHost().getHostName());
		} catch (UnknownHostException uhE) {
			event.setHost("unresolved.host");
		}

		event.setTime(EventConstants.formatToString(new java.util.Date()));
		try {
			Util.createEventProxy().send(event);
		} catch (Exception e) {
			throw new ServletException("Could not send event " + event.getUei(), e);
		}
	}
	
%>
<%
	NotifdConfigFactory.init(); //Must do this early on - if it fails, then just throw the exception to the web gui

	// @i18n
	final HashMap<String, String> shortDayNames = new HashMap<String, String>();
	shortDayNames.put("sunday", "Sunday");
	shortDayNames.put("monday", "Monday");
	shortDayNames.put("tuesday", "Tuesday");
	shortDayNames.put("wednesday", "Wednesday");
	shortDayNames.put("thursday", "Thursday");
	shortDayNames.put("friday", "Friday");
	shortDayNames.put("saturday", "Saturday");
	shortDayNames.put("1", "1st");
	shortDayNames.put("2", "2nd");
	shortDayNames.put("3", "3rd");
	shortDayNames.put("4", "4th");
	shortDayNames.put("5", "5th");
	shortDayNames.put("6", "6th");
	shortDayNames.put("7", "7th");
	shortDayNames.put("8", "8th");
	shortDayNames.put("9", "9th");
	shortDayNames.put("10", "10th");
	shortDayNames.put("11", "11th");
	shortDayNames.put("12", "12th");
	shortDayNames.put("13", "13th");
	shortDayNames.put("14", "14th");
	shortDayNames.put("15", "15th");
	shortDayNames.put("16", "16th");
	shortDayNames.put("17", "17th");
	shortDayNames.put("18", "18th");
	shortDayNames.put("19", "19th");
	shortDayNames.put("20", "20th");
	shortDayNames.put("21", "21st");
	shortDayNames.put("22", "22nd");
	shortDayNames.put("23", "23rd");
	shortDayNames.put("24", "24th");
	shortDayNames.put("25", "25th");
	shortDayNames.put("26", "26th");
	shortDayNames.put("27", "27th");
	shortDayNames.put("28", "28th");
	shortDayNames.put("29", "29th");
	shortDayNames.put("30", "30th");
	shortDayNames.put("31", "31st");

	GregorianCalendar today = new GregorianCalendar();
	final int date = today.get(Calendar.DATE);
	final int month = today.get(Calendar.MONTH) + 1;
	final int year = today.get(Calendar.YEAR);

	PollOutagesConfigFactory.init(); //Only init - do *not* reload
	PollOutagesConfigFactory pollFactory = PollOutagesConfigFactory.getInstance();
	Outage theOutage;

	if ("Cancel".equals(request.getParameter("cancelButton"))) {
		response.sendRedirect("index.jsp");
		return;
	}

	String nameParam = request.getParameter("name");
	if (nameParam != null) {
		//first time in - name is passed as a param.  Find the outage, copy it, and shove it in the session
		//Also keep a copy of the name, for later saving (replacing the original with the edited copy)
		Outage tempOutage = pollFactory.getOutage(nameParam);
		CharArrayWriter writer = new CharArrayWriter();
		tempOutage.marshal(writer);
		theOutage = (Outage) Outage.unmarshal(new CharArrayReader(writer.toCharArray()));
		request.getSession().setAttribute("opennms.editoutage", theOutage);
		request.getSession().setAttribute("opennms.editoutage.origname", nameParam);
	} else if ("true".equals(request.getParameter("addNew"))) {
		theOutage = new Outage();
		String nodes[] = request.getParameterValues("nodeID");
		String interfaces[] = request.getParameterValues("ipAddr");


		//Nuke whitespace - it messes with all sorts of things
		theOutage.setName(request.getParameter("newName").trim());
		
		request.getSession().setAttribute("opennms.editoutage", theOutage);
		request.getSession().removeAttribute("opennms.editoutage.origname");
		if (nodes != null) {
			for(int i = 0 ; i < nodes.length; i++ ) {
				int node = WebSecurityUtils.safeParseInt(nodes[i]);
				addNode(theOutage, node);
			}
		}
		if (interfaces != null) {
			for(int i = 0 ; i < interfaces.length; i++ ) {
				org.opennms.netmgt.config.poller.Interface newInterface = new org.opennms.netmgt.config.poller.Interface();
				// hope this has builtin safeParseStuff
				newInterface.setAddress(interfaces[i]);
				addInterface(theOutage, newInterface);
			}
		}
	} else {
		//Neither starting the edit, nor adding a new outage.  
		theOutage = (Outage) request.getSession().getAttribute("opennms.editoutage");
		if (theOutage == null) {
			//No name, and no outage in the session.  Give up
%>
<html>
<body>
<p>
Could not find an outage to edit because no outage name parameter was specified nor is any outage stored in the session. Cannot edit!
</p>
</body>
</html>
<%
	return;

		}
	}
	//Load the initial set of enabled outages from the external configurations
	// This will be overridden by a formSubmission to use the form values, but is necessary for the initial load of the page
	//It is more efficient to piggy back on this initial setup (creating the hashmaps) than doing it separately
	Set<String> enabledOutages = new HashSet<String>();

	// ******* Notification outages config *********
	Collection<String> notificationOutages = NotifdConfigFactory.getInstance().getConfiguration().getOutageCalendarCollection();
	if (notificationOutages.contains(theOutage.getName())) {
		enabledOutages.add("notifications");
	}

	// ******* Threshd outages config *********
	ThreshdConfigFactory.init();
	Map<org.opennms.netmgt.config.threshd.Package, List<String>> thresholdOutages = new HashMap<org.opennms.netmgt.config.threshd.Package, List<String>>();
	for (org.opennms.netmgt.config.threshd.Package thisPackage : ThreshdConfigFactory.getInstance().getConfiguration().getPackage()) {
		thresholdOutages.put(thisPackage, thisPackage.getOutageCalendarCollection());
		if (thisPackage.getOutageCalendarCollection().contains(theOutage.getName())) {
			enabledOutages.add("threshold-" + thisPackage.getName());
		}
	}

	// ******* Polling outages config *********
	PollerConfigFactory.init();
	Map<org.opennms.netmgt.config.poller.Package, List<String>> pollingOutages = new HashMap<org.opennms.netmgt.config.poller.Package, List<String>>();
	for (org.opennms.netmgt.config.poller.Package thisPackage : PollerConfigFactory.getInstance().getConfiguration().getPackage()) {
		pollingOutages.put(thisPackage, thisPackage.getOutageCalendarCollection());
		if (thisPackage.getOutageCalendarCollection().contains(theOutage.getName())) {
			enabledOutages.add("polling-" + thisPackage.getName());
		}
	}

	// ******* Collectd outages config *********
	CollectdConfigFactory.init();
	Map<org.opennms.netmgt.config.collectd.Package, List<String>> collectionOutages = new HashMap<org.opennms.netmgt.config.collectd.Package, List<String>>();
	for (CollectdPackage pkg : CollectdConfigFactory.getInstance().getCollectdConfig().getPackages()) {
		org.opennms.netmgt.config.collectd.Package thisPackage = pkg.getPackage();
		collectionOutages.put(thisPackage, thisPackage.getOutageCalendarCollection());
		if (thisPackage.getOutageCalendarCollection().contains(theOutage.getName())) {
			enabledOutages.add("collect-" + thisPackage.getName());
		}
	}

	if (request.getParameter("deleteOutageType") != null) {
		theOutage.setType(null);
		theOutage.removeAllTime();
	} else {
	    if (request.getParameter("outageType") != null) {
			theOutage.setType(request.getParameter("outageType"));
	    }
	}
	
	String isFormSubmission = request.getParameter("formSubmission");
	if ("true".equals(isFormSubmission)) {

		//Process the form submission - yeah, this should be a servlet, but this is a quick and dirty hack for now
		//It can be tidied up later -- of course, it's been what, almost 3 years?  "later" means 2.0 + rewrite  ;)
		//First, process any changes to the editable inputs

		//Process the notifications status.  NB: we keep an in-memory copy initially, and only save when the save button is clicked
		if ("on".equals(request.getParameter("notifications"))) {
			//Want to turn it on.
			enabledOutages.add("notifications");
		} else {
			//Want to turn off (missing, or set to something other than "on")
			enabledOutages.remove("notifications");
		}

		for (org.opennms.netmgt.config.poller.Package thisKey : pollingOutages.keySet()) {
			String name = "polling-" + thisKey.getName();
			System.out.println("Checking " + name);
			if ("on".equals(request.getParameter(name))) {
				System.out.println(" is on - adding to enabledOutages");
				enabledOutages.add(name);
			} else {
				enabledOutages.remove(name);
			}
		}

		for (org.opennms.netmgt.config.threshd.Package thisKey : thresholdOutages.keySet()) {
			String name = "threshold-" + thisKey.getName();
			System.out.println("Checking " + name);
			if ("on".equals(request.getParameter(name))) {
				enabledOutages.add(name);
			} else {
				enabledOutages.remove(name);
			}
		}

		for (org.opennms.netmgt.config.collectd.Package thisKey : collectionOutages.keySet()) {
			String name = "collect-" + thisKey.getName();
			System.out.println("Checking " + name);
			if ("on".equals(request.getParameter(name))) {
				enabledOutages.add(name);
			} else {
				enabledOutages.remove(name);
			}
		}

		//Now handle any buttons that were clicked.  There should be only one
		//If there is more than one, we use the first and ignore the rest.
		if (request.getParameter("saveButton") != null) {
			//Save was clicked - save 

			//Check if the outage is a new one, or an edited old one
			String origname = (String) request.getSession().getAttribute("opennms.editoutage.origname");
			if (origname == null) {
				//A new outage - just plonk it in place
				pollFactory.addOutage(theOutage);
			} else {
				//An edited outage - replace the old one
				pollFactory.replaceOutage(pollFactory.getOutage(origname), theOutage);
			}
			//Push the enabledOutages into the actual configuration of the various packages
			//Don't do until after we've successfully put the outage into the polloutages configuration (for coherency)
			if (enabledOutages.contains("notifications")) {
				if (!notificationOutages.contains(theOutage.getName())) {
					NotifdConfigFactory.getInstance().getConfiguration().addOutageCalendar(theOutage.getName());
				}
			} else {
				if (notificationOutages.contains(theOutage.getName())) {
					NotifdConfigFactory.getInstance().getConfiguration().removeOutageCalendar(theOutage.getName());
				}
			}

			for (org.opennms.netmgt.config.poller.Package thisKey : pollingOutages.keySet()) {
				Collection<String> pollingPackage = pollingOutages.get(thisKey);
				String name = "polling-" + thisKey.getName();
				if (enabledOutages.contains(name)) {
					if (!pollingPackage.contains(theOutage.getName())) {
						thisKey.addOutageCalendar(theOutage.getName());
					}
				} else {
					if (pollingPackage.contains(theOutage.getName())) {
						thisKey.removeOutageCalendar(theOutage.getName());
					}
				}
			}

			for (org.opennms.netmgt.config.threshd.Package thisKey : thresholdOutages.keySet()) {
				Collection<String> thresholdPackage = thresholdOutages.get(thisKey);
				String name = "threshold-" + thisKey.getName();
				if (enabledOutages.contains(name)) {
					if (!thresholdPackage.contains(theOutage.getName())) {
						thisKey.addOutageCalendar(theOutage.getName());
					}
				} else {
					if (thresholdPackage.contains(theOutage.getName())) {
						thisKey.removeOutageCalendar(theOutage.getName());
					}
				}
			}

			for (org.opennms.netmgt.config.collectd.Package thisKey : collectionOutages.keySet()) {
				Collection<String> collectPackage = collectionOutages.get(thisKey);
				String name = "collect-" + thisKey.getName();
				if (enabledOutages.contains(name)) {
					if (!collectPackage.contains(theOutage.getName())) {
						thisKey.addOutageCalendar(theOutage.getName());
					}
				} else {
					if (collectPackage.contains(theOutage.getName())) {
						thisKey.removeOutageCalendar(theOutage.getName());
					}
				}
			}

			//Save to disk	
			pollFactory.saveCurrent();
			NotifdConfigFactory.getInstance().saveCurrent();
			ThreshdConfigFactory.getInstance().saveCurrent();
			CollectdConfigFactory.getInstance().saveCurrent();
			PollerConfigFactory.getInstance().save();
			sendOutagesChangedEvent();

			//forward the request for proper display
			// RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher("/admin/sched-outages/index.jsp");
			response.sendRedirect("index.jsp");
			// dispatcher.forward(request, response);
			return;
		} else if (request.getParameter("addNodeButton") != null) {
			String newNode = request.getParameter("newNode");
			if (newNode == null || "".equals(newNode.trim())) {
				// No node was specified
			} else {
				int newNodeId = WebSecurityUtils.safeParseInt(newNode);
				addNode(theOutage, newNodeId);
			}
		} else if (request.getParameter("addInterfaceButton") != null) {
			String newIface = request.getParameter("newInterface");
			if (newIface == null || "".equals(newIface.trim())) {
				// No interface was specified
			} else {
				org.opennms.netmgt.config.poller.Interface newInterface = new org.opennms.netmgt.config.poller.Interface();
				newInterface.setAddress(newIface);
				addInterface(theOutage, newInterface);
			}
		} else if (request.getParameter("matchAny") != null) {
			//To turn on matchAny, all normal nodes and interfaces are removed
			theOutage.removeAllInterface();
			theOutage.removeAllNode();
			theOutage.addInterface(matchAnyInterface);
		} else if (request.getParameter("addOutage") != null && theOutage.getType() != null) {
			if (theOutage.getType().equalsIgnoreCase("specific")) {
				Time newTime = new Time();

				StringBuffer timeBuffer = new StringBuffer(17);
				timeBuffer.append(request.getParameter("chooseStartDay"));
				timeBuffer.append("-");
				timeBuffer.append(request.getParameter("chooseStartMonth"));
{'fraction_non_alphanumeric': 0.14816783299376973, 'fraction_numerical': 0.0073818171080993296, 'mean_word_length': 4.469638242894057, 'pattern_counts': {'":': 2, '<': 87, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 3}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4765381', 'n_tokens_mistral': 11955, 'n_tokens_neox': 10568, 'n_words': 2200}