Actually log the PCB events to the database, as a proof of concept for the XML reading utility functions, and the DAO objects

This commit is contained in:
skogaby
2019-01-11 13:16:36 -06:00
parent a186e19f7e
commit 66483b5d42
4 changed files with 113 additions and 6 deletions

View File

@@ -268,7 +268,7 @@ public class ButterflyHttpServer {
// TODO: Verify the PCBID
// 4) return the XML document
return rootNode;
// 4) return the node corresponding to the actual call
return moduleNode;
}
}

View File

@@ -4,7 +4,9 @@ import com.buttongames.butterfly.hibernate.dao.impl.Ddr16PcbEventLogDao;
import com.buttongames.butterfly.http.exception.InvalidRequestMethodException;
import com.buttongames.butterfly.http.handlers.BaseRequestHandler;
import com.buttongames.butterfly.model.Ddr16PcbEventLog;
import com.buttongames.butterfly.util.TimeUtils;
import com.buttongames.butterfly.xml.KXmlBuilder;
import com.buttongames.butterfly.xml.XmlUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;
@@ -12,8 +14,7 @@ import org.w3c.dom.Element;
import spark.Request;
import spark.Response;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
import java.time.LocalDateTime;
/**
* Handler for any requests that come to the <code>pcbevent</code> module.
@@ -62,9 +63,14 @@ public class PcbEventRequestHandler extends BaseRequestHandler {
// log the event to the database
final String reqModel = request.attribute("model");
final String reqPcbId = request.attribute("pcbid");
final XPath xpath = XPathFactory.newInstance().newXPath();
final LocalDateTime time1 = TimeUtils.timeFromEpoch(XmlUtils.longValueAtPath(requestBody, "/pcbevent/time"));
final long sequence = XmlUtils.longValueAtPath(requestBody, "/pcbevent/seq");
final String name = XmlUtils.strValueAtPath(requestBody, "/pcbevent/item/name");
final int value = XmlUtils.intValueAtPath(requestBody, "/pcbevent/item/value");
final LocalDateTime time2 = TimeUtils.timeFromEpoch(XmlUtils.longValueAtPath(requestBody, "/pcbevent/item/time"));
// TODO: finish this
final Ddr16PcbEventLog event = new Ddr16PcbEventLog(reqPcbId, reqModel, time1, time2, sequence, name, value);
this.pcbEventLogDao.create(event);
// send the response
final KXmlBuilder respBuilder = KXmlBuilder.create("response")

View File

@@ -0,0 +1,21 @@
package com.buttongames.butterfly.util;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
/**
* Simple class with utility functions for dealing with time.
* @author skogaby (skogabyskogaby@gmail.com)
*/
public class TimeUtils {
/**
* Convert an epoch timestamp to a LocalDateTime.
* @param millis
* @return
*/
public static LocalDateTime timeFromEpoch(final long millis) {
return LocalDateTime.ofInstant(Instant.ofEpochMilli(millis), ZoneId.systemDefault());
}
}

View File

@@ -9,6 +9,9 @@ import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@@ -18,6 +21,8 @@ import java.io.IOException;
*/
public class XmlUtils {
private static final XPath XPATH = XPathFactory.newInstance().newXPath();
/**
* Scrubs empty nodes from a document so we don't accidentally read them.
* @param node The root node of the document to clean.
@@ -65,4 +70,79 @@ public class XmlUtils {
return null;
}
}
/**
* Reads the String value at the given XPath expression from the given document.
* @param doc
* @param path
* @return
*/
public static String strValueAtPath(final Element doc, final String path) {
try {
return (String) XPATH.compile(path).evaluate(doc, XPathConstants.STRING);
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
/**
* Reads the boolean value at the given XPath expression from the given document.
* @param doc
* @param path
* @return
*/
public static Boolean boolValueAtPath(final Element doc, final String path) {
try {
return (Boolean) XPATH.compile(path).evaluate(doc, XPathConstants.BOOLEAN);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* Reads the double value at the given XPath expression from the given document.
* @param doc
* @param path
* @return
*/
public static Double doubleValueAtPath(final Element doc, final String path) {
try {
return (Double) XPATH.compile(path).evaluate(doc, XPathConstants.NUMBER);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* Reads the long value at the given XPath expression from the given document.
* @param doc
* @param path
* @return
*/
public static Long longValueAtPath(final Element doc, final String path) {
try {
return Long.parseLong((String) XPATH.compile(path).evaluate(doc, XPathConstants.STRING));
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* Reads the integer value at the given XPath expression from the given document.
* @param doc
* @param path
* @return
*/
public static Integer intValueAtPath(final Element doc, final String path) {
try {
return ((Double) XPATH.compile(path).evaluate(doc, XPathConstants.NUMBER)).intValue();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}