Skip to content

JaiSureshS pull request for Java chanllenge #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion ANSWERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
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..
12 changes: 12 additions & 0 deletions UML diagram.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=5,IE=9" ><![endif]-->
<!DOCTYPE html>
<html>
<head>
<title>UML diagram.html</title>
<meta charset="utf-8"/>
</head>
<body>
<div class="mxgraph" style="max-width:100%;border:1px solid transparent;" data-mxgraph="{&quot;highlight&quot;:&quot;#0000ff&quot;,&quot;nav&quot;:true,&quot;resize&quot;:true,&quot;xml&quot;:&quot;&lt;mxfile host=\&quot;app.diagrams.net\&quot; modified=\&quot;2021-07-17T21:41:10.088Z\&quot; agent=\&quot;5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36\&quot; etag=\&quot;9BGKIPqewPWn6wpTjSX2\&quot; version=\&quot;14.8.6\&quot; type=\&quot;device\&quot;&gt;&lt;diagram id=\&quot;nYdVNrhF6791ZzVGLfTD\&quot; name=\&quot;Page-1\&quot;&gt;7VnbctowEP0aZtoHGF+45ZGLaTJpEgbSSdKXjGwL49ZYriwC5Ou7suS7uZSStplCJuA9Wq3Wu0fHNtT0wWL9iaJgfkNs7NU0xV7X9GFN0zpdDd45sJGA3hWAQ11bQGoKTN1XLEFFokvXxmHOkRHiMTfIgxbxfWyxHIYoJau824x4+VUD5OASMLWQV0YfXJvNBdptKSl+iV1nHq+sKnJkgWJnCYRzZJNVBtKNmj6ghDBxtFgPsMdrF9dFzBttGU0So9hnh0yYP14PN4NgPZz0Lr95AV4Zj5O6jPKCvKU84ZrW9iBe33ZfeNJsIyvR/rHkmfZNsq6H7qvrOzW9Bx4moTamdYDBgHF4X4lC8dGoHK1kxETWd4eSpW+L4Zqm4yb/SzwCZNtJbC1YpwvDkcM/b+6Gxuc4SzjdKFExFKMMmZB1VfbbU5sRn/ETw3IUL7JLKxYUM8ktE1AreYUBsopeSvk0klxNAnujCNISYsfI+Dpz8ilcdowK9XwFaSh1+L8CmuyayMHysjsSGV2rB2Yy7k2M2/vn6Zf+81sndWBGt70bQ6YwZZS3S7qYSezR5AYO0SKAY98MAxEnZ++afn/3W7Ofxsavzz+8hBysIh7HxeaJcS23j7QXTJkL8tjzXMcHiBGeCpKWh2d8FgGvmReJ3cwFEdL7c7aAj6GahORx8HqrlKmJQMKFBZMFZhRyVeIJsabKi0pbmqtUoRNsnlXnCwkieVVwktCpcMKB1M5f0FHt/emocfvp6tY4C+l+sYgRSMiv7mXSjrpFPELFmVPH/AC3ClF7BrwL+eOPB7RErCgG/wkl/0fke3z3YEzO+vpW+tq8yOurzrd3QWDVboXAarHjyQVWf38C+3BpGJ+nZ4Hdv52jSp31LUWmV1+PkKfd8vb/ypnaLshZxf1ipZwlE08uZ833J2fw9HZ+9D5w/8bIm98xVjTlfM+4282sVLf/QQeLt3V/XwZbFTJYqBz27R7/Hhcs0yPWd76rGKKsCILfKCpaVK3IJWNnCgnFoptHMOpKQ+HlEsgTIEqj1YrtIS+aklibrDXG1IUCYCpBkTS2S18lFzqjKZCWg9m+C0O5g9kWxd2g2EPMfcmvWdUiGW5MXL71Yjbo3Twb1GY3HyIkS2phOSv7xXIhUKtVuLx2C3QRJ10KBB1Em4xbwB3CEqeSKhxPs3aJZl9CvItpJMC+4JT8RULlPLFROMf2DkYpjWb7IksotaE0O8cwCq9dJiMKSoL9JDmrqxJIA3JjkzGOJ6ho+v4Hsb1E3iJFb0BktfS0WuDfoUTWi0QuBtpC5FPRtPPHaNrtdAo01bW/LHx/ji+tU/EFAsH+VpKXmgsLvGxcZF/NE5EJzPRXQ+Ge/vSqGz8B&lt;/diagram&gt;&lt;/mxfile&gt;&quot;,&quot;toolbar&quot;:&quot;pages zoom layers lightbox&quot;,&quot;page&quot;:0}"></div>
<script type="text/javascript" src="https://app.diagrams.net/js/viewer-static.min.js"></script>
</body>
</html>
190 changes: 190 additions & 0 deletions cars/src/main/java/com/mooveit/cars/controller/CarsController.java
Original file line number Diff line number Diff line change
@@ -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<MODELpojo> 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<MODELpojo>(ModelObj,
HttpStatus.OK);
// } catch (JAXBException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }

// return new ResponseEntity<MODEL>(carsService.findModelById(id),
// HttpStatus.OK);

}


@GetMapping
@RequestMapping(value = "/getCarByBrand")
public ResponseEntity<List<MODELpojo>> getCarByBrand(@RequestParam("brand") String brand) {
List<MODELpojo> modelObjLst = new ArrayList<MODELpojo>();
List<MODEL> 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<List<MODELpojo>>(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<MODEL> childmodellist = new ArrayList<MODEL>();
childmodellist.add(mychildmodel);

submodel.setChildmodellst(childmodellist);


ModelRepository.save(mymodel);
//ModelRepository.save(mychildmodel);



return true;
}
}
68 changes: 68 additions & 0 deletions cars/src/main/java/com/mooveit/cars/domain/ENGINE.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
Loading