diff --git a/ANSWERS.md b/ANSWERS.md index 9cd3f0d..97a06f2 100644 --- a/ANSWERS.md +++ b/ANSWERS.md @@ -2,10 +2,70 @@ ## A - The entities +Planning to parse the xml data in to four tables. + +MODEL +SUBMODEL +WHEELS +ENGINE + +Plan is to use the same MODEL table for all Model xml elements. +The differentiation between models under submodels and a regular model, will be based on the parent_sub_model_id column in MODEL table. +If the parent_sub_model_id column is blank or null then it is a regular model, if it has a value, then it is a model under submodels. + +Each Model table row will link to other three tables by it's Primary key model_id. Except for the submodels-model rows. + +For submodels-model rows there will be no link/dependency to Submodel table. (as there are no furher submodels under these models) + +This data design is to use minimum set of tables, by using a data dependency(parent_sub_model_id column). + + + + + + ## B - Ingest the data +Added the service, which will be called from the task. + + + + + ## C - Expose data with a RESTful API +Added API's in controller for the below + +http://localhost:8080/saveCar +Test call to save hard coded to database, to check the tables are loaded fine. + +http://localhost:8080/processCars +Call to ingest the xml data to database + +http://localhost:8080/getCarById?id=1 +Get car by model id url + +http://localhost:8080/getCarByBrand?brand=Aspire +Get car by brand name url + + + + + ## D - Adding images -## E - Improvements \ No newline at end of file +Different ways to do it for different scenarios + +#1 Save the image as BLOB in DB and send the same in response and convert it to Base64 to display. + +#2 Store the images in a filesystem and its path/uri in the database, send the uri links in json response if they can be directly accessed. + +#3 Send the response as Multipart HTTP respons, and add the images in the multi-part response as attachments to the caller. + + + +## E - Improvements + +If needed, we can add an additional table for submodels-model, +so the differentiation between model and submodels-model will be identified based on table rather than on data. +Not implemented.. diff --git a/UML diagram.html b/UML diagram.html new file mode 100644 index 0000000..097a687 --- /dev/null +++ b/UML diagram.html @@ -0,0 +1,12 @@ + + + + +UML diagram.html + + + +
+ + + diff --git a/cars/src/main/java/com/mooveit/cars/controller/CarsController.java b/cars/src/main/java/com/mooveit/cars/controller/CarsController.java new file mode 100644 index 0000000..3b70448 --- /dev/null +++ b/cars/src/main/java/com/mooveit/cars/controller/CarsController.java @@ -0,0 +1,190 @@ +package com.mooveit.cars.controller; + +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Marshaller; +import javax.xml.bind.Unmarshaller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + + +import com.mooveit.cars.domain.ENGINE; +import com.mooveit.cars.domain.MODEL; +import com.mooveit.cars.domain.SUBMODEL; +import com.mooveit.cars.domain.WHEELS; +import com.mooveit.cars.domain.model.mapper.DomainModelObjMapper; +import com.mooveit.cars.jaxb.model.Catalogue; +import com.mooveit.cars.jaxb.model.Model; +import com.mooveit.cars.model.MODELpojo; +import com.mooveit.cars.repositories.ModelRepository; +import com.mooveit.cars.service.CarsService; + +@RestController +public class CarsController { + + @Autowired + private ModelRepository ModelRepository; + + @Autowired + private CarsService carsService; + + @GetMapping + @RequestMapping(value = "/processCars") + public boolean processCars() { + + return carsService.processcars(); + + } + + + @GetMapping + @RequestMapping(value = "/getCarById") + public ResponseEntity getCarById(@RequestParam("id") int id) { +// StringWriter sw = new StringWriter(); +// try { +// JAXBContext jaxbContext = JAXBContext.newInstance(Model.class); +// Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); +// carsService.findModelById(id); +// jaxbMarshaller.marshal(carsService.findModelById(id) , sw); +// String xmlString = sw.toString(); +// return xmlString; + + MODEL DBModelObj = carsService.findModelById(id); + MODELpojo ModelObj = new MODELpojo(); + DomainModelObjMapper mapperObj = new DomainModelObjMapper(); + ModelObj = mapperObj.getDomainToModelObj(DBModelObj); + + return new ResponseEntity(ModelObj, + HttpStatus.OK); +// } catch (JAXBException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// } + +// return new ResponseEntity(carsService.findModelById(id), +// HttpStatus.OK); + + } + + + @GetMapping + @RequestMapping(value = "/getCarByBrand") + public ResponseEntity> getCarByBrand(@RequestParam("brand") String brand) { + List modelObjLst = new ArrayList(); + List DBModelObjLst = carsService.findModelByBrand(brand); + DomainModelObjMapper mapperObj = new DomainModelObjMapper(); + for(MODEL DBModelObj: DBModelObjLst) { + MODELpojo ModelObj = new MODELpojo(); + + ModelObj = mapperObj.getDomainToModelObj(DBModelObj); + modelObjLst.add(ModelObj); + } + return new ResponseEntity>(modelObjLst, + HttpStatus.OK); + + + } + + + + @GetMapping + @RequestMapping(value = "/saveCar") + public boolean saveCar() { + + + + + + + MODEL mymodel = new MODEL(); + //mymodel.setModelid(1); +// mymodel.setSubModelid(10); +// mymodel.setEngineid(101); +// mymodel.setWheelid(201); + mymodel.setName("Aspire"); + mymodel.setFrom(1994); + mymodel.setTo(1997); + mymodel.setType("subcompact"); + + //ModelRepository.save(mymodel); + + ENGINE engine = new ENGINE(); + engine.setPower("1400"); + engine.setType("gas"); + engine.setPrimarymodel(mymodel); + //engine.setEngineid(mymodel.getEngineid()); + + mymodel.setEngine(engine); + + WHEELS wheels = new WHEELS(); + wheels.setSize("R15"); + wheels.setType("STEEL"); + wheels.setPrimarymodel(mymodel); + //wheels.setWheelid(mymodel.getWheelid()); + + mymodel.setEngine(engine); + mymodel.setWheels(wheels); + + MODEL mychildmodel = new MODEL(); + //mysubmodel.setModelid(2); + //mysubmodel.setEngineid(102); + //mysubmodel.setWheelid(202); + mychildmodel.setName("Aspire 2"); + mychildmodel.setLine("hatchback"); + + ENGINE subengine = new ENGINE(); + subengine.setPower("1600"); + subengine.setType("gas"); + subengine.setPrimarymodel(mychildmodel); + + mychildmodel.setEngine(subengine); + + WHEELS subwheels = new WHEELS(); + subwheels.setSize("R15"); + subwheels.setType("STEEL"); + subwheels.setPrimarymodel(mychildmodel); + + mychildmodel.setWheels(subwheels); + + //mysubmodel.setEngine(subengine); + //mysubmodel.setWheels(subwheels); + + + + //ModelRepository.save(mysubmodel); + //submodel.setModelid(10000); + + SUBMODEL submodel = new SUBMODEL(); + + submodel.setPrimarymodel(mymodel); + + + mymodel.setSubmodel(submodel); + + + mychildmodel.setParentsubmodel(submodel); + + List childmodellist = new ArrayList(); + childmodellist.add(mychildmodel); + + submodel.setChildmodellst(childmodellist); + + + ModelRepository.save(mymodel); + //ModelRepository.save(mychildmodel); + + + + return true; + } +} diff --git a/cars/src/main/java/com/mooveit/cars/domain/ENGINE.java b/cars/src/main/java/com/mooveit/cars/domain/ENGINE.java new file mode 100644 index 0000000..331f5b4 --- /dev/null +++ b/cars/src/main/java/com/mooveit/cars/domain/ENGINE.java @@ -0,0 +1,68 @@ +package com.mooveit.cars.domain; + +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.MapsId; +import javax.persistence.OneToOne; +import javax.persistence.Table; + +@Entity +@Table(name = "ENGINE") +public class ENGINE implements Serializable { + + private static final long serialVersionUID = 5886234486065777539L; + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "ENGINE_ID") + private int engineid; + + @OneToOne + @JoinColumn(name = "MODEL_ID",nullable = false) + private MODEL primarymodel; + + public MODEL getPrimarymodel() { + return primarymodel; + } + + public void setPrimarymodel(MODEL primarymodel) { + this.primarymodel = primarymodel; + } + + @Column(name = "POWER") + private String power; + + @Column(name = "TYPE") + private String type; + + public String getPower() { + return power; + } + + public void setPower(String power) { + this.power = power; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public int getEngineid() { + return engineid; + } + + public void setEngineid(int engineid) { + this.engineid = engineid; + } + +} diff --git a/cars/src/main/java/com/mooveit/cars/domain/MODEL.java b/cars/src/main/java/com/mooveit/cars/domain/MODEL.java new file mode 100644 index 0000000..b0343ce --- /dev/null +++ b/cars/src/main/java/com/mooveit/cars/domain/MODEL.java @@ -0,0 +1,132 @@ +package com.mooveit.cars.domain; + +import java.io.Serializable; + +import javax.annotation.Generated; +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.OneToOne; +import javax.persistence.Table; + +@Entity +@Table(name = "MODEL") +public class MODEL implements Serializable { + + private static final long serialVersionUID = 5886434486065777539L; + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "MODEL_ID") + private int modelid; + +// @Column(name = "SUB_MODEL_ID") +// private int subModelid; +// +// @Column(name = "ENGINE_ID") +// private int engineid; +// +// @Column(name = "WHEEL_ID") +// private int wheelid; + + @Column(name = "NAME") + private String name; + + @Column(name = "FRM") + private int from; + + @Column(name = "TO") + private int to; + + @Column(name = "TYPE") + private String type; + + @Column(name = "LINE") + private String line; + + + @OneToOne(cascade=CascadeType.ALL,fetch = FetchType.LAZY,mappedBy="primarymodel") + private SUBMODEL submodel; + + @OneToOne(cascade=CascadeType.ALL,fetch = FetchType.LAZY,mappedBy="primarymodel") + private WHEELS wheels; + + @OneToOne(cascade=CascadeType.ALL,fetch = FetchType.LAZY,mappedBy="primarymodel") + private ENGINE engine; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "PARENT_SUB_MODEL_ID") + private SUBMODEL parentsubmodel; + + public int getModelid() { + return modelid; + } + public void setModelid(int modelid) { + this.modelid = modelid; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public int getFrom() { + return from; + } + public void setFrom(int from) { + this.from = from; + } + public int getTo() { + return to; + } + public void setTo(int to) { + this.to = to; + } + public String getType() { + return type; + } + public void setType(String type) { + this.type = type; + } + + public WHEELS getWheels() { + return wheels; + } + public void setWheels(WHEELS wheels) { + this.wheels = wheels; + } + public ENGINE getEngine() { + return engine; + } + public void setEngine(ENGINE engine) { + this.engine = engine; + } + public SUBMODEL getSubmodel() { + return submodel; + } + public void setSubmodel(SUBMODEL submodel) { + this.submodel = submodel; + } + public String getLine() { + return line; + } + public void setLine(String line) { + this.line = line; + } + public SUBMODEL getParentsubmodel() { + return parentsubmodel; + } + public void setParentsubmodel(SUBMODEL parentsubmodel) { + this.parentsubmodel = parentsubmodel; + } + + + + +} diff --git a/cars/src/main/java/com/mooveit/cars/domain/SUBMODEL.java b/cars/src/main/java/com/mooveit/cars/domain/SUBMODEL.java new file mode 100644 index 0000000..467ae12 --- /dev/null +++ b/cars/src/main/java/com/mooveit/cars/domain/SUBMODEL.java @@ -0,0 +1,60 @@ +package com.mooveit.cars.domain; + +import java.io.Serializable; +import java.util.List; + +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.MapsId; +import javax.persistence.OneToMany; +import javax.persistence.OneToOne; +import javax.persistence.Table; + +import com.fasterxml.jackson.annotation.JsonBackReference; + +@Entity +@Table(name = "SUBMODEL") +public class SUBMODEL implements Serializable { + + private static final long serialVersionUID = 5186434486065777539L; + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "SUB_MODEL_ID") + private int subModelid; + + @OneToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "MODEL_ID",nullable = false) + private MODEL primarymodel; + + @OneToMany(cascade=CascadeType.ALL,fetch = FetchType.LAZY,mappedBy="parentsubmodel") + private List childmodellst; + + + public MODEL getPrimarymodel() { + return primarymodel; + } + + public void setPrimarymodel(MODEL primarymodel) { + this.primarymodel = primarymodel; + } + + public List getChildmodellst() { + return childmodellst; + } + + public void setChildmodellst(List childmodellst) { + this.childmodellst = childmodellst; + } + + + + +} \ No newline at end of file diff --git a/cars/src/main/java/com/mooveit/cars/domain/WHEELS.java b/cars/src/main/java/com/mooveit/cars/domain/WHEELS.java new file mode 100644 index 0000000..8a0f266 --- /dev/null +++ b/cars/src/main/java/com/mooveit/cars/domain/WHEELS.java @@ -0,0 +1,68 @@ +package com.mooveit.cars.domain; + +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.OneToOne; +import javax.persistence.Table; + +@Entity +@Table(name = "WHEELS") +public class WHEELS implements Serializable{ + + private static final long serialVersionUID = 5886434486065777739L; + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "WHEEL_ID") + private int wheelid; + + @OneToOne + //@JoinColumn(name = "MODEL_ID", insertable = false, updatable = false, nullable = false) + @JoinColumn(name = "MODEL_ID",nullable = false) + private MODEL primarymodel; + + @Column(name = "SIZE") + private String size; + + public MODEL getPrimarymodel() { + return primarymodel; + } + + public void setPrimarymodel(MODEL primarymodel) { + this.primarymodel = primarymodel; + } + + @Column(name = "TYPE") + private String type; + + public String getSize() { + return size; + } + + public void setSize(String size) { + this.size = size; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public int getWheelid() { + return wheelid; + } + + public void setWheelid(int wheelid) { + this.wheelid = wheelid; + } + +} diff --git a/cars/src/main/java/com/mooveit/cars/domain/model/mapper/DomainModelObjMapper.java b/cars/src/main/java/com/mooveit/cars/domain/model/mapper/DomainModelObjMapper.java new file mode 100644 index 0000000..6ff0c64 --- /dev/null +++ b/cars/src/main/java/com/mooveit/cars/domain/model/mapper/DomainModelObjMapper.java @@ -0,0 +1,71 @@ +package com.mooveit.cars.domain.model.mapper; + +import java.util.ArrayList; +import java.util.List; + +import com.mooveit.cars.domain.ENGINE; +import com.mooveit.cars.domain.MODEL; +import com.mooveit.cars.domain.SUBMODEL; +import com.mooveit.cars.domain.WHEELS; +import com.mooveit.cars.model.ENGINEpojo; +import com.mooveit.cars.model.MODELpojo; +import com.mooveit.cars.model.SUBMODELpojo; +import com.mooveit.cars.model.WHEELSpojo; + +public class DomainModelObjMapper { + + public MODELpojo getDomainToModelObj(MODEL DBModelObj) { + MODELpojo ModelObj = new MODELpojo(); + + ModelObj.setFrom(DBModelObj.getFrom()); + ModelObj.setTo(DBModelObj.getTo()); + ModelObj.setType(DBModelObj.getType()); + ModelObj.setLine(DBModelObj.getLine()); + ModelObj.setName(DBModelObj.getName()); + + ModelObj.setEngine(getEnginePojoFromDBEngineObj(DBModelObj.getEngine())); + ModelObj.setWheels(getWheelsPojoFromDBWheelsObj(DBModelObj.getWheels())); + ModelObj.setSubmodel(getSubMdlPojoFromDBSubMdlObj(DBModelObj.getSubmodel())); + + return ModelObj; + } + + private ENGINEpojo getEnginePojoFromDBEngineObj(ENGINE DBEngineObj) { + ENGINEpojo EngineObj = new ENGINEpojo(); + + EngineObj.setPower(DBEngineObj.getPower()); + EngineObj.setType(DBEngineObj.getType()); + return EngineObj; + } + + private WHEELSpojo getWheelsPojoFromDBWheelsObj(WHEELS DBWHeelsObj) { + WHEELSpojo WheelsObj = new WHEELSpojo(); + + WheelsObj.setSize(DBWHeelsObj.getSize()); + WheelsObj.setType(DBWHeelsObj.getType()); + + return WheelsObj; + } + + + private SUBMODELpojo getSubMdlPojoFromDBSubMdlObj(SUBMODEL DBSubModelObj) { + SUBMODELpojo SubModelObj = new SUBMODELpojo(); + List modelLst = new ArrayList(); + if (DBSubModelObj != null && DBSubModelObj.getChildmodellst() != null) { + for (MODEL childModel : DBSubModelObj.getChildmodellst()) { + modelLst.add(getDomainToModelObj(childModel)); + } + SubModelObj.setChildmodellst(modelLst); + } + else { + SubModelObj = null; + } + + return SubModelObj; + } + + + + +} + diff --git a/cars/src/main/java/com/mooveit/cars/jaxb/model/Catalogue.java b/cars/src/main/java/com/mooveit/cars/jaxb/model/Catalogue.java new file mode 100644 index 0000000..915fdb9 --- /dev/null +++ b/cars/src/main/java/com/mooveit/cars/jaxb/model/Catalogue.java @@ -0,0 +1,25 @@ +package com.mooveit.cars.jaxb.model; + +import java.util.List; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "CATALOGUE") +@XmlAccessorType (XmlAccessType.FIELD) +public class Catalogue { + + @XmlElement(name="MODEL") + private List models; + + public List getModels() { + return models; + } + + public void setModels(List models) { + this.models = models; + } + +} diff --git a/cars/src/main/java/com/mooveit/cars/jaxb/model/Engine.java b/cars/src/main/java/com/mooveit/cars/jaxb/model/Engine.java new file mode 100644 index 0000000..d9862e3 --- /dev/null +++ b/cars/src/main/java/com/mooveit/cars/jaxb/model/Engine.java @@ -0,0 +1,33 @@ +package com.mooveit.cars.jaxb.model; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "ENGINE") +@XmlAccessorType (XmlAccessType.FIELD) +public class Engine { + + @XmlAttribute(name = "power") + private String power; + @XmlAttribute(name = "type") + private String type; + + public String getPower() { + return power; + } + + + public void setPower(String power) { + this.power = power; + } + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + +} diff --git a/cars/src/main/java/com/mooveit/cars/jaxb/model/Model.java b/cars/src/main/java/com/mooveit/cars/jaxb/model/Model.java new file mode 100644 index 0000000..2cb74a3 --- /dev/null +++ b/cars/src/main/java/com/mooveit/cars/jaxb/model/Model.java @@ -0,0 +1,99 @@ +package com.mooveit.cars.jaxb.model; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "MODEL") +@XmlAccessorType (XmlAccessType.FIELD) +public class Model { + + @XmlAttribute(name = "name") + private String name; + @XmlAttribute(name = "from") + private int from; + @XmlAttribute(name = "to") + private int to; + @XmlAttribute(name = "type") + private String type; + @XmlAttribute(name = "line") + private String line; + + @XmlElement(name="ENGINE") + private Engine engine; + @XmlElement(name="WHEELS") + private Wheel wheel; + @XmlElement(name="SUBMODELS") + private SubModel submodel; + + public String getName() { + return name; + } + + + public void setName(String name) { + this.name = name; + } + + public int getFrom() { + return from; + } + + + public void setFrom(int from) { + this.from = from; + } + + public int getTo() { + return to; + } + + + public void setTo(int to) { + this.to = to; + } + + public String getType() { + return type; + } + + + public void setType(String type) { + this.type = type; + } + + public String getLine() { + return line; + } + + public void setLine(String line) { + this.line = line; + } + + public Engine getEngine() { + return engine; + } + + public void setEngine(Engine engine) { + this.engine = engine; + } + + public Wheel getWheel() { + return wheel; + } + + public void setWheel(Wheel wheel) { + this.wheel = wheel; + } + + public SubModel getSubmodel() { + return submodel; + } + + public void setSubmodel(SubModel submodel) { + this.submodel = submodel; + } + +} \ No newline at end of file diff --git a/cars/src/main/java/com/mooveit/cars/jaxb/model/SubModel.java b/cars/src/main/java/com/mooveit/cars/jaxb/model/SubModel.java new file mode 100644 index 0000000..14d5988 --- /dev/null +++ b/cars/src/main/java/com/mooveit/cars/jaxb/model/SubModel.java @@ -0,0 +1,26 @@ +package com.mooveit.cars.jaxb.model; + +import java.util.List; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "SUBMODELS") +@XmlAccessorType (XmlAccessType.FIELD) +public class SubModel { + + @XmlElement(name="MODEL") + private List submodels; + + public List getSubmodels() { + return submodels; + } + + public void setSubmodels(List submodels) { + this.submodels = submodels; + } + + +} diff --git a/cars/src/main/java/com/mooveit/cars/jaxb/model/Wheel.java b/cars/src/main/java/com/mooveit/cars/jaxb/model/Wheel.java new file mode 100644 index 0000000..4699f72 --- /dev/null +++ b/cars/src/main/java/com/mooveit/cars/jaxb/model/Wheel.java @@ -0,0 +1,34 @@ +package com.mooveit.cars.jaxb.model; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "WHEELS") +@XmlAccessorType (XmlAccessType.FIELD) +public class Wheel { + + @XmlAttribute(name = "size") + private String size; + @XmlAttribute(name = "type") + private String type; + + public String getSize() { + return size; + } + + + public void setSize(String size) { + this.size = size; + } + public String getType() { + return type; + } + + + public void setType(String type) { + this.type = type; + } + +} diff --git a/cars/src/main/java/com/mooveit/cars/model/ENGINEpojo.java b/cars/src/main/java/com/mooveit/cars/model/ENGINEpojo.java new file mode 100644 index 0000000..8e22779 --- /dev/null +++ b/cars/src/main/java/com/mooveit/cars/model/ENGINEpojo.java @@ -0,0 +1,43 @@ +package com.mooveit.cars.model; + +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.MapsId; +import javax.persistence.OneToOne; +import javax.persistence.Table; + +public class ENGINEpojo implements Serializable { + + private static final long serialVersionUID = 5886222286065777539L; + + + private String power; + + private String type; + + + public String getPower() { + return power; + } + + public void setPower(String power) { + this.power = power; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + + +} diff --git a/cars/src/main/java/com/mooveit/cars/model/MODELpojo.java b/cars/src/main/java/com/mooveit/cars/model/MODELpojo.java new file mode 100644 index 0000000..14c21af --- /dev/null +++ b/cars/src/main/java/com/mooveit/cars/model/MODELpojo.java @@ -0,0 +1,97 @@ +package com.mooveit.cars.model; + +import java.io.Serializable; + +import javax.annotation.Generated; +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.OneToOne; +import javax.persistence.Table; + +import com.mooveit.cars.domain.ENGINE; +import com.mooveit.cars.domain.SUBMODEL; +import com.mooveit.cars.domain.WHEELS; + +public class MODELpojo implements Serializable { + + private static final long serialVersionUID = 5886411186065777539L; + + + private String name; + + private int from; + + private int to; + + private String type; + + private String line; + + private SUBMODELpojo submodel; + + private WHEELSpojo wheels; + + private ENGINEpojo engine; + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public int getFrom() { + return from; + } + public void setFrom(int from) { + this.from = from; + } + public int getTo() { + return to; + } + public void setTo(int to) { + this.to = to; + } + public String getType() { + return type; + } + public void setType(String type) { + this.type = type; + } + + public String getLine() { + return line; + } + public void setLine(String line) { + this.line = line; + } + public SUBMODELpojo getSubmodel() { + return submodel; + } + public void setSubmodel(SUBMODELpojo submodel) { + this.submodel = submodel; + } + public WHEELSpojo getWheels() { + return wheels; + } + public void setWheels(WHEELSpojo wheels) { + this.wheels = wheels; + } + public ENGINEpojo getEngine() { + return engine; + } + public void setEngine(ENGINEpojo engine) { + this.engine = engine; + } + + + + + +} diff --git a/cars/src/main/java/com/mooveit/cars/model/SUBMODELpojo.java b/cars/src/main/java/com/mooveit/cars/model/SUBMODELpojo.java new file mode 100644 index 0000000..c0f144d --- /dev/null +++ b/cars/src/main/java/com/mooveit/cars/model/SUBMODELpojo.java @@ -0,0 +1,44 @@ +package com.mooveit.cars.model; + +import java.io.Serializable; +import java.util.List; + +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.MapsId; +import javax.persistence.OneToMany; +import javax.persistence.OneToOne; +import javax.persistence.Table; + +import com.fasterxml.jackson.annotation.JsonBackReference; +import com.mooveit.cars.domain.MODEL; + +public class SUBMODELpojo implements Serializable { + + private static final long serialVersionUID = 5186433386065777539L; + + private List childmodellst; + + public List getChildmodellst() { + return childmodellst; + } + + public void setChildmodellst(List childmodellst) { + this.childmodellst = childmodellst; + } + + + + + + + + +} \ No newline at end of file diff --git a/cars/src/main/java/com/mooveit/cars/model/WHEELSpojo.java b/cars/src/main/java/com/mooveit/cars/model/WHEELSpojo.java new file mode 100644 index 0000000..c9e3bd9 --- /dev/null +++ b/cars/src/main/java/com/mooveit/cars/model/WHEELSpojo.java @@ -0,0 +1,40 @@ +package com.mooveit.cars.model; + +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.OneToOne; +import javax.persistence.Table; + +public class WHEELSpojo implements Serializable{ + + private static final long serialVersionUID = 5886434486065777739L; + + + private String size; + + private String type; + + public String getSize() { + return size; + } + + public void setSize(String size) { + this.size = size; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + +} diff --git a/cars/src/main/java/com/mooveit/cars/repositories/EngineRepository.java b/cars/src/main/java/com/mooveit/cars/repositories/EngineRepository.java new file mode 100644 index 0000000..5cb8047 --- /dev/null +++ b/cars/src/main/java/com/mooveit/cars/repositories/EngineRepository.java @@ -0,0 +1,11 @@ +package com.mooveit.cars.repositories; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +import com.mooveit.cars.domain.ENGINE; + +@Repository +public interface EngineRepository extends CrudRepository { + +} diff --git a/cars/src/main/java/com/mooveit/cars/repositories/ModelRepository.java b/cars/src/main/java/com/mooveit/cars/repositories/ModelRepository.java new file mode 100644 index 0000000..89f0973 --- /dev/null +++ b/cars/src/main/java/com/mooveit/cars/repositories/ModelRepository.java @@ -0,0 +1,16 @@ +package com.mooveit.cars.repositories; + +import org.springframework.stereotype.Repository; + +import java.util.List; + +import org.springframework.data.repository.CrudRepository; + +import com.mooveit.cars.domain.MODEL; + +@Repository +public interface ModelRepository extends CrudRepository { + + List findByName(String brand); + +} diff --git a/cars/src/main/java/com/mooveit/cars/repositories/SubmodelRepository.java b/cars/src/main/java/com/mooveit/cars/repositories/SubmodelRepository.java new file mode 100644 index 0000000..02880fb --- /dev/null +++ b/cars/src/main/java/com/mooveit/cars/repositories/SubmodelRepository.java @@ -0,0 +1,11 @@ +package com.mooveit.cars.repositories; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +import com.mooveit.cars.domain.SUBMODEL; + +@Repository +public interface SubmodelRepository extends CrudRepository { + +} diff --git a/cars/src/main/java/com/mooveit/cars/repositories/WheelsRepository.java b/cars/src/main/java/com/mooveit/cars/repositories/WheelsRepository.java new file mode 100644 index 0000000..1811277 --- /dev/null +++ b/cars/src/main/java/com/mooveit/cars/repositories/WheelsRepository.java @@ -0,0 +1,11 @@ +package com.mooveit.cars.repositories; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +import com.mooveit.cars.domain.WHEELS; + +@Repository +public interface WheelsRepository extends CrudRepository { + +} diff --git a/cars/src/main/java/com/mooveit/cars/service/CarsService.java b/cars/src/main/java/com/mooveit/cars/service/CarsService.java new file mode 100644 index 0000000..649aeb5 --- /dev/null +++ b/cars/src/main/java/com/mooveit/cars/service/CarsService.java @@ -0,0 +1,123 @@ +package com.mooveit.cars.service; + +import java.util.ArrayList; +import java.util.List; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.Unmarshaller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.ResourceLoader; +import org.springframework.stereotype.Service; + +import com.mooveit.cars.domain.ENGINE; +import com.mooveit.cars.domain.MODEL; +import com.mooveit.cars.domain.SUBMODEL; +import com.mooveit.cars.domain.WHEELS; +import com.mooveit.cars.jaxb.model.Catalogue; +import com.mooveit.cars.jaxb.model.Engine; +import com.mooveit.cars.jaxb.model.Model; +import com.mooveit.cars.jaxb.model.SubModel; +import com.mooveit.cars.jaxb.model.Wheel; +import com.mooveit.cars.repositories.ModelRepository; + +@Service +public class CarsService { + + @Autowired + ResourceLoader resourceLoader; + + @Autowired + private ModelRepository ModelRepository; + + + + public boolean processcars() { + + try { + JAXBContext jaxbContext = JAXBContext.newInstance(Catalogue.class); + Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); + Catalogue catalog = (Catalogue) jaxbUnmarshaller.unmarshal(resourceLoader.getResource("classpath:ford-example.xml").getFile()); + System.out.println("completed processing"); + + for(Model data : catalog.getModels()) { + ModelRepository.save(mapModelForDB(data)); + } + + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return true; + + } + + + public MODEL findModelById(Integer id) { + return ModelRepository.findById(id).orElse(null); + } + + + public List findModelByBrand(String brand) { + return ModelRepository.findByName(brand); + } + + private MODEL mapModelForDB(Model model) { + MODEL retval = new MODEL(); + retval.setFrom(model.getFrom()); + retval.setLine(model.getLine()); + retval.setName(model.getName()); + retval.setTo(model.getTo()); + retval.setType(model.getType()); + retval.setEngine(mapEngineForDB(model.getEngine(),retval)); + retval.setWheels(mapWheelsForDB(model.getWheel(),retval)); + retval.setSubmodel(mapSubModelsDB(model.getSubmodel(),retval)); + if(retval.getSubmodel() != null) + for(MODEL childModel: retval.getSubmodel().getChildmodellst()) { + childModel.setParentsubmodel(retval.getSubmodel()); + } + + return retval; + } + + private ENGINE mapEngineForDB(Engine engine,MODEL model) { + ENGINE retval = new ENGINE(); + retval.setPower(engine.getPower()); + retval.setType(engine.getType()); + retval.setPrimarymodel(model); + return retval; + } + + private WHEELS mapWheelsForDB(Wheel wheel,MODEL model) { + WHEELS retval = new WHEELS(); + retval.setSize(wheel.getSize()); + retval.setType(wheel.getType()); + retval.setPrimarymodel(model); + return retval; + } + + private SUBMODEL mapSubModelsDB(SubModel submodel,MODEL model) { + SUBMODEL retval = new SUBMODEL(); + + if (null != submodel ) { + List childmodellst = new ArrayList(); + for( Model data : submodel.getSubmodels()) { + MODEL dbmodel = mapModelForDB(data); + //ModelRepository.save(dbmodel); + childmodellst.add(dbmodel); + } + retval.setChildmodellst(childmodellst); + retval.setPrimarymodel(model); + return retval; + }else + { + return null; + } + + + + } + + +} diff --git a/cars/src/main/java/com/mooveit/cars/tasks/FordIngesterTask.java b/cars/src/main/java/com/mooveit/cars/tasks/FordIngesterTask.java index a04f791..d60cc0e 100644 --- a/cars/src/main/java/com/mooveit/cars/tasks/FordIngesterTask.java +++ b/cars/src/main/java/com/mooveit/cars/tasks/FordIngesterTask.java @@ -1,15 +1,31 @@ package com.mooveit.cars.tasks; -import lombok.extern.slf4j.Slf4j; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; +import com.mooveit.cars.service.CarsService; + +import lombok.extern.slf4j.Slf4j; + @Slf4j @Service public class FordIngesterTask { + protected Logger log = LogManager.getLogger(this.getClass()); + + @Autowired + private CarsService carsService; + @Scheduled(cron = "${cars.ford.ingester.runCron}") public void ingestFile() { - log.warn("Not implemented yet."); + + log.info("startnig the batch process"); + boolean success = carsService.processcars(); + log.info("ending the batch process, status is : " + success); + + } } diff --git a/cars/src/main/resources/application.yml b/cars/src/main/resources/application.yml index 436f591..c66afd2 100644 --- a/cars/src/main/resources/application.yml +++ b/cars/src/main/resources/application.yml @@ -1,7 +1,7 @@ cars: ford: ingester: - runCron: '0 * * ? * *' #each minute + runCron: '0 * * ? * *' spring: datasource: @@ -11,4 +11,7 @@ spring: password: jpa: database-platform: 'org.hibernate.dialect.H2Dialect' + show-sql: true + hibernate: + ddl-auto: create-drop h2.console.enabled: true diff --git a/cars/src/main/resources/cars-format.xsd b/cars/src/main/resources/cars-format.xsd new file mode 100644 index 0000000..ae68271 --- /dev/null +++ b/cars/src/main/resources/cars-format.xsd @@ -0,0 +1,74 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file