|
1 | 1 | package com.iluwatar.daofactory; |
2 | 2 |
|
3 | | -import java.sql.SQLException; |
4 | 3 | import java.util.List; |
5 | 4 | import lombok.extern.slf4j.Slf4j; |
6 | 5 | import org.bson.types.ObjectId; |
7 | | -import org.slf4j.Logger; |
8 | | -import org.slf4j.LoggerFactory; |
9 | 6 |
|
10 | | -/** |
11 | | - * Created by: IntelliJ IDEA |
12 | | - * User : dthanh |
13 | | - * Date : 16/04/2025 |
14 | | - * Time : 23:08 |
15 | | - * Filename : ${NAME} |
16 | | - */ |
17 | 7 | @Slf4j |
18 | 8 | public class App { |
19 | 9 |
|
20 | | - private static final Logger logger = LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME); |
21 | 10 |
|
22 | | - |
23 | | - |
24 | | - public static void main(String[] args) throws SQLException { |
25 | | - final var h2DAO = DAOFactory.getDataSource(DataSourceEnum.H2); |
26 | | - final var mongoDAO = DAOFactory.getDataSource(DataSourceEnum.Mongo); |
27 | | - final var flatFileDAO = DAOFactory.getDataSource(DataSourceEnum.FlatFile); |
28 | | - |
29 | | - final var h2CustomerDAO = h2DAO.getCustomerDAO(); |
30 | | - final var mongoCustomerDAO = mongoDAO.getCustomerDAO(); |
31 | | - final var flatFileCustomerDAO = flatFileDAO.getCustomerDAO(); |
| 11 | + public static void main(String[] args) { |
| 12 | + var daoFactory = DAOFactory.getDataSource(DataSourceType.H2); |
| 13 | + var customerDAO = daoFactory.createCustomerDAO(); |
32 | 14 |
|
33 | 15 | // Perform CRUD H2 Database |
| 16 | + if (customerDAO instanceof H2CustomerDAO h2CustomerDAO) { |
| 17 | + h2CustomerDAO.deleteSchema(); |
| 18 | + h2CustomerDAO.createSchema(); |
| 19 | + } |
34 | 20 | Customer<Long> customerInmemory1 = new Customer<>(1L, "Green"); |
35 | 21 | Customer<Long> customerInmemory2 = new Customer<>(2L, "Red"); |
36 | 22 | Customer<Long> customerInmemory3 = new Customer<>(3L, "Blue"); |
37 | 23 | Customer<Long> customerUpdateInmemory = new Customer<>(1L, "Yellow"); |
38 | 24 |
|
39 | 25 | LOGGER.debug("H2 - Create customer"); |
40 | | - performCreateCustomer(h2CustomerDAO, |
| 26 | + performCreateCustomer(customerDAO, |
41 | 27 | List.of(customerInmemory1, customerInmemory2, customerInmemory3)); |
42 | 28 | LOGGER.debug("H2 - Update customer"); |
43 | | - performUpdateCustomer(h2CustomerDAO, customerUpdateInmemory); |
| 29 | + performUpdateCustomer(customerDAO, customerUpdateInmemory); |
44 | 30 | LOGGER.debug("H2 - Delete customer"); |
45 | | - performDeleteCustomer(h2CustomerDAO, 3L); |
46 | | - deleteSchema(h2CustomerDAO); |
| 31 | + performDeleteCustomer(customerDAO, 3L); |
| 32 | + deleteSchema(customerDAO); |
47 | 33 |
|
48 | 34 | // Perform CRUD MongoDb |
| 35 | + daoFactory = DAOFactory.getDataSource(DataSourceType.Mongo); |
| 36 | + customerDAO = daoFactory.createCustomerDAO(); |
49 | 37 | ObjectId idCustomerMongo1 = new ObjectId(); |
50 | 38 | ObjectId idCustomerMongo2 = new ObjectId(); |
51 | 39 | Customer<ObjectId> customer4 = new Customer<>(idCustomerMongo1, "Masca"); |
52 | 40 | Customer<ObjectId> customer5 = new Customer<>(idCustomerMongo2, "Elliot"); |
53 | 41 | Customer<ObjectId> customerUpdateMongo = new Customer<>(idCustomerMongo2, "Henry"); |
54 | 42 |
|
55 | 43 | LOGGER.debug("Mongo - Create customer"); |
56 | | - performCreateCustomer(mongoCustomerDAO, List.of(customer4, customer5)); |
| 44 | + performCreateCustomer(customerDAO, List.of(customer4, customer5)); |
57 | 45 | LOGGER.debug("Mongo - Update customer"); |
58 | | - performUpdateCustomer(mongoCustomerDAO, customerUpdateMongo); |
| 46 | + performUpdateCustomer(customerDAO, customerUpdateMongo); |
59 | 47 | LOGGER.debug("Mongo - Delete customer"); |
60 | | - performDeleteCustomer(mongoCustomerDAO, idCustomerMongo2); |
61 | | - deleteSchema(mongoCustomerDAO); |
| 48 | + performDeleteCustomer(customerDAO, idCustomerMongo2); |
| 49 | + deleteSchema(customerDAO); |
62 | 50 |
|
63 | 51 | // Perform CRUD Flat file |
| 52 | + daoFactory = DAOFactory.getDataSource(DataSourceType.FlatFile); |
| 53 | + customerDAO = daoFactory.createCustomerDAO(); |
64 | 54 | Customer<Long> customerFlatFile1 = new Customer<>(1L, "Duc"); |
65 | 55 | Customer<Long> customerFlatFile2 = new Customer<>(2L, "Quang"); |
66 | 56 | Customer<Long> customerFlatFile3 = new Customer<>(3L, "Nhat"); |
67 | 57 | Customer<Long> customerUpdateFlatFile = new Customer<>(1L, "Thanh"); |
68 | 58 | LOGGER.debug("Flat file - Create customer"); |
69 | | - performCreateCustomer(flatFileCustomerDAO, |
| 59 | + performCreateCustomer(customerDAO, |
70 | 60 | List.of(customerFlatFile1, customerFlatFile2, customerFlatFile3)); |
71 | 61 | LOGGER.debug("Flat file - Update customer"); |
72 | | - performUpdateCustomer(flatFileCustomerDAO, customerUpdateFlatFile); |
| 62 | + performUpdateCustomer(customerDAO, customerUpdateFlatFile); |
73 | 63 | LOGGER.debug("Flat file - Delete customer"); |
74 | | - performDeleteCustomer(flatFileCustomerDAO, 3L); |
75 | | - deleteSchema(flatFileCustomerDAO); |
| 64 | + performDeleteCustomer(customerDAO, 3L); |
| 65 | + deleteSchema(customerDAO); |
76 | 66 | } |
77 | 67 |
|
78 | 68 | public static void deleteSchema(CustomerDAO<?> customerDAO) { |
79 | 69 | customerDAO.deleteSchema(); |
80 | 70 | } |
81 | 71 |
|
82 | | - public static <ID> void performCreateCustomer(CustomerDAO<ID> customerDAO, |
83 | | - List<Customer<ID>> customerList) { |
84 | | - for (Customer<ID> customer : customerList) { |
| 72 | + public static <T> void performCreateCustomer(CustomerDAO<T> customerDAO, |
| 73 | + List<Customer<T>> customerList) { |
| 74 | + for (Customer<T> customer : customerList) { |
85 | 75 | customerDAO.save(customer); |
86 | 76 | } |
87 | | - List<Customer<ID>> customers = customerDAO.findAll(); |
88 | | - for (Customer<ID> customer : customers) { |
| 77 | + List<Customer<T>> customers = customerDAO.findAll(); |
| 78 | + for (Customer<T> customer : customers) { |
89 | 79 | LOGGER.debug(customer.toString()); |
90 | 80 | } |
91 | 81 | } |
92 | 82 |
|
93 | | - public static <ID> void performUpdateCustomer(CustomerDAO<ID> customerDAO, |
94 | | - Customer<ID> customerUpdate) { |
| 83 | + public static <T> void performUpdateCustomer(CustomerDAO<T> customerDAO, |
| 84 | + Customer<T> customerUpdate) { |
95 | 85 | customerDAO.update(customerUpdate); |
96 | | - List<Customer<ID>> customers = customerDAO.findAll(); |
97 | | - for (Customer<ID> customer : customers) { |
98 | | - LOGGER.error(customer.toString()); |
| 86 | + List<Customer<T>> customers = customerDAO.findAll(); |
| 87 | + for (Customer<T> customer : customers) { |
| 88 | + LOGGER.debug(customer.toString()); |
99 | 89 | } |
100 | 90 | } |
101 | 91 |
|
102 | | - public static <ID> void performDeleteCustomer(CustomerDAO<ID> customerDAO, |
103 | | - ID customerId) { |
| 92 | + public static <T> void performDeleteCustomer(CustomerDAO<T> customerDAO, |
| 93 | + T customerId) { |
104 | 94 | customerDAO.delete(customerId); |
105 | | - List<Customer<ID>> customers = customerDAO.findAll(); |
106 | | - for (Customer<ID> customer : customers) { |
| 95 | + List<Customer<T>> customers = customerDAO.findAll(); |
| 96 | + for (Customer<T> customer : customers) { |
107 | 97 | LOGGER.debug(customer.toString()); |
108 | 98 | } |
109 | 99 | } |
|
0 commit comments