Skip to content

Commit

Permalink
VehiclesInteractor CRUD operations, together with Camel-REST API endp…
Browse files Browse the repository at this point in the history
…oints and Postman collection
  • Loading branch information
alexm98 committed Jan 24, 2021
1 parent 6eb3a88 commit e8f17ba
Show file tree
Hide file tree
Showing 6 changed files with 275 additions and 493 deletions.
141 changes: 42 additions & 99 deletions src/CamelComponents/CamelREST.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package CamelComponents;

import Models.Vehicle;
import core.StationsInteractor;
import core.TimeTablesInteractor;
import core.VehiclesInteractor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.rest.RestBindingMode;
import org.springframework.stereotype.Component;
import org.xml.sax.SAXException;
import parsers.ParserUtils;

import javax.xml.bind.JAXBException;
Expand All @@ -13,9 +15,13 @@
@Component
public class CamelREST extends RouteBuilder {
private final StationsInteractor stations_interactor;
private final VehiclesInteractor vehicles_interactor;
private final TimeTablesInteractor timetables_interactor;
private final ParserUtils putils;

public CamelREST() throws JAXBException, ParserConfigurationException, SAXException {
public CamelREST() throws JAXBException, ParserConfigurationException {
this.vehicles_interactor = new VehiclesInteractor("data/vehicles.xml");
this.timetables_interactor = new TimeTablesInteractor("data/timetables.xml");
this.stations_interactor = new StationsInteractor("data/statii-ratt.xml");
this.putils = new ParserUtils("data/statii-ratt.xml");
}
Expand All @@ -38,103 +44,40 @@ public void configure() throws Exception {
.produces("application/xml")
.route()
.bean(stations_interactor, "getStation(${header.id})")
.endRest()

.get("/vehicles")
.produces("application/xml")
.route()
.bean(vehicles_interactor, "getAllVehicles")
.endRest()

.get("/vehicle/{id}")
.produces("application/xml")
.route()
.bean(vehicles_interactor, "getVehicle(${header.id})")
.endRest()

.put("/vehicle/{id}")
.type(Vehicle.class)
.consumes("application/xml")
.produces("application/xml")
.route()
.bean(vehicles_interactor, "replaceVehicle(${header.id}, ${body})")
.endRest()

.post("/vehicle")
.type(Vehicle.class)
.consumes("application/xml")
.produces("application/xml")
.route()
.bean(vehicles_interactor, "createVehicle(${body})")
.endRest()

.delete("/vehicle/{id}")
.produces("application/xml")
.route()
.bean(vehicles_interactor, "deleteVehicle(${header.id})")
.endRest();
//
// .get("/publications")
// .produces("application/xml")
// .route()
// .bean(xml_interactor, "getAllPublications")
// .endRest()
//
// .get("/affiliations")
// .produces("application/xml")
// .route()
// .bean(xml_interactor, "getAllAffiliations")
// .endRest()
//
// .get("/author/{id}")
// .produces("application/xml")
// .route()
// .bean(xml_interactor, "getAuthor(${header.id})")
// .endRest()
//
// .put("/author/{id}")
// .type(Author.class)
// .consumes("application/xml")
// .produces("application/xml")
// .route()
// .to("log:mylogger?showAll=true")
// .bean(xml_interactor, "replaceAuthor(${header.id}, ${body})")
// .endRest()
//
// .delete("/author/{id}")
// .produces("application/xml")
// .route()
// .bean(xml_interactor, "deleteAuthor(${header.id})")
// .endRest()
//
// .post("/author/{name}")
// .produces("application/xml")
// .route()
// .bean(xml_interactor, "createAuthor(${header.name})")
// .endRest()
//
// // affiliations
//
// .get("/affiliation/{rid}")
// .produces("application/xml")
// .route()
// .bean(xml_interactor, "getAffiliation(${header.rid})")
// .endRest()
//
// .put("/affiliation/{rid}")
// .type(Affiliation.class)
// .produces("application/xml")
// .consumes("application/xml")
// .route()
// .to("log:mylogger?showAll=true")
// .bean(xml_interactor, "replaceAffiliation(${header.rid}, ${body})")
// .endRest()
//
// .delete("/affiliation/{rid}")
// .produces("application/xml")
// .route()
// .bean(xml_interactor, "deleteAffiliation(${header.rid})")
// .endRest()
//
// .post("/affiliation/{rid}/{institution_name}")
// .produces("application/xml")
// .route()
// .bean(xml_interactor, "createAffiliation(${header.rid}, ${header.institution_name})")
// .endRest()
//
// // Publications
//
// .get("/publication?doi={doi}")
// .produces("application/xml")
// .route()
// .bean(xml_interactor, "getPublication(${header.doi})")
// .endRest()
//
// .put("/publication?doi={doi}")
// .type(Article.class)
// .produces("application/xml")
// .consumes("application/xml")
// .route()
// .bean(xml_interactor, "replacePublication(${body})")
// .endRest()
//
// .delete("/publication?doi={doi}")
// .produces("application/xml")
// .route()
// .bean(xml_interactor, "deletePublication(${header.doi})")
// .endRest()
//
// .post("/publication?doi={doi}")
// .type(Article.class)
// .produces("application/xml")
// .route()
// .bean(xml_interactor, "createPublication(${body})")
// .endRest();
}
};
4 changes: 2 additions & 2 deletions src/Models/Vehicle.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public Vehicle(int id, String name, String type){

public String toString(){
return "Vehicle{" +
", id=" + this.vehicleID +
"id=" + this.vehicleID +
", name=" + this.vehicleName +
", type=" + this.vehicleType;
", type=" + this.vehicleType + "}";
}
}
22 changes: 10 additions & 12 deletions src/STPTMain.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import parsers.ParserUtils;
import core.*;

import org.w3c.dom.Document;
import CamelComponents.CamelREST;
import org.apache.camel.main.Main;
import org.w3c.dom.NodeList;

public class STPTMain {
Expand All @@ -14,11 +12,11 @@ public static void IterateNodeList(NodeList query_res){
}

public static void main(String args[]) throws Exception {
ParserUtils putils = new ParserUtils("data/statii-ratt.xml");
Document doc = putils.parseJAXB();
StationsInteractor s = new StationsInteractor("data/statii-ratt.xml");
IterateNodeList(s.getAllStations());
System.out.println(s.getStation(4680));
// ParserUtils putils = new ParserUtils("data/statii-ratt.xml");
// Document doc = putils.parseJAXB();
// StationsInteractor s = new StationsInteractor("data/statii-ratt.xml");
// IterateNodeList(s.getAllStations());
// System.out.println(s.getStation(4680));
//
// VehiclesInteractor v = new VehiclesInteractor("data/vehicles.xml");
// IterateNodeList(v.getAllVehicles());
Expand All @@ -28,8 +26,8 @@ public static void main(String args[]) throws Exception {
// IterateNodeList(t.getAllTimeTables());
// IterateNodeList(t.getTimeTable(1106));

// Main main = new Main();
// main.configure().addRoutesBuilder(new CamelREST());
// main.run(args);
Main main = new Main();
main.configure().addRoutesBuilder(new CamelREST());
main.run(args);
}
}
50 changes: 48 additions & 2 deletions src/core/VehiclesInteractor.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package core;

import Models.Vehicle;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import parsers.ParserUtils;
import parsers.XPathUtils;
Expand All @@ -23,12 +26,55 @@ public VehiclesInteractor(String path_to_doc) throws
this.xputils = new XPathUtils(this.document);
}

public Node createVehicle(Integer new_id, String vehicleName, String vehicleType) throws XPathExpressionException {
// get last vehicle ID, create a new node and add it to it's parent
Element last_vehicle = (Element) (xputils.QueryXPath("//vehicle[not(@id <= preceding-sibling::vehicle/@id) and not(@id <=following-sibling::vehicle/@id)]").item(0));

if(new_id == null) {
new_id = Integer.parseInt(last_vehicle.getAttribute("id")) + 1;
}

Element new_vehicle = this.document.createElement("vehicle");
new_vehicle.setAttribute("id", new_id.toString());
Element vehicle_name = this.document.createElement("vehicle-name");
vehicle_name.setTextContent(vehicleName);
Element vehicle_type = this.document.createElement("vehicle-type");
vehicle_type.setTextContent(vehicleType);

new_vehicle.appendChild(vehicle_name);
new_vehicle.appendChild(vehicle_type);
last_vehicle.getParentNode().appendChild(new_vehicle);

return new_vehicle;
}

public Node createVehicle(Vehicle v) throws XPathExpressionException {
return this.createVehicle(v.vehicleID, v.vehicleName, v.vehicleType);
}


public NodeList getAllVehicles() throws XPathExpressionException {
return xputils.QueryXPath("/root/vehicles");
}

public NodeList getVehicle(Integer vehicle_id) throws XPathExpressionException {
return xputils.QueryXPath(String.format("//vehicle[@id=%s]", vehicle_id));
public Node getVehicle(Integer vehicle_id) throws XPathExpressionException {
return xputils.QueryXPath(String.format("//vehicle[@id=%s]", vehicle_id)).item(0);
}

public Document deleteVehicle(Integer id) throws XPathExpressionException {
Element vehicle_to_delete = (Element) xputils.QueryXPath(String.format("//vehicle[@id=%s]", id)).item(0);
vehicle_to_delete.getParentNode().removeChild(vehicle_to_delete);

return this.document;
}

public Document replaceVehicle(Integer id, Vehicle vehicle) throws XPathExpressionException {
System.out.println("Replacing: " + vehicle.toString());
Node node_to_replace = this.getVehicle(id);
Node new_au = this.createVehicle(vehicle.vehicleID, vehicle.vehicleName, vehicle.vehicleType);
node_to_replace.getParentNode().replaceChild(new_au, node_to_replace);

return this.document;
}

public Document getDocument(){
Expand Down
Loading

0 comments on commit e8f17ba

Please sign in to comment.