diff --git a/my-app/.mvn/jvm.config b/my-app/.mvn/jvm.config new file mode 100644 index 0000000..e69de29 diff --git a/my-app/.mvn/maven.config b/my-app/.mvn/maven.config new file mode 100644 index 0000000..e69de29 diff --git a/my-app/pom.xml b/my-app/pom.xml new file mode 100644 index 0000000..a9f6c33 --- /dev/null +++ b/my-app/pom.xml @@ -0,0 +1,95 @@ + + + 4.0.0 + + com.loogibot.lil + my-app + 1.0-SNAPSHOT + + my-app + + http://www.example.com + + + UTF-8 + 17 + + + + + + org.junit + junit-bom + 5.11.0 + pom + import + + + + + + + org.junit.jupiter + junit-jupiter-api + test + + + org.postgresql + postgresql + 42.7.4 + + + + org.junit.jupiter + junit-jupiter-params + test + + + + + + + + + maven-clean-plugin + 3.4.0 + + + + maven-resources-plugin + 3.3.1 + + + maven-compiler-plugin + 3.13.0 + + + maven-surefire-plugin + 3.3.0 + + + maven-jar-plugin + 3.4.2 + + + maven-install-plugin + 3.1.2 + + + maven-deploy-plugin + 3.1.2 + + + + maven-site-plugin + 3.12.1 + + + maven-project-info-reports-plugin + 3.6.1 + + + + + diff --git a/my-app/src/main/java/com/loogibot/lil/App.java b/my-app/src/main/java/com/loogibot/lil/App.java new file mode 100644 index 0000000..75021c3 --- /dev/null +++ b/my-app/src/main/java/com/loogibot/lil/App.java @@ -0,0 +1,82 @@ +package com.loogibot.lil; + +import java.math.BigDecimal; +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +import com.loogibot.lil.data.dao.CustomerDao; +import com.loogibot.lil.data.dao.ServiceDao; +import com.loogibot.lil.data.dao.SimpleProductDao; +import com.loogibot.lil.data.entity.Customer; +import com.loogibot.lil.data.entity.Service; + +/** + * Hello world! + */ +public class App { + public static void main(String[] args) { + ServiceDao serviceDao = new ServiceDao(); + List services = serviceDao.getAll(); + System.out.println("**** SERVICES BELOW ****"); + System.out.println("\n *** GET_ALL ***"); + services.forEach(System.out::println); + + Optional service = serviceDao.getOne(services.get(0).getServiceId()); + System.out.println("\n *** GET ONE ***\n" + service.get()); + + Service newService = new Service(); + newService.setName("FooBarBaz" + System.currentTimeMillis()); + newService.setPrice(new BigDecimal(4.35)); + newService = serviceDao.create(newService); + System.out.println("\n *** CREATE ***\n" + newService); + + newService.setPrice(new BigDecimal(13.45)); + newService = serviceDao.update(newService); + System.out.println("\n *** UPDATE *** \n" + newService); + + serviceDao.delete(newService.getServiceId()); + System.out.println("\n *** DELETE *** \n"); + + // + + CustomerDao customerDao = new CustomerDao(); + List customers = customerDao.getAll(); + System.out.println("**** CUSTOMERS BELOW ****"); + System.out.println("\n *** GET_ALL ***"); + customers.forEach(System.out::println); + + Optional customer = customerDao.getOne(customers.get(0).getCustomerId()); + System.out.println("\n *** GET ONE ***\n" + customer.get()); + + Customer newCustomer = new Customer(); + newCustomer.setFirst_name("Joey" + System.currentTimeMillis()); + newCustomer.setLast_name("Castillo" + System.currentTimeMillis()); + newCustomer.setEmail("Joey@Castillo.com" + System.currentTimeMillis()); + newCustomer.setPhone("5552746" + System.currentTimeMillis()); + + newCustomer = customerDao.create(newCustomer); + System.out.println("\n *** CREATE ***\n" + newCustomer); + + newCustomer = customerDao.update(newCustomer); + System.out.println("\n *** UPDATE *** \n" + newCustomer); + + customerDao.delete(newCustomer.getCustomerId()); + System.out.println("\n *** DELETE *** \n"); + + System.out.println("**** Simple Product BELOW ****"); + SimpleProductDao spdao = new SimpleProductDao(); + UUID productId = spdao.createProduct("Zafa", new BigDecimal(83.76), "Jaloo"); + System.out.println(productId); + + System.out.println("**** LIMIT ****"); + serviceDao.getAllLimit(2).forEach(System.out::println); + + System.out.println("**** PAGED ****"); + + for(int i = 1; i<11; i++){ + System.out.println("Page numbe: " + i); + customerDao.getAllPaged(i, 10).forEach(System.out::println); + } + } +} diff --git a/my-app/src/main/java/com/loogibot/lil/data/dao/CustomerDao.java b/my-app/src/main/java/com/loogibot/lil/data/dao/CustomerDao.java new file mode 100644 index 0000000..e73588a --- /dev/null +++ b/my-app/src/main/java/com/loogibot/lil/data/dao/CustomerDao.java @@ -0,0 +1,165 @@ +package com.loogibot.lil.data.dao; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.UUID; +import java.util.logging.Logger; + +import com.loogibot.lil.data.entity.Customer; +import com.loogibot.lil.data.util.DatabaseUtils; + +public class CustomerDao implements Dao { + + private static final Logger LOGGER = Logger.getLogger(CustomerDao.class.getName()); + private static final String GET_ALL = "select customer_id, first_name, last_name, email, phone from wisdom.customers"; + private static final String GET_BY_ID = "select customer_id, last_name, first_name, email, phone from wisdom.customers where customer_id = ?"; + private static final String CREATE = "insert into wisdom.customers (customer_id, last_name, first_name, email, phone) values (?,?,?,?,?)"; + private static final String UPDATE = "update wisdom.customers set first_name = ?, last_name = ?, email = ?, phone = ? where customer_id = ?"; + private static final String DELETE = "delete from wisdom.customers where customer_id = ?"; + private static final String GET_ALL_PAGED = "select customer_id, first_name, last_name, email, phone from wisdom.customers order by last_name, first_name, email, phone LIMIT ? OFFSET ?"; + + + public List getAllPaged(int pageNumber, int limit){ + List customers = new ArrayList<>(); + Connection connection = DatabaseUtils.getConnection(); + int offset = ((pageNumber - 1) * limit); + try(PreparedStatement statement = connection.prepareStatement(GET_ALL_PAGED)){ + statement.setInt(1, limit); + statement.setInt(2, offset); + ResultSet rs = statement.executeQuery(); + customers = this.processResultSet(rs); + rs.close(); + } catch (SQLException e){ + DatabaseUtils.handleSqlException("CustomerDao.getAllPaged", e, LOGGER); + } + return customers; + } + + @Override + public List getAll() { + List customers = new ArrayList<>(); + Connection connection = DatabaseUtils.getConnection(); + try (Statement statement = connection.createStatement()) { + ResultSet rs = statement.executeQuery(GET_ALL); + customers = this.processResultSet(rs); + rs.close(); + } catch (SQLException e) { + DatabaseUtils.handleSqlException("CustomerDao.getAll", e, LOGGER); + } + return customers; + } + + @Override + public Customer create(Customer entity) { + UUID customerId = UUID.randomUUID(); + Connection connection = DatabaseUtils.getConnection(); + try { + connection.setAutoCommit(false); + PreparedStatement statement = connection.prepareStatement(CREATE); + statement.setObject(1, customerId); + statement.setString(2, entity.getFirst_name()); + statement.setString(3, entity.getLast_name()); + statement.setString(4, entity.getEmail()); + statement.setString(5, entity.getPhone()); + statement.execute(); + connection.commit(); + statement.close(); + } catch (SQLException e) { + try { + connection.rollback(); + } catch (SQLException sqle) { + DatabaseUtils.handleSqlException("CustomerDao.create.rollback", sqle, LOGGER); + } + DatabaseUtils.handleSqlException("CustomerDao.create", e, LOGGER); + } + Optional customer = this.getOne(customerId); + if (!customer.isPresent()) { + return null; + } + return customer.get(); + } + + @Override + public Optional getOne(UUID id) { + try (PreparedStatement statement = DatabaseUtils.getConnection().prepareStatement(GET_BY_ID)) { + statement.setObject(1, id); + ResultSet rs = statement.executeQuery(); + List customers = this.processResultSet(rs); + if (customers.isEmpty()) { + return Optional.empty(); + } + return Optional.of(customers.get(0)); + } catch (SQLException e) { + // TODO: handle exception + DatabaseUtils.handleSqlException("CustomerDao.getOne", e, LOGGER); + } + return Optional.empty(); + } + + @Override + public Customer update(Customer entity) { + Connection connection = DatabaseUtils.getConnection(); + try { + connection.setAutoCommit(false); + PreparedStatement statement = connection.prepareStatement(UPDATE); + // statement.setObject(1, entity.getCustomerId()); + statement.setString(1, entity.getFirst_name()); + statement.setString(2, entity.getLast_name()); + statement.setString(3, entity.getEmail()); + statement.setString(4, entity.getPhone()); + statement.setObject(5, entity.getCustomerId()); + statement.execute(); + connection.commit(); + statement.close(); + } catch (SQLException e) { + try { + connection.rollback(); + } catch (SQLException sqle) { + DatabaseUtils.handleSqlException("CustomerDao.update.rollback", sqle, LOGGER); + } + DatabaseUtils.handleSqlException("CustomerDao.update", e, LOGGER); + } + return this.getOne(entity.getCustomerId()).get(); + } + + @Override + public void delete(UUID id) { + Connection connection = DatabaseUtils.getConnection(); + + try { + connection.setAutoCommit(false); + PreparedStatement statement = connection.prepareStatement(DELETE); + statement.setObject(1, id); + statement.execute(); + connection.commit(); + statement.close(); + } catch (SQLException e) { + try { + connection.rollback(); + } catch (SQLException sqle) { + DatabaseUtils.handleSqlException("CustomerDao.delete.rollback", sqle, LOGGER); + } + DatabaseUtils.handleSqlException("CustomerDao.delete", e, LOGGER); + } + } + + private List processResultSet(ResultSet rs) throws SQLException { + List customers = new ArrayList<>(); + while (rs.next()) { + Customer customer = new Customer(); + customer.setCustomerId((UUID) rs.getObject("customer_id")); + customer.setFirst_name(rs.getString("first_name")); + customer.setLast_name(rs.getString("last_name")); + customer.setEmail(rs.getString("email")); + customer.setPhone(rs.getString("phone")); + customers.add(customer); + } + return customers; + } +} diff --git a/my-app/src/main/java/com/loogibot/lil/data/dao/Dao.java b/my-app/src/main/java/com/loogibot/lil/data/dao/Dao.java new file mode 100644 index 0000000..6780e22 --- /dev/null +++ b/my-app/src/main/java/com/loogibot/lil/data/dao/Dao.java @@ -0,0 +1,12 @@ +package com.loogibot.lil.data.dao; +import java.util.UUID; +import java.util.Optional; +import java.util.List; + +public interface Dao { + List getAll(); + T create(T entity); + Optional getOne(Id id); + T update(T entity); + void delete(Id id); +} diff --git a/my-app/src/main/java/com/loogibot/lil/data/dao/ServiceDao.java b/my-app/src/main/java/com/loogibot/lil/data/dao/ServiceDao.java new file mode 100644 index 0000000..5f488b1 --- /dev/null +++ b/my-app/src/main/java/com/loogibot/lil/data/dao/ServiceDao.java @@ -0,0 +1,164 @@ +package com.loogibot.lil.data.dao; + +import java.sql.Statement; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.UUID; +import java.util.logging.Logger; + +import javax.xml.crypto.Data; + +import com.loogibot.lil.data.entity.Service; +import com.loogibot.lil.data.util.DatabaseUtils; + +public class ServiceDao implements Dao { + private static final Logger LOGGER = Logger.getLogger(ServiceDao.class.getName()); + private static final String GET_ALL = "select service_id, name, price from wisdom.services"; + private static final String GET_BY_ID = "select service_id, name, price from wisdom.services where service_id = ?"; + private static final String CREATE = "insert into wisdom.services (service_id, name, price) values (?,?,?)"; + private static final String UPDATE = "update wisdom.services set name = ?, price = ? where service_id = ?"; + private static final String DELETE = "delete from wisdom.services where service_id = ?"; + private static final String GET_ALL_LIMIT = "select service_id, name, price from wisdom.services order by name limit ?"; + + @Override + public List getAll() { + // TODO Auto-generated method stub + List services = new ArrayList<>(); + Connection connection = DatabaseUtils.getConnection(); + + try (Statement statement = connection.createStatement()) { + // make sure its not beans.Statement, use sql.Statement + ResultSet rs = statement.executeQuery(GET_ALL); + services = this.processResultSet(rs); + } catch (SQLException e) { + // TODO: handle exception + DatabaseUtils.handleSqlException("ServiceDao.getAll", e, LOGGER); + } + return services; + } + + @Override + public Service create(Service entity) { + // TODO Auto-generated method stub + UUID serviceId = UUID.randomUUID(); + Connection connection = DatabaseUtils.getConnection(); + try { + connection.setAutoCommit(false); + PreparedStatement statement = connection.prepareStatement(CREATE); + statement.setObject(1, serviceId); + statement.setString(2, entity.getName()); + statement.setBigDecimal(3, entity.getPrice()); + statement.execute(); + connection.commit(); + statement.close(); + } catch (SQLException e) { + try { + connection.rollback(); + } catch (SQLException sqle) { + DatabaseUtils.handleSqlException("ServiceDao.create.rollback", sqle, LOGGER); + } + DatabaseUtils.handleSqlException("ServiceDao.create", e, LOGGER); + } + Optional service = this.getOne(serviceId); + if (!service.isPresent()) { + return null; + } + return service.get(); + } + + @Override + public Optional getOne(UUID id) { + // TODO Auto-generated method stub + try (PreparedStatement statement = DatabaseUtils.getConnection().prepareStatement(GET_BY_ID)) { + statement.setObject(1, id); + ResultSet rs = statement.executeQuery(); + List services = this.processResultSet(rs); + if (services.isEmpty()) { + return Optional.empty(); + } + return Optional.of(services.get(0)); + } catch (SQLException e) { + // TODO: handle exception + DatabaseUtils.handleSqlException("ServiceDao.getOne", e, LOGGER); + } + return Optional.empty(); + } + + @Override + public Service update(Service entity) { + // TODO Auto-generated method stub + Connection connection = DatabaseUtils.getConnection(); + try { + connection.setAutoCommit(false); + PreparedStatement statement = connection.prepareStatement(UPDATE); + statement.setString(1, entity.getName()); + statement.setBigDecimal(2, entity.getPrice()); + statement.setObject(3, entity.getServiceId()); + statement.execute(); + connection.commit(); + statement.close(); + } catch (SQLException e) { + try { + connection.rollback(); + } catch (SQLException sqle) { + DatabaseUtils.handleSqlException("ServiceDao.update.rollback", sqle, LOGGER); + } + DatabaseUtils.handleSqlException("ServiceDao.update", e, LOGGER); + } + return this.getOne(entity.getServiceId()).get(); + } + + @Override + public void delete(UUID id) { + // TODO Auto-generated method stub + Connection connection = DatabaseUtils.getConnection(); + + try { + connection.setAutoCommit(false); + PreparedStatement statement = connection.prepareStatement(DELETE); + statement.setObject(1, id); + statement.execute(); + connection.commit(); + statement.close(); + } catch (SQLException e) { + try { + connection.rollback(); + } catch (SQLException sqle) { + DatabaseUtils.handleSqlException("ServiceDao.delete.rollback", sqle, LOGGER); + } + DatabaseUtils.handleSqlException("ServiceDao.delete", e, LOGGER); + } + } + + public List getAllLimit(int limit){ + List services = new ArrayList<>(); + Connection connection = DatabaseUtils.getConnection(); + try(PreparedStatement statement = connection.prepareStatement(GET_ALL_LIMIT)){ + statement.setInt(1, limit); + ResultSet rs = statement.executeQuery(); + services = this.processResultSet(rs); + rs.close(); + } catch (SQLException e) { + DatabaseUtils.handleSqlException("ServiceDao.getAll", null, LOGGER); + } + return services; + } + + private List processResultSet(ResultSet rs) throws SQLException { + List services = new ArrayList<>(); + while (rs.next()) { + Service service = new Service(); + service.setServiceId((UUID) rs.getObject("service_id")); + service.setName(rs.getString("name")); + service.setPrice(rs.getBigDecimal("price")); + services.add(service); + } + return services; + } + +} diff --git a/my-app/src/main/java/com/loogibot/lil/data/dao/SimpleProductDao.java b/my-app/src/main/java/com/loogibot/lil/data/dao/SimpleProductDao.java new file mode 100644 index 0000000..11e7f74 --- /dev/null +++ b/my-app/src/main/java/com/loogibot/lil/data/dao/SimpleProductDao.java @@ -0,0 +1,40 @@ +package com.loogibot.lil.data.dao; + +import java.math.BigDecimal; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.UUID; +import java.util.logging.Logger; + +import com.loogibot.lil.data.util.DatabaseUtils; + +public class SimpleProductDao { + private static final Logger LOGGER = Logger.getLogger(SimpleProductDao.class.getName()); + private static final String CREATE = "select * from createproduct(?,?,?)"; + + public UUID createProduct(String name, BigDecimal price, String vendorName) { + Connection connection = DatabaseUtils.getConnection(); + UUID returnVal = null; + try { + connection.setAutoCommit(false); + PreparedStatement statement = connection.prepareStatement(CREATE); + statement.setString(1, name); + statement.setBigDecimal(2, price); + statement.setString(3, vendorName); + ResultSet rs = statement.executeQuery(); + while (rs.next()) { + returnVal = (UUID) rs.getObject("createProduct"); + } + } catch (SQLException e) { + try { + connection.rollback(); + } catch (SQLException sqle) { + DatabaseUtils.handleSqlException("SimpleProductDao.create.rollback", sqle, LOGGER); + } + DatabaseUtils.handleSqlException("SimpleProductDao.create.rollback", e, LOGGER); + } + return returnVal; + } +} diff --git a/my-app/src/main/java/com/loogibot/lil/data/entity/Customer.java b/my-app/src/main/java/com/loogibot/lil/data/entity/Customer.java new file mode 100644 index 0000000..9a55cef --- /dev/null +++ b/my-app/src/main/java/com/loogibot/lil/data/entity/Customer.java @@ -0,0 +1,50 @@ +package com.loogibot.lil.data.entity; + +import java.util.UUID; + +public class Customer { + private UUID customerId; + private String first_name; + private String last_name; + private String email; + private String phone; + + public String getFirst_name() { + return first_name; + } + public void setFirst_name(String first_name) { + this.first_name = first_name; + } + public String getLast_name() { + return last_name; + } + public void setLast_name(String last_name) { + this.last_name = last_name; + } + public String getEmail() { + return email; + } + public void setEmail(String email) { + this.email = email; + } + public String getPhone() { + return phone; + } + public void setPhone(String phone) { + this.phone = phone; + } + public UUID getCustomerId() { + return customerId; + } + public void setCustomerId(UUID serviceId) { + this.customerId = serviceId; + } + + @Override + public String toString() { + return "Customer [customerId=" + customerId + ", first_name=" + first_name + ", last_name=" + last_name + ", email=" + + email + ", phone=" + phone + "]"; + } + + +} diff --git a/my-app/src/main/java/com/loogibot/lil/data/entity/Service.java b/my-app/src/main/java/com/loogibot/lil/data/entity/Service.java new file mode 100644 index 0000000..dcc826e --- /dev/null +++ b/my-app/src/main/java/com/loogibot/lil/data/entity/Service.java @@ -0,0 +1,35 @@ +package com.loogibot.lil.data.entity; + +import java.math.BigDecimal; +import java.util.UUID; + +public class Service { + private UUID serviceId; + private String name; + private BigDecimal price; + + public UUID getServiceId() { + return serviceId; + } + public void setServiceId(UUID serviceId) { + this.serviceId = serviceId; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public BigDecimal getPrice() { + return price; + } + public void setPrice(BigDecimal price) { + this.price = price; + } + + @Override + public String toString(){ + return "Service [serviceId=" + serviceId + ", name=" + name + ", price=" + price + "]"; + } + +} diff --git a/my-app/src/main/java/com/loogibot/lil/data/entity/SimpleProduct.java b/my-app/src/main/java/com/loogibot/lil/data/entity/SimpleProduct.java new file mode 100644 index 0000000..c14cb93 --- /dev/null +++ b/my-app/src/main/java/com/loogibot/lil/data/entity/SimpleProduct.java @@ -0,0 +1,72 @@ +package com.loogibot.lil.data.entity; + +import java.math.BigDecimal; +import java.util.UUID; + +public class SimpleProduct { + private UUID productId; + private String name; + private BigDecimal price; + private UUID vendorId; + private String vendorName; + private String contact; + private String phoneNumber; + private String email; + private String address; + + public UUID getProductId() { + return productId; + } + public void setProductId(UUID productId) { + this.productId = productId; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public BigDecimal getPrice() { + return price; + } + public void setPrice(BigDecimal price) { + this.price = price; + } + public UUID getVendorId() { + return vendorId; + } + public void setVendorId(UUID vendorId) { + this.vendorId = vendorId; + } + public String getVendorName() { + return vendorName; + } + public void setVendorName(String vendorName) { + this.vendorName = vendorName; + } + public String getContact() { + return contact; + } + public void setContact(String contact) { + this.contact = contact; + } + public String getPhoneNumber() { + return phoneNumber; + } + public void setPhoneNumber(String phoneNumber) { + this.phoneNumber = phoneNumber; + } + public String getEmail() { + return email; + } + public void setEmail(String email) { + this.email = email; + } + public String getAddress() { + return address; + } + public void setAddress(String address) { + this.address = address; + } + +} diff --git a/my-app/src/main/java/com/loogibot/lil/data/util/DatabaseUtils.java b/my-app/src/main/java/com/loogibot/lil/data/util/DatabaseUtils.java new file mode 100644 index 0000000..8398015 --- /dev/null +++ b/my-app/src/main/java/com/loogibot/lil/data/util/DatabaseUtils.java @@ -0,0 +1,36 @@ +package com.loogibot.lil.data.util; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.logging.Logger; + +public class DatabaseUtils { + private static final String URL = "jdbc:postgresql://localhost:5432/localdb"; + private static final String USERNAME = "localdbuser"; + private static final String PASSWORD = "P0shW3ird!"; + private static final Logger LOGGER = Logger.getLogger(DatabaseUtils.class.getName()); + private static final String exceptionFormat = "exception in %s, message: %s, code: %s"; + private static Connection connection; + + public static Connection getConnection() { + if(connection == null){ + synchronized(DatabaseUtils.class){ + if(connection == null){ + try{ + connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); + } catch(SQLException e) { + handleSqlException("DatabaseUtils.getConnection", e, LOGGER); + // System.out.println(exceptionFormat); + } + } + } + } + return connection; + } + + public static void handleSqlException(String method, SQLException e, Logger log){ + log.warning(String.format(exceptionFormat,method, e.getMessage(), e.getErrorCode())); + throw new RuntimeException(); + } +} diff --git a/my-app/src/test/java/com/loogibot/lil/AppTest.java b/my-app/src/test/java/com/loogibot/lil/AppTest.java new file mode 100644 index 0000000..2e8ce86 --- /dev/null +++ b/my-app/src/test/java/com/loogibot/lil/AppTest.java @@ -0,0 +1,19 @@ +package com.loogibot.lil; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +/** + * Unit test for simple App. + */ +public class AppTest { + + /** + * Rigorous Test :-) + */ + @Test + public void shouldAnswerWithTrue() { + assertTrue(true); + } +}