From 33613a85afc4b1481367fbe92a17ee59c240250b Mon Sep 17 00:00:00 2001 From: Sven Eisenhauer Date: Fri, 10 Nov 2023 15:11:48 +0100 Subject: add new repo --- .../CamelPrototype/.classpath | 112 ++++++++++++ .../CamelPrototype/.project | 23 +++ .../.settings/org.eclipse.jdt.core.prefs | 5 + .../CamelPrototype/pom.xml | 200 +++++++++++++++++++++ .../ws0910/camelprototype/FilenameGenerator.java | 9 + .../h_da/fbi/dsi/ws0910/camelprototype/Main.java | 13 ++ .../dsi/ws0910/camelprototype/OrderNormalizer.java | 26 +++ .../fbi/dsi/ws0910/camelprototype/OrderRoutes.java | 74 ++++++++ .../fbi/dsi/ws0910/camelprototype/OrderSystem.java | 148 +++++++++++++++ .../ws0910/camelprototype/OrderSystemAdaptor.java | 18 ++ .../ws0910/camelprototype/OrderSystemDerby.java | 173 ++++++++++++++++++ .../ws0910/camelprototype/OrderTransformer.java | 25 +++ .../resources/META-INF/spring/camel-context.xml | 23 +++ .../src/main/resources/OrderMailBody.vm | 12 ++ .../src/main/resources/OrderService.wsdl | 71 ++++++++ .../src/main/resources/log4j.properties | 22 +++ .../CamelPrototype/src/main/webapp/WEB-INF/web.xml | 32 ++++ .../camelprototype/CreateOrderRoutesTest.java | 112 ++++++++++++ .../resources/CreateOrderRoutesTest-context.xml | 31 ++++ .../src/test/resources/log4j.properties | 22 +++ 20 files changed, 1151 insertions(+) create mode 100644 Master/Daten- und Systemintegration/CamelPrototype/.classpath create mode 100644 Master/Daten- und Systemintegration/CamelPrototype/.project create mode 100644 Master/Daten- und Systemintegration/CamelPrototype/.settings/org.eclipse.jdt.core.prefs create mode 100644 Master/Daten- und Systemintegration/CamelPrototype/pom.xml create mode 100644 Master/Daten- und Systemintegration/CamelPrototype/src/main/java/de/h_da/fbi/dsi/ws0910/camelprototype/FilenameGenerator.java create mode 100644 Master/Daten- und Systemintegration/CamelPrototype/src/main/java/de/h_da/fbi/dsi/ws0910/camelprototype/Main.java create mode 100644 Master/Daten- und Systemintegration/CamelPrototype/src/main/java/de/h_da/fbi/dsi/ws0910/camelprototype/OrderNormalizer.java create mode 100644 Master/Daten- und Systemintegration/CamelPrototype/src/main/java/de/h_da/fbi/dsi/ws0910/camelprototype/OrderRoutes.java create mode 100644 Master/Daten- und Systemintegration/CamelPrototype/src/main/java/de/h_da/fbi/dsi/ws0910/camelprototype/OrderSystem.java create mode 100644 Master/Daten- und Systemintegration/CamelPrototype/src/main/java/de/h_da/fbi/dsi/ws0910/camelprototype/OrderSystemAdaptor.java create mode 100644 Master/Daten- und Systemintegration/CamelPrototype/src/main/java/de/h_da/fbi/dsi/ws0910/camelprototype/OrderSystemDerby.java create mode 100644 Master/Daten- und Systemintegration/CamelPrototype/src/main/java/de/h_da/fbi/dsi/ws0910/camelprototype/OrderTransformer.java create mode 100644 Master/Daten- und Systemintegration/CamelPrototype/src/main/resources/META-INF/spring/camel-context.xml create mode 100644 Master/Daten- und Systemintegration/CamelPrototype/src/main/resources/OrderMailBody.vm create mode 100644 Master/Daten- und Systemintegration/CamelPrototype/src/main/resources/OrderService.wsdl create mode 100644 Master/Daten- und Systemintegration/CamelPrototype/src/main/resources/log4j.properties create mode 100644 Master/Daten- und Systemintegration/CamelPrototype/src/main/webapp/WEB-INF/web.xml create mode 100644 Master/Daten- und Systemintegration/CamelPrototype/src/test/java/de/h_da/fbi/dsi/ws0910/camelprototype/CreateOrderRoutesTest.java create mode 100644 Master/Daten- und Systemintegration/CamelPrototype/src/test/resources/CreateOrderRoutesTest-context.xml create mode 100644 Master/Daten- und Systemintegration/CamelPrototype/src/test/resources/log4j.properties (limited to 'Master/Daten- und Systemintegration/CamelPrototype') diff --git a/Master/Daten- und Systemintegration/CamelPrototype/.classpath b/Master/Daten- und Systemintegration/CamelPrototype/.classpath new file mode 100644 index 0000000..130901c --- /dev/null +++ b/Master/Daten- und Systemintegration/CamelPrototype/.classpath @@ -0,0 +1,112 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Master/Daten- und Systemintegration/CamelPrototype/.project b/Master/Daten- und Systemintegration/CamelPrototype/.project new file mode 100644 index 0000000..a61246b --- /dev/null +++ b/Master/Daten- und Systemintegration/CamelPrototype/.project @@ -0,0 +1,23 @@ + + + camelprototype + + + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.wst.common.project.facet.core.nature + + diff --git a/Master/Daten- und Systemintegration/CamelPrototype/.settings/org.eclipse.jdt.core.prefs b/Master/Daten- und Systemintegration/CamelPrototype/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..d6bcbfb --- /dev/null +++ b/Master/Daten- und Systemintegration/CamelPrototype/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,5 @@ +#Tue Nov 10 12:21:39 CET 2009 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.source=1.5 +org.eclipse.jdt.core.compiler.compliance=1.5 diff --git a/Master/Daten- und Systemintegration/CamelPrototype/pom.xml b/Master/Daten- und Systemintegration/CamelPrototype/pom.xml new file mode 100644 index 0000000..b74243b --- /dev/null +++ b/Master/Daten- und Systemintegration/CamelPrototype/pom.xml @@ -0,0 +1,200 @@ + + + + 4.0.0 + de.h_da.fbi.dsi.ws0910 + camelprototype + war + 1.0-SNAPSHOT + H_DA/FBI/DSI Camel Prototype WS2009/2010 + + + 2.2.5 + 6.1.9 + 2.1.0 + + + + + + + + org.apache.camel + camel-core + ${camel-version} + + + org.apache.camel + camel-spring-javaconfig + ${camel-version} + + + org.apache.camel + camel-spring + ${camel-version} + + + org.apache.camel + camel-csv + ${camel-version} + + + org.apache.camel + camel-cxf + ${camel-version} + + + org.apache.camel + camel-jetty + ${camel-version} + + + org.apache.camel + camel-velocity + ${camel-version} + + + org.apache.camel + camel-mail + ${camel-version} + + + xerces + xercesImpl + 2.8.1 + + + + + hsqldb + hsqldb + 1.8.0.10 + + + + org.apache.derby + derby + 10.4.2.0 + + + + + org.apache.cxf + cxf-rt-core + ${cxf-version} + + + org.apache.cxf + cxf-rt-frontend-jaxws + ${cxf-version} + + + + + org.apache.cxf + cxf-rt-transports-http-jetty + ${cxf-version} + + + + + log4j + log4j + 1.2.14 + + + + + + + junit + junit + + 4.7 + test + + + org.springframework + spring-test + 2.5.6 + + + + org.jvnet.mock-javamail + mock-javamail + 1.7 + test + + + + + javax.mail + mail + 1.4.1 + test + + + + org.apache.camel + camel-core + ${camel-version} + test + test-jar + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.5 + 1.5 + + + + + org.apache.cxf + cxf-codegen-plugin + ${cxf-version} + + + generate-sources + generate-sources + + ${basedir}/target/generated/src/main/java + + + ${basedir}/src/main/resources/OrderService.wsdl + + + + + wsdl2java + + + + + + + org.mortbay.jetty + maven-jetty-plugin + ${jetty-version} + + + + org.apache.camel + camel-maven-plugin + ${camel-version} + + + + diff --git a/Master/Daten- und Systemintegration/CamelPrototype/src/main/java/de/h_da/fbi/dsi/ws0910/camelprototype/FilenameGenerator.java b/Master/Daten- und Systemintegration/CamelPrototype/src/main/java/de/h_da/fbi/dsi/ws0910/camelprototype/FilenameGenerator.java new file mode 100644 index 0000000..fbf4e04 --- /dev/null +++ b/Master/Daten- und Systemintegration/CamelPrototype/src/main/java/de/h_da/fbi/dsi/ws0910/camelprototype/FilenameGenerator.java @@ -0,0 +1,9 @@ +package de.h_da.fbi.dsi.ws0910.camelprototype; + +import org.apache.camel.Exchange; + +public class FilenameGenerator { + public String getFilename(Exchange exchange) { + return "ordermail-"+exchange.getIn().getHeader("orderno")+".txt"; + } +} diff --git a/Master/Daten- und Systemintegration/CamelPrototype/src/main/java/de/h_da/fbi/dsi/ws0910/camelprototype/Main.java b/Master/Daten- und Systemintegration/CamelPrototype/src/main/java/de/h_da/fbi/dsi/ws0910/camelprototype/Main.java new file mode 100644 index 0000000..e3b4315 --- /dev/null +++ b/Master/Daten- und Systemintegration/CamelPrototype/src/main/java/de/h_da/fbi/dsi/ws0910/camelprototype/Main.java @@ -0,0 +1,13 @@ +package de.h_da.fbi.dsi.ws0910.camelprototype; + +public final class Main { + + private Main() { + // do nothing here + } + + public static void main(String[] args) throws Exception { + org.apache.camel.spring.Main.main(args); + } + +} diff --git a/Master/Daten- und Systemintegration/CamelPrototype/src/main/java/de/h_da/fbi/dsi/ws0910/camelprototype/OrderNormalizer.java b/Master/Daten- und Systemintegration/CamelPrototype/src/main/java/de/h_da/fbi/dsi/ws0910/camelprototype/OrderNormalizer.java new file mode 100644 index 0000000..f7dc857 --- /dev/null +++ b/Master/Daten- und Systemintegration/CamelPrototype/src/main/java/de/h_da/fbi/dsi/ws0910/camelprototype/OrderNormalizer.java @@ -0,0 +1,26 @@ +package de.h_da.fbi.dsi.ws0910.camelprototype; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.model.dataformat.CsvDataFormat; + +public class OrderNormalizer extends RouteBuilder{ + @Override + public void configure() throws Exception { + from("direct:new-orders-in") + .choice() + .when(header("from").isEqualTo(constant("csvinput"))) + //csv order transformer + // convert CSV-File to InputNewOrder that further processing expects + .unmarshal(new CsvDataFormat()) + .bean(OrderTransformer.class, "transformFromCsv") + // put the InputNewOrder into the channel + .to("direct:normalized-orders") + + .when(header("from").isEqualTo(constant("cxf"))) + // webservice order transformer + // we need to convert the CXF payload to InputNewOrder that further processing expects + .convertBodyTo(InputNewOrder.class) + // put the InputNewOrder into the channel + .to("direct:normalized-orders"); + } +} diff --git a/Master/Daten- und Systemintegration/CamelPrototype/src/main/java/de/h_da/fbi/dsi/ws0910/camelprototype/OrderRoutes.java b/Master/Daten- und Systemintegration/CamelPrototype/src/main/java/de/h_da/fbi/dsi/ws0910/camelprototype/OrderRoutes.java new file mode 100644 index 0000000..19686c7 --- /dev/null +++ b/Master/Daten- und Systemintegration/CamelPrototype/src/main/java/de/h_da/fbi/dsi/ws0910/camelprototype/OrderRoutes.java @@ -0,0 +1,74 @@ +package de.h_da.fbi.dsi.ws0910.camelprototype; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; + +public class OrderRoutes extends RouteBuilder { + //OrderSystemAdaptor orderSysAdaptor = new OrderSystemAdaptor(); + private String email_authuser = null; + private String email_authpw = null; + private String email_recipient = null; + + public OrderRoutes() { + this("neworders@eisy.net.eu.org","crasher@smtp.eisy.net.eu.org","crashm3"); + } + public OrderRoutes(String email_recipient,String email_authuser,String email_authpw) { + super(); + this.email_recipient = email_recipient; + this.email_authuser = email_authuser; + this.email_authpw = email_authpw; + } + + public void configure() throws Exception { + // endpoint to our CXF webservice + String cxfEndpoint = "cxf://http://localhost:8080/camelprototype/webservices/order" + + "?serviceClass=de.h_da.fbi.dsi.ws0910.camelprototype.OrderEndpoint" + + "&wsdlURL=OrderService.wsdl"; + + from(cxfEndpoint) + // set header field to keep track of requester + .setHeader("from",constant("cxf")) + .to("direct:new-orders-in"); + from("file://target/csvinput?recursive=true&delete=true") + .setHeader("from",constant("csvinput")) + .to("direct:new-orders-in"); + + from("direct:normalized-orders") + // pass the new order to the order management system gateway + // function integration + .processRef("ordersystemadaptor") + // send the notification email + .to("direct:sendordermail") + // CBR: if the order was sent by a webservice request + .choice() + .when(header("from").isEqualTo("cxf")) + // send a response back to webservice client + // return OK as response + .process(new Processor() { + public void process(Exchange exchange) throws Exception { + OutputNewOrder res = new OutputNewOrder(); + res.setStatus("0"); + res.setMessage((String) exchange.getIn().getHeader("orderno")); + exchange.getOut().setBody(res,OutputNewOrder.class); + } + }) + .otherwise().end(); + + // generate order mail file + from("direct:sendordermail") + // set the file name + .setHeader("CamelFileName", bean(FilenameGenerator.class,"getFilename")) + // and create the mail body using velocity templating + .to("velocity:OrderMailBody.vm") + // and store the file + .to("file://target/ordermails/"); + + // from the file -> send email + from("file://target/ordermails/?move=sent") + // set the subject of the email + .setHeader("subject", constant("New Order notification")) + // send the email + .to("smtp://"+email_authuser+"?password="+email_authpw+"&to="+email_recipient); + } +} diff --git a/Master/Daten- und Systemintegration/CamelPrototype/src/main/java/de/h_da/fbi/dsi/ws0910/camelprototype/OrderSystem.java b/Master/Daten- und Systemintegration/CamelPrototype/src/main/java/de/h_da/fbi/dsi/ws0910/camelprototype/OrderSystem.java new file mode 100644 index 0000000..28a06d2 --- /dev/null +++ b/Master/Daten- und Systemintegration/CamelPrototype/src/main/java/de/h_da/fbi/dsi/ws0910/camelprototype/OrderSystem.java @@ -0,0 +1,148 @@ +package de.h_da.fbi.dsi.ws0910.camelprototype; + +import java.sql.CallableStatement; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +public class OrderSystem { + + private static final String DB_NAME = "jdbc:hsqldb:file:target/db/orderDB"; + private static final String DB_USER="sa"; + private static final String DB_PASSWORD=""; + private static final String DB_DRV = "org.hsqldb.jdbcDriver"; + + private static final String CREATE_ORDERTBL_SQL = "CREATE TABLE \"orders\" (" + + "orderid INT generated by default as identity (start with 1) PRIMARY KEY, "+ + "customerno VARCHAR(50) NOT NULL, " + + "order_created_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP )"; + private static final String CREATE_ORDERPOSTBL_SQL = "CREATE TABLE \"orderpos\" (" + + "orderposid INT generated by default as identity (start with 1) PRIMARY KEY, "+ + "orderid INT NOT NULL, "+ + "articleno VARCHAR(50) NOT NULL,"+ + "articlename VARCHAR(50) NOT NULL,"+ + "amount INT NOT NULL )"; + + private static final String ORDER_INSERT_SQL = "INSERT INTO \"orders\" " + + "(orderid,customerno) VALUES(null,?)"; + private static final String ORDERPOS_INSERT_SQL = "INSERT INTO \"orderpos\" " + + "(orderposid,orderid,articleno,articlename,amount) VALUES(null,?,?,?,?)"; + private Connection con = null; + private static OrderSystem theInstance = null; + + public static OrderSystem getInstance() throws ClassNotFoundException, SQLException { + if(theInstance == null) { + theInstance = new OrderSystem(); + } + return theInstance; + } + + public OrderSystem() throws ClassNotFoundException, SQLException { + Class.forName(DB_DRV); + initDB(); + } + + private void initDB() throws SQLException { + try { + Statement stmt = getDbConnection().createStatement(); + ResultSet rs = getDbConnection().getMetaData().getTables(null,null, "orders", null); + if(!rs.next()) stmt.execute(CREATE_ORDERTBL_SQL); + rs = getDbConnection().getMetaData().getTables(null,null, "orderpos", null); + if(!rs.next()) stmt.execute(CREATE_ORDERPOSTBL_SQL); + stmt.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + private Connection getDbConnection() { + if (con == null) { + try { + con = DriverManager.getConnection(DB_NAME,DB_USER,DB_PASSWORD); + } catch (SQLException sqle) { + sqle.printStackTrace(); + //throw new RuntimeException("Error connecting to DB",sqle); + } + } + return con; + } + + private String insertNewOrder(InputNewOrder newOrder) { + int orderNo = -1; + try { + PreparedStatement pStmt = getDbConnection().prepareStatement(ORDER_INSERT_SQL); + pStmt.setString(1, newOrder.getCustomerNo()); + pStmt.execute(); + CallableStatement cstmt = getDbConnection().prepareCall("{CALL IDENTITY()}"); + ResultSet row = cstmt.executeQuery(); + if(row.next()) { + orderNo = row.getInt(1); + } + row.close(); + cstmt.close(); + if (orderNo != -1) { + for(OrderPosition pos : newOrder.getOrderpositions()) { + pStmt = getDbConnection().prepareStatement(ORDERPOS_INSERT_SQL); + int j = 0; + pStmt.setInt(++j, orderNo); + pStmt.setString(++j, pos.getArticleNo()); + pStmt.setString(++j, pos.getArticle()); + pStmt.setInt(++j, pos.getAmount()); + pStmt.executeUpdate(); + } + } + pStmt.close(); + } catch (SQLException sqle) { + sqle.printStackTrace(); + } + return Integer.toString(orderNo); + } + + public synchronized String handleNewOrder(InputNewOrder newOrder) { + String orderNum = insertNewOrder(newOrder); + return orderNum; + } + public synchronized void dumpOrders() { + StringBuilder dump = new StringBuilder(); + try { + dump.append("=============================\n"); + dump.append("\tStart Order Dump\n"); + dump.append("=============================\n"); + dump.append("OrderID").append("\t").append("CustNo").append("\t"); + dump.append("ArtNo").append("\t").append("Article").append("\t\t"); + dump.append("Amount").append("\t").append("Created on").append("\n"); + dump.append("--------------------------------------------------------\n"); + Statement stmt = getDbConnection().createStatement(); + String sql = "SELECT * from \"orders\" AS o JOIN \"orderpos\" AS op ON o.orderid = op.orderid"; + ResultSet rs = stmt.executeQuery(sql); + while(rs.next()) { + dump.append(rs.getString("orderid")).append("\t"); + dump.append(rs.getString("customerno")).append("\t"); + dump.append(rs.getString("articleno")).append("\t"); + dump.append(rs.getString("articlename")).append("\t"); + dump.append(rs.getString("amount")).append("\t"); + dump.append(rs.getTimestamp("order_created_ts")).append("\n"); + } + rs.close(); + stmt.close(); + dump.append("=============================\n"); + dump.append("\tFinish Order Dump\n"); + dump.append("=============================\n"); + System.out.println(dump.toString()); + } catch (SQLException e) { + e.printStackTrace(); + } + } + public synchronized void shutdown() { + try { + Statement stmt = getDbConnection().createStatement(); + stmt.execute("SHUTDOWN"); + stmt.close(); + theInstance = null; + } catch (SQLException sqle) { + sqle.printStackTrace(); + } + } +} diff --git a/Master/Daten- und Systemintegration/CamelPrototype/src/main/java/de/h_da/fbi/dsi/ws0910/camelprototype/OrderSystemAdaptor.java b/Master/Daten- und Systemintegration/CamelPrototype/src/main/java/de/h_da/fbi/dsi/ws0910/camelprototype/OrderSystemAdaptor.java new file mode 100644 index 0000000..551454b --- /dev/null +++ b/Master/Daten- und Systemintegration/CamelPrototype/src/main/java/de/h_da/fbi/dsi/ws0910/camelprototype/OrderSystemAdaptor.java @@ -0,0 +1,18 @@ +package de.h_da.fbi.dsi.ws0910.camelprototype; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; + +public class OrderSystemAdaptor implements Processor{ + private OrderSystem ordersystem; + public OrderSystemAdaptor(OrderSystem ordersystem) { + super(); + this.ordersystem = ordersystem; + } + public void process(Exchange exchange) throws Exception { + InputNewOrder newOrder = exchange.getIn().getBody(InputNewOrder.class); + // here happens functional integration of a legacy system + String orderNum =ordersystem.handleNewOrder(newOrder); + exchange.getIn().setHeader("orderno", orderNum); + } +} diff --git a/Master/Daten- und Systemintegration/CamelPrototype/src/main/java/de/h_da/fbi/dsi/ws0910/camelprototype/OrderSystemDerby.java b/Master/Daten- und Systemintegration/CamelPrototype/src/main/java/de/h_da/fbi/dsi/ws0910/camelprototype/OrderSystemDerby.java new file mode 100644 index 0000000..dff0acf --- /dev/null +++ b/Master/Daten- und Systemintegration/CamelPrototype/src/main/java/de/h_da/fbi/dsi/ws0910/camelprototype/OrderSystemDerby.java @@ -0,0 +1,173 @@ +package de.h_da.fbi.dsi.ws0910.camelprototype; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +public class OrderSystemDerby { + + private static final String DB_URL = "jdbc:derby:;databaseName=orderDB;create=true;"; + private static final String DB_DRV = "org.apache.derby.jdbc.EmbeddedDriver"; + + private static final String CREATE_ORDERTBL_SQL = "CREATE TABLE \"orders\" (" + + "orderid INT generated by default as identity (start with 1) PRIMARY KEY, "+ + "customerno VARCHAR(50) NOT NULL, " + + "order_created_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP )"; + private static final String CREATE_ORDERPOSTBL_SQL = "CREATE TABLE \"orderpos\" (" + + "orderposid INT generated by default as identity (start with 1) PRIMARY KEY, "+ + "orderid INT NOT NULL, "+ + "articleno VARCHAR(50) NOT NULL,"+ + "articlename VARCHAR(50) NOT NULL,"+ + "amount INT NOT NULL )"; + + private static Connection con = null; + + static { + String dbdir = System.getProperty("user.dir"); + dbdir += System.getProperty("file.separator")+"target"; + dbdir += System.getProperty("file.separator")+"db"; + System.setProperty("derby.system.home", dbdir); + try { + Class.forName(DB_DRV); + + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + initDB(); + } + + private static void initDB() { + try { + Statement stmt = getDbConnection().createStatement(); + ResultSet rs = getDbConnection().getMetaData().getTables(null,"APP", "orders", null); + if(!rs.next()) stmt.execute(CREATE_ORDERTBL_SQL); + rs = getDbConnection().getMetaData().getTables(null,"APP", "orderpos", null); + if(!rs.next()) stmt.execute(CREATE_ORDERPOSTBL_SQL); + stmt.close(); + } catch (SQLException e) { + if(!e.getSQLState().equals("X0Y32")) { + e.printStackTrace(); + } + } + } + private static Connection getDbConnection() { + if (con == null) { + try { + con = DriverManager.getConnection(DB_URL); + } catch (SQLException sqle) { + sqle.printStackTrace(); + if(!sqle.getSQLState().equals("XBM0J")) { + try { + Class.forName(DB_DRV); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + try { + con = DriverManager.getConnection(DB_URL); + } catch (SQLException e) { + e.printStackTrace(); + throw new RuntimeException("Error connecting to DB",sqle); + } + initDB(); + } else { + shutdown(); + try { + con = DriverManager.getConnection(DB_URL); + } catch (SQLException e) { + e.printStackTrace(); + throw new RuntimeException("Error connecting to DB",sqle); + } + } + } catch (NullPointerException npe) { + try { + Class.forName(DB_DRV); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + initDB(); + } + } + return con; + } + + private static String insertNewOrder(InputNewOrder newOrder) { + int orderNo = -1; + try { + Statement stmt = getDbConnection().createStatement(); + String sql = "INSERT INTO \"orders\" (customerno) VALUES('"+newOrder.getCustomerNo()+"')"; + stmt.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS); + ResultSet row = stmt.getGeneratedKeys(); + if(row.next()) { + orderNo = row.getInt(1); + } + row.close(); + if (orderNo != -1) { + + for(OrderPosition pos : newOrder.getOrderpositions()) { + sql = "INSERT INTO \"orderpos\" " + + "(orderid,articleno,articlename,amount) " + + "VALUES("+orderNo+",'"+pos.getArticleNo()+"','" + +pos.getArticle()+"',"+pos.getAmount()+")"; + stmt.executeUpdate(sql); + } + } + stmt.close(); + } catch (SQLException sqle) { + sqle.printStackTrace(); + } + return Integer.toString(orderNo); + } + + public static String handleNewOrder(InputNewOrder newOrder) { + String orderNum = insertNewOrder(newOrder); + return orderNum; + } + public static void dumpOrders() { + StringBuilder dump = new StringBuilder(); + try { + dump.append("=============================\n"); + dump.append("\tStart Order Dump\n"); + dump.append("=============================\n"); + dump.append("OrderID").append("\t").append("CustNo").append("\t"); + dump.append("ArtNo").append("\t").append("Article").append("\t\t"); + dump.append("Amount").append("\t").append("Created on").append("\n"); + dump.append("--------------------------------------------------------\n"); + Statement stmt = getDbConnection().createStatement(); + String sql = "SELECT * from \"orders\" AS o JOIN \"orderpos\" AS op ON o.orderid = op.orderid"; + ResultSet rs = stmt.executeQuery(sql); + while(rs.next()) { + dump.append(rs.getString("orderid")).append("\t"); + dump.append(rs.getString("customerno")).append("\t"); + dump.append(rs.getString("articleno")).append("\t"); + dump.append(rs.getString("articlename")).append("\t"); + dump.append(rs.getString("amount")).append("\t"); + dump.append(rs.getTimestamp("order_created_ts")).append("\n"); + } + rs.close(); + stmt.close(); + dump.append("=============================\n"); + dump.append("\tFinish Order Dump\n"); + dump.append("=============================\n"); + System.out.println(dump.toString()); + } catch (SQLException e) { + e.printStackTrace(); + } + } + public static void shutdown() { + boolean gotSQLExc = false; + try { + DriverManager.getConnection("jdbc:derby:;shutdown=true"); + } catch (SQLException se) { + if ( se.getSQLState().equals("XJ015") ) { + gotSQLExc = true; + } + } + if (!gotSQLExc) { + System.out.println("Database did not shut down normally"); + } else { + System.out.println("Database shut down normally"); + } + } +} diff --git a/Master/Daten- und Systemintegration/CamelPrototype/src/main/java/de/h_da/fbi/dsi/ws0910/camelprototype/OrderTransformer.java b/Master/Daten- und Systemintegration/CamelPrototype/src/main/java/de/h_da/fbi/dsi/ws0910/camelprototype/OrderTransformer.java new file mode 100644 index 0000000..6c27c7b --- /dev/null +++ b/Master/Daten- und Systemintegration/CamelPrototype/src/main/java/de/h_da/fbi/dsi/ws0910/camelprototype/OrderTransformer.java @@ -0,0 +1,25 @@ +package de.h_da.fbi.dsi.ws0910.camelprototype; + +import java.util.List; +import org.apache.camel.Exchange; + +public class OrderTransformer { + public void transformFromCsv(Exchange ex) { + String filename = ex.getIn().getHeader("CamelFileName",String.class); + // csv format will generate a List> as body + // to avoid unchecked casting this ugly thing is necessary + List lines = (List) ex.getIn().getBody(); + InputNewOrder res = new InputNewOrder(); + String customerNo = filename.split("\\.")[0]; + res.setCustomerNo(customerNo); + for(Object line: lines) { + List cols = (List) line; + OrderPosition orderPos = new OrderPosition(); + orderPos.articleNo = (String) cols.get(0); + orderPos.article = (String) cols.get(1); + orderPos.amount = Integer.parseInt((String) cols.get(2)); + res.getOrderpositions().add(orderPos); + } + ex.getIn().setBody(res); + } +} diff --git a/Master/Daten- und Systemintegration/CamelPrototype/src/main/resources/META-INF/spring/camel-context.xml b/Master/Daten- und Systemintegration/CamelPrototype/src/main/resources/META-INF/spring/camel-context.xml new file mode 100644 index 0000000..c412226 --- /dev/null +++ b/Master/Daten- und Systemintegration/CamelPrototype/src/main/resources/META-INF/spring/camel-context.xml @@ -0,0 +1,23 @@ + + + + + + + + + + de.h_da.fbi.dsi.ws0910.camelprototype + + + + + + \ No newline at end of file diff --git a/Master/Daten- und Systemintegration/CamelPrototype/src/main/resources/OrderMailBody.vm b/Master/Daten- und Systemintegration/CamelPrototype/src/main/resources/OrderMailBody.vm new file mode 100644 index 0000000..0b85af6 --- /dev/null +++ b/Master/Daten- und Systemintegration/CamelPrototype/src/main/resources/OrderMailBody.vm @@ -0,0 +1,12 @@ +Customer $body.customerNo has placed a new order via $headers.from + +Order-No.: $headers.orderno +Cust.No.: $body.customerNo + +Art.-No. Article Amount +==================================================== +#foreach($orderPos in $body.orderpositions) +$orderPos.articleNo $orderPos.article $orderPos.amount +#end + +This is an auto generated email. You cannot reply. \ No newline at end of file diff --git a/Master/Daten- und Systemintegration/CamelPrototype/src/main/resources/OrderService.wsdl b/Master/Daten- und Systemintegration/CamelPrototype/src/main/resources/OrderService.wsdl new file mode 100644 index 0000000..296f6be --- /dev/null +++ b/Master/Daten- und Systemintegration/CamelPrototype/src/main/resources/OrderService.wsdl @@ -0,0 +1,71 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Master/Daten- und Systemintegration/CamelPrototype/src/main/resources/log4j.properties b/Master/Daten- und Systemintegration/CamelPrototype/src/main/resources/log4j.properties new file mode 100644 index 0000000..82dd219 --- /dev/null +++ b/Master/Daten- und Systemintegration/CamelPrototype/src/main/resources/log4j.properties @@ -0,0 +1,22 @@ + +# default properties to initialise log4j +log4j.rootLogger=INFO, console + +# Console appender +log4j.appender.console=org.apache.log4j.ConsoleAppender +log4j.appender.console.layout=org.apache.log4j.PatternLayout +log4j.appender.console.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n + +# File appender +log4j.appender.file=org.apache.log4j.FileAppender +log4j.appender.file.layout=org.apache.log4j.PatternLayout +log4j.appender.file.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n +log4j.appender.file.file=target/dsi-order.log +log4j.appender.file.append=true + +# settings for specific packages +#log4j.logger.org.springframework=WARN +#log4j.logger.org.apache.cxf=WARN + +# Camel logging +log4j.logger.org.apache.camel=INFO \ No newline at end of file diff --git a/Master/Daten- und Systemintegration/CamelPrototype/src/main/webapp/WEB-INF/web.xml b/Master/Daten- und Systemintegration/CamelPrototype/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..da15020 --- /dev/null +++ b/Master/Daten- und Systemintegration/CamelPrototype/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,32 @@ + + + + DSI CamelPrototype Web Application + + + + contextConfigLocation + classpath:camel-config.xml + + + + + org.springframework.web.context.ContextLoaderListener + + + + + CXFServlet + org.apache.cxf.transport.servlet.CXFServlet + 1 + + + + + CXFServlet + /webservices/* + + + diff --git a/Master/Daten- und Systemintegration/CamelPrototype/src/test/java/de/h_da/fbi/dsi/ws0910/camelprototype/CreateOrderRoutesTest.java b/Master/Daten- und Systemintegration/CamelPrototype/src/test/java/de/h_da/fbi/dsi/ws0910/camelprototype/CreateOrderRoutesTest.java new file mode 100644 index 0000000..20a9bbd --- /dev/null +++ b/Master/Daten- und Systemintegration/CamelPrototype/src/test/java/de/h_da/fbi/dsi/ws0910/camelprototype/CreateOrderRoutesTest.java @@ -0,0 +1,112 @@ +package de.h_da.fbi.dsi.ws0910.camelprototype; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import java.io.BufferedWriter; +import java.io.FileWriter; +import java.io.IOException; +import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.jvnet.mock_javamail.Mailbox; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; + +@ContextConfiguration(locations="/CreateOrderRoutesTest-context.xml") +public class CreateOrderRoutesTest extends AbstractJUnit4SpringContextTests{ + private static final long WAIT = 2000; + private static Mailbox inbox; + private static String EMAILRECIPIENT = "neworders@mycompany.com"; + // should be the same address as we have in our route + private static String ADDRESS = "http://localhost:8080/camelprototype/webservices/order"; + + protected static OrderEndpoint createCXFClient() { + // we use CXF to create a client for us as its easier than JAXWS and works + JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); + factory.setServiceClass(OrderEndpoint.class); + factory.setAddress(ADDRESS); + return (OrderEndpoint) factory.create(); + } + protected InputNewOrder createWSTestData() { + // create input parameter + InputNewOrder input = new InputNewOrder(); + input.setCustomerNo("456"); + OrderPosition orderPos1 = new OrderPosition(); + orderPos1.setAmount(2); + orderPos1.setArticle("Super Gadget"); + orderPos1.setArticleNo("7890"); + input.getOrderpositions().add(orderPos1); + + OrderPosition orderPos2 = new OrderPosition(); + orderPos2.setAmount(8); + orderPos2.setArticle("Mega Gadget"); + orderPos2.setArticleNo("6543"); + input.getOrderpositions().add(orderPos2); + + OrderPosition orderPos3 = new OrderPosition(); + orderPos3.setAmount(3); + orderPos3.setArticle("Great Nonsens"); + orderPos3.setArticleNo("N6543"); + input.getOrderpositions().add(orderPos3); + return input; + } + private void createCsvTestData() throws IOException { + BufferedWriter bw = new BufferedWriter(new FileWriter("target/csvinput/34524.csv")); + bw.write("574,One Article,3\n"); + bw.write("575,Second Article,4\n"); + bw.write("852,Another Article,5"); + bw.close(); + } + @Test + public void testWebserviceNewOrder() throws Exception { + try { + // assert mailbox is empty before starting + assertEquals("Should not have mails", 0, inbox.size()); + // create the webservice client + OrderEndpoint client = createCXFClient(); + // create Test Order for webservice testclient + InputNewOrder testOrder = createWSTestData(); + for (int count=1;count<=5;count++) { + // send the request + OutputNewOrder out = client.createOrder(testOrder); + // assert we got a OK back + assertEquals("0", out.getStatus()); + //assert we got an order number back + assertFalse("Should have an order number", out.getMessage().isEmpty()); + System.out.println("Received orderno: "+out.getMessage()); + // let some time pass to allow Camel to pickup the file and send it as an email + Thread.sleep(WAIT); + // assert mail box + assertEquals("Should have got "+count+" mails", count, inbox.size()); + } + } catch (Exception ex) { + throw new Exception("Error executing Webservice Test", ex); + } + } + + @Test + public void testCSVNewOrder() throws Exception { + // assert mailbox is empty before starting + assertEquals("Should not have mails", 0, inbox.size()); + // create csv test data + createCsvTestData(); + // let some time pass to allow Camel to pickup the file and send it as an email + Thread.sleep(WAIT); + // assert mail box + assertEquals("Should have got 1 mail", 1, inbox.size()); + } + @Before + public void cleanInbox() { + inbox.clear(); + } + @BeforeClass + public static void setUp() throws Exception { + inbox = Mailbox.get(EMAILRECIPIENT); + } + @AfterClass + public static void tearDown() throws Exception { + OrderSystem.getInstance().dumpOrders(); + } +} diff --git a/Master/Daten- und Systemintegration/CamelPrototype/src/test/resources/CreateOrderRoutesTest-context.xml b/Master/Daten- und Systemintegration/CamelPrototype/src/test/resources/CreateOrderRoutesTest-context.xml new file mode 100644 index 0000000..c485b04 --- /dev/null +++ b/Master/Daten- und Systemintegration/CamelPrototype/src/test/resources/CreateOrderRoutesTest-context.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Master/Daten- und Systemintegration/CamelPrototype/src/test/resources/log4j.properties b/Master/Daten- und Systemintegration/CamelPrototype/src/test/resources/log4j.properties new file mode 100644 index 0000000..6d3a2b3 --- /dev/null +++ b/Master/Daten- und Systemintegration/CamelPrototype/src/test/resources/log4j.properties @@ -0,0 +1,22 @@ + +# default properties to initialise log4j +log4j.rootLogger=INFO, file + +# Console appender +log4j.appender.console=org.apache.log4j.ConsoleAppender +log4j.appender.console.layout=org.apache.log4j.PatternLayout +log4j.appender.console.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n + +# File appender +log4j.appender.file=org.apache.log4j.FileAppender +log4j.appender.file.layout=org.apache.log4j.PatternLayout +log4j.appender.file.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n +log4j.appender.file.file=target/dsi-order-test.log +log4j.appender.file.append=true + +# settings for specific packages +log4j.logger.org.springframework=WARN +log4j.logger.org.apache.cxf=WARN + +# Camel logging +log4j.logger.org.apache.camel=DEBUG \ No newline at end of file -- cgit v1.2.3