Skip to content

Changes for Java Coding Challenge #17

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
19 changes: 19 additions & 0 deletions cars/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,25 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0-b170201.1204</version>
</dependency>

<!-- https://mvnrepository.com/artifact/javax.activation/activation -->
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.glassfish.jaxb/jaxb-runtime -->
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.0-b170127.1453</version>
</dependency>
</dependencies>

<build>
Expand Down
45 changes: 45 additions & 0 deletions cars/src/main/java/com/mooveit/cars/CarController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.mooveit.cars;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.mooveit.cars.domain.Car;
import com.mooveit.cars.repositories.CarRepository;

@RestController
public class CarController {

@Autowired
private CarRepository carRepository;


@GetMapping("/cars/{id}")
@ResponseBody
public Optional<Car> findById(@PathVariable Long id)
{
return carRepository.findById(id);
}

// @GetMapping("/cars/{brand}")
// @ResponseBody
// public Car findByBrand(@PathVariable String brand)
// {
// return carRepository.findByBrand(brand);
// }



@GetMapping("/cars")
@ResponseBody
public List<Car> findAll()
{
return carRepository.findAll();

}
}
6 changes: 3 additions & 3 deletions cars/src/main/java/com/mooveit/cars/CarsApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
@EnableScheduling
public class CarsApplication {

public static void main(String[] args) {
SpringApplication.run(CarsApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(CarsApplication.class, args);
}

}
184 changes: 184 additions & 0 deletions cars/src/main/java/com/mooveit/cars/domain/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
package com.mooveit.cars.domain;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;


@Entity
@Table(name = "Model")
@XmlRootElement(name = "MODEL")
//@SecondaryTable(name = "SUBMODEL", pkJoinColumns = @PrimaryKeyJoinColumn(name = "model_name"))
public class Car implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue
private long id;

@Column(name = "model_name")
@XmlAttribute(name = "name")
private String name;

@Column(name = "model_from")
@XmlAttribute(name = "from")
private String froom;

@Column(name = "to")
@XmlAttribute(name = "to")
private String too;

@Column(name = "type")
@XmlAttribute(name = "type")
private String type;

@Embedded
@XmlElement(name = "ENGINE")
private Engine engine;

@Embedded
@XmlElement(name = "WHEELS")
private Wheel wheel;

// private ArrayList<Model> models = new ArrayList<>();
//
// @XmlElementWrapper(name = "submodels")
// @XmlElement(name = "model")
// public ArrayList<Model> getModels() {
// return models;
// }
//
// public void setModels(ArrayList<Model> models) {
// this.models = models;
// }

public Car() {
super();
}

public Car(String name, String from, String to, String type, Engine engine, Wheel wheel) {
super();
this.name = name;
this.froom = from;
this.too = to;
this.type = type;
this.engine = engine;
this.wheel = wheel;
}

//Setters and Getters

@Override
public String toString() {
return "Model [name=" + name + ", from=" + froom + ", to="
+ too + ", type ="+ type + "]";
}
}


@XmlRootElement(name = "WHEELS")
class Wheel implements Serializable {

private static final long serialVersionUID = 1L;

@Column(name = "wheel_size")
@XmlAttribute(name = "size")
String size;

@Column(name = "wheel_type")
@XmlAttribute(name = "type")
String type;

public Wheel() {
super();
}

public Wheel(String size, String type) {
super();
this.size = size;
this.type = type;
}

//Setters and Getters

@Override
public String toString() {
return "Wheel [size=" + size + ", type=" + type + "]";
}

}


@XmlRootElement(name = "ENGINE")
class Engine implements Serializable {

private static final long serialVersionUID = 1L;

@Column(name = "engine_power")
@XmlAttribute(name = "power")
String power;

@Column(name = "engine_type")
@XmlAttribute(name = "type")
String type;

public Engine() {
super();
}

public Engine(String power, String type) {
super();
this.power = power;
this.type = type;
}

//Setters and Getters

@Override
public String toString() {
return "Engine [power=" + power + ", type=" + type + "]";
}

}

/*@XmlRootElement(name = "Model")
class Model implements Serializable {

private static final long serialVersionUID = 1L;

//@Column(name = "submodel_name")
@XmlAttribute(name = "name")
String name;

//@Column(name = "line")
@XmlAttribute(name = "line")
String line;

public Model() {
super();
}

public Model(String name, String line) {
super();
this.name = name;
this.line = line;
}

//Setters and Getters

@Override
public String toString() {
return "Model [name=" + name + ", line=" + line + "]";
}
}*/


42 changes: 42 additions & 0 deletions cars/src/main/java/com/mooveit/cars/domain/Catalogue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.mooveit.cars.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "CATALOGUE")
public class Catalogue implements Serializable {

private static final long serialVersionUID = 1L;

@XmlElement(name = "MODEL")
private ArrayList<Car> models;

public void setModelsList(ArrayList < Car > modelsList) {
this.models = modelsList;
}

public List < Car > getModelsList() {
return models;
}

public Catalogue() {
super();
}

public Catalogue(ArrayList<Car> models) {
super();

this.models = models;
}

//Setters and Getters

@Override
public String toString() {
return "Catalogue [number of models =" + models.size() + "]";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.mooveit.cars.repositories;

import org.springframework.data.jpa.repository.JpaRepository;

import com.mooveit.cars.domain.Car;

public interface CarRepository extends JpaRepository<Car, Long>
{
// Car findByBrand(String brand);

}
52 changes: 47 additions & 5 deletions cars/src/main/java/com/mooveit/cars/tasks/FordIngesterTask.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,57 @@
package com.mooveit.cars.tasks;

import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.IOException;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.mooveit.cars.domain.Catalogue;
import com.mooveit.cars.repositories.CarRepository;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class FordIngesterTask {
@Autowired
ResourceLoader resourceLoader;

@Autowired
private CarRepository carRepository;

@Scheduled(cron = "${cars.ford.ingester.runCron}")
public void ingestFile() throws IOException {
System.out.println("*********Ingesting File****************");
Resource resource=resourceLoader.getResource("classpath:ford-example.xml");
File xmlFile = resource.getFile();
JAXBContext jaxbContext;
try
{
jaxbContext = JAXBContext.newInstance(Catalogue.class);

Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

Catalogue catalogue = (Catalogue) jaxbUnmarshaller.unmarshal(xmlFile);
System.out.println("***Printing Catalogue models count******" + catalogue.getModelsList().size());
System.out.println(catalogue.getModelsList().get(0).toString());
carRepository.saveAll(catalogue.getModelsList());
System.out.println("**********Print inserted data*********" + carRepository.findAll());
}
catch (JAXBException e)
{
e.printStackTrace();
}


@Scheduled(cron = "${cars.ford.ingester.runCron}")
public void ingestFile() {
log.warn("Not implemented yet.");
}
}
}
1 change: 1 addition & 0 deletions cars/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ spring:
jpa:
database-platform: 'org.hibernate.dialect.H2Dialect'
h2.console.enabled: true
ddl-auto: create-drop
Loading