From eb72493f1363b0ab4aa381f636aa9ebab2333158 Mon Sep 17 00:00:00 2001 From: Amit Dixit Date: Mon, 4 Apr 2016 12:33:43 +0530 Subject: [PATCH] JDBC removed... --- .../java/com/iluwatar/datamapper/App.java | 14 ++- .../datamapper/StudentMySQLDataMapper.java | 23 +++-- .../datamapper/StudentOracleDataMapper.java | 23 +++-- .../java/com/iluwatar/datamapper/AppTest.java | 99 +++++++++++++++++++ 4 files changed, 140 insertions(+), 19 deletions(-) create mode 100644 data-mapper/src/test/java/com/iluwatar/datamapper/AppTest.java diff --git a/data-mapper/src/main/java/com/iluwatar/datamapper/App.java b/data-mapper/src/main/java/com/iluwatar/datamapper/App.java index 8b5853609..d8a968a17 100644 --- a/data-mapper/src/main/java/com/iluwatar/datamapper/App.java +++ b/data-mapper/src/main/java/com/iluwatar/datamapper/App.java @@ -83,11 +83,15 @@ public final class App { /* Add student in respectibe db */ mapper.insert(student); + if (log.isDebugEnabled()) { + log.debug("App.main(), student : " + student + ", is inserted"); + } + /* Find this student */ final Optional studentToBeFound = mapper.find(student.getStudentId()); if (log.isDebugEnabled()) { - log.debug("App.main(), db find returned : " + studentToBeFound); + log.debug("App.main(), student : " + studentToBeFound + ", is searched"); } /* Update existing student object */ @@ -96,7 +100,15 @@ public final class App { /* Update student in respectibe db */ mapper.update(student); + if (log.isDebugEnabled()) { + log.debug("App.main(), student : " + student + ", is updated"); + } + /* Delete student in db */ + + if (log.isDebugEnabled()) { + log.debug("App.main(), student : " + student + ", is deleted"); + } mapper.delete(student); } diff --git a/data-mapper/src/main/java/com/iluwatar/datamapper/StudentMySQLDataMapper.java b/data-mapper/src/main/java/com/iluwatar/datamapper/StudentMySQLDataMapper.java index cbb97bfa9..ec7507eed 100644 --- a/data-mapper/src/main/java/com/iluwatar/datamapper/StudentMySQLDataMapper.java +++ b/data-mapper/src/main/java/com/iluwatar/datamapper/StudentMySQLDataMapper.java @@ -18,19 +18,20 @@ */ package com.iluwatar.datamapper; +import java.util.ArrayList; import java.util.List; import java.util.Optional; public final class StudentMySQLDataMapper implements StudentDataMapper { /* Note: Normally this would be in the form of an actual database */ - private List students; + private List students = new ArrayList<>(); @Override public final Optional find(final int studentId) { /* Compare with existing students */ - for (final Student student : this.students) { + for (final Student student : this.getStudents()) { /* Check if student is found */ if (student.getStudentId() == studentId) { @@ -48,13 +49,13 @@ public final class StudentMySQLDataMapper implements StudentDataMapper { /* Check with existing students */ - if (this.students.contains(studentToBeUpdated)) { + if (this.getStudents().contains(studentToBeUpdated)) { /* Get the index of student in list */ - final int index = this.students.indexOf(studentToBeUpdated); + final int index = this.getStudents().indexOf(studentToBeUpdated); /* Update the student in list */ - this.students.set(index, studentToBeUpdated); + this.getStudents().set(index, studentToBeUpdated); } else { @@ -67,10 +68,10 @@ public final class StudentMySQLDataMapper implements StudentDataMapper { public final void insert(final Student studentToBeInserted) throws DataMapperException { /* Check with existing students */ - if (!this.students.contains(studentToBeInserted)) { + if (!this.getStudents().contains(studentToBeInserted)) { /* Add student in list */ - this.students.add(studentToBeInserted); + this.getStudents().add(studentToBeInserted); } else { @@ -83,10 +84,10 @@ public final class StudentMySQLDataMapper implements StudentDataMapper { public final void delete(final Student studentToBeDeleted) throws DataMapperException { /* Check with existing students */ - if (this.students.contains(studentToBeDeleted)) { + if (this.getStudents().contains(studentToBeDeleted)) { /* Delete the student from list */ - this.students.remove(studentToBeDeleted); + this.getStudents().remove(studentToBeDeleted); } else { @@ -94,4 +95,8 @@ public final class StudentMySQLDataMapper implements StudentDataMapper { throw new DataMapperException("Student [" + studentToBeDeleted.getName() + "] is not found"); } } + + public List getStudents() { + return this.students; + } } diff --git a/data-mapper/src/main/java/com/iluwatar/datamapper/StudentOracleDataMapper.java b/data-mapper/src/main/java/com/iluwatar/datamapper/StudentOracleDataMapper.java index 6abf1c55a..ca66f11c9 100644 --- a/data-mapper/src/main/java/com/iluwatar/datamapper/StudentOracleDataMapper.java +++ b/data-mapper/src/main/java/com/iluwatar/datamapper/StudentOracleDataMapper.java @@ -18,19 +18,20 @@ */ package com.iluwatar.datamapper; +import java.util.ArrayList; import java.util.List; import java.util.Optional; public final class StudentOracleDataMapper implements StudentDataMapper { /* Note: Normally this would be in the form of an actual database */ - private List students; + private List students = new ArrayList<>(); @Override public final Optional find(final int studentId) { /* Compare with existing students */ - for (final Student student : this.students) { + for (final Student student : this.getStudents()) { /* Check if student is found */ if (student.getStudentId() == studentId) { @@ -48,13 +49,13 @@ public final class StudentOracleDataMapper implements StudentDataMapper { /* Check with existing students */ - if (this.students.contains(studentToBeUpdated)) { + if (this.getStudents().contains(studentToBeUpdated)) { /* Get the index of student in list */ - final int index = this.students.indexOf(studentToBeUpdated); + final int index = this.getStudents().indexOf(studentToBeUpdated); /* Update the student in list */ - this.students.set(index, studentToBeUpdated); + this.getStudents().set(index, studentToBeUpdated); } else { @@ -67,10 +68,10 @@ public final class StudentOracleDataMapper implements StudentDataMapper { public final void insert(final Student studentToBeInserted) throws DataMapperException { /* Check with existing students */ - if (!this.students.contains(studentToBeInserted)) { + if (!this.getStudents().contains(studentToBeInserted)) { /* Add student in list */ - this.students.add(studentToBeInserted); + this.getStudents().add(studentToBeInserted); } else { @@ -83,10 +84,10 @@ public final class StudentOracleDataMapper implements StudentDataMapper { public final void delete(final Student studentToBeDeleted) throws DataMapperException { /* Check with existing students */ - if (this.students.contains(studentToBeDeleted)) { + if (this.getStudents().contains(studentToBeDeleted)) { /* Delete the student from list */ - this.students.remove(studentToBeDeleted); + this.getStudents().remove(studentToBeDeleted); } else { @@ -94,4 +95,8 @@ public final class StudentOracleDataMapper implements StudentDataMapper { throw new DataMapperException("Student [" + studentToBeDeleted.getName() + "] is not found"); } } + + public List getStudents() { + return this.students; + } } diff --git a/data-mapper/src/test/java/com/iluwatar/datamapper/AppTest.java b/data-mapper/src/test/java/com/iluwatar/datamapper/AppTest.java new file mode 100644 index 000000000..6e415bccb --- /dev/null +++ b/data-mapper/src/test/java/com/iluwatar/datamapper/AppTest.java @@ -0,0 +1,99 @@ +/** + * 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.datamapper; + +import java.util.Optional; +import java.util.UUID; + +import org.apache.log4j.Logger; + +/** + * The Data Mapper (DM) is a layer of software that separates the in-memory objects from the + * database. Its responsibility is to transfer data between the two and also to isolate them from + * each other. With Data Mapper the in-memory objects needn't know even that there's a database + * present; they need no SQL interface code, and certainly no knowledge of the database schema. (The + * database schema is always ignorant of the objects that use it.) Since it's a form of Mapper , + * Data Mapper itself is even unknown to the domain layer. + *

+ * The below example demonstrates basic CRUD operations: select, add, update, and delete. + * + */ +public final class App { + + private static Logger log = Logger.getLogger(App.class); + + + private static final String DB_TYPE_ORACLE = "Oracle"; + private static final String DB_TYPE_MYSQL = "MySQL"; + + + /** + * Program entry point. + * + * @param args command line args. + */ + public static final void main(final String... args) { + + if (log.isInfoEnabled() & args.length > 0) { + log.debug("App.main(), db type: " + args[0]); + } + + StudentDataMapper mapper = null; + + /* Check the desired db type from runtime arguments */ + if (DB_TYPE_ORACLE.equalsIgnoreCase(args[0])) { + + /* Create new data mapper for mysql */ + mapper = new StudentMySQLDataMapper(); + + } else if (DB_TYPE_MYSQL.equalsIgnoreCase(args[0])) { + + /* Create new data mapper for oracle */ + mapper = new StudentMySQLDataMapper(); + } else { + + /* Don't couple any Data Mapper to java.sql.SQLException */ + throw new DataMapperException("Following data source(" + args[0] + ") is not supported"); + } + + /* Create new student */ + Student student = new Student(UUID.randomUUID(), 1, "Adam", 'A'); + + /* Add student in respectibe db */ + mapper.insert(student); + + /* Find this student */ + final Optional studentToBeFound = mapper.find(student.getGuId()); + + if (log.isDebugEnabled()) { + log.debug("App.main(), db find returned : " + studentToBeFound); + } + + /* Update existing student object */ + student = new Student(student.getGuId(), 1, "AdamUpdated", 'A'); + + /* Update student in respectibe db */ + mapper.update(student); + + /* Delete student in db */ + mapper.delete(student); + } + + private App() {} +}