Skip to content

Commit b7d580c

Browse files
committed
add license
1 parent bc3941f commit b7d580c

17 files changed

+1706
-0
lines changed

dao-factory/pom.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,30 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
5+
6+
The MIT License
7+
Copyright © 2014-2022 Ilkka Seppälä
8+
9+
Permission is hereby granted, free of charge, to any person obtaining a copy
10+
of this software and associated documentation files (the "Software"), to deal
11+
in the Software without restriction, including without limitation the rights
12+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
copies of the Software, and to permit persons to whom the Software is
14+
furnished to do so, subject to the following conditions:
15+
16+
The above copyright notice and this permission notice shall be included in
17+
all copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
THE SOFTWARE.
26+
27+
-->
228
<project xmlns="http://maven.apache.org/POM/4.0.0"
329
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
430
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package com.iluwatar.daofactory;
26+
27+
import java.util.List;
28+
import lombok.extern.slf4j.Slf4j;
29+
import org.bson.types.ObjectId;
30+
31+
@Slf4j
32+
public class App {
33+
34+
public static void main(String[] args) {
35+
var daoFactory = DAOFactory.getDataSource(DataSourceType.H2);
36+
var customerDAO = daoFactory.createCustomerDAO();
37+
38+
// Perform CRUD H2 Database
39+
if (customerDAO instanceof H2CustomerDAO h2CustomerDAO) {
40+
h2CustomerDAO.deleteSchema();
41+
h2CustomerDAO.createSchema();
42+
}
43+
Customer<Long> customerInmemory1 = new Customer<>(1L, "Green");
44+
Customer<Long> customerInmemory2 = new Customer<>(2L, "Red");
45+
Customer<Long> customerInmemory3 = new Customer<>(3L, "Blue");
46+
Customer<Long> customerUpdateInmemory = new Customer<>(1L, "Yellow");
47+
48+
LOGGER.debug("H2 - Create customer");
49+
performCreateCustomer(
50+
customerDAO, List.of(customerInmemory1, customerInmemory2, customerInmemory3));
51+
LOGGER.debug("H2 - Update customer");
52+
performUpdateCustomer(customerDAO, customerUpdateInmemory);
53+
LOGGER.debug("H2 - Delete customer");
54+
performDeleteCustomer(customerDAO, 3L);
55+
deleteSchema(customerDAO);
56+
57+
// Perform CRUD MongoDb
58+
daoFactory = DAOFactory.getDataSource(DataSourceType.Mongo);
59+
customerDAO = daoFactory.createCustomerDAO();
60+
ObjectId idCustomerMongo1 = new ObjectId();
61+
ObjectId idCustomerMongo2 = new ObjectId();
62+
Customer<ObjectId> customer4 = new Customer<>(idCustomerMongo1, "Masca");
63+
Customer<ObjectId> customer5 = new Customer<>(idCustomerMongo2, "Elliot");
64+
Customer<ObjectId> customerUpdateMongo = new Customer<>(idCustomerMongo2, "Henry");
65+
66+
LOGGER.debug("Mongo - Create customer");
67+
performCreateCustomer(customerDAO, List.of(customer4, customer5));
68+
LOGGER.debug("Mongo - Update customer");
69+
performUpdateCustomer(customerDAO, customerUpdateMongo);
70+
LOGGER.debug("Mongo - Delete customer");
71+
performDeleteCustomer(customerDAO, idCustomerMongo2);
72+
deleteSchema(customerDAO);
73+
74+
// Perform CRUD Flat file
75+
daoFactory = DAOFactory.getDataSource(DataSourceType.FlatFile);
76+
customerDAO = daoFactory.createCustomerDAO();
77+
Customer<Long> customerFlatFile1 = new Customer<>(1L, "Duc");
78+
Customer<Long> customerFlatFile2 = new Customer<>(2L, "Quang");
79+
Customer<Long> customerFlatFile3 = new Customer<>(3L, "Nhat");
80+
Customer<Long> customerUpdateFlatFile = new Customer<>(1L, "Thanh");
81+
LOGGER.debug("Flat file - Create customer");
82+
performCreateCustomer(
83+
customerDAO, List.of(customerFlatFile1, customerFlatFile2, customerFlatFile3));
84+
LOGGER.debug("Flat file - Update customer");
85+
performUpdateCustomer(customerDAO, customerUpdateFlatFile);
86+
LOGGER.debug("Flat file - Delete customer");
87+
performDeleteCustomer(customerDAO, 3L);
88+
deleteSchema(customerDAO);
89+
}
90+
91+
public static void deleteSchema(CustomerDAO<?> customerDAO) {
92+
customerDAO.deleteSchema();
93+
}
94+
95+
public static <T> void performCreateCustomer(
96+
CustomerDAO<T> customerDAO, List<Customer<T>> customerList) {
97+
for (Customer<T> customer : customerList) {
98+
customerDAO.save(customer);
99+
}
100+
List<Customer<T>> customers = customerDAO.findAll();
101+
for (Customer<T> customer : customers) {
102+
LOGGER.debug(customer.toString());
103+
}
104+
}
105+
106+
public static <T> void performUpdateCustomer(
107+
CustomerDAO<T> customerDAO, Customer<T> customerUpdate) {
108+
customerDAO.update(customerUpdate);
109+
List<Customer<T>> customers = customerDAO.findAll();
110+
for (Customer<T> customer : customers) {
111+
LOGGER.debug(customer.toString());
112+
}
113+
}
114+
115+
public static <T> void performDeleteCustomer(CustomerDAO<T> customerDAO, T customerId) {
116+
customerDAO.delete(customerId);
117+
List<Customer<T>> customers = customerDAO.findAll();
118+
for (Customer<T> customer : customers) {
119+
LOGGER.debug(customer.toString());
120+
}
121+
}
122+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package com.iluwatar.daofactory;
26+
27+
import java.io.Serializable;
28+
import lombok.AllArgsConstructor;
29+
import lombok.Getter;
30+
import lombok.NoArgsConstructor;
31+
import lombok.Setter;
32+
import lombok.ToString;
33+
34+
/**
35+
* A customer generic POJO that represents the data that can be stored in any supported data source.
36+
* This class is designed t work with various ID types (e.g., Long, String, or ObjectId) through
37+
* generic, making it adaptable to different persistence system.
38+
*/
39+
@Getter
40+
@Setter
41+
@NoArgsConstructor
42+
@AllArgsConstructor
43+
@ToString
44+
public class Customer<T> implements Serializable {
45+
private T id;
46+
private String name;
47+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package com.iluwatar.daofactory;
26+
27+
import java.util.List;
28+
import java.util.Optional;
29+
30+
/**
31+
* The Data Access Object (DAO) pattern provides an abstraction layer between the application and
32+
* the database. It encapsulates data access logic, allowing the application to work with domain
33+
* objects instead of direct database operations.
34+
*
35+
* <p>Implementations handle specific storage mechanisms (e.g., in-memory, databases) while keeping
36+
* client code unchanged.
37+
*
38+
* @see H2CustomerDAO
39+
* @see MongoCustomerDAO
40+
* @see FlatFileCustomerDAO
41+
*/
42+
public interface CustomerDAO<T> {
43+
/**
44+
* Persist the given customer
45+
*
46+
* @param customer the customer to persist
47+
*/
48+
void save(Customer<T> customer);
49+
50+
/**
51+
* Update the given customer
52+
*
53+
* @param customer the customer to update
54+
*/
55+
void update(Customer<T> customer);
56+
57+
/**
58+
* Delete the customer with the given id
59+
*
60+
* @param id the id of the customer to delete
61+
*/
62+
void delete(T id);
63+
64+
/**
65+
* Find all customers
66+
*
67+
* @return a list of customers
68+
*/
69+
List<Customer<T>> findAll();
70+
71+
/**
72+
* Find the customer with the given id
73+
*
74+
* @param id the id of the customer to find
75+
* @return the customer with the given id
76+
*/
77+
Optional<Customer<T>> findById(T id);
78+
79+
/**
80+
* Delete the customer schema. After executing the statements, this function will be called to
81+
* clean up the data and delete the records.
82+
*/
83+
void deleteSchema();
84+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package com.iluwatar.daofactory;
26+
27+
/**
28+
* An abstract factory class that provides a way to create concrete DAO (Data Access Object)
29+
* factories for different data sources types (e.g., H2, Mongo, FlatFile).
30+
*
31+
* <p>This class follows the Abstract Factory design pattern, allowing applications to retrieve the
32+
* approriate DAO implementation without being tightly coupled to a specific data source.
33+
*
34+
* @see H2DataSourceFactory
35+
* @see MongoDataSourceFactory
36+
* @see FlatFileDataSourceFactory
37+
*/
38+
public abstract class DAOFactory {
39+
/**
40+
* Returns a concrete {@link DAOFactory} intance based on the specified data source type.
41+
*
42+
* @param dataSourceType The type of data source for which a factory is needed. Supported values:
43+
* {@code H2}, {@code Mongo}, {@code FlatFile}
44+
* @return A {@link DAOFactory} implementation corresponding to the given data source type.
45+
* @throws IllegalArgumentException if the given data source type is not supported.
46+
*/
47+
public static DAOFactory getDataSource(DataSourceType dataSourceType) {
48+
return switch (dataSourceType) {
49+
case H2 -> new H2DataSourceFactory();
50+
case Mongo -> new MongoDataSourceFactory();
51+
case FlatFile -> new FlatFileDataSourceFactory();
52+
};
53+
}
54+
55+
/**
56+
* Retrieves a {@link CustomerDAO} implementation specific to the underlying data source..
57+
*
58+
* @return A data source-specific implementation of {@link CustomerDAO}
59+
*/
60+
public abstract CustomerDAO createCustomerDAO();
61+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package com.iluwatar.daofactory;
26+
27+
/** Enumerates the types of data sources supported by the application. */
28+
public enum DataSourceType {
29+
H2,
30+
Mongo,
31+
FlatFile
32+
}

0 commit comments

Comments
 (0)