Commit 0b7da064 authored by Dr. Martin Goik's avatar Dr. Martin Goik

Exercise skeleton

parent 2bc5916b
.project
.classpath
/.settings/
/target/
A1.log
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="catalog">
<xs:complexType>
<xs:sequence>
<xs:element ref="product" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:key name="uniqueProductId">
<xs:selector xpath="product"/>
<xs:field xpath="@id"/>
</xs:key>
</xs:element>
<xs:element name="product">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="description" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="age" type="xs:int" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
<xs:attribute name="id" type="xs:int" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Schema/catalog.xsd">
<product id="17">
<name>Monkey Picked Tea</name>
<description>Rare wild Chinese tea</description>
<description>Picked only by specially trained monkeys</description>
</product>
<product id="42">
<name>4-Person Instant Tent</name>
<description>4-person, 1-room tent</description>
<description>Pre-attached tent poles</description>
<description>Exclusive WeatherTec system.</description>
<age>15</age>
</product>
</catalog>
\ No newline at end of file
DROP TABLE IF EXISTS Description;
DROP TABLE IF EXISTS Product;
CREATE TABLE Product (
id INTEGER NOT NULL PRIMARY KEY
,name VARCHAR(255) NOT NULL
,age SMALLINT
) ENGINE=InnoDB;
CREATE TABLE Description (
product INTEGER NOT NULL
,orderIndex int NOT NULL -- preserving the order of descriptions belonging to a given product
,text VARCHAR(255) NOT NULL
,UNIQUE(product, orderIndex)
,FOREIGN KEY(product) REFERENCES Product(id)
) ENGINE=InnoDB;
-- example data corresponding to products.xml --
-- Product lacking age property --
INSERT INTO Product (id, name) VALUES (1, 'Monkey Picked Tea');
INSERT INTO Description VALUES(1, 0, 'Picked only by specially trained monkeys');
INSERT INTO Description VALUES(1, 1, 'Rare wild Chinese tea');
INSERT INTO Product VALUES (2, '4-Person Instant Tent', 15);
INSERT INTO Description VALUES(2, 0, 'Exclusive WeatherTec system.');
INSERT INTO Description VALUES(2, 1, '4-person, 1-compartment tent');
INSERT INTO Description VALUES(2, 2, 'Pre-attached tent poles');
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>de.hdm-stuttgart.mi</groupId>
<artifactId>lecturenotes-pom</artifactId>
<version>1.0</version>
<relativePath>../../../../pom.xml</relativePath>
</parent>
<groupId>de.hdm-stuttgart.mi.sda1</groupId>
<artifactId>rdbms2catalog</artifactId>
<version>2.0</version>
<packaging>jar</packaging>
<name>rdbms2catalogJpa</name>
<url>http://www.mi.hdm-stuttgart.de/freedocs</url>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.0.0.CR1</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>5.0.0.CR1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom2</artifactId>
<version>2.0.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
</dependencies>
</project>
package de.hdm_stuttgart.mi.sda1.sql2catalog;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import org.xml.sax.SAXException;
/**
* Dumping RDBMS catalog data
* to XML.
*
*/
public class Rdbms2Xml {
/**
* @param args
* Unused
* @throws SAXException
* @throws ParserConfigurationException
* @throws IOException
*/
public static void main(String[] args) throws ParserConfigurationException,
SAXException, IOException {
final Element catalogElement = new Element("catalog");
new RdbmsAccess().appendProducts(catalogElement);
// Write XML content to standard output
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
outputter.output(catalogElement, System.out);
}
}
package de.hdm_stuttgart.mi.sda1.sql2catalog;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import org.jdom2.Element;
/**
* Reading product data
*
*/
public class RdbmsAccess {
static final EntityManager em;
static {
final EntityManagerFactory emFactory = Persistence.createEntityManagerFactory("none");
em = emFactory.createEntityManager();
}
public void appendProducts(final Element root) {
final EntityTransaction transaction = em.getTransaction();
transaction.begin();
// TODO: read RDBMS for products and related descriptions, then create DOM tree
transaction.commit();
}
}
\ No newline at end of file
package de.hdm_stuttgart.mi.sda1.sql2catalog.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Description {
@Id
@GeneratedValue
Long id;
}
package de.hdm_stuttgart.mi.sda1.sql2catalog.model;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Product {
@Id
int id;
}
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="create-drop" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/hdm" />
<property name="javax.persistence.jdbc.user" value="hdmuser" />
<property name="javax.persistence.jdbc.password" value="XYZ" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<!-- Configuring Connection Pool -->
<property name="hibernate.c3p0.min_size" value="5" />
<property name="hibernate.c3p0.max_size" value="20" />
<property name="hibernate.c3p0.timeout" value="500" />
<property name="hibernate.c3p0.max_statements" value="50" />
<property name="hibernate.c3p0.idle_test_period" value="2000" />
</properties>
</persistence-unit>
<persistence-unit name="none" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/hdm" />
<property name="javax.persistence.jdbc.user" value="hdmuser" />
<property name="javax.persistence.jdbc.password" value="XYZ" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="hibernate.hbm2ddl.auto" value="none" />
<!-- Configuring Connection Pool -->
<property name="hibernate.c3p0.min_size" value="5" />
<property name="hibernate.c3p0.max_size" value="20" />
<property name="hibernate.c3p0.timeout" value="500" />
<property name="hibernate.c3p0.max_statements" value="50" />
<property name="hibernate.c3p0.idle_test_period" value="2000" />
</properties>
</persistence-unit>
</persistence>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<File name="A1" fileName="A1.log" append="false">
<PatternLayout pattern="%t %-5p %c{2} - %m%n"/>
</File>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%C{2} (%F:%L) - %m%n"/>
</Console>
</Appenders>
<Loggers>
<!-- You my want to define class or package level per-logger rules -->
<Logger name="de.hdm_stuttgart.mi.sda1.catalog2sax.App" level="debug">
<AppenderRef ref="A1"/>
</Logger>
<Root level="debug">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>
\ No newline at end of file
package rdbms2catalog;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import org.junit.Test;
import de.hdm_stuttgart.mi.sda1.sql2catalog.model.Description;
import de.hdm_stuttgart.mi.sda1.sql2catalog.model.Product;
public class TestSchema {
static final EntityManager em;
static {
final EntityManagerFactory emFactory = Persistence.createEntityManagerFactory("create-drop");
em = emFactory.createEntityManager();
}
@Test
public void insertData() {
{
final EntityTransaction transaction = em.getTransaction();
transaction.begin();
//TODO: Once your your Entities de.hdm_stuttgart.mi.sda1.sql2catalog.model.Product and
// de.hdm_stuttgart.mi.sda1.sql2catalog.model.Description are being defined
// accordingly the following commented code should work.
// final Product monkeyPickedTea = new Product(1, "Monkey Picked Tea", null);
// new Description(monkeyPickedTea, 0, "Picked only by specially trained monkeys");
// new Description(monkeyPickedTea, 1, "Rare wild Chinese tea");
//
// final Product instantTent = new Product(2, "4-Person Instant Tent", 15);
// new Description(instantTent, 0, "Exclusive WeatherTec system.");
// new Description(instantTent, 1, "4-person, 1-compartment tent");
// new Description(instantTent, 2, "Pre-attached tent poles");
//
// em.persist(monkeyPickedTea);
// em.persist(instantTent);
transaction.commit();
}
}
}
\ No newline at end of file
......@@ -7,7 +7,7 @@
<artifactId>lecturenotes-pom</artifactId>
<version>1.0</version>
<relativePath>../../pom.xml</relativePath>
<relativePath>../../../../pom.xml</relativePath>
</parent>
<groupId>de.hdm-stuttgart.mi.sda1</groupId>
......
package de.hdm_stuttgart.mi.sda1.sql2catalog;
/**
* Project wide helper methods
*
*/
public class Helper {
/**
* Write message to stderr and exit with given error code
* @param errMsg
* @param errorCode
*/
public static void exitWithErrorMessage(final String errMsg, int errorCode) {
System.err.println(errMsg);
System.exit(errorCode);
}
}
package de.hdm_stuttgart.mi.sda1.sql2catalog;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
public class Messages {
private static final String BUNDLE_NAME = "de.hdm_stuttgart.mi.sda1.sql2catalog.messages"; //$NON-NLS-1$
private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle
.getBundle(BUNDLE_NAME);
private Messages() {
}
public static String getString(String key) {
try {
return RESOURCE_BUNDLE.getString(key);
} catch (MissingResourceException e) {
return '!' + key + '!';
}
}
}
Xml2Rdbms.14='
Xml2Rdbms.errMsgUnableCloseRdbms=Unable to close RDBMS access
Xml2Rdbms.errMsgUnableInitDataInsert=Unable to initialize data inserter
Xml2Rdbms.errMsgUnableRegisterDriverClass=Unable to register driver class '
Xml2Rdbms.errMsgUnableToConnect=Unable to connect as user '
Xml2Rdbms.jdbcDriverName=com.mysql.jdbc.Driver
Xml2Rdbms.jdbcPasswd=XYZ
Xml2Rdbms.jdbcUrl=jdbc:mysql://localhost:3306/hdm
Xml2Rdbms.jdbcUser=hdmuser
Xml2Rdbms.xmlSourceFileName=products.xml
Xml2Rdbms.errMsgUnableCommitTransaction=Unable to commit transaction
\ No newline at end of file
......@@ -111,7 +111,8 @@
<module>Sda1/NoCast</module>
<module>Sda1/rdbms2catalog</module>
<module>Sda1/rdbms2catalogJpa</module>
<module>Sda1/Jpa/rdbms2catalog/skeleton</module>
<module>Sda1/Jpa/rdbms2catalog/solution</module>
<module>Sda1/VerifyImgAccess</module>
<module>Sda1/xmlstatistics</module>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment