Skip to content

Commit 2e1fb8a

Browse files
author
chilimannen
committed
Initial commit
0 parents  commit 2e1fb8a

38 files changed

+10775
-0
lines changed

.travis.yml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
language: java
2+
sudo: false
3+
jdk:
4+
- oraclejdk8
5+
script: mvn clean test
6+
notifications:
7+
email: false

configuration.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"row_offset": 5,
3+
"web_port": 8080,
4+
"elastic_port": 9200,
5+
"elastic_index": "transactions_v4"
6+
}

pom.xml

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<properties>
8+
<project.version.sourceEncoding>UTF-8</project.version.sourceEncoding>
9+
</properties>
10+
11+
<groupId>com.codingchili</groupId>
12+
<artifactId>transaction-parser-es</artifactId>
13+
<version>1.0-SNAPSHOT</version>
14+
<build>
15+
<plugins>
16+
<plugin>
17+
<groupId>org.apache.maven.plugins</groupId>
18+
<artifactId>maven-compiler-plugin</artifactId>
19+
<version>3.5.1</version>
20+
<configuration>
21+
<source>1.8</source>
22+
<target>1.8</target>
23+
</configuration>
24+
</plugin>
25+
<plugin>
26+
<groupId>org.apache.maven.plugins</groupId>
27+
<artifactId>maven-shade-plugin</artifactId>
28+
<version>1.6</version>
29+
<executions>
30+
<execution>
31+
<phase>package</phase>
32+
<goals>
33+
<goal>shade</goal>
34+
</goals>
35+
<configuration>
36+
<outputFile>${project.artifactId}-${project.version}.jar</outputFile>
37+
<transformers>
38+
<transformer
39+
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
40+
<mainClass>io.vertx.core.Launcher</mainClass>
41+
</transformer>
42+
</transformers>
43+
</configuration>
44+
</execution>
45+
</executions>
46+
</plugin>
47+
</plugins>
48+
</build>
49+
<dependencies>
50+
<dependency>
51+
<groupId>junit</groupId>
52+
<artifactId>junit</artifactId>
53+
<version>RELEASE</version>
54+
<scope>test</scope>
55+
</dependency>
56+
57+
<dependency>
58+
<groupId>io.vertx</groupId>
59+
<artifactId>vertx-core</artifactId>
60+
<version>3.2.1</version>
61+
</dependency>
62+
63+
<dependency>
64+
<groupId>io.vertx</groupId>
65+
<artifactId>vertx-unit</artifactId>
66+
<version>3.2.1</version>
67+
</dependency>
68+
69+
<dependency>
70+
<groupId>io.vertx</groupId>
71+
<artifactId>vertx-web</artifactId>
72+
<version>3.2.1</version>
73+
</dependency>
74+
75+
<dependency>
76+
<groupId>org.apache.poi</groupId>
77+
<artifactId>poi</artifactId>
78+
<version>3.13</version>
79+
</dependency>
80+
81+
<dependency>
82+
<groupId>org.apache.poi</groupId>
83+
<artifactId>poi-ooxml</artifactId>
84+
<version>3.13</version>
85+
</dependency>
86+
87+
</dependencies>
88+
89+
</project>

src/main/java/Controller/Website.java

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package Controller;
2+
3+
import Model.Configuration;
4+
import Model.FileParser;
5+
import Model.ParserException;
6+
import io.vertx.core.AbstractVerticle;
7+
import io.vertx.core.Context;
8+
import io.vertx.core.Future;
9+
import io.vertx.core.Vertx;
10+
import io.vertx.core.json.JsonArray;
11+
import io.vertx.ext.web.FileUpload;
12+
import io.vertx.ext.web.Router;
13+
import io.vertx.ext.web.RoutingContext;
14+
import io.vertx.ext.web.handler.BodyHandler;
15+
import io.vertx.ext.web.handler.StaticHandler;
16+
17+
import java.util.Iterator;
18+
19+
/**
20+
* @author Robin Duda
21+
*/
22+
public class Website extends AbstractVerticle {
23+
private Vertx vertx;
24+
25+
@Override
26+
public void init(Vertx vertx, Context context) {
27+
this.vertx = vertx;
28+
}
29+
30+
@Override
31+
public void start(Future<Void> start) {
32+
Router router = Router.router(vertx);
33+
34+
router.route().handler(BodyHandler.create());
35+
setRouterAPI(router);
36+
router.route("/*").handler(StaticHandler.create());
37+
38+
vertx.createHttpServer().requestHandler(router::accept).listen(Configuration.WEB_PORT);
39+
40+
start.complete();
41+
}
42+
43+
private void setRouterAPI(Router router) {
44+
router.route("/api/upload").handler(context -> {
45+
Iterator<FileUpload> iterator = context.fileUploads().iterator();
46+
47+
if (iterator.hasNext()) {
48+
FileUpload upload = context.fileUploads().iterator().next();
49+
50+
vertx.fileSystem().readFile(upload.uploadedFileName(), file -> {
51+
try {
52+
sendbus(new FileParser(file.result().getBytes()).toJsonArray());
53+
redirect(context, "/done.html");
54+
} catch (ParserException e) {
55+
redirect(context, "/error.html");
56+
}
57+
});
58+
} else {
59+
redirect(context, "/error.html");
60+
}
61+
});
62+
}
63+
64+
private void sendbus(JsonArray list) {
65+
for (int i = 0; i < list.size(); i++) {
66+
vertx.eventBus().send(Configuration.BUS_TRANSACTIONS, list.getJsonObject(i));
67+
}
68+
}
69+
70+
private void redirect(RoutingContext context, String uri) {
71+
context.request().response().setStatusCode(301).putHeader("location", uri).end();
72+
}
73+
}

src/main/java/Launcher.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import Controller.Website;
2+
import Model.ElasticWriter;
3+
import io.vertx.core.Context;
4+
import io.vertx.core.Future;
5+
import io.vertx.core.Verticle;
6+
import io.vertx.core.Vertx;
7+
8+
/**
9+
* @author Robin Duda
10+
*/
11+
public class Launcher implements Verticle {
12+
private Vertx vertx;
13+
14+
@Override
15+
public Vertx getVertx() {
16+
return vertx;
17+
}
18+
19+
@Override
20+
public void init(Vertx vertx, Context context) {
21+
this.vertx = vertx;
22+
}
23+
24+
@Override
25+
public void start(Future<Void> start) throws Exception {
26+
vertx.deployVerticle(new ElasticWriter());
27+
vertx.deployVerticle(new Website());
28+
start.complete();
29+
}
30+
31+
@Override
32+
public void stop(Future<Void> stop) throws Exception {
33+
stop.complete();
34+
}
35+
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package Model;
2+
3+
import io.vertx.core.json.JsonObject;
4+
5+
import java.io.IOException;
6+
import java.nio.file.FileSystems;
7+
import java.nio.file.Files;
8+
9+
/**
10+
* @author Robin Duda
11+
*/
12+
public class Configuration {
13+
public static final int ROW_OFFSET = configuration().getInteger("row_offset");
14+
public static final int ELASTIC_PORT = configuration().getInteger("elastic_port");
15+
public static final String ELASTIC_INDEX = configuration().getString("elastic_index");
16+
public static final int WEB_PORT = configuration().getInteger("web_port");
17+
public static final String BUS_TRANSACTIONS = "bus.transactions";
18+
private static JsonObject configuration;
19+
20+
private static JsonObject configuration() {
21+
try {
22+
if (configuration == null) {
23+
configuration = new JsonObject(
24+
new String(Files.readAllBytes(FileSystems.getDefault().getPath("configuration.json"))));
25+
}
26+
return configuration;
27+
} catch (IOException e) {
28+
throw new RuntimeException(e);
29+
}
30+
}
31+
}
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package Model;
2+
3+
import io.vertx.core.AbstractVerticle;
4+
import io.vertx.core.Context;
5+
import io.vertx.core.Future;
6+
import io.vertx.core.Vertx;
7+
import io.vertx.core.json.JsonObject;
8+
9+
/**
10+
* @author Robin Duda
11+
*/
12+
public class ElasticWriter extends AbstractVerticle {
13+
private Vertx vertx;
14+
15+
@Override
16+
public void init(Vertx vertx, Context context) {
17+
this.vertx = vertx;
18+
}
19+
20+
@Override
21+
public void start(Future<Void> start) {
22+
createIndex();
23+
processTransactions();
24+
25+
start.complete();
26+
}
27+
28+
private void createIndex() {
29+
vertx.createHttpClient().put(
30+
Configuration.ELASTIC_PORT, "localhost",
31+
Configuration.ELASTIC_INDEX + "/all/").handler(response -> {
32+
33+
}).end(template().encode());
34+
}
35+
36+
private JsonObject template() {
37+
return new JsonObject()
38+
.put("template", new JsonObject()
39+
.put("mappings", new JsonObject()
40+
.put(Configuration.ELASTIC_INDEX, new JsonObject()
41+
.put("properties", new JsonObject()
42+
.put("Valutadatum", new JsonObject()
43+
.put("type", "date"))))));
44+
}
45+
46+
private void processTransactions() {
47+
vertx.eventBus().consumer(Configuration.BUS_TRANSACTIONS, handler -> {
48+
JsonObject data = (JsonObject) handler.body();
49+
50+
vertx.createHttpClient().post(
51+
Configuration.ELASTIC_PORT, "localhost",
52+
Configuration.ELASTIC_INDEX + "/all/").handler(response -> {
53+
54+
}).end(data.encode());
55+
});
56+
}
57+
}

0 commit comments

Comments
 (0)