Work on #404, javadocs and test cases for DB and in memory dao.
This commit is contained in:
@@ -49,9 +49,11 @@ public class App {
|
||||
* Program entry point.
|
||||
*
|
||||
* @param args command line args.
|
||||
* @throws Exception if any error occurs.
|
||||
*/
|
||||
public static void main(final String[] args) {
|
||||
final CustomerDao customerDao = new InMemoryCustomerDao(generateSampleCustomers());
|
||||
public static void main(final String[] args) throws Exception {
|
||||
final CustomerDao customerDao = new InMemoryCustomerDao();
|
||||
addCustomers(customerDao);
|
||||
log.info("customerDao.getAllCustomers(): " + customerDao.getAll());
|
||||
log.info("customerDao.getCusterById(2): " + customerDao.getById(2));
|
||||
final Customer customer = new Customer(4, "Dan", "Danson");
|
||||
@@ -65,6 +67,12 @@ public class App {
|
||||
log.info("customerDao.getAllCustomers(): " + customerDao.getAll());
|
||||
}
|
||||
|
||||
private static void addCustomers(CustomerDao customerDao) throws Exception {
|
||||
for (Customer customer : generateSampleCustomers()) {
|
||||
customerDao.add(customer);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate customers.
|
||||
*
|
||||
|
||||
@@ -26,18 +26,54 @@ import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
*
|
||||
* CustomerDao
|
||||
*
|
||||
* In an application the Data Access Object (DAO) is a part of Data access layer. It is an object
|
||||
* that provides an interface to some type of persistence mechanism. By mapping application calls
|
||||
* to the persistence layer, DAO provides some specific data operations without exposing details
|
||||
* of the database. This isolation supports the Single responsibility principle. It separates what
|
||||
* data accesses the application needs, in terms of domain-specific objects and data types
|
||||
* (the public interface of the DAO), from how these needs can be satisfied with a specific DBMS,
|
||||
* database schema, etc.
|
||||
* <br/><br/>
|
||||
* Any change in the way data is stored and retrieved will not change the client code as the client
|
||||
* will be using interface and need not worry about exact source.
|
||||
*
|
||||
* @see InMemoryCustomerDao
|
||||
* @see DBCustomerDao
|
||||
*/
|
||||
public interface CustomerDao {
|
||||
|
||||
Stream<Customer> getAll();
|
||||
/**
|
||||
* @return all the customers as a stream. The stream may be lazily or eagerly evaluated based on the
|
||||
* implementation. The stream must be closed after use.
|
||||
* @throws Exception if any error occurs.
|
||||
*/
|
||||
Stream<Customer> getAll() throws Exception;
|
||||
|
||||
/**
|
||||
* @param id unique identifier of the customer.
|
||||
* @return customer with unique identifier <code>id</code> is found, null otherwise.
|
||||
* @throws Exception if any error occurs.
|
||||
*/
|
||||
Customer getById(int id) throws Exception;
|
||||
|
||||
Customer getById(int id);
|
||||
/**
|
||||
* @param customer the customer to be added.
|
||||
* @return true if customer is successfully added, false if customer already exists.
|
||||
* @throws Exception if any error occurs.
|
||||
*/
|
||||
boolean add(Customer customer) throws Exception;
|
||||
|
||||
boolean add(Customer customer);
|
||||
/**
|
||||
* @param customer the customer to be updated.
|
||||
* @return true if customer exists and is successfully updated, false otherwise.
|
||||
* @throws Exception if any error occurs.
|
||||
*/
|
||||
boolean update(Customer customer) throws Exception;
|
||||
|
||||
boolean update(Customer customer);
|
||||
|
||||
boolean delete(Customer customer);
|
||||
/**
|
||||
* @param customer the customer to be deleted.
|
||||
* @return true if customer exists and is successfully deleted, false otherwise.
|
||||
* @throws Exception if any error occurs.
|
||||
*/
|
||||
boolean delete(Customer customer) throws Exception;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,28 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.dao;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
@@ -11,16 +32,22 @@ import java.util.function.Consumer;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class DBCustomerDao implements CustomerDao {
|
||||
|
||||
private String dbUrl;
|
||||
private final DataSource dataSource;
|
||||
|
||||
public DBCustomerDao(String dbUrl) {
|
||||
this.dbUrl = dbUrl;
|
||||
public DBCustomerDao(DataSource dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Stream<Customer> getAll() {
|
||||
public Stream<Customer> getAll() throws Exception {
|
||||
|
||||
Connection connection;
|
||||
try {
|
||||
@@ -41,14 +68,16 @@ public class DBCustomerDao implements CustomerDao {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
|
||||
}}, false).onClose(() -> mutedClose(connection));
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
throw new Exception(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private Connection getConnection() throws SQLException {
|
||||
return dataSource.getConnection();
|
||||
}
|
||||
|
||||
private void mutedClose(Connection connection) {
|
||||
try {
|
||||
connection.close();
|
||||
@@ -64,22 +93,23 @@ public class DBCustomerDao implements CustomerDao {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Customer getById(int id) {
|
||||
public Customer getById(int id) throws Exception {
|
||||
try (Connection connection = getConnection();
|
||||
PreparedStatement statement = connection.prepareStatement("SELECT * FROM CUSTOMERS WHERE ID = ?")) {
|
||||
statement.setInt(1, id);
|
||||
ResultSet resultSet = statement.executeQuery();
|
||||
if (resultSet.next()) {
|
||||
return createCustomer(resultSet);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
throw new Exception(ex.getMessage(), ex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(Customer customer) {
|
||||
public boolean add(Customer customer) throws Exception {
|
||||
if (getById(customer.getId()) != null) {
|
||||
return false;
|
||||
}
|
||||
@@ -92,13 +122,12 @@ public class DBCustomerDao implements CustomerDao {
|
||||
statement.execute();
|
||||
return true;
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
return false;
|
||||
throw new Exception(ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean update(Customer customer) {
|
||||
public boolean update(Customer customer) throws Exception {
|
||||
try (Connection connection = getConnection();
|
||||
PreparedStatement statement = connection.prepareStatement("UPDATE CUSTOMERS SET FNAME = ?, LNAME = ? WHERE ID = ?")) {
|
||||
statement.setString(1, customer.getFirstName());
|
||||
@@ -106,25 +135,18 @@ public class DBCustomerDao implements CustomerDao {
|
||||
statement.setInt(3, customer.getId());
|
||||
return statement.executeUpdate() > 0;
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
return false;
|
||||
throw new Exception(ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean delete(Customer customer) {
|
||||
public boolean delete(Customer customer) throws Exception {
|
||||
try (Connection connection = getConnection();
|
||||
PreparedStatement statement = connection.prepareStatement("DELETE FROM CUSTOMERS WHERE ID = ?")) {
|
||||
statement.setInt(1, customer.getId());
|
||||
return statement.executeUpdate() > 0;
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
return false;
|
||||
throw new Exception(ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
|
||||
private Connection getConnection() throws SQLException {
|
||||
return DriverManager.getConnection(dbUrl);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -23,30 +23,22 @@
|
||||
package com.iluwatar.dao;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
*
|
||||
* The data access object (DAO) is an object that provides an abstract interface to some type of
|
||||
* database or other persistence mechanism. By mapping application calls to the persistence layer,
|
||||
* DAO provide some specific data operations without exposing details of the database. This
|
||||
* isolation supports the Single responsibility principle. It separates what data accesses the
|
||||
* application needs, in terms of domain-specific objects and data types (the public interface of
|
||||
* the DAO), from how these needs can be satisfied with a specific DBMS, database schema, etc.
|
||||
*
|
||||
* An in memory implementation of {@link CustomerDao}, which stores the customers in JVM memory
|
||||
* and data is lost when the application exits.
|
||||
* <br/>
|
||||
* This implementation is useful as temporary database or for testing.
|
||||
*/
|
||||
// TODO update the javadoc
|
||||
public class InMemoryCustomerDao implements CustomerDao {
|
||||
|
||||
private Map<Integer, Customer> idToCustomer = new HashMap<>();
|
||||
|
||||
public InMemoryCustomerDao(final List<Customer> customers) {
|
||||
customers.stream()
|
||||
.forEach((customer) -> idToCustomer.put(customer.getId(), customer));
|
||||
}
|
||||
|
||||
/**
|
||||
* An eagerly evaluated stream of customers stored in memory.
|
||||
*/
|
||||
@Override
|
||||
public Stream<Customer> getAll() {
|
||||
return idToCustomer.values().stream();
|
||||
@@ -67,7 +59,6 @@ public class InMemoryCustomerDao implements CustomerDao {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean update(final Customer customer) {
|
||||
return idToCustomer.replace(customer.getId(), customer) != null;
|
||||
|
||||
Reference in New Issue
Block a user