From 3c65441dd5ca76c478d1b3238978fe2dfcfddd40 Mon Sep 17 00:00:00 2001 From: skogaby Date: Fri, 11 Jan 2019 01:47:42 -0600 Subject: [PATCH] Checkpoint commit, working on saving event logs --- .../http/handlers/BaseRequestHandler.java | 7 +++++ .../handlers/impl/PcbEventRequestHandler.java | 31 ++++++++++++++++--- .../butterfly/model/Ddr16Facility.java | 20 +----------- .../model/Ddr16GameplayEventLog.java | 18 +---------- .../butterfly/model/Ddr16PcbEventLog.java | 20 +----------- .../configuration/HttpConfiguration.java | 5 +-- 6 files changed, 39 insertions(+), 62 deletions(-) diff --git a/src/main/java/com/buttongames/butterfly/http/handlers/BaseRequestHandler.java b/src/main/java/com/buttongames/butterfly/http/handlers/BaseRequestHandler.java index f737195..1f501fe 100644 --- a/src/main/java/com/buttongames/butterfly/http/handlers/BaseRequestHandler.java +++ b/src/main/java/com/buttongames/butterfly/http/handlers/BaseRequestHandler.java @@ -43,6 +43,13 @@ public abstract class BaseRequestHandler { */ public abstract Object handleRequest(final Element requestBody, final Request request, final Response response); + /** + * Sends the response to the client. + * @param request The original request. + * @param response The response object we can use to send the data. + * @param respBody The XML document of the response. + * @return + */ protected Object sendResponse(final Request request, final Response response, final BaseXMLBuilder respBody) { // get the bytes of the XML document final ByteArrayOutputStream bos = new ByteArrayOutputStream(); diff --git a/src/main/java/com/buttongames/butterfly/http/handlers/impl/PcbEventRequestHandler.java b/src/main/java/com/buttongames/butterfly/http/handlers/impl/PcbEventRequestHandler.java index 18e3493..7a4d71c 100644 --- a/src/main/java/com/buttongames/butterfly/http/handlers/impl/PcbEventRequestHandler.java +++ b/src/main/java/com/buttongames/butterfly/http/handlers/impl/PcbEventRequestHandler.java @@ -1,7 +1,9 @@ package com.buttongames.butterfly.http.handlers.impl; +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.xml.KXmlBuilder; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -10,6 +12,9 @@ import org.w3c.dom.Element; import spark.Request; import spark.Response; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathFactory; + /** * Handler for any requests that come to the pcbevent module. * @author skogaby (skogabyskogaby@gmail.com) @@ -19,6 +24,15 @@ public class PcbEventRequestHandler extends BaseRequestHandler { private final Logger LOG = LogManager.getLogger(PcbEventRequestHandler.class); + /** + * The DAO for creating new PCB event logs. + */ + private final Ddr16PcbEventLogDao pcbEventLogDao; + + public PcbEventRequestHandler(final Ddr16PcbEventLogDao pcbEventLogDao) { + this.pcbEventLogDao = pcbEventLogDao; + } + /** * Handles an incoming request for the pcbevent module. * @param requestBody The XML document of the incoming request. @@ -31,7 +45,7 @@ public class PcbEventRequestHandler extends BaseRequestHandler { final String requestMethod = request.queryParams("method"); if (requestMethod.equals("put")) { - return handlePutRequest(request, response); + return handlePutRequest(requestBody, request, response); } else { throw new InvalidRequestMethodException(); } @@ -39,15 +53,22 @@ public class PcbEventRequestHandler extends BaseRequestHandler { /** * Handles an incoming request for pcbevent.put + * @param requestBody The XML body of the request * @param request The Spark request * @param response The Spark response * @return A response object for Spark */ - private Object handlePutRequest(final Request request, final Response response) { - // TODO: Remove all the hardcoded stuff and actually do something with the input - KXmlBuilder respBuilder = KXmlBuilder.create("response") - .e("pcbevent"); + private Object handlePutRequest(final Element requestBody, final Request request, final Response response) { + // log the event to the database + final String reqModel = requestBody.getAttribute("model"); + final String reqPcbId = requestBody.getAttribute("srcid"); + final XPath xpath = XPathFactory.newInstance().newXPath(); + // TODO: finish this + + // send the response + final KXmlBuilder respBuilder = KXmlBuilder.create("response") + .e("pcbevent"); return this.sendResponse(request, response, respBuilder); } } diff --git a/src/main/java/com/buttongames/butterfly/model/Ddr16Facility.java b/src/main/java/com/buttongames/butterfly/model/Ddr16Facility.java index 22ab1ad..3d10095 100644 --- a/src/main/java/com/buttongames/butterfly/model/Ddr16Facility.java +++ b/src/main/java/com/buttongames/butterfly/model/Ddr16Facility.java @@ -4,8 +4,6 @@ import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.ManyToOne; import javax.persistence.Table; import java.io.Externalizable; import java.io.IOException; @@ -28,11 +26,6 @@ public class Ddr16Facility implements Externalizable { @Column(name = "id") private long id; - /** The user who owns the machine this event occurred on. */ - @ManyToOne - @JoinColumn(name = "user_id") - private ButterflyUser machineOwner; - /** The PCBID for this facility. */ @Column(name = "pcb_id") private String pcbId; @@ -79,10 +72,9 @@ public class Ddr16Facility implements Externalizable { public Ddr16Facility() { } - public Ddr16Facility(final ButterflyUser machineOwner, final String pcbId, final String locationId, final String name, + public Ddr16Facility(final String pcbId, final String locationId, final String name, final String country, final String region, final boolean isPublic, final String latitude, final String longitude, final int notchAmount, final int notchCount, final int supplyLimit) { - this.machineOwner = machineOwner; this.pcbId = pcbId; this.locationId = locationId; this.name = name; @@ -99,7 +91,6 @@ public class Ddr16Facility implements Externalizable { @Override public void writeExternal(ObjectOutput out) throws IOException { out.writeLong(this.id); - out.writeObject(this.machineOwner); out.writeUTF(this.pcbId); out.writeUTF(this.locationId); out.writeUTF(this.name); @@ -116,7 +107,6 @@ public class Ddr16Facility implements Externalizable { @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { this.setId(in.readLong()); - this.setMachineOwner((ButterflyUser) in.readObject()); this.setPcbId(in.readUTF()); this.setLocationId(in.readUTF()); this.setName(in.readUTF()); @@ -138,14 +128,6 @@ public class Ddr16Facility implements Externalizable { this.id = id; } - public ButterflyUser getMachineOwner() { - return machineOwner; - } - - public void setMachineOwner(ButterflyUser machineOwner) { - this.machineOwner = machineOwner; - } - public String getPcbId() { return pcbId; } diff --git a/src/main/java/com/buttongames/butterfly/model/Ddr16GameplayEventLog.java b/src/main/java/com/buttongames/butterfly/model/Ddr16GameplayEventLog.java index ff4582d..88ec555 100644 --- a/src/main/java/com/buttongames/butterfly/model/Ddr16GameplayEventLog.java +++ b/src/main/java/com/buttongames/butterfly/model/Ddr16GameplayEventLog.java @@ -28,11 +28,6 @@ public class Ddr16GameplayEventLog implements Externalizable { @Column(name = "id") private long id; - /** The user who owns the machine this event occurred on. */ - @ManyToOne - @JoinColumn(name = "user_id") - private ButterflyUser machineOwner; - /** The PCBID of the event. */ @Column(name = "pcbid") private String pcbId; @@ -84,11 +79,10 @@ public class Ddr16GameplayEventLog implements Externalizable { public Ddr16GameplayEventLog() { } - public Ddr16GameplayEventLog(final ButterflyUser machineOwner, final String pcbId, final String model, final int retryCount, + public Ddr16GameplayEventLog(final String pcbId, final String model, final int retryCount, final String eventId, final int eventOrder, final long pcbTime, final long gameSession, final String stringData1, final String stringData2, final long numData1, final long numData2, final Ddr16Facility facility) { - this.machineOwner = machineOwner; this.pcbId = pcbId; this.model = model; this.retryCount = retryCount; @@ -106,7 +100,6 @@ public class Ddr16GameplayEventLog implements Externalizable { @Override public void writeExternal(ObjectOutput out) throws IOException { out.writeLong(this.id); - out.writeObject(this.machineOwner); out.writeUTF(this.pcbId); out.writeUTF(this.model); out.writeInt(this.retryCount); @@ -124,7 +117,6 @@ public class Ddr16GameplayEventLog implements Externalizable { @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { this.setId(in.readLong()); - this.setMachineOwner((ButterflyUser) in.readObject()); this.setPcbId(in.readUTF()); this.setModel(in.readUTF()); this.setRetryCount(in.readInt()); @@ -147,14 +139,6 @@ public class Ddr16GameplayEventLog implements Externalizable { this.id = id; } - public ButterflyUser getMachineOwner() { - return machineOwner; - } - - public void setMachineOwner(ButterflyUser machineOwner) { - this.machineOwner = machineOwner; - } - public String getPcbId() { return pcbId; } diff --git a/src/main/java/com/buttongames/butterfly/model/Ddr16PcbEventLog.java b/src/main/java/com/buttongames/butterfly/model/Ddr16PcbEventLog.java index b51761c..a957100 100644 --- a/src/main/java/com/buttongames/butterfly/model/Ddr16PcbEventLog.java +++ b/src/main/java/com/buttongames/butterfly/model/Ddr16PcbEventLog.java @@ -4,8 +4,6 @@ import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.ManyToOne; import javax.persistence.Table; import java.io.Externalizable; import java.io.IOException; @@ -29,11 +27,6 @@ public class Ddr16PcbEventLog implements Externalizable { @Column(name = "id") private long id; - /** The user who owns the machine this event occurred on. */ - @ManyToOne - @JoinColumn(name = "user_id") - private ButterflyUser machineOwner; - /** The PCBID of the event. */ @Column(name = "pcbid") private String pcbId; @@ -64,9 +57,8 @@ public class Ddr16PcbEventLog implements Externalizable { public Ddr16PcbEventLog() { } - public Ddr16PcbEventLog(final ButterflyUser machineOwner, final String pcbId, String model, final LocalDateTime time1, + public Ddr16PcbEventLog(final String pcbId, String model, final LocalDateTime time1, final LocalDateTime time2, final long sequence, final String name, final int value) { - this.machineOwner = machineOwner; this.pcbId = pcbId; this.model = model; this.time1 = time1; @@ -79,7 +71,6 @@ public class Ddr16PcbEventLog implements Externalizable { @Override public void writeExternal(ObjectOutput out) throws IOException { out.writeLong(this.id); - out.writeObject(this.machineOwner); out.writeUTF(this.pcbId); out.writeUTF(this.model); out.writeObject(this.time1); @@ -92,7 +83,6 @@ public class Ddr16PcbEventLog implements Externalizable { @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { this.setId(in.readLong()); - this.setMachineOwner((ButterflyUser) in.readObject()); this.setPcbId(in.readUTF()); this.setModel(in.readUTF()); this.setTime1((LocalDateTime) in.readObject()); @@ -110,14 +100,6 @@ public class Ddr16PcbEventLog implements Externalizable { this.id = id; } - public ButterflyUser getMachineOwner() { - return machineOwner; - } - - public void setMachineOwner(ButterflyUser machineOwner) { - this.machineOwner = machineOwner; - } - public String getPcbId() { return pcbId; } diff --git a/src/main/java/com/buttongames/butterfly/spring/configuration/HttpConfiguration.java b/src/main/java/com/buttongames/butterfly/spring/configuration/HttpConfiguration.java index b9b2035..3570ebb 100644 --- a/src/main/java/com/buttongames/butterfly/spring/configuration/HttpConfiguration.java +++ b/src/main/java/com/buttongames/butterfly/spring/configuration/HttpConfiguration.java @@ -1,5 +1,6 @@ package com.buttongames.butterfly.spring.configuration; +import com.buttongames.butterfly.hibernate.dao.impl.Ddr16PcbEventLogDao; import com.buttongames.butterfly.http.ButterflyHttpServer; import com.buttongames.butterfly.http.handlers.impl.EventLogRequestHandler; import com.buttongames.butterfly.http.handlers.impl.FacilityRequestHandler; @@ -56,8 +57,8 @@ public class HttpConfiguration { } @Bean - public PcbEventRequestHandler pcbEventRequestHandler() { - return new PcbEventRequestHandler(); + public PcbEventRequestHandler pcbEventRequestHandler(final Ddr16PcbEventLogDao pcbEventLogDao) { + return new PcbEventRequestHandler(pcbEventLogDao); } @Bean