mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-14 12:58:37 +00:00
* #2449 bump maven-checkstyle-plugin from 3.1.0 to 3.2.0 + resolve checkstyle issues * remove FileSelectorJFrame.java to resolve checkstyle issue * remove FileSelectorJFrame.java to resolve checkstyle issue * remove FileSelectorJFrame.java to resolve checkstyle issue * add refactored file with correct filename to resolve checkstyle issue * add the test data * change filenames from JFrame to Jframe for checkstyle * fix code smell from sonar report * add new testcases to improve the test coverage * remove code smell
This commit is contained in:
@@ -25,22 +25,20 @@
|
||||
package com.iluwatar.embedded.value;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/*
|
||||
* Many small objects make sense in an OO system that don’t make sense as
|
||||
/**
|
||||
* <p> Many small objects make sense in an OO system that don’t make sense as
|
||||
* tables in a database. Examples include currency-aware money objects (amount, currency) and date
|
||||
* ranges. Although the default thinking is to save an object as a table, no sane
|
||||
* person would want a table of money values.
|
||||
* person would want a table of money values. </p>
|
||||
*
|
||||
* An Embedded Value maps the values of an object to fields in the record of
|
||||
* <p> An Embedded Value maps the values of an object to fields in the record of
|
||||
* the object’s owner. In this implementation we have an Order object with links to an
|
||||
* ShippingAddress object. In the resulting table the fields in the ShippingAddress
|
||||
* object map to fields in the Order table rather than make new records
|
||||
* themselves.
|
||||
* themselves. </p>
|
||||
*/
|
||||
|
||||
@Slf4j
|
||||
public class App {
|
||||
|
||||
@@ -62,11 +60,9 @@ public class App {
|
||||
final var order3 = new Order("Carrie Soto is Back", "Shiva",
|
||||
new ShippingAddress("Bangalore", "Karnataka", "560004"));
|
||||
|
||||
/**
|
||||
* Create table for orders - Orders(id, name, orderedBy, city, state, pincode).
|
||||
* We can see that table is different from the Order object we have.
|
||||
* We're mapping ShippingAddress into city, state, pincode colummns of the database and not creating a separate table.
|
||||
*/
|
||||
// Create table for orders - Orders(id, name, orderedBy, city, state, pincode).
|
||||
// We can see that table is different from the Order object we have.
|
||||
// We're mapping ShippingAddress into city, state, pincode colummns of the database and not creating a separate table.
|
||||
if (dataSource.createSchema()) {
|
||||
LOGGER.info("TABLE CREATED");
|
||||
LOGGER.info("Table \"Orders\" schema:\n" + dataSource.getSchema());
|
||||
@@ -84,21 +80,17 @@ public class App {
|
||||
dataSource.insertOrder(order2);
|
||||
dataSource.insertOrder(order3);
|
||||
|
||||
/**
|
||||
* Query orders
|
||||
* We'll create ShippingAddress object from city, state, pincode values from the table
|
||||
* and add it to Order object
|
||||
*/
|
||||
|
||||
// Query orders.
|
||||
// We'll create ShippingAddress object from city, state, pincode values from the table and add it to Order object
|
||||
LOGGER.info("Orders Query: {}", dataSource.queryOrders().collect(Collectors.toList()) + "\n");
|
||||
|
||||
//Query order by given id
|
||||
LOGGER.info("Query Order with id=2: {}", dataSource.queryOrder(2));
|
||||
|
||||
/**
|
||||
* Remove order by given id.
|
||||
* Since we'd mapped address in the same table, deleting order will also take
|
||||
* out the shipping address details
|
||||
*/
|
||||
|
||||
//Remove order by given id.
|
||||
//Since we'd mapped address in the same table, deleting order will also take out the shipping address details.
|
||||
LOGGER.info("Remove Order with id=1");
|
||||
dataSource.removeOrder(1);
|
||||
LOGGER.info("\nOrders Query: {}", dataSource.queryOrders().collect(Collectors.toList()) + "\n");
|
||||
|
||||
@@ -31,36 +31,29 @@ import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
/*
|
||||
* Communicates with H2 database with the help of JDBC API
|
||||
*
|
||||
* Inherits the SQL queries and methods from @link AbstractDataSource class
|
||||
*/
|
||||
|
||||
/**
|
||||
* Communicates with H2 database with the help of JDBC API.
|
||||
* Inherits the SQL queries and methods from @link AbstractDataSource class.
|
||||
*/
|
||||
@Slf4j
|
||||
public class DataSource implements DataSourceInterface {
|
||||
private Connection conn;
|
||||
|
||||
/**
|
||||
* Statements are objects which are used to execute queries which will not be
|
||||
* repeated.
|
||||
*/
|
||||
// Statements are objects which are used to execute queries which will not be repeated.
|
||||
private Statement getschema;
|
||||
private Statement deleteschema;
|
||||
private Statement queryOrders;
|
||||
|
||||
/*
|
||||
* PreparedStatements are used to execute queries which will be repeated.
|
||||
*/
|
||||
// PreparedStatements are used to execute queries which will be repeated.
|
||||
private PreparedStatement insertIntoOrders;
|
||||
private PreparedStatement removeorder;
|
||||
private PreparedStatement queyOrderByID;
|
||||
private PreparedStatement queyOrderById;
|
||||
|
||||
/**
|
||||
* {@summary}
|
||||
* Establish connection to database.
|
||||
* {@summary Establish connection to database.
|
||||
* Constructor to create DataSource object.}
|
||||
*/
|
||||
public DataSource() {
|
||||
try {
|
||||
@@ -79,7 +72,7 @@ public class DataSource implements DataSourceInterface {
|
||||
getschema = conn.createStatement();
|
||||
queryOrders = conn.createStatement();
|
||||
removeorder = conn.prepareStatement(REMOVE_ORDER);
|
||||
queyOrderByID = conn.prepareStatement(QUERY_ORDER);
|
||||
queyOrderById = conn.prepareStatement(QUERY_ORDER);
|
||||
deleteschema = conn.createStatement();
|
||||
} catch (SQLException e) {
|
||||
LOGGER.error(e.getLocalizedMessage(), e.getCause());
|
||||
@@ -156,7 +149,6 @@ public class DataSource implements DataSourceInterface {
|
||||
}
|
||||
|
||||
/**
|
||||
* {@summary}
|
||||
* Query order by given id.
|
||||
* @param id as the parameter
|
||||
* @return Order objct
|
||||
@@ -166,9 +158,9 @@ public class DataSource implements DataSourceInterface {
|
||||
@Override
|
||||
public Order queryOrder(int id) throws SQLException {
|
||||
Order order = null;
|
||||
queyOrderByID.setInt(1, id);
|
||||
try (var rSet = queyOrderByID.executeQuery()) {
|
||||
queyOrderByID.setInt(1, id);
|
||||
queyOrderById.setInt(1, id);
|
||||
try (var rSet = queyOrderById.executeQuery()) {
|
||||
queyOrderById.setInt(1, id);
|
||||
if (rSet.next()) {
|
||||
var address = new ShippingAddress(rSet.getString(4),
|
||||
rSet.getString(5), rSet.getString(6));
|
||||
@@ -204,7 +196,7 @@ public class DataSource implements DataSourceInterface {
|
||||
try {
|
||||
deleteschema.execute(DELETE_SCHEMA);
|
||||
queryOrders.close();
|
||||
queyOrderByID.close();
|
||||
queyOrderById.close();
|
||||
deleteschema.close();
|
||||
insertIntoOrders.close();
|
||||
conn.close();
|
||||
|
||||
@@ -27,7 +27,8 @@ package com.iluwatar.embedded.value;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
/*
|
||||
|
||||
/**
|
||||
* A POJO which represents the Order object.
|
||||
*/
|
||||
@ToString
|
||||
|
||||
Reference in New Issue
Block a user