diff --git a/Klausuren/Sda1/WS2014/Letter_solution/Test/Negative/displayNoBoolean.xml.log b/Klausuren/Sda1/WS2014/Letter_solution/Test/Negative/displayNoBoolean.xml.log deleted file mode 100644 index fe2d548cd015508ada2641406c6d8916c8ea762e..0000000000000000000000000000000000000000 --- a/Klausuren/Sda1/WS2014/Letter_solution/Test/Negative/displayNoBoolean.xml.log +++ /dev/null @@ -1,2 +0,0 @@ -file:///ma/goik/Klausur/Letter_solution/Test/Negative/displayNoBoolean.xml:48:27: parse error org.xml.sax.SAXParseException; systemId: file:///ma/goik/Klausur/Letter_solution/Test/Negative/displayNoBoolean.xml; lineNumber: 48; columnNumber: 27; cvc-pattern-valid: Value 'no' is not facet-valid with respect to pattern 'false|true' for type '#AnonType_displayaddressletter'. -parsing error diff --git a/Klausuren/Sda1/WS2014/Letter_solution/Test/Negative/missingAbout.xml.log b/Klausuren/Sda1/WS2014/Letter_solution/Test/Negative/missingAbout.xml.log deleted file mode 100644 index 2b17d44a77a7f7d29e956d20dc9b5c8f49b3c7cf..0000000000000000000000000000000000000000 --- a/Klausuren/Sda1/WS2014/Letter_solution/Test/Negative/missingAbout.xml.log +++ /dev/null @@ -1,2 +0,0 @@ -file:///ma/goik/Klausur/Letter_solution/Test/Negative/missingAbout.xml:8:11: parse error org.xml.sax.SAXParseException; systemId: file:///ma/goik/Klausur/Letter_solution/Test/Negative/missingAbout.xml; lineNumber: 8; columnNumber: 11; cvc-complex-type.2.4.a: Invalid content was found starting with element 'date'. One of '{about}' is expected. -parsing error diff --git a/Klausuren/Sda1/WS2014/Letter_solution/Test/Negative/missingSignature.xml.log b/Klausuren/Sda1/WS2014/Letter_solution/Test/Negative/missingSignature.xml.log deleted file mode 100644 index ab61dc49d2d8f81649545292f3f0283420964d95..0000000000000000000000000000000000000000 --- a/Klausuren/Sda1/WS2014/Letter_solution/Test/Negative/missingSignature.xml.log +++ /dev/null @@ -1,2 +0,0 @@ -file:///ma/goik/Klausur/Letter_solution/Test/Negative/missingSignature.xml:43:30: parse error org.xml.sax.SAXParseException; systemId: file:///ma/goik/Klausur/Letter_solution/Test/Negative/missingSignature.xml; lineNumber: 43; columnNumber: 30; cvc-complex-type.2.4.a: Invalid content was found starting with element 'address'. One of '{signature}' is expected. -parsing error diff --git a/Klausuren/Sda1/WS2014/Letter_solution/Test/Negative/missingZip.xml.log b/Klausuren/Sda1/WS2014/Letter_solution/Test/Negative/missingZip.xml.log deleted file mode 100644 index f7aafdc2520b9e16509f4b1f2a2bd982c92f400e..0000000000000000000000000000000000000000 --- a/Klausuren/Sda1/WS2014/Letter_solution/Test/Negative/missingZip.xml.log +++ /dev/null @@ -1,2 +0,0 @@ -file:///ma/goik/Klausur/Letter_solution/Test/Negative/missingZip.xml:56:15: parse error org.xml.sax.SAXParseException; systemId: file:///ma/goik/Klausur/Letter_solution/Test/Negative/missingZip.xml; lineNumber: 56; columnNumber: 15; cvc-complex-type.2.4.a: Invalid content was found starting with element 'town'. One of '{zip}' is expected. -parsing error diff --git a/Klausuren/Sda1/WS2014/Letter_solution/Test/Negative/noParagraph.xml.log b/Klausuren/Sda1/WS2014/Letter_solution/Test/Negative/noParagraph.xml.log deleted file mode 100644 index 8613cc22c964e6f44fba0e7cbb258027bcc63dc5..0000000000000000000000000000000000000000 --- a/Klausuren/Sda1/WS2014/Letter_solution/Test/Negative/noParagraph.xml.log +++ /dev/null @@ -1,2 +0,0 @@ -file:///ma/goik/Klausur/Letter_solution/Test/Negative/noParagraph.xml:24:25: parse error org.xml.sax.SAXParseException; systemId: file:///ma/goik/Klausur/Letter_solution/Test/Negative/noParagraph.xml; lineNumber: 24; columnNumber: 25; cvc-complex-type.2.4.a: Invalid content was found starting with element 'complimentaryClose'. One of '{paragraph}' is expected. -parsing error diff --git a/Klausuren/Sda1/WS2014/Letter_solution/Test/Negative/salutation31.xml.log b/Klausuren/Sda1/WS2014/Letter_solution/Test/Negative/salutation31.xml.log deleted file mode 100644 index bf66f9848603593985cba78689c21f23b09f6410..0000000000000000000000000000000000000000 --- a/Klausuren/Sda1/WS2014/Letter_solution/Test/Negative/salutation31.xml.log +++ /dev/null @@ -1,2 +0,0 @@ -file:///ma/goik/Klausur/Letter_solution/Test/Negative/salutation31.xml:19:61: parse error org.xml.sax.SAXParseException; systemId: file:///ma/goik/Klausur/Letter_solution/Test/Negative/salutation31.xml; lineNumber: 19; columnNumber: 61; cvc-maxLength-valid: Value '012345678901234567890123456789X' with length = '31' is not facet-valid with respect to maxLength '30' for type '#AnonType_salutationletter'. -parsing error diff --git a/Klausuren/Sda1/WS2014/Letter_solution/Test/Negative/wrongDate.xml.log b/Klausuren/Sda1/WS2014/Letter_solution/Test/Negative/wrongDate.xml.log deleted file mode 100644 index d9d7ef50a0db47189ec2ad8a1fb2d38fe015f9eb..0000000000000000000000000000000000000000 --- a/Klausuren/Sda1/WS2014/Letter_solution/Test/Negative/wrongDate.xml.log +++ /dev/null @@ -1,2 +0,0 @@ -file:///ma/goik/Klausur/Letter_solution/Test/Negative/wrongDate.xml:14:28: parse error org.xml.sax.SAXParseException; systemId: file:///ma/goik/Klausur/Letter_solution/Test/Negative/wrongDate.xml; lineNumber: 14; columnNumber: 28; cvc-datatype-valid.1.2.1: '20.04.2015' is not a valid value for 'date'. -parsing error diff --git a/Klausuren/Sda1/WS2014/Letter_solution/Test/report.log b/Klausuren/Sda1/WS2014/Letter_solution/Test/report.log deleted file mode 100644 index 5cf8fb98804a834854926213ac7c4abecd182294..0000000000000000000000000000000000000000 --- a/Klausuren/Sda1/WS2014/Letter_solution/Test/report.log +++ /dev/null @@ -1,36 +0,0 @@ -Testing invalid file Negative/displayNoBoolean.xml -file:///ma/goik/Klausur/Letter_solution/Test/Negative/displayNoBoolean.xml:48:27: parse error org.xml.sax.SAXParseException; systemId: file:///ma/goik/Klausur/Letter_solution/Test/Negative/displayNoBoolean.xml; lineNumber: 48; columnNumber: 27; cvc-datatype-valid.1.2.1: 'no' is not a valid value for 'boolean'. -parsing error -File expectedly flagged as invalid, adding 1 point -____________________________________________________ -Testing invalid file Negative/missingSignature.xml -file:///ma/goik/Klausur/Letter_solution/Test/Negative/missingSignature.xml:43:30: parse error org.xml.sax.SAXParseException; systemId: file:///ma/goik/Klausur/Letter_solution/Test/Negative/missingSignature.xml; lineNumber: 43; columnNumber: 30; cvc-complex-type.2.4.a: Invalid content was found starting with element 'address'. One of '{signature}' is expected. -parsing error -File expectedly flagged as invalid, adding 1 point -____________________________________________________ -Testing invalid file Negative/noParagraph.xml -file:///ma/goik/Klausur/Letter_solution/Test/Negative/noParagraph.xml:24:25: parse error org.xml.sax.SAXParseException; systemId: file:///ma/goik/Klausur/Letter_solution/Test/Negative/noParagraph.xml; lineNumber: 24; columnNumber: 25; cvc-complex-type.2.4.a: Invalid content was found starting with element 'complimentaryClose'. One of '{paragraph}' is expected. -parsing error -File expectedly flagged as invalid, adding 1 point -____________________________________________________ -Testing invalid file Negative/wrongDate.xml -file:///ma/goik/Klausur/Letter_solution/Test/Negative/wrongDate.xml:14:28: parse error org.xml.sax.SAXParseException; systemId: file:///ma/goik/Klausur/Letter_solution/Test/Negative/wrongDate.xml; lineNumber: 14; columnNumber: 28; cvc-datatype-valid.1.2.1: '20.04.2015' is not a valid value for 'date'. -parsing error -File expectedly flagged as invalid, adding 1 point -____________________________________________________ -Testing invalid file Negative/missingAbout.xml -file:///ma/goik/Klausur/Letter_solution/Test/Negative/missingAbout.xml:8:11: parse error org.xml.sax.SAXParseException; systemId: file:///ma/goik/Klausur/Letter_solution/Test/Negative/missingAbout.xml; lineNumber: 8; columnNumber: 11; cvc-complex-type.2.4.a: Invalid content was found starting with element 'date'. One of '{about}' is expected. -parsing error -File expectedly flagged as invalid, adding 1 point -____________________________________________________ -Testing invalid file Negative/missingZip.xml -file:///ma/goik/Klausur/Letter_solution/Test/Negative/missingZip.xml:56:15: parse error org.xml.sax.SAXParseException; systemId: file:///ma/goik/Klausur/Letter_solution/Test/Negative/missingZip.xml; lineNumber: 56; columnNumber: 15; cvc-complex-type.2.4.a: Invalid content was found starting with element 'town'. One of '{zip}' is expected. -parsing error -File expectedly flagged as invalid, adding 1 point -____________________________________________________ -Testing invalid file Negative/salutation31.xml -file:///ma/goik/Klausur/Letter_solution/Test/Negative/salutation31.xml:19:61: parse error org.xml.sax.SAXParseException; systemId: file:///ma/goik/Klausur/Letter_solution/Test/Negative/salutation31.xml; lineNumber: 19; columnNumber: 61; cvc-maxLength-valid: Value '012345678901234567890123456789X' with length = '31' is not facet-valid with respect to maxLength '30' for type 'shortString'. -parsing error -File expectedly flagged as invalid, adding 1 point -____________________________________________________ -Summary: 7 out of 10 points have been reached diff --git a/Klausuren/Sda1/WS2014/Letter_solution/letter.xsd.log b/Klausuren/Sda1/WS2014/Letter_solution/letter.xsd.log deleted file mode 100644 index c0b8e7e90da91f90ec3cbee381d40164d1cac5b8..0000000000000000000000000000000000000000 --- a/Klausuren/Sda1/WS2014/Letter_solution/letter.xsd.log +++ /dev/null @@ -1,34 +0,0 @@ -Testing invalid file Negative/missingZip.xml -Expected error message: Invalid content was found starting with element town. One of {zip} is expected., adding 1 point -____________________________________________________ -Testing invalid file Negative/salutation31.xml -Expected error message: Value 012345678901234567890123456789X with length = 31 is not facet-valid with respect to maxLength 30 for type shortString., adding 1 point -____________________________________________________ -Testing invalid file Negative/missingSignature.xml -Expected error message: Invalid content was found starting with element address. One of {signature} is expected., adding 1 point -____________________________________________________ -Testing invalid file Negative/displayNoBoolean.xml -Expected error message: no is not a valid value for boolean., adding 1 point -____________________________________________________ -Testing invalid file Negative/wrongDate.xml -Expected error message: 20.04.2015 is not a valid value for date., adding 1 point -____________________________________________________ -Testing invalid file Negative/noParagraph.xml -Expected error message: Invalid content was found starting with element complimentaryClose. One of {paragraph} is expected., adding 1 point -____________________________________________________ -Testing invalid file Negative/missingAbout.xml -Expected error message: Invalid content was found starting with element date. One of {about} is expected., adding 1 point -____________________________________________________ -Testing valid file Positive/missingCompany.xml -Parsing was successfull -Valid File flagged expectedly, adding 1 point -____________________________________________________ -Testing valid file Positive/displayTrue.xml -Parsing was successfull -Valid File flagged expectedly, adding 1 point -____________________________________________________ -Testing valid file Positive/lettersample.xml -Parsing was successfull -Valid File flagged expectedly, adding 1 point -____________________________________________________ -Summary: 10 out of 10 points have been reached diff --git a/Klausuren/Sda1/WS2014/Rewriteprepared/src/main/java/de/hdm_stuttgart/mi/sda1/reewriteprepared/Driver.java b/Klausuren/Sda1/WS2014/Rewriteprepared/src/main/java/de/hdm_stuttgart/mi/sda1/reewriteprepared/Driver.java new file mode 100644 index 0000000000000000000000000000000000000000..5f1cc9eae654d4a721e71226959a164485c93b37 --- /dev/null +++ b/Klausuren/Sda1/WS2014/Rewriteprepared/src/main/java/de/hdm_stuttgart/mi/sda1/reewriteprepared/Driver.java @@ -0,0 +1,33 @@ +package de.hdm_stuttgart.mi.sda1.reewriteprepared; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +import de.hdm_stuttgart.mi.sda1.reewriteprepared.rdbms.CustomerInsert; + + +/** + * Inserting customer into database demoSELECT count(*) FROM Customers WHERE uid = 'SELECT count(*) FROM Customers WHERE uid = ' + * + */ +public class Driver { + + /** + * @param args Unused + * @throws SQLException Possible database handling quirks + */ + public static void main( String[] args ) throws SQLException { + + final Connection connection = DriverManager.getConnection ( + "jdbc:mysql://localhost:3306/hdm", "hdmuser", "XYZ"); + + final CustomerInsert userInsert = new CustomerInsert(connection); + + userInsert.conditionallyInserCustomer("fred", "Fred Astaire"); + + } +} + + + diff --git a/Klausuren/Sda1/WS2014/Rewriteprepared/src/main/java/de/hdm_stuttgart/mi/sda1/reewriteprepared/rdbms/CustomerInsert.java b/Klausuren/Sda1/WS2014/Rewriteprepared/src/main/java/de/hdm_stuttgart/mi/sda1/reewriteprepared/rdbms/CustomerInsert.java new file mode 100644 index 0000000000000000000000000000000000000000..fbdbc573dcd7a2b6ca01b5957fcf5fabd26d3bc0 --- /dev/null +++ b/Klausuren/Sda1/WS2014/Rewriteprepared/src/main/java/de/hdm_stuttgart/mi/sda1/reewriteprepared/rdbms/CustomerInsert.java @@ -0,0 +1,80 @@ +package de.hdm_stuttgart.mi.sda1.reewriteprepared.rdbms; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +/** + * Helper class implementing database access. + * + */ +public class CustomerInsert { + + private static Logger log = LogManager.getLogger(CustomerInsert.class); + + final Connection connection; + final PreparedStatement searchExistingCustomer, insertCustomer; + + @SuppressWarnings("javadoc") + public CustomerInsert(final Connection connection) throws SQLException { + this.connection = connection; + connection.setAutoCommit(true); + searchExistingCustomer = connection.prepareStatement("SELECT uidNumber FROM Customers WHERE uid = ?"); + insertCustomer = connection.prepareStatement("INSERT INTO Customers (uid, cname) VALUES (?, ?)"); + } + + /** + * Trying to add a new user represented by the user's uid (e.g. "fred") and common name (e.g. "Fred Astaire") + * to the current database. An insertion attempt will only happen if no database entry of the corresponding + * uid value yet exists. + * + * @param uid The new user's uid value like "fred" + * @param cname The new user's common name like "Fred Astaire" + * @return true if new user has been inserted, false if uid is already present or arguments are either null or inappropriate. + * @throws SQLException + * + * Precondition: The values of uid and cname must not be null and within the given database schema boundaries + * i.e. concerning string length. + * + */ + public boolean conditionallyInserCustomer (final String uid, final String cname) throws SQLException { + + final Statement statement = connection.createStatement(); + + final String searchexistingUserQuery = + "SELECT uidNumber FROM Customers WHERE uid = '" + uid + "'"; + + try { + final ResultSet existingUser = statement.executeQuery(searchexistingUserQuery); + if (existingUser.next()) { + log.warn("uid = '" + uid + "' is already in database"); + return false; // Insertion error: An entry with this uid already exists + } + } catch (SQLException e) { + log.error("Unable search for user '" + uid + "': "+ e.getLocalizedMessage()); + throw e; + } + + final String insertCustomerStatement = + "INSERT INTO Customers (uid, cname) VALUES ('" + uid + "', '" + cname + "')"; + + try { + final int numInsertedDatasets = statement.executeUpdate(insertCustomerStatement); + if (1 == numInsertedDatasets) { + log.info("Successfully inserted uid = " + uid); + return true; + } else { + log.error("We should not be here: No exeption for failed customer insert uid = " + uid); + return false; + } + } catch (SQLException e) { + log.error("Unable to insert new customer: '" + e.getLocalizedMessage()); + throw e; + } + } +} diff --git a/Klausuren/Sda1/WS2014/Rewriteprepared/src/test/java/de/hdm_stuttgart/mi/sda1/reewriteprepared/CustomerInsertTest.java b/Klausuren/Sda1/WS2014/Rewriteprepared/src/test/java/de/hdm_stuttgart/mi/sda1/reewriteprepared/CustomerInsertTest.java new file mode 100644 index 0000000000000000000000000000000000000000..9ffab428b240e372297bb651d8322bb1e5e657a3 --- /dev/null +++ b/Klausuren/Sda1/WS2014/Rewriteprepared/src/test/java/de/hdm_stuttgart/mi/sda1/reewriteprepared/CustomerInsertTest.java @@ -0,0 +1,85 @@ +package de.hdm_stuttgart.mi.sda1.reewriteprepared; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import de.hdm_stuttgart.mi.sda1.reewriteprepared.rdbms.CustomerInsert; + +/** + * Creating a database connection, an initial schema and one record in {{@link #initDatabase()}. Subsequent tests + * try to add more records and perform related checks. + */ + +@SuppressWarnings({ "javadoc", "resource" }) +public class CustomerInsertTest { + + Connection connection = null; + CustomerInsert customerInsert = null; + + // Create schema from scratch and insert a customer record "jim" prior to execution of test methods + @Before + public void initDatabase() throws SQLException { + + connection = DriverManager.getConnection ( + "jdbc:mysql://localhost:3306/hdm", "hdmuser", "XYZ"); + + final Statement statement = connection.createStatement(); + + statement.executeUpdate("DROP TABLE IF EXISTS Customers"); + + statement.executeUpdate( + "CREATE TABLE Customers (\n" + + "uidNumber INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,\n" + + "uid CHAR(20) NOT NULL UNIQUE,\n" + + "cname VARCHAR(255) NOT NULL\n" + + ");"); + + statement.executeUpdate("INSERT INTO Customers (uid, cname) VALUES ('jim', 'Jim Bone');"); + + statement.close(); + customerInsert = new CustomerInsert(connection); + } + + /** + * @throws SQLException Failure should not occur during test phase + */ + @Test + public void testInsertDuplicate() throws SQLException { + // "jim" was inserted by initDatabase already, so the subsequent call is bound to fail + Assert.assertFalse(customerInsert.conditionallyInserCustomer("jim", "Jim Alternate")); + } + + /** + * @throws SQLException Failure should not occur during test phase + */ + @Test + public void testInsertNew() throws SQLException { + + // Insert second customer on top "jim" from initialization phase + // + Assert.assertTrue(customerInsert.conditionallyInserCustomer("eve", "Eve Gardener")); + + // Search for all customers We expect two customers "eve" followed by "jim" + // to be present in our database. + final ResultSet customers = connection.createStatement().executeQuery( + "SELECT uid FROM Customers ORDER BY uid"); + + // Check for "eve" + Assert.assertTrue("No customer record found: Should be two", customers.next()); + Assert.assertEquals("eve", customers.getString("uid")); + + // Check for "jim" + Assert.assertTrue("Just one customer record found: Should be two", customers.next()); + Assert.assertEquals("jim", customers.getString("uid")); + + // No further record should be present + Assert.assertFalse("More than two customer records found", customers.next()); + } +} diff --git a/Klausuren/Sda1/WS2014/reewriteprepared_solution/.gitignore b/Klausuren/Sda1/WS2014/Rewriteprepared_solution/.gitignore similarity index 100% rename from Klausuren/Sda1/WS2014/reewriteprepared_solution/.gitignore rename to Klausuren/Sda1/WS2014/Rewriteprepared_solution/.gitignore diff --git a/Klausuren/Sda1/WS2014/reewriteprepared_solution/Sql/schema.sql b/Klausuren/Sda1/WS2014/Rewriteprepared_solution/Sql/schema.sql similarity index 100% rename from Klausuren/Sda1/WS2014/reewriteprepared_solution/Sql/schema.sql rename to Klausuren/Sda1/WS2014/Rewriteprepared_solution/Sql/schema.sql diff --git a/Klausuren/Sda1/WS2014/reewriteprepared_solution/description.xhtml b/Klausuren/Sda1/WS2014/Rewriteprepared_solution/description.xhtml similarity index 100% rename from Klausuren/Sda1/WS2014/reewriteprepared_solution/description.xhtml rename to Klausuren/Sda1/WS2014/Rewriteprepared_solution/description.xhtml diff --git a/Klausuren/Sda1/WS2014/reewriteprepared_solution/pom.xml b/Klausuren/Sda1/WS2014/Rewriteprepared_solution/pom.xml similarity index 100% rename from Klausuren/Sda1/WS2014/reewriteprepared_solution/pom.xml rename to Klausuren/Sda1/WS2014/Rewriteprepared_solution/pom.xml diff --git a/Klausuren/Sda1/WS2014/Rewriteprepared_solution/src/main/java/de/hdm_stuttgart/mi/sda1/reewriteprepared/Driver.java b/Klausuren/Sda1/WS2014/Rewriteprepared_solution/src/main/java/de/hdm_stuttgart/mi/sda1/reewriteprepared/Driver.java new file mode 100644 index 0000000000000000000000000000000000000000..ea6f31fc25f8b4beb371f11e4b9526cf2d8cebb2 --- /dev/null +++ b/Klausuren/Sda1/WS2014/Rewriteprepared_solution/src/main/java/de/hdm_stuttgart/mi/sda1/reewriteprepared/Driver.java @@ -0,0 +1,33 @@ +package de.hdm_stuttgart.mi.sda1.reewriteprepared; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +import de.hdm_stuttgart.mi.sda1.reewriteprepared.rdbms.CustomerInsert; + + +/** + * Inserting customer into database demoSELECT count(*) FROM Customers WHERE uid = 'SELECT count(*) FROM Customers WHERE uid = ' + * + */ +public class Driver { + + /** + * @param args Unused + * @throws SQLException Possible database handling quirks + */ + public static void main( String[] args ) throws SQLException { + + final Connection connection = DriverManager.getConnection ( + "jdbc:mysql://localhost:3306/hdm", "hdmuser", "XYZ"); + + final CustomerInsert userInsert = new CustomerInsert(connection); + + userInsert.conditionallyInserCustomer ("fred", "Fred Astaire"); + + } +} + + + diff --git a/Klausuren/Sda1/WS2014/Rewriteprepared_solution/src/main/java/de/hdm_stuttgart/mi/sda1/reewriteprepared/rdbms/CustomerInsert.java b/Klausuren/Sda1/WS2014/Rewriteprepared_solution/src/main/java/de/hdm_stuttgart/mi/sda1/reewriteprepared/rdbms/CustomerInsert.java new file mode 100644 index 0000000000000000000000000000000000000000..0ca3b9621833fb99016fa63ba6d30a6b4f462655 --- /dev/null +++ b/Klausuren/Sda1/WS2014/Rewriteprepared_solution/src/main/java/de/hdm_stuttgart/mi/sda1/reewriteprepared/rdbms/CustomerInsert.java @@ -0,0 +1,76 @@ +package de.hdm_stuttgart.mi.sda1.reewriteprepared.rdbms; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +/** + * Helper class implementing database access. + * + */ +public class CustomerInsert { + + private static Logger log = LogManager.getLogger(CustomerInsert.class); + + final Connection connection; + final PreparedStatement searchExistingCustomer, insertCustomer; + + @SuppressWarnings("javadoc") + public CustomerInsert(final Connection connection) throws SQLException { + this.connection = connection; + connection.setAutoCommit(false); + searchExistingCustomer = connection.prepareStatement("SELECT uidNumber FROM Customers WHERE uid = ?"); + insertCustomer = connection.prepareStatement("INSERT INTO Customers (uid, cname) VALUES (?, ?)"); + } + + /** + * Trying to add a new user represented by the user's uid (e.g. "fred") and common name (e.g. "Fred Astaire") + * to the current database. An insertion attempt will only happen if no database entry of the corresponding + * uid value yet exists. + * + * @param uid The new user's uid value like "fred" + * @param cname The new user's common name like "Fred Astaire" + * @return true if new user has been inserted, false if uid is already present or arguments are either null or inappropriate. + * @throws SQLException + * + * Precondition: The values of uid and cname must not be null and within the given database schema boundaries + * i.e. concerning string length. + * + */ + public boolean conditionallyInserCustomer (final String uid, final String cname) throws SQLException { + + try { + searchExistingCustomer.setString(1, uid); + final ResultSet existingUser = searchExistingCustomer.executeQuery(); + if (existingUser.next()) { + log.warn("uid = '" + uid + "' is already in database"); + connection.rollback(); + return false; // Insertion error: An entry with this uid already exists + } + } catch (SQLException e) { + log.error("Unable search for customer '" + uid + "': "+ e.getLocalizedMessage()); + throw e; + } + + try { + insertCustomer.setString(1, uid); + insertCustomer.setString(2, cname); + final int numInsertedDatasets = insertCustomer.executeUpdate(); + connection.commit(); + if (1 == numInsertedDatasets) { + log.info("Successfully inserted uid = " + uid); + return true; + } else { + log.error("We should not be here: No exeption for failed customer insert uid = " + uid); + return false; + } + } catch (SQLException e) { + log.error("Unable to insert new customer: '" + e.getLocalizedMessage()); + throw e; + } + } +} diff --git a/Klausuren/Sda1/WS2014/reewriteprepared_solution/src/main/resources/log4j2.xml b/Klausuren/Sda1/WS2014/Rewriteprepared_solution/src/main/resources/log4j2.xml similarity index 100% rename from Klausuren/Sda1/WS2014/reewriteprepared_solution/src/main/resources/log4j2.xml rename to Klausuren/Sda1/WS2014/Rewriteprepared_solution/src/main/resources/log4j2.xml diff --git a/Klausuren/Sda1/WS2014/Rewriteprepared_solution/src/test/java/de/hdm_stuttgart/mi/sda1/reewriteprepared/CustomerInsertTest.java b/Klausuren/Sda1/WS2014/Rewriteprepared_solution/src/test/java/de/hdm_stuttgart/mi/sda1/reewriteprepared/CustomerInsertTest.java new file mode 100644 index 0000000000000000000000000000000000000000..2cfeed60226f4a59f43756a460530ebc56bfd152 --- /dev/null +++ b/Klausuren/Sda1/WS2014/Rewriteprepared_solution/src/test/java/de/hdm_stuttgart/mi/sda1/reewriteprepared/CustomerInsertTest.java @@ -0,0 +1,87 @@ +package de.hdm_stuttgart.mi.sda1.reewriteprepared; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import de.hdm_stuttgart.mi.sda1.reewriteprepared.rdbms.CustomerInsert; + +/** + * Creating a database connection, an initial schema and one record in {{@link #initDatabase()}. Subsequent tests + * try to add more records and perform related checks. + */ + +@SuppressWarnings({ "javadoc", "resource" }) +public class CustomerInsertTest { + + Connection connection = null; + CustomerInsert customerInsert = null; + + // Create schema from scratch and insert a customer record "jim" prior to execution of test methods + @Before + public void initDatabase() throws SQLException { + + connection = DriverManager.getConnection ( + "jdbc:mysql://localhost:3306/hdm", "hdmuser", "XYZ"); + + customerInsert = new CustomerInsert(connection); // Will set auto commit to true + + + final Statement statement = connection.createStatement(); + + statement.executeUpdate("DROP TABLE IF EXISTS Customers"); + + statement.executeUpdate( + "CREATE TABLE Customers (\n" + + "uidNumber INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,\n" + + "uid CHAR(20) NOT NULL UNIQUE,\n" + + "cname VARCHAR(255) NOT NULL\n" + + ");"); + + statement.executeUpdate("INSERT INTO Customers (uid, cname) VALUES ('jim', 'Jim Bone');"); + connection.commit(); + statement.close(); + } + + /** + * @throws SQLException Failure should not occur during test phase + */ + @Test + public void testInsertDuplicate() throws SQLException { + // "jim" was inserted by initDatabase already, so the subsequent call is bound to fail + Assert.assertFalse(customerInsert.conditionallyInserCustomer("jim", "Jim Alternate")); + } + + /** + * @throws SQLException Failure should not occur during test phase + */ + @Test + public void testInsertNew() throws SQLException { + + // Insert second customer on top "jim" from initialization phase + // + Assert.assertTrue(customerInsert.conditionallyInserCustomer("eve", "Eve Gardener")); + + // Search for all customers We expect two customers "eve" followed by "jim" + // to be present in our database. + final ResultSet customers = connection.createStatement().executeQuery( + "SELECT uid FROM Customers ORDER BY uid"); + + // Check for "eve" + Assert.assertTrue("No customer record found: Should be two", customers.next()); + Assert.assertEquals("eve", customers.getString("uid")); + + // Check for "jim" + Assert.assertTrue("Just one customer record found: Should be two", customers.next()); + Assert.assertEquals("jim", customers.getString("uid")); + + // No further record should be present + Assert.assertFalse("More than two customer records found", customers.next()); + } +} diff --git a/Klausuren/Sda2/SoSe2013/Catalog/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/CreateSampleData.java b/Klausuren/Sda2/SoSe2013/Catalog/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/CreateSampleData.java new file mode 100644 index 0000000000000000000000000000000000000000..138ca43d3830883fac0fca2fa0809ae39668f2a6 --- /dev/null +++ b/Klausuren/Sda2/SoSe2013/Catalog/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/CreateSampleData.java @@ -0,0 +1,48 @@ +package de.hdm_stuttgart.mi.sda2.catalog; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityTransaction; +import javax.persistence.Persistence; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import de.hdm_stuttgart.mi.sda2.catalog.domain.FoodProduct; +import de.hdm_stuttgart.mi.sda2.catalog.domain.ProductProvider; + +/** + * A simple http://logging.apache.org/log4j/2.x demo, see file log4j2.xml for + * configuration options. + * + */ +public class CreateSampleData { + private static Logger log = LogManager.getLogger(CreateSampleData.class); + + /** + * @param args Unused + */ + public static void main(String[] args) { + + final EntityManagerFactory factory = Persistence + .createEntityManagerFactory("persistenceUnit"); + final EntityManager manager = factory.createEntityManager(); + + final EntityTransaction tx = manager.getTransaction(); + + tx.begin(); + { +// final FoodProduct +// mountainCheese = new FoodProduct(32, "Mountain cheese", 43), +// goatCheese = new FoodProduct(36, "Goat cheese", 12); +// +// final ProductProvider smith = new ProductProvider("Smith & Sons"); +// smith.addProduct(mountainCheese); +// log.info("Start persisting some objects"); +// +// manager.persist(mountainCheese); +// manager.persist(goatCheese); + } + tx.commit(); + } +} diff --git a/Klausuren/Sda2/SoSe2013/Catalog/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/domain/FoodProduct.java b/Klausuren/Sda2/SoSe2013/Catalog/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/domain/FoodProduct.java new file mode 100644 index 0000000000000000000000000000000000000000..64b18feca9a328276f59fa9db039cdb4fd9523b4 --- /dev/null +++ b/Klausuren/Sda2/SoSe2013/Catalog/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/domain/FoodProduct.java @@ -0,0 +1,19 @@ +package de.hdm_stuttgart.mi.sda2.catalog.domain; + +import javax.persistence.Entity; + + +/** + * Product of type food having a limited shelf life + * + */ +@Entity @SuppressWarnings("javadoc") +public class FoodProduct extends Product { + + int daysOfShelfLife; + + protected FoodProduct() { + super(); + } + +} diff --git a/Klausuren/Sda2/SoSe2013/Catalog/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/domain/NonFoodProduct.java b/Klausuren/Sda2/SoSe2013/Catalog/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/domain/NonFoodProduct.java new file mode 100644 index 0000000000000000000000000000000000000000..0d6438299f6ee93fbb4c22703c73a2c128c07344 --- /dev/null +++ b/Klausuren/Sda2/SoSe2013/Catalog/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/domain/NonFoodProduct.java @@ -0,0 +1,10 @@ +package de.hdm_stuttgart.mi.sda2.catalog.domain; + +import javax.persistence.Entity; + +@SuppressWarnings("javadoc") +@Entity +public class NonFoodProduct extends Product { + + +} diff --git a/Klausuren/Sda2/SoSe2013/Catalog/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/domain/Product.java b/Klausuren/Sda2/SoSe2013/Catalog/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/domain/Product.java new file mode 100644 index 0000000000000000000000000000000000000000..1bacbdf8ad4fbd057249401c488291a3447f310c --- /dev/null +++ b/Klausuren/Sda2/SoSe2013/Catalog/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/domain/Product.java @@ -0,0 +1,24 @@ +package de.hdm_stuttgart.mi.sda2.catalog.domain; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; + + +/** + * Products of a grocery company + * + */ +@Entity +@Inheritance(strategy=InheritanceType.JOINED) +@SuppressWarnings("javadoc") +public abstract class Product { + + @Id + @GeneratedValue(strategy=GenerationType.IDENTITY) + Long id; + +} diff --git a/Klausuren/Sda2/SoSe2013/Catalog/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/domain/ProductProvider.java b/Klausuren/Sda2/SoSe2013/Catalog/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/domain/ProductProvider.java new file mode 100644 index 0000000000000000000000000000000000000000..6088e4e83a4ee01816cc24e1453ba6162ccfa9db --- /dev/null +++ b/Klausuren/Sda2/SoSe2013/Catalog/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/domain/ProductProvider.java @@ -0,0 +1,18 @@ +package de.hdm_stuttgart.mi.sda2.catalog.domain; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +/** + * Supplier for products + * + */ +@Entity @SuppressWarnings("javadoc") +public class ProductProvider { + + @Id + @GeneratedValue(strategy=GenerationType.IDENTITY) + Long id; +} diff --git a/Klausuren/Sda2/SoSe2013/Catalog/src/test/java/de/hdm_stuttgart/mi/sda2/jpa/university/AppTest.java b/Klausuren/Sda2/SoSe2013/Catalog/src/test/java/de/hdm_stuttgart/mi/sda2/jpa/university/AppTest.java new file mode 100644 index 0000000000000000000000000000000000000000..f9dc416cbfde3150f56b8eba5c28178eb5f6b10c --- /dev/null +++ b/Klausuren/Sda2/SoSe2013/Catalog/src/test/java/de/hdm_stuttgart/mi/sda2/jpa/university/AppTest.java @@ -0,0 +1,18 @@ +package de.hdm_stuttgart.mi.sda2.jpa.university; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Unit test for simple App. + */ +@SuppressWarnings("static-method") +public class AppTest { + /** + * Dummy test method + */ + @Test + public void testApp() { + Assert.assertTrue( true ); + } +} diff --git a/Klausuren/Sda2/SoSe2013/Catalog_solution/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/CreateSampleData.java b/Klausuren/Sda2/SoSe2013/Catalog_solution/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/CreateSampleData.java new file mode 100644 index 0000000000000000000000000000000000000000..d578706025a6c1c39ddfa865ffe13e3e2eed9129 --- /dev/null +++ b/Klausuren/Sda2/SoSe2013/Catalog_solution/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/CreateSampleData.java @@ -0,0 +1,48 @@ +package de.hdm_stuttgart.mi.sda2.catalog; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityTransaction; +import javax.persistence.Persistence; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import de.hdm_stuttgart.mi.sda2.catalog.domain.FoodProduct; +import de.hdm_stuttgart.mi.sda2.catalog.domain.ProductProvider; + +/** + * A simple http://logging.apache.org/log4j/2.x demo, see file log4j2.xml for + * configuration options. + * + */ +public class CreateSampleData { + private static Logger log = LogManager.getLogger(CreateSampleData.class); + + /** + * @param args Unused + */ + public static void main(String[] args) { + + final EntityManagerFactory factory = Persistence + .createEntityManagerFactory("persistenceUnit"); + final EntityManager manager = factory.createEntityManager(); + + final EntityTransaction tx = manager.getTransaction(); + + tx.begin(); + { + final FoodProduct + mountainCheese = new FoodProduct(32, "Mountain cheese", 43), + goatCheese = new FoodProduct(36, "Goat cheese", 12); + + final ProductProvider smith = new ProductProvider("Smith & Sons"); + smith.addProduct(mountainCheese); + log.info("Start persisting some objects"); + + manager.persist(mountainCheese); + manager.persist(goatCheese); + } + tx.commit(); + } +} diff --git a/Klausuren/Sda2/SoSe2013/Catalog_solution/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/LoadXml.java b/Klausuren/Sda2/SoSe2013/Catalog_solution/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/LoadXml.java new file mode 100644 index 0000000000000000000000000000000000000000..101483cb1448fa36d77d4f90ebd3ef1722e20a59 --- /dev/null +++ b/Klausuren/Sda2/SoSe2013/Catalog_solution/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/LoadXml.java @@ -0,0 +1,40 @@ +package de.hdm_stuttgart.mi.sda2.catalog; + +import java.io.File; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Unmarshaller; + +import de.hdm_stuttgart.mi.sda2.catalog.jaxb.Article; +import de.hdm_stuttgart.mi.sda2.catalog.jaxb.Group; +import de.hdm_stuttgart.mi.sda2.catalog.jaxb.ProductGroups; + +/** + * Loading objects from XML file based data + * + */ +public class LoadXml { + + /** + * @param args Unused + * @throws JAXBException + */ + @SuppressWarnings("javadoc") + public static void main(String[] args) throws JAXBException { + + final JAXBContext cdContext = JAXBContext.newInstance("de.hdm_stuttgart.mi.sda2.catalog.jaxb"); + final Unmarshaller u = cdContext.createUnmarshaller(); + final ProductGroups productGroups= (ProductGroups) u.unmarshal(new File("XmlData/samplegroups.xml")); + + System.out.println(productGroups.getGroup().size() + " product group(s):"); + for (final Group g : productGroups.getGroup()) { + System.out.println("Group title:" + g.getTitle() + " containing " + g.getArticle().size() + " article(s)"); + + for (Article a : g.getArticle()) { + System.out.println( "Article no. " + a.getProductNumber()); + } + System.out.println("----------------"); + } + } +} diff --git a/Klausuren/Sda2/SoSe2013/Catalog_solution/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/LoadXmlRdbms.java b/Klausuren/Sda2/SoSe2013/Catalog_solution/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/LoadXmlRdbms.java new file mode 100644 index 0000000000000000000000000000000000000000..831793d75635127ca8544f76f7848fa7963f4ce4 --- /dev/null +++ b/Klausuren/Sda2/SoSe2013/Catalog_solution/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/LoadXmlRdbms.java @@ -0,0 +1,80 @@ +package de.hdm_stuttgart.mi.sda2.catalog; + +import java.io.File; +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityTransaction; +import javax.persistence.Persistence; +import javax.persistence.TypedQuery; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Unmarshaller; + +import de.hdm_stuttgart.mi.sda2.catalog.domain.Product; +import de.hdm_stuttgart.mi.sda2.catalog.domain.ProductProvider; +import de.hdm_stuttgart.mi.sda2.catalog.jaxb.Article; +import de.hdm_stuttgart.mi.sda2.catalog.jaxb.Group; +import de.hdm_stuttgart.mi.sda2.catalog.jaxb.ProductGroups; + +/** + * Loading XML and RDBMS data + * + */ +public class LoadXmlRdbms { + + /** + * @param args Unused + */ + @SuppressWarnings("javadoc") + public static void main(String[] args) throws JAXBException { + + final EntityManagerFactory factory = Persistence + .createEntityManagerFactory("persistenceUnitNoDrop"); + final EntityManager manager = factory.createEntityManager(); + + + + final JAXBContext cdContext = JAXBContext.newInstance("de.hdm_stuttgart.mi.sda2.catalog.jaxb"); + final Unmarshaller u = cdContext.createUnmarshaller(); + final ProductGroups productGroups= (ProductGroups) u.unmarshal(new File("XmlData/samplegroups.xml")); + + System.out.println(productGroups.getGroup().size() + " product group(s):"); + for (final Group g : productGroups.getGroup()) { + System.out.println("Group title:" + g.getTitle() + " containing " + g.getArticle().size() + " article(s)"); + + for (Article a : g.getArticle()) { + System.out.println( "Article no. " + a.getProductNumber()); + addRdbmsData(manager, a.getProductNumber()); + } + System.out.println("----------------"); + } + } + + private static void addRdbmsData(final EntityManager em, final int productNumber) { + final EntityTransaction tx = em.getTransaction(); + tx.begin(); + { + + final TypedQuery<Product> query = em.createQuery( + "SELECT p FROM Product p WHERE p.productNumber = :productNumber", Product.class); + query.setParameter("productNumber", productNumber); + final List<Product> retrievedProducts = query.getResultList(); + if (0 == retrievedProducts.size()) { + System.out.println("No product data available"); + } else { + final Product p = retrievedProducts.get(0); + System.out.print(p.toString()); + ProductProvider pp = p.getPrimaryProvider(); + if (null == pp) { + System.out.println(); + } else { + System.out.println(", primary provider:" + pp.getName()); + } + } + + } + tx.commit(); + } +} diff --git a/Klausuren/Sda2/SoSe2013/Catalog_solution/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/domain/FoodProduct.java b/Klausuren/Sda2/SoSe2013/Catalog_solution/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/domain/FoodProduct.java new file mode 100644 index 0000000000000000000000000000000000000000..4d5e101f0709b4c4f88904e6dd4f88ebfe29615c --- /dev/null +++ b/Klausuren/Sda2/SoSe2013/Catalog_solution/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/domain/FoodProduct.java @@ -0,0 +1,38 @@ +package de.hdm_stuttgart.mi.sda2.catalog.domain; + +import javax.persistence.Entity; + + +/** + * Product of type food having a limited shelf life + * + */ +@Entity @SuppressWarnings("javadoc") +public class FoodProduct extends Product { + + int daysOfShelfLife; + + protected FoodProduct() { + super(); + } + + public FoodProduct(int productNumber, String name, int daysOfShelfLife) { + super(productNumber, name); + setDaysOfShelfLife(daysOfShelfLife); + } + + + public int getDaysOfShelfLife() { + return daysOfShelfLife; + } + + public void setDaysOfShelfLife(int daysOfShelfLife) { + this.daysOfShelfLife = daysOfShelfLife; + } + + @Override + public String toString() { + return "Food product " + super.toString() + ", days shelf life=" + getDaysOfShelfLife(); + } + +} diff --git a/Klausuren/Sda2/SoSe2013/Catalog_solution/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/domain/NonFoodProduct.java b/Klausuren/Sda2/SoSe2013/Catalog_solution/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/domain/NonFoodProduct.java new file mode 100644 index 0000000000000000000000000000000000000000..b79acef3eebaf54920a259c91c1d2f098803fc05 --- /dev/null +++ b/Klausuren/Sda2/SoSe2013/Catalog_solution/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/domain/NonFoodProduct.java @@ -0,0 +1,33 @@ +package de.hdm_stuttgart.mi.sda2.catalog.domain; + +import javax.persistence.Entity; + +@SuppressWarnings("javadoc") +@Entity +public class NonFoodProduct extends Product { + + boolean onlineAvailable; // May be ordered online by customers? + + + protected NonFoodProduct() { + super(); + } + + public NonFoodProduct(int productNumber, String name, boolean onlineAvailable) { + super(productNumber, name); + this.onlineAvailable = onlineAvailable; + } + + public boolean isOnlineAvailable() { + return onlineAvailable; + } + + public void setOnlineAvailable(boolean onlineAvailable) { + this.onlineAvailable = onlineAvailable; + } + + @Override + public String toString() { + return "Non-food product " + super.toString() + ", online available=" + isOnlineAvailable(); + } +} diff --git a/Klausuren/Sda2/SoSe2013/Catalog_solution/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/domain/Product.java b/Klausuren/Sda2/SoSe2013/Catalog_solution/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/domain/Product.java new file mode 100644 index 0000000000000000000000000000000000000000..7e5728fd1755705622c7f757d1a42ff690f02450 --- /dev/null +++ b/Klausuren/Sda2/SoSe2013/Catalog_solution/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/domain/Product.java @@ -0,0 +1,77 @@ +package de.hdm_stuttgart.mi.sda2.catalog.domain; + +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; + + +/** + * Products of a supermarket group + * + */ +@Entity +@Inheritance(strategy=InheritanceType.JOINED) +@SuppressWarnings("javadoc") +public abstract class Product { + + @Id + @GeneratedValue(strategy=GenerationType.IDENTITY) + Long id; + + @Column(unique=true, nullable=false) + int productNumber; + + String name; // e.g. "Tennis racket" + + @ManyToOne(cascade=CascadeType.ALL) + @JoinColumn(nullable=true) + ProductProvider primaryProvider; + + protected Product() {} + + public Product(int productNumber, String name) { + setProductNumber(productNumber); + setName(name); + } + + public int getProductNumber() { + return productNumber; + } + + public void setProductNumber(int productNumber) { + this.productNumber = productNumber; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Long getId() { + return id; + } + + public ProductProvider getPrimaryProvider() { + return primaryProvider; + } + + protected void setPrimaryProvider(ProductProvider primaryProvider) { + this.primaryProvider = primaryProvider; + } + + @Override + public String toString() { + return ", productNo. =" + getProductNumber() + ", id=" + getId(); + } + +} diff --git a/Klausuren/Sda2/SoSe2013/Catalog_solution/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/domain/ProductProvider.java b/Klausuren/Sda2/SoSe2013/Catalog_solution/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/domain/ProductProvider.java new file mode 100644 index 0000000000000000000000000000000000000000..ee2c342183e3745650db8a764990b17547d58e74 --- /dev/null +++ b/Klausuren/Sda2/SoSe2013/Catalog_solution/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/domain/ProductProvider.java @@ -0,0 +1,58 @@ +package de.hdm_stuttgart.mi.sda2.catalog.domain; + +import java.util.HashSet; +import java.util.Set; + +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToMany; + +/** + * Supplier for products + * + */ +@Entity @SuppressWarnings("javadoc") +public class ProductProvider { + + @Id + @GeneratedValue(strategy=GenerationType.IDENTITY) + Long id; + String name; // e.g. "Tennis racket" + + @OneToMany(mappedBy="primaryProvider", cascade=CascadeType.ALL) + Set<Product> products; + + protected ProductProvider() {} + + public ProductProvider(String name) { + products = new HashSet<Product>(); + setName(name); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Long getId() { + return id; + } + + public void addProduct(final Product product) { + if (!products.contains(product)) { + ProductProvider pp = product.getPrimaryProvider(); + if (null != pp) { + pp.products.remove(product); + } + product.setPrimaryProvider(this); + products.add(product); + } + } + +} diff --git a/Klausuren/Sda2/SoSe2013/Catalog_solution/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/jaxb/Article.java b/Klausuren/Sda2/SoSe2013/Catalog_solution/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/jaxb/Article.java new file mode 100644 index 0000000000000000000000000000000000000000..2e1acdd53258395322d93b55a78dfa7b71ef70fb --- /dev/null +++ b/Klausuren/Sda2/SoSe2013/Catalog_solution/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/jaxb/Article.java @@ -0,0 +1,67 @@ +// +// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.8-b130911.1802 +// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> +// Any modifications to this file will be lost upon recompilation of the source schema. +// Generated on: 2015.02.18 at 01:14:13 PM CET +// + + +package de.hdm_stuttgart.mi.sda2.catalog.jaxb; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlType; + + +/** + * <p>Java class for anonymous complex type. + * + * <p>The following schema fragment specifies the expected content contained within this class. + * + * <pre> + * <complexType> + * <complexContent> + * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> + * <attribute name="productNumber" type="{http://www.w3.org/2001/XMLSchema}int" /> + * </restriction> + * </complexContent> + * </complexType> + * </pre> + * + * + */ +@XmlAccessorType(XmlAccessType.FIELD) +@XmlType(name = "") +@XmlRootElement(name = "article") +public class Article { + + @XmlAttribute(name = "productNumber") + protected Integer productNumber; + + /** + * Gets the value of the productNumber property. + * + * @return + * possible object is + * {@link Integer } + * + */ + public Integer getProductNumber() { + return productNumber; + } + + /** + * Sets the value of the productNumber property. + * + * @param value + * allowed object is + * {@link Integer } + * + */ + public void setProductNumber(Integer value) { + this.productNumber = value; + } + +} diff --git a/Klausuren/Sda2/SoSe2013/Catalog_solution/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/jaxb/Group.java b/Klausuren/Sda2/SoSe2013/Catalog_solution/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/jaxb/Group.java new file mode 100644 index 0000000000000000000000000000000000000000..47523a009370ed19f5a1376e118dd26dc24cce21 --- /dev/null +++ b/Klausuren/Sda2/SoSe2013/Catalog_solution/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/jaxb/Group.java @@ -0,0 +1,105 @@ +// +// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.8-b130911.1802 +// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> +// Any modifications to this file will be lost upon recompilation of the source schema. +// Generated on: 2015.02.18 at 01:14:13 PM CET +// + + +package de.hdm_stuttgart.mi.sda2.catalog.jaxb; + +import java.util.ArrayList; +import java.util.List; +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlType; + + +/** + * <p>Java class for anonymous complex type. + * + * <p>The following schema fragment specifies the expected content contained within this class. + * + * <pre> + * <complexType> + * <complexContent> + * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> + * <sequence> + * <element name="title" type="{http://www.w3.org/2001/XMLSchema}string"/> + * <element ref="{}article" maxOccurs="unbounded" minOccurs="0"/> + * </sequence> + * </restriction> + * </complexContent> + * </complexType> + * </pre> + * + * + */ +@XmlAccessorType(XmlAccessType.FIELD) +@XmlType(name = "", propOrder = { + "title", + "article" +}) +@XmlRootElement(name = "group") +public class Group { + + @XmlElement(required = true) + protected String title; + protected List<Article> article; + + /** + * Gets the value of the title property. + * + * @return + * possible object is + * {@link String } + * + */ + public String getTitle() { + return title; + } + + /** + * Sets the value of the title property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setTitle(String value) { + this.title = value; + } + + /** + * Gets the value of the article property. + * + * <p> + * This accessor method returns a reference to the live list, + * not a snapshot. Therefore any modification you make to the + * returned list will be present inside the JAXB object. + * This is why there is not a <CODE>set</CODE> method for the article property. + * + * <p> + * For example, to add a new item, do as follows: + * <pre> + * getArticle().add(newItem); + * </pre> + * + * + * <p> + * Objects of the following type(s) are allowed in the list + * {@link Article } + * + * + */ + public List<Article> getArticle() { + if (article == null) { + article = new ArrayList<Article>(); + } + return this.article; + } + +} diff --git a/Klausuren/Sda2/SoSe2013/Catalog_solution/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/jaxb/ObjectFactory.java b/Klausuren/Sda2/SoSe2013/Catalog_solution/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/jaxb/ObjectFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..8734830be7a342393c1f3e6fc08fb3b71f73d30f --- /dev/null +++ b/Klausuren/Sda2/SoSe2013/Catalog_solution/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/jaxb/ObjectFactory.java @@ -0,0 +1,63 @@ +// +// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.8-b130911.1802 +// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> +// Any modifications to this file will be lost upon recompilation of the source schema. +// Generated on: 2015.02.18 at 01:14:13 PM CET +// + + +package de.hdm_stuttgart.mi.sda2.catalog.jaxb; + +import javax.xml.bind.annotation.XmlRegistry; + + +/** + * This object contains factory methods for each + * Java content interface and Java element interface + * generated in the de.hdm_stuttgart.mi.sda2.catalog.jaxb package. + * <p>An ObjectFactory allows you to programatically + * construct new instances of the Java representation + * for XML content. The Java representation of XML + * content can consist of schema derived interfaces + * and classes representing the binding of schema + * type definitions, element declarations and model + * groups. Factory methods for each of these are + * provided in this class. + * + */ +@XmlRegistry +public class ObjectFactory { + + + /** + * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: de.hdm_stuttgart.mi.sda2.catalog.jaxb + * + */ + public ObjectFactory() { + } + + /** + * Create an instance of {@link ProductGroups } + * + */ + public ProductGroups createProductGroups() { + return new ProductGroups(); + } + + /** + * Create an instance of {@link Group } + * + */ + public Group createGroup() { + return new Group(); + } + + /** + * Create an instance of {@link Article } + * + */ + public Article createArticle() { + return new Article(); + } + +} diff --git a/Klausuren/Sda2/SoSe2013/Catalog_solution/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/jaxb/ProductGroups.java b/Klausuren/Sda2/SoSe2013/Catalog_solution/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/jaxb/ProductGroups.java new file mode 100644 index 0000000000000000000000000000000000000000..5479e1c9d2424c88118ddf845bea275e11f10e54 --- /dev/null +++ b/Klausuren/Sda2/SoSe2013/Catalog_solution/src/main/java/de/hdm_stuttgart/mi/sda2/catalog/jaxb/ProductGroups.java @@ -0,0 +1,76 @@ +// +// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.8-b130911.1802 +// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> +// Any modifications to this file will be lost upon recompilation of the source schema. +// Generated on: 2015.02.18 at 01:14:13 PM CET +// + + +package de.hdm_stuttgart.mi.sda2.catalog.jaxb; + +import java.util.ArrayList; +import java.util.List; +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlType; + + +/** + * <p>Java class for anonymous complex type. + * + * <p>The following schema fragment specifies the expected content contained within this class. + * + * <pre> + * <complexType> + * <complexContent> + * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> + * <sequence> + * <element ref="{}group" maxOccurs="unbounded" minOccurs="0"/> + * </sequence> + * </restriction> + * </complexContent> + * </complexType> + * </pre> + * + * + */ +@XmlAccessorType(XmlAccessType.FIELD) +@XmlType(name = "", propOrder = { + "group" +}) +@XmlRootElement(name = "productGroups") +public class ProductGroups { + + protected List<Group> group; + + /** + * Gets the value of the group property. + * + * <p> + * This accessor method returns a reference to the live list, + * not a snapshot. Therefore any modification you make to the + * returned list will be present inside the JAXB object. + * This is why there is not a <CODE>set</CODE> method for the group property. + * + * <p> + * For example, to add a new item, do as follows: + * <pre> + * getGroup().add(newItem); + * </pre> + * + * + * <p> + * Objects of the following type(s) are allowed in the list + * {@link Group } + * + * + */ + public List<Group> getGroup() { + if (group == null) { + group = new ArrayList<Group>(); + } + return this.group; + } + +} diff --git a/Klausuren/Sda2/SoSe2013/Catalog_solution/src/test/java/de/hdm_stuttgart/mi/sda2/jpa/university/AppTest.java b/Klausuren/Sda2/SoSe2013/Catalog_solution/src/test/java/de/hdm_stuttgart/mi/sda2/jpa/university/AppTest.java new file mode 100644 index 0000000000000000000000000000000000000000..f9dc416cbfde3150f56b8eba5c28178eb5f6b10c --- /dev/null +++ b/Klausuren/Sda2/SoSe2013/Catalog_solution/src/test/java/de/hdm_stuttgart/mi/sda2/jpa/university/AppTest.java @@ -0,0 +1,18 @@ +package de.hdm_stuttgart.mi.sda2.jpa.university; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Unit test for simple App. + */ +@SuppressWarnings("static-method") +public class AppTest { + /** + * Dummy test method + */ + @Test + public void testApp() { + Assert.assertTrue( true ); + } +}