#352- Unit Of Work : added the launching point of the module
This commit is contained in:
parent
5fe9d2be6c
commit
2c9098aecc
@ -43,4 +43,15 @@ public class Student {
|
|||||||
this.address = address;
|
this.address = address;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAddress() {
|
||||||
|
return address;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,52 @@
|
|||||||
|
/*
|
||||||
|
* The MIT License (MIT)
|
||||||
|
*
|
||||||
|
* Copyright (c) 2017 Piyush Chaudhari
|
||||||
|
*
|
||||||
|
* 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.unitofwork;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link StudentManagementApp} Application for managing student data.
|
||||||
|
*/
|
||||||
|
public class StudentManagementApp {
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param args no argument sent
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Student ram = new Student(1, "Ram", "Street 9, Cupertino");
|
||||||
|
Student shyam = new Student(2, "Shyam", "Z bridge, Pune");
|
||||||
|
Student gopi = new Student(3, "Gopi", "Street 10, Mumbai");
|
||||||
|
|
||||||
|
HashMap<String, List<Student>> context = new HashMap<>();
|
||||||
|
StudentDatabase studentDatabase = new StudentDatabase();
|
||||||
|
StudentRepository studentRepository = new StudentRepository(context, studentDatabase);
|
||||||
|
|
||||||
|
studentRepository.registerNew(ram);
|
||||||
|
studentRepository.registerModified(shyam);
|
||||||
|
studentRepository.registerDeleted(gopi);
|
||||||
|
studentRepository.commit();
|
||||||
|
}
|
||||||
|
}
|
@ -24,6 +24,9 @@
|
|||||||
|
|
||||||
package com.iluwatar.unitofwork;
|
package com.iluwatar.unitofwork;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -33,6 +36,8 @@ import java.util.Map;
|
|||||||
* supports unit of work for student data.
|
* supports unit of work for student data.
|
||||||
*/
|
*/
|
||||||
public class StudentRepository implements IUnitOfWork<Student> {
|
public class StudentRepository implements IUnitOfWork<Student> {
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(StudentRepository.class);
|
||||||
|
|
||||||
private Map<String, List<Student>> context;
|
private Map<String, List<Student>> context;
|
||||||
private StudentDatabase studentDatabase;
|
private StudentDatabase studentDatabase;
|
||||||
|
|
||||||
@ -47,17 +52,20 @@ public class StudentRepository implements IUnitOfWork<Student> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void registerNew(Student student) {
|
public void registerNew(Student student) {
|
||||||
|
LOGGER.info("Registering {} for insert in context.", student.getName());
|
||||||
register(student, IUnitOfWork.INSERT);
|
register(student, IUnitOfWork.INSERT);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void registerModified(Student student) {
|
public void registerModified(Student student) {
|
||||||
|
LOGGER.info("Registering {} for modify in context.", student.getName());
|
||||||
register(student, IUnitOfWork.MODIFY);
|
register(student, IUnitOfWork.MODIFY);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void registerDeleted(Student student) {
|
public void registerDeleted(Student student) {
|
||||||
|
LOGGER.info("Registering {} for delete in context.", student.getName());
|
||||||
register(student, IUnitOfWork.DELETE);
|
register(student, IUnitOfWork.DELETE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,6 +83,7 @@ public class StudentRepository implements IUnitOfWork<Student> {
|
|||||||
if (context == null || context.size() == 0) {
|
if (context == null || context.size() == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
LOGGER.info("Commit started");
|
||||||
if (context.containsKey(IUnitOfWork.INSERT)) {
|
if (context.containsKey(IUnitOfWork.INSERT)) {
|
||||||
commitInsert();
|
commitInsert();
|
||||||
}
|
}
|
||||||
@ -85,26 +94,30 @@ public class StudentRepository implements IUnitOfWork<Student> {
|
|||||||
if (context.containsKey(IUnitOfWork.DELETE)) {
|
if (context.containsKey(IUnitOfWork.DELETE)) {
|
||||||
commitDelete();
|
commitDelete();
|
||||||
}
|
}
|
||||||
|
LOGGER.info("Commit finished.");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void commitDelete() {
|
private void commitInsert() {
|
||||||
List<Student> deletedStudents = context.get(IUnitOfWork.DELETE);
|
List<Student> studentsToBeInserted = context.get(IUnitOfWork.INSERT);
|
||||||
for (Student student : deletedStudents) {
|
for (Student student : studentsToBeInserted) {
|
||||||
studentDatabase.delete(student);
|
LOGGER.info("Saving {} to database.", student.getName());
|
||||||
|
studentDatabase.insert(student);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void commitModify() {
|
private void commitModify() {
|
||||||
List<Student> modifiedStudents = context.get(IUnitOfWork.MODIFY);
|
List<Student> modifiedStudents = context.get(IUnitOfWork.MODIFY);
|
||||||
for (Student student : modifiedStudents) {
|
for (Student student : modifiedStudents) {
|
||||||
|
LOGGER.info("Modifying {} to database.", student.getName());
|
||||||
studentDatabase.modify(student);
|
studentDatabase.modify(student);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void commitInsert() {
|
private void commitDelete() {
|
||||||
List<Student> studentsToBeInserted = context.get(IUnitOfWork.INSERT);
|
List<Student> deletedStudents = context.get(IUnitOfWork.DELETE);
|
||||||
for (Student student : studentsToBeInserted) {
|
for (Student student : deletedStudents) {
|
||||||
studentDatabase.insert(student);
|
LOGGER.info("Deleting {} to database.", student.getName());
|
||||||
|
studentDatabase.delete(student);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user