changed project structure
This commit is contained in:
141
src/main/java/com/release11/DAO.java
Normal file
141
src/main/java/com/release11/DAO.java
Normal file
@@ -0,0 +1,141 @@
|
||||
package com.release11;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.transform.OutputKeys;
|
||||
import javax.xml.transform.Result;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class DAO {
|
||||
|
||||
static List<Material> materials = new ArrayList<>();
|
||||
static List<Package> packages = new ArrayList<>();
|
||||
|
||||
public String test(LinkedHashMap materialMap) throws Exception {
|
||||
Material material = mapToMaterial(materialMap);
|
||||
String xml = singleXML(material);
|
||||
return xml;
|
||||
}
|
||||
|
||||
static int counter=0;
|
||||
public void hello(){
|
||||
counter++;
|
||||
System.out.println("helloWorld"+counter);
|
||||
}
|
||||
|
||||
public void splitPackage(LinkedHashMap packageMap) {
|
||||
Package pack = new Package();
|
||||
pack.setId((Integer) packageMap.get("id"));
|
||||
pack.setNumber((String) packageMap.get("number"));
|
||||
pack.setMaterial_id((Integer) packageMap.get("material_id"));
|
||||
pack.setEan((String.valueOf(packageMap.get("ean"))));
|
||||
pack.setUnit_of_measure((String) packageMap.get("unit_of_measure"));
|
||||
pack.setDimension((String) packageMap.get("dimension"));
|
||||
pack.setDescription((String) packageMap.get("description"));
|
||||
packages.add(pack);
|
||||
}
|
||||
|
||||
public void splitMaterial(LinkedHashMap materialMap) {
|
||||
Material material = mapToMaterial(materialMap);
|
||||
materials.add(material);
|
||||
}
|
||||
|
||||
public String singleXML(Material material) throws Exception {
|
||||
|
||||
File file = new File("src/main/resources/test.xml");
|
||||
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder builder = dbf.newDocumentBuilder();
|
||||
Document doc = builder.newDocument();
|
||||
Element root = doc.createElement("material");
|
||||
|
||||
Element materialNumber = doc.createElement("material_number");
|
||||
materialNumber.setTextContent(String.valueOf(material.getNumber()));
|
||||
root.appendChild(materialNumber);
|
||||
|
||||
Element materialType = doc.createElement("material_type");
|
||||
materialType.setTextContent(String.valueOf(material.getType()));
|
||||
root.appendChild(materialType);
|
||||
|
||||
Element materialName = doc.createElement("material_name");
|
||||
materialName.setTextContent(String.valueOf(material.getName()));
|
||||
root.appendChild(materialName);
|
||||
|
||||
Element description = doc.createElement("description");
|
||||
description.setTextContent(material.getDescription());
|
||||
root.appendChild(description);
|
||||
|
||||
Element is_deleted = doc.createElement("is_deleted");
|
||||
is_deleted.setTextContent(String.valueOf(material.getIs_deleted()));
|
||||
root.appendChild(is_deleted);
|
||||
|
||||
for (Package p : material.getPackageList()) {
|
||||
|
||||
Element packageElement = doc.createElement("packages");
|
||||
|
||||
Element packageNumber = doc.createElement("package_number");
|
||||
packageNumber.setTextContent(String.valueOf(p.getNumber()));
|
||||
packageElement.appendChild(packageNumber);
|
||||
|
||||
Element ean = doc.createElement("ean");
|
||||
ean.setTextContent(String.valueOf(p.getEan()));
|
||||
packageElement.appendChild(ean);
|
||||
|
||||
Element unit = doc.createElement("unit_of_measure");
|
||||
unit.setTextContent(String.valueOf(p.getUnit_of_measure()));
|
||||
packageElement.appendChild(unit);
|
||||
|
||||
Element dimension = doc.createElement("dimension");
|
||||
dimension.setTextContent(String.valueOf(p.getDimension()));
|
||||
packageElement.appendChild(dimension);
|
||||
|
||||
Element descriptionPackage = doc.createElement("description");
|
||||
descriptionPackage.setTextContent(String.valueOf(p.getDescription()));
|
||||
packageElement.appendChild(descriptionPackage);
|
||||
|
||||
root.appendChild(packageElement);
|
||||
}
|
||||
|
||||
doc.appendChild(root);
|
||||
DOMSource src = new DOMSource(doc);
|
||||
Result result = new StreamResult(file);
|
||||
TransformerFactory tf = TransformerFactory.newInstance();
|
||||
Transformer transformer = tf.newTransformer();
|
||||
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
||||
transformer.transform(src, result);
|
||||
|
||||
String xml = "";
|
||||
Scanner myReader = new Scanner(file);
|
||||
while (myReader.hasNextLine()) {
|
||||
xml+=myReader.nextLine()+"\n";
|
||||
}
|
||||
|
||||
return xml;
|
||||
|
||||
}
|
||||
|
||||
public Material mapToMaterial(LinkedHashMap list) {
|
||||
Material material = new Material();
|
||||
material.setId((Integer) list.get("id"));
|
||||
material.setNumber((String) list.get("number"));
|
||||
material.setType(Material.enumType.valueOf((String) list.get("type")));
|
||||
material.setName((String) list.get("name"));
|
||||
material.setDescription((String) list.get("description"));
|
||||
material.setIs_deleted((Boolean) list.get("is_deleted"));
|
||||
for (int j = 0; j < packages.size(); j++) {
|
||||
if (packages.get(j).getMaterial_id() == material.getId())
|
||||
material.getPackageList().add(packages.get(j));
|
||||
}
|
||||
return material;
|
||||
}
|
||||
}
|
||||
68
src/main/java/com/release11/Main.java
Normal file
68
src/main/java/com/release11/Main.java
Normal file
@@ -0,0 +1,68 @@
|
||||
package com.release11;
|
||||
|
||||
import com.mysql.cj.jdbc.MysqlDataSource;
|
||||
import org.apache.activemq.ActiveMQConnectionFactory;
|
||||
import org.apache.camel.AggregationStrategy;
|
||||
import org.apache.camel.CamelContext;
|
||||
import org.apache.camel.ProducerTemplate;
|
||||
import org.apache.camel.builder.RouteBuilder;
|
||||
import org.apache.camel.component.jms.JmsComponent;
|
||||
import org.apache.camel.impl.DefaultCamelContext;
|
||||
import org.apache.camel.support.SimpleRegistry;
|
||||
import org.apache.log4j.BasicConfigurator;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
//MyBuilder myBuilder = new MyBuilder();
|
||||
//myBuilder.doRandomMaterials();
|
||||
//myBuilder.doRandomPackage();
|
||||
|
||||
//to("log:?level=INFO&showHeaders=true&showBody=true")
|
||||
|
||||
MysqlDataSource source = new MysqlDataSource();
|
||||
String jacek = "jdbc:mysql://10.101.111.19:3306/camel_db";
|
||||
source.setURL(jacek);
|
||||
source.setUser("root");
|
||||
source.setPassword("admin");
|
||||
SimpleRegistry registry = new SimpleRegistry();
|
||||
registry.bind("source", MysqlDataSource.class, source);
|
||||
CamelContext context = new DefaultCamelContext(registry);
|
||||
BasicConfigurator.configure();
|
||||
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://10.101.111.19:8088");
|
||||
connectionFactory.setUserName("admin");
|
||||
connectionFactory.setPassword("admin");
|
||||
context.addComponent("activemq", JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
|
||||
|
||||
|
||||
AggregationStrategy aggregationStrategy = new MyAggregation();
|
||||
|
||||
|
||||
DAO dao = new DAO();
|
||||
context.addRoutes(new RouteBuilder() {
|
||||
@Override
|
||||
public void configure() throws Exception {
|
||||
from("direct:start").to("jdbc:source")
|
||||
.split(body())
|
||||
.bean(dao,"splitMaterial")
|
||||
.to("activemq:queue:material")
|
||||
.to("log:?level=INFO&showHeaders=true&showBody=true");
|
||||
|
||||
from("activemq:queue:material")
|
||||
.enrich("jdbc:source",aggregationStrategy)
|
||||
.to("activemq:queue:materialPackage")
|
||||
.to("log:?level=INFO&showHeaders=true&showBody=true");
|
||||
}
|
||||
});
|
||||
|
||||
context.start();
|
||||
ProducerTemplate template = context.createProducerTemplate();
|
||||
template.sendBody("direct:start", "SELECT * FROM material LIMIT 1");
|
||||
|
||||
Thread.sleep(1000);
|
||||
template.setDefaultEndpointUri("activemq:queue:material");
|
||||
|
||||
context.stop();
|
||||
}
|
||||
}
|
||||
77
src/main/java/com/release11/Material.java
Normal file
77
src/main/java/com/release11/Material.java
Normal file
@@ -0,0 +1,77 @@
|
||||
package com.release11;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Material {
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getNumber() {
|
||||
return number;
|
||||
}
|
||||
|
||||
public void setNumber(String number) {
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public Boolean getIs_deleted() {
|
||||
return is_deleted;
|
||||
}
|
||||
|
||||
public void setIs_deleted(Boolean is_deleted) {
|
||||
this.is_deleted = is_deleted;
|
||||
}
|
||||
|
||||
public enumType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(enumType type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public List<Package> getPackageList() {
|
||||
return packageList;
|
||||
}
|
||||
|
||||
public void setPackageList(List<Package> packageList) {
|
||||
this.packageList = packageList;
|
||||
}
|
||||
|
||||
public enum enumType {
|
||||
A1, A2, A3, B1, B2, B3, Z1, Z2, Z3;
|
||||
}
|
||||
|
||||
|
||||
private int id;
|
||||
private String number;
|
||||
private enumType type;
|
||||
private String name;
|
||||
private String description;
|
||||
private Boolean is_deleted;
|
||||
private List<Package> packageList = new ArrayList<>();
|
||||
|
||||
}
|
||||
11
src/main/java/com/release11/MyAggregation.java
Normal file
11
src/main/java/com/release11/MyAggregation.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package com.release11;
|
||||
|
||||
import org.apache.camel.AggregationStrategy;
|
||||
import org.apache.camel.Exchange;
|
||||
|
||||
public class MyAggregation implements AggregationStrategy {
|
||||
@Override
|
||||
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
|
||||
return oldExchange;
|
||||
}
|
||||
}
|
||||
116
src/main/java/com/release11/MyBuilder.java
Normal file
116
src/main/java/com/release11/MyBuilder.java
Normal file
@@ -0,0 +1,116 @@
|
||||
package com.release11;
|
||||
|
||||
import org.apache.camel.CamelContext;
|
||||
import org.apache.camel.ProducerTemplate;
|
||||
import org.apache.camel.builder.RouteBuilder;
|
||||
import org.apache.camel.impl.DefaultCamelContext;
|
||||
import org.apache.camel.support.SimpleRegistry;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.Random;
|
||||
|
||||
public class MyBuilder extends RouteBuilder {
|
||||
|
||||
//MyBuilder myBuilder = new MyBuilder();
|
||||
//context.addRoutes(myBuilder);
|
||||
|
||||
@Override
|
||||
public void configure() throws Exception {
|
||||
from("timer:foo?fixedRate=true&period=400")
|
||||
.log("Hello Camel");
|
||||
}
|
||||
|
||||
public enum enumType {
|
||||
A1, A2, A3, B1, B2, B3, Z1, Z2, Z3;
|
||||
}
|
||||
|
||||
public void doRandomMaterials() throws IOException {
|
||||
|
||||
File fileName = new File("/home/igor/Documents/XML_Tasks/Camel/sql_script/sql.dml");
|
||||
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
|
||||
Random random = new Random();
|
||||
for (int i = 1; i <= 100; i++) {
|
||||
String number = "number" + random.nextInt(1000, 10000);
|
||||
String name = "name" + random.nextInt(1000, 10000);
|
||||
String description = "description" + random.nextInt(1000, 10000);
|
||||
String is_deleted = "";
|
||||
if (random.nextInt() % 2 == 0)
|
||||
is_deleted = "true";
|
||||
else
|
||||
is_deleted = "false";
|
||||
|
||||
enumType[] x = enumType.values();
|
||||
String type = x[random.nextInt(x.length)].toString();
|
||||
writer.write("INSERT INTO material VALUES(" +
|
||||
i + ", '" + number + "', '" + type + "', '" + name + "', '" + description + "', " + is_deleted + ");\n");
|
||||
}
|
||||
writer.close();
|
||||
|
||||
}
|
||||
|
||||
public void doRandomPackage() throws IOException {
|
||||
|
||||
File fileName = new File("/home/igor/Documents/XML_Tasks/Camel/sql_script/sql.dml");
|
||||
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName, true));
|
||||
Random random = new Random();
|
||||
for (int i = 1; i <= 100; i++) {
|
||||
String number = "number" + random.nextInt(1000, 10000);
|
||||
Integer material_id = i;
|
||||
String ean = eanGenerator();
|
||||
String unit_of_measure = "unit" + random.nextInt(1000, 10000);
|
||||
String dimension = random.nextInt(5, 35) + "x" + random.nextInt(5, 35) + "x" + random.nextInt(5, 35);
|
||||
String description = "description" + random.nextInt(1000, 10000);
|
||||
|
||||
writer.write("INSERT INTO package VALUES(" +
|
||||
i + ", '" + number + "', '" + material_id + "', '" + ean + "', '"
|
||||
+ unit_of_measure + "', '" + dimension + "', '" + description + "');\n");
|
||||
}
|
||||
|
||||
for (int i = 101; i <= 200; i++) {
|
||||
String number = "number" + random.nextInt(1000, 10000);
|
||||
Integer material_id = random.nextInt(1, 101);
|
||||
String ean = eanGenerator();
|
||||
String unit_of_measure = "unit" + random.nextInt(1000, 10000);
|
||||
String dimension = random.nextInt(5, 35) + "x" + random.nextInt(5, 35) + "x" + random.nextInt(5, 35);
|
||||
String description = "description" + random.nextInt(1000, 10000);
|
||||
|
||||
writer.write("INSERT INTO package VALUES(" +
|
||||
i + ", '" + number + "', '" + material_id + "', '" + ean + "', '"
|
||||
+ unit_of_measure + "', '" + dimension + "', '" + description + "');\n");
|
||||
}
|
||||
|
||||
|
||||
writer.close();
|
||||
|
||||
}
|
||||
|
||||
public String eanGenerator() {
|
||||
|
||||
String result = "590";
|
||||
Random random = new Random();
|
||||
while (result.length() < 12) {
|
||||
result += random.nextInt(10);
|
||||
}
|
||||
|
||||
int sum = 0;
|
||||
for (int i = 0; i < result.length(); i++) {
|
||||
if (i % 2 == 1)
|
||||
sum += (result.charAt(i) - '0') * 3;
|
||||
else
|
||||
sum += (int) result.charAt(i) - '0';
|
||||
}
|
||||
|
||||
sum = sum - (sum / 10) * (10);
|
||||
if (sum != 0)
|
||||
sum = 10 - sum;
|
||||
result += sum;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
69
src/main/java/com/release11/Package.java
Normal file
69
src/main/java/com/release11/Package.java
Normal file
@@ -0,0 +1,69 @@
|
||||
package com.release11;
|
||||
|
||||
public class Package {
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getNumber() {
|
||||
return number;
|
||||
}
|
||||
|
||||
public void setNumber(String number) {
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
public int getMaterial_id() {
|
||||
return material_id;
|
||||
}
|
||||
|
||||
public void setMaterial_id(int material_id) {
|
||||
this.material_id = material_id;
|
||||
}
|
||||
|
||||
public String getEan() {
|
||||
return ean;
|
||||
}
|
||||
|
||||
public void setEan(String ean) {
|
||||
this.ean = ean;
|
||||
}
|
||||
|
||||
public String getUnit_of_measure() {
|
||||
return unit_of_measure;
|
||||
}
|
||||
|
||||
public void setUnit_of_measure(String unit_of_measure) {
|
||||
this.unit_of_measure = unit_of_measure;
|
||||
}
|
||||
|
||||
public String getDimension() {
|
||||
return dimension;
|
||||
}
|
||||
|
||||
public void setDimension(String dimension) {
|
||||
this.dimension = dimension;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
int id;
|
||||
String number;
|
||||
int material_id;
|
||||
String ean;
|
||||
String unit_of_measure;
|
||||
String dimension;
|
||||
String description;
|
||||
|
||||
}
|
||||
20
src/main/resources/material.xml
Normal file
20
src/main/resources/material.xml
Normal file
@@ -0,0 +1,20 @@
|
||||
<material>
|
||||
<material_number>7</material_number>
|
||||
<material_type>A1</material_type>
|
||||
<material_name>LEGO 17272</material_name>
|
||||
<description>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</description>
|
||||
<is_deleted>false</is_deleted>
|
||||
<packages>
|
||||
<package_number>11988</package_number>
|
||||
<ean>80975098498590834</ean>
|
||||
<unit_of_measure>pcs</unit_of_measure>
|
||||
<dimension>17x17x17</dimension>
|
||||
<description>Lorem ipsum dolor sit amet, consectetur adipiscing elit</description>
|
||||
</packages>
|
||||
<packages>
|
||||
<package_number>118</package_number>
|
||||
<ean>877854875843774</ean>
|
||||
<unit_of_measure>pcs</unit_of_measure>
|
||||
<dimension>20x20x20</dimension>
|
||||
</packages>
|
||||
</material>
|
||||
42
src/main/resources/material.xsd
Normal file
42
src/main/resources/material.xsd
Normal file
@@ -0,0 +1,42 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
|
||||
|
||||
<xs:complexType name="package">
|
||||
<xs:sequence>
|
||||
<xs:element name="package_number" type="xs:int"/>
|
||||
<xs:element name="ean" type="xs:string"/>
|
||||
<xs:element name="unit_of_measure" type="xs:string"/>
|
||||
<xs:element name="dimension" type="xs:string"/>
|
||||
<xs:element name="description" type="xs:string" minOccurs="0"/>
|
||||
</xs:sequence>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:simpleType name="material_type" final="restriction">
|
||||
<xs:restriction base="xs:string">
|
||||
<xs:enumeration value="A1"/>
|
||||
<xs:enumeration value="A2"/>
|
||||
<xs:enumeration value="A3"/>
|
||||
<xs:enumeration value="B1"/>
|
||||
<xs:enumeration value="B2"/>
|
||||
<xs:enumeration value="B3"/>
|
||||
<xs:enumeration value="Z1"/>
|
||||
<xs:enumeration value="Z2"/>
|
||||
<xs:enumeration value="Z3"/>
|
||||
</xs:restriction>
|
||||
</xs:simpleType>
|
||||
|
||||
<xs:complexType name="materialType">
|
||||
<xs:sequence>
|
||||
<xs:element name="material_number" type="xs:int"/>
|
||||
<xs:element name="material_type" type="material_type"/>
|
||||
<xs:element name="material_name" type="xs:string"/>
|
||||
<xs:element name="description" type="xs:string"/>
|
||||
<xs:element name="is_deleted" type="xs:boolean"/>
|
||||
<xs:element name="packages" type="package" maxOccurs="unbounded"/>
|
||||
</xs:sequence>
|
||||
</xs:complexType>
|
||||
|
||||
<xs:element name="material" type="materialType"/>
|
||||
|
||||
|
||||
</xs:schema>
|
||||
15
src/main/resources/test.xml
Normal file
15
src/main/resources/test.xml
Normal file
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<material>
|
||||
<material_number>number8186</material_number>
|
||||
<material_type>Z3</material_type>
|
||||
<material_name>name2686</material_name>
|
||||
<description>description7225</description>
|
||||
<is_deleted>true</is_deleted>
|
||||
<packages>
|
||||
<package_number>number2274</package_number>
|
||||
<ean>5903339152912</ean>
|
||||
<unit_of_measure>unit1350</unit_of_measure>
|
||||
<dimension>18x30x18</dimension>
|
||||
<description>description7460</description>
|
||||
</packages>
|
||||
</material>
|
||||
Reference in New Issue
Block a user