diff --git a/Sda1/Etest/RdbmsStudents2xml/exercise1.xhtml b/Sda1/Etest/RdbmsStudents2xml/exercise1.xhtml new file mode 100644 index 0000000000000000000000000000000000000000..6435c6733e41c217b389eef22969861b361bbaae --- /dev/null +++ b/Sda1/Etest/RdbmsStudents2xml/exercise1.xhtml @@ -0,0 +1,38 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE html> +<html xmlns="http://www.w3.org/1999/xhtml"> + <head> + <title>Exercise 1</title> + </head> + + <body><h2>Objective</h2><p>Transforming a relational into an XML + schema</p><h2>Preparations / Description</h2><ol> + <li>Download <a + href="/iliasData/goik/rdbmsStudents2xml_zcx9sazr3FFDS_++vcds/rdbmsStudents2xml.zip">rdbmsStudents2xml.zip</a> + and import it as a project into your Eclipse workspace.</li> + + <li><p>Your imported project <code>rdbmsstudent2xml</code> + contains:</p><ul> + <li><p><strong><code>Schema/schema.sql</code> describing university + data:</strong></p><ul> + <li>A relational schema consisting of two related tables + <code>StudyCourse</code> and <code>Student</code>.</li> + + <li><code>INSERT</code> statements providing sample data.</li> + </ul></li> + + <li><p><strong><code>Schema/testdata.xml</code> corresponding to the + previously mentioned relational data being contained in + <code>Schema/schema.sql</code>.</strong></p></li> + + <li><p><strong> <code>Schema/university.xsd</code>, a sample XML + schema to be extended</strong></p></li> + </ul></li> + </ol><h2>ToDo</h2><p>Complete <code>Schema/university.xsd</code> to match + all integrity constraints of the corresponding relational + <code>Schema/schema.sql</code>. The XML sample + <code>Schema/testdata.xml</code> may guide you but may be misleading, too: + Examine the two <code>CREATE TABLE</code> statements thoroughly in order to + find all data integrity constraints.</p><p>On completion upload your file + <code>university.xsd</code> to the exam system.</p></body> +</html> diff --git a/Sda1/Etest/RdbmsStudents2xml/exercise2.xhtml b/Sda1/Etest/RdbmsStudents2xml/exercise2.xhtml new file mode 100644 index 0000000000000000000000000000000000000000..8d11eeff15fa30b4ccbad425b4f09184b02b473b --- /dev/null +++ b/Sda1/Etest/RdbmsStudents2xml/exercise2.xhtml @@ -0,0 +1,46 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE html> +<html xmlns="http://www.w3.org/1999/xhtml"> + <head> + <title>Exercise 2</title> + </head> + + <body><h2>Objective</h2><p>Transforming relational data into XML + output.</p><h2>Preparations / Description</h2><ol> + <li>Download <a + href="/iliasData/goik/rdbmsStudents2xml_zcx9sazr3FFDS_++vcds/rdbmsStudents2xml.zip">rdbmsStudents2xml.zip</a> + and import it as a project into your Eclipse workspace.</li> + + <li><p>Your imported project <code>rdbmsstudent2xml</code> + contains:</p><ul> + <li><p><strong><code>Schema/schema.sql</code> describing university + data:</strong></p><ul> + <li>A relational schema consisting of two related tables + <code>StudyCourse</code> and <code>Student</code>.</li> + + <li><code>INSERT</code> statements providing sample data.</li> + </ul></li> + + <li><p><strong>A driver class + <code>de.hdm_stuttgart.mi/sda1.sql2catalog.Rdbms2Xml</code>:</strong></p><p>On + Java execution this class currently just produces:</p><pre><university> + <studyCourses/> + <students/> +</university></pre></li> + + <li><p><strong><code>Schema/testdata.xml</code> corresponding to the + previously mentioned relational data being contained in + <code>Schema/schema.sql</code>.</strong></p></li> + </ul></li> + </ol><h2>ToDo</h2><p>Extend + <code>de.hdm_stuttgart.mi/sda1.sql2catalog.Rdbms2Xml</code> to generate + suitable XML data from relational sources. Data being contained in + <code>Schema/schema.sql</code> shall for example be transformed to + <code>Schema/testdata.xml</code>. But off course your implementation is + expected to work for arbitrary relational data input being consistent with + the current relational schema.</p><p>It might be a good idea to extend class + <code>de.hdm_stuttgart.mi.sda1.sql2catalog.RdbmsAccess</code> implementing a + database access layer.</p><p>On completion export your project as a + <code>.zip</code> file and upload it. Only the last uploaded file will + become subject to marking.</p></body> +</html> diff --git a/Sda1/Etest/RdbmsStudents2xml/rdbmsStudents2xml/.gitignore b/Sda1/Etest/RdbmsStudents2xml/rdbmsStudents2xml/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..660be2a5fdb9e3019ef8fc737d47fba56fc32ff3 --- /dev/null +++ b/Sda1/Etest/RdbmsStudents2xml/rdbmsStudents2xml/.gitignore @@ -0,0 +1,5 @@ +.project +.classpath +/.settings/ +/target/ +A1.log \ No newline at end of file diff --git a/Sda1/Etest/RdbmsStudents2xml/rdbmsStudents2xml/Schema/schema.sql b/Sda1/Etest/RdbmsStudents2xml/rdbmsStudents2xml/Schema/schema.sql new file mode 100644 index 0000000000000000000000000000000000000000..3f3d2c86f8eba5f7f682d060d9b8a04a2b77b8d9 --- /dev/null +++ b/Sda1/Etest/RdbmsStudents2xml/rdbmsStudents2xml/Schema/schema.sql @@ -0,0 +1,33 @@ +DROP TABLE IF EXISTS Student; +DROP TABLE IF EXISTS StudyCourse; + +CREATE TABLE StudyCourse ( + id INTEGER NOT NULL PRIMARY KEY + ,name VARCHAR(255) NOT NULL + ,shortName CHAR(3) + ,program CHAR(1) /* Bachelor or Master program? */ + ,CHECK (program IN ('B', 'M')) /* Mysql does not really honour CHECK constraints, but pretend it does! */ +) ENGINE=InnoDB; + + +CREATE TABLE Student ( + id INTEGER NOT NULL PRIMARY KEY + ,fullName VARCHAR(255) NOT NULL + ,email VARCHAR(255) NOT NULL UNIQUE + ,studyCourse INTEGER + ,FOREIGN KEY(studyCourse) REFERENCES StudyCourse(id) +) ENGINE=InnoDB; + +-- example data corresponding to university.xml -- + +INSERT INTO StudyCourse VALUES (1, 'Mobile Media', 'MMB', 'B'); +INSERT INTO StudyCourse VALUES (2, 'Computer Science in Media', NULL, 'M'); + + +INSERT INTO Student VALUES(33, 'Jim Bone', 'bone@foo.org', NULL); +INSERT INTO Student VALUES(40, 'Tracy Adams', 'adams@security.com', 2); + +-- Selects -- + +SELECT * FROM Student; +SELECT * FROM StudyCourse; diff --git a/Sda1/Etest/RdbmsStudents2xml/rdbmsStudents2xml/Schema/testdata.xml b/Sda1/Etest/RdbmsStudents2xml/rdbmsStudents2xml/Schema/testdata.xml new file mode 100644 index 0000000000000000000000000000000000000000..342bf4f46b8a4b8a3e29c7ebf060af5c5d977f1a --- /dev/null +++ b/Sda1/Etest/RdbmsStudents2xml/rdbmsStudents2xml/Schema/testdata.xml @@ -0,0 +1,24 @@ +<?xml version="1.0" encoding="UTF-8"?> +<university xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:noNamespaceSchemaLocation="university.xsd"> + + <studyCourses> + <studyCourse id="1" program="B"> + <name>Mobile Media</name> + <shortName>MMB</shortName> + </studyCourse> + <studyCourse id="2" program="M"> + <name>Computer Science in Media</name> + </studyCourse> + </studyCourses> + <students> + <student id="33"> + <fullName>Jim Bone</fullName> + <email>bone@foo.org</email> + </student> + <student id="40" studyCourse="2"> + <fullName>Tracy Adams</fullName> + <email>adams@security.com</email> + </student> + </students> +</university> \ No newline at end of file diff --git a/Sda1/Etest/RdbmsStudents2xml/rdbmsStudents2xml/Schema/university.xsd b/Sda1/Etest/RdbmsStudents2xml/rdbmsStudents2xml/Schema/university.xsd new file mode 100644 index 0000000000000000000000000000000000000000..724380a5a35557681ef57f53f2e77ff172ffe2e9 --- /dev/null +++ b/Sda1/Etest/RdbmsStudents2xml/rdbmsStudents2xml/Schema/university.xsd @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> + + <xs:element name="university"> + + </xs:element> + +</xs:schema> diff --git a/Sda1/Etest/RdbmsStudents2xml/rdbmsStudents2xml/pom.xml b/Sda1/Etest/RdbmsStudents2xml/rdbmsStudents2xml/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..8801a7f77c36df834719f66cc90fa8f5f2eab16b --- /dev/null +++ b/Sda1/Etest/RdbmsStudents2xml/rdbmsStudents2xml/pom.xml @@ -0,0 +1,97 @@ +<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> + + <groupId>de.hdm-stuttgart.mi.sda1</groupId> + <artifactId>rdbmsstudent2xml</artifactId> + <version>0.8</version> + <packaging>jar</packaging> + + <name>rdbmsstudent2xml</name> + + <url>http://www.mi.hdm-stuttgart.de/freedocs</url> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> + + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.11</version> + <scope>test</scope> + </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> + + <build> + <plugins> + + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>3.1</version> + <configuration> + <source>1.8</source> + <target>1.8</target> + </configuration> + </plugin> + + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-javadoc-plugin</artifactId> + <version>2.10.1</version> + <configuration /> + </plugin> + + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-shade-plugin</artifactId> + <version>2.3</version> + <configuration> + <transformers> + <transformer + implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> + <manifestEntries> + <Main-Class>de.hdm_stuttgart.mi.sda1.catalog2sax.App</Main-Class> + </manifestEntries> + </transformer> + </transformers> + </configuration> + <executions> + <execution> + <phase>package</phase> + <goals> + <goal>shade</goal> + </goals> + </execution> + </executions> + </plugin> + + </plugins> + </build> +</project> diff --git a/Sda1/Etest/RdbmsStudents2xml/rdbmsStudents2xml/src/main/java/de/hdm_stuttgart/mi/sda1/sql2catalog/Helper.java b/Sda1/Etest/RdbmsStudents2xml/rdbmsStudents2xml/src/main/java/de/hdm_stuttgart/mi/sda1/sql2catalog/Helper.java new file mode 100644 index 0000000000000000000000000000000000000000..f7879587615344dbd2eef926cfb7b55f4a0c451e --- /dev/null +++ b/Sda1/Etest/RdbmsStudents2xml/rdbmsStudents2xml/src/main/java/de/hdm_stuttgart/mi/sda1/sql2catalog/Helper.java @@ -0,0 +1,18 @@ +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); + } + +} diff --git a/Sda1/Etest/RdbmsStudents2xml/rdbmsStudents2xml/src/main/java/de/hdm_stuttgart/mi/sda1/sql2catalog/Rdbms2Xml.java b/Sda1/Etest/RdbmsStudents2xml/rdbmsStudents2xml/src/main/java/de/hdm_stuttgart/mi/sda1/sql2catalog/Rdbms2Xml.java new file mode 100644 index 0000000000000000000000000000000000000000..f733fc76c3d903baafdcb2fc9f7d75a7fde95bc1 --- /dev/null +++ b/Sda1/Etest/RdbmsStudents2xml/rdbmsStudents2xml/src/main/java/de/hdm_stuttgart/mi/sda1/sql2catalog/Rdbms2Xml.java @@ -0,0 +1,58 @@ +package de.hdm_stuttgart.mi.sda1.sql2catalog; + +import java.io.IOException; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +import org.jdom2.Element; +import org.jdom2.output.Format; +import org.jdom2.output.XMLOutputter; + +/** + * A simple SAX parser demo + * + */ +public class Rdbms2Xml { + + /** + * @param args Unused + */ + public static void main(String[] args) { + + // Register Driver + final String sqlDriverClassName = "com.mysql.jdbc.Driver"; + try { + Class.forName(sqlDriverClassName); + } catch (ClassNotFoundException e) { + Helper.exitWithErrorMessage("Unable to register driver class '" + sqlDriverClassName + "'", 1); + } + + // Opening a JDBC connection + // + Connection conn = null; + + final String jdbcConnectionUrl = "jdbc:mysql://localhost:3306/hdm"; + final String userName = "hdmuser"; + try { + conn = DriverManager.getConnection(jdbcConnectionUrl, userName, "XYZ"); + } catch (SQLException e) { + Helper.exitWithErrorMessage("Unable to connect as user '" + userName + "' to '" + + jdbcConnectionUrl + "'", 1); + } + + final Element universityElement = new Element("university"); + final Element studyCourses = new Element("studyCourses"); + universityElement.addContent(studyCourses); + final Element students = new Element("students"); + universityElement.addContent(students); + + // Write XML content to standard output + XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat()); + try { + outputter.output(universityElement, System.out); + } catch (IOException e) { + Helper.exitWithErrorMessage("Unable to write XML data:" + e.getLocalizedMessage(), 1); + } + } +} diff --git a/Sda1/Etest/RdbmsStudents2xml/rdbmsStudents2xml/src/main/java/de/hdm_stuttgart/mi/sda1/sql2catalog/RdbmsAccess.java b/Sda1/Etest/RdbmsStudents2xml/rdbmsStudents2xml/src/main/java/de/hdm_stuttgart/mi/sda1/sql2catalog/RdbmsAccess.java new file mode 100644 index 0000000000000000000000000000000000000000..740a996a2fb8aac9d4cf5f95a9fc46b65d967027 --- /dev/null +++ b/Sda1/Etest/RdbmsStudents2xml/rdbmsStudents2xml/src/main/java/de/hdm_stuttgart/mi/sda1/sql2catalog/RdbmsAccess.java @@ -0,0 +1,18 @@ +package de.hdm_stuttgart.mi.sda1.sql2catalog; + +import java.sql.Connection; +import java.sql.SQLException; + +/** + * Reading study course and student data + * + */ +public class RdbmsAccess { + + /** + * @param conn Destination RDBMS + */ + public RdbmsAccess(final Connection conn) { + } + +} diff --git a/Sda1/Etest/RdbmsStudents2xml/rdbmsStudents2xml/src/main/resources/log4j2.xml b/Sda1/Etest/RdbmsStudents2xml/rdbmsStudents2xml/src/main/resources/log4j2.xml new file mode 100644 index 0000000000000000000000000000000000000000..eda4f3b0d781b09217fb4218285c39adf8d3c9c0 --- /dev/null +++ b/Sda1/Etest/RdbmsStudents2xml/rdbmsStudents2xml/src/main/resources/log4j2.xml @@ -0,0 +1,21 @@ +<?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 diff --git a/Sda1/Etest/RdbmsStudents2xml/rdbmsStudents2xml_solution/Schema/schema.sql b/Sda1/Etest/RdbmsStudents2xml/rdbmsStudents2xml_solution/Schema/schema.sql index 3f3d2c86f8eba5f7f682d060d9b8a04a2b77b8d9..5867d4df1ddfd5b832ec41fa35fa8ca2fa99ee4f 100644 --- a/Sda1/Etest/RdbmsStudents2xml/rdbmsStudents2xml_solution/Schema/schema.sql +++ b/Sda1/Etest/RdbmsStudents2xml/rdbmsStudents2xml_solution/Schema/schema.sql @@ -3,13 +3,12 @@ DROP TABLE IF EXISTS StudyCourse; CREATE TABLE StudyCourse ( id INTEGER NOT NULL PRIMARY KEY - ,name VARCHAR(255) NOT NULL - ,shortName CHAR(3) + ,name VARCHAR(255) NOT NULL UNIQUE + ,shortName CHAR(3) UNIQUE ,program CHAR(1) /* Bachelor or Master program? */ ,CHECK (program IN ('B', 'M')) /* Mysql does not really honour CHECK constraints, but pretend it does! */ ) ENGINE=InnoDB; - CREATE TABLE Student ( id INTEGER NOT NULL PRIMARY KEY ,fullName VARCHAR(255) NOT NULL @@ -23,7 +22,6 @@ CREATE TABLE Student ( INSERT INTO StudyCourse VALUES (1, 'Mobile Media', 'MMB', 'B'); INSERT INTO StudyCourse VALUES (2, 'Computer Science in Media', NULL, 'M'); - INSERT INTO Student VALUES(33, 'Jim Bone', 'bone@foo.org', NULL); INSERT INTO Student VALUES(40, 'Tracy Adams', 'adams@security.com', 2); diff --git a/Sda1/Etest/RdbmsStudents2xml/rdbmsStudents2xml_solution/Schema/testdata.xml b/Sda1/Etest/RdbmsStudents2xml/rdbmsStudents2xml_solution/Schema/testdata.xml new file mode 100644 index 0000000000000000000000000000000000000000..342bf4f46b8a4b8a3e29c7ebf060af5c5d977f1a --- /dev/null +++ b/Sda1/Etest/RdbmsStudents2xml/rdbmsStudents2xml_solution/Schema/testdata.xml @@ -0,0 +1,24 @@ +<?xml version="1.0" encoding="UTF-8"?> +<university xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:noNamespaceSchemaLocation="university.xsd"> + + <studyCourses> + <studyCourse id="1" program="B"> + <name>Mobile Media</name> + <shortName>MMB</shortName> + </studyCourse> + <studyCourse id="2" program="M"> + <name>Computer Science in Media</name> + </studyCourse> + </studyCourses> + <students> + <student id="33"> + <fullName>Jim Bone</fullName> + <email>bone@foo.org</email> + </student> + <student id="40" studyCourse="2"> + <fullName>Tracy Adams</fullName> + <email>adams@security.com</email> + </student> + </students> +</university> \ No newline at end of file diff --git a/Sda1/Etest/RdbmsStudents2xml/rdbmsStudents2xml_solution/Schema/university.xsd b/Sda1/Etest/RdbmsStudents2xml/rdbmsStudents2xml_solution/Schema/university.xsd new file mode 100644 index 0000000000000000000000000000000000000000..519f8962184e839d5da5f7e07a8a5a3e3bdf7b3f --- /dev/null +++ b/Sda1/Etest/RdbmsStudents2xml/rdbmsStudents2xml_solution/Schema/university.xsd @@ -0,0 +1,81 @@ +<?xml version="1.0" encoding="UTF-8"?> +<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> + + <xs:element name="studyCourse"> + <xs:complexType> + <xs:sequence> + <xs:element name="name" type="xs:string"/> + <xs:element name="shortName" minOccurs="0"> + <xs:simpleType> + <xs:restriction base="xs:string"> + <xs:maxLength value="3"/> + </xs:restriction> + </xs:simpleType> + </xs:element> + </xs:sequence> + <xs:attribute name="id" type="xs:int" use="required"/> + <xs:attribute name="program"> + <xs:simpleType> + <xs:restriction base="xs:string"> + <xs:enumeration value="B"/> + <xs:enumeration value="M"/> + </xs:restriction> + </xs:simpleType> + </xs:attribute> + </xs:complexType> + </xs:element> + + <xs:element name="studyCourses"> + <xs:complexType> + <xs:sequence> + <xs:element ref="studyCourse" minOccurs="0" maxOccurs="unbounded"/> + </xs:sequence> + </xs:complexType> + </xs:element> + + <xs:element name="student"> + <xs:complexType> + <xs:sequence> + <xs:element name="fullName" type="xs:string"/> + <xs:element name="email" type="xs:string"/> + </xs:sequence> + <xs:attribute name="id" type="xs:int" use="required"/> + <xs:attribute name="studyCourse" type="xs:int" use="optional"/> + </xs:complexType> + </xs:element> + + <xs:element name="students"> + <xs:complexType> + <xs:sequence> + <xs:element ref="student" minOccurs="0" maxOccurs="unbounded"/> + </xs:sequence> + </xs:complexType> + </xs:element> + + <xs:element name="university"> + <xs:complexType> + <xs:sequence> + <xs:element ref="studyCourses"/> + <xs:element ref="students"/> + </xs:sequence> + </xs:complexType> + + <xs:key name="uniqueStudyCourse"> + <xs:selector xpath="studyCourses/studyCourse"/> + <xs:field xpath="@id"/> + </xs:key> + + <xs:keyref refer="uniqueStudyCourse" name="studyCourse2student"> + <xs:selector xpath="students/student"/> + <xs:field xpath="@studyCourse"/> + </xs:keyref> + + <xs:key name="uniqueStudent"> + <xs:selector xpath="students/student"/> + <xs:field xpath="@id"/> + </xs:key> + + </xs:element> + + +</xs:schema>