Skip to content

Commit

Permalink
Merge pull request iluwatar#2 from iismail1997/Issue#1312
Browse files Browse the repository at this point in the history
Edited Source Code based on suggestions made by PMD. No bugs found. JavaDoc added
  • Loading branch information
iismail19 authored Nov 21, 2021
2 parents ae53c27 + 638721c commit fb45c8e
Show file tree
Hide file tree
Showing 9 changed files with 362 additions and 255 deletions.
2 changes: 1 addition & 1 deletion row-data-gateway/ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ requirements, adding database logic to these objects increases the code complexi

## UML for this implementation of Row Data Gateway Pattern

![alt text](./etc/parrotUML.png "SQL CRUD Operations")
![alt text](./etc/ParrotUML.png "SQL CRUD Operations")

## Database (MySQL)

Expand Down
3 changes: 0 additions & 3 deletions row-data-gateway/src/main/java/App.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import ParrotDataModel.OwnedParrot;
import ParrotRowGateWay.OwnedParrotFinder;
import ParrotRowGateWay.OwnedParrotGateWay;
import ParrotRowGateWay.ParrotRegistry;
import ParrotRowGateWay.ParrotTypeRegistry;
import db.DataBaseConnection;

import java.sql.Connection;
import java.sql.SQLException;

public class App {
Expand Down
23 changes: 17 additions & 6 deletions row-data-gateway/src/main/java/ParrotDataModel/OwnedParrot.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,32 @@ public class OwnedParrot {

private Boolean tamed;

/**
* Print out all Parrot information
*
* @throws SQLException
*/
public void printParrotInformation() throws SQLException {
StringBuilder sb = new StringBuilder();
sb.append("My parrot information: \n");
if(ownedParrotId != null)
if (ownedParrotId != null) {
sb.append("ParrotId: " + ownedParrotId + "\n");
if(this.parrotTypeId != null)
}
if (this.parrotTypeId != null) {
sb.append("Species: " + ParrotTypeRegistry.getParrotTypeById(parrotTypeId).getSpecies() + "\n");
if(!StringUtils.isNullOrEmpty(parrotName))
}
if (!StringUtils.isNullOrEmpty(parrotName)) {
sb.append("Name: " + parrotName + "\n");
if(parrotAge != null)
}
if (parrotAge != null) {
sb.append("Age: " + parrotAge + "\n");
if(StringUtils.isNullOrEmpty(color))
}
if (StringUtils.isNullOrEmpty(color)) {
sb.append("Name: " + color + "\n");
if(tamed != null)
}
if (tamed != null) {
sb.append("Tamed: " + BooleanUtils.toStringYesNo(tamed));
}

String parrotInformation = sb.toString();
System.out.println(parrotInformation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
@Setter
public class ParrotType {

/**
* Class constructor
*
* @param parrotTypeId
* @param species
*/
public ParrotType(Integer parrotTypeId, String species) {
this.parrotTypeId = parrotTypeId;
this.species = species;
Expand Down
222 changes: 122 additions & 100 deletions row-data-gateway/src/main/java/ParrotRowGateWay/OwnedParrotFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,105 +11,127 @@

public class OwnedParrotFinder {

private static final String FIND_ALL = "Select * from OwnedParrot";
private static final String FIND_BY_ID = "Select * from OwnedParrot where OwnedParrotId=?";
private static final String COUNT_FROM_OWNED_PARROT = "Select Count(*) as count from OwnedParrot";

public static final String OWNED_PARROT_ID = "OwnedParrotId";
public static final String PARROT_TYPE_ID = "ParrotTypeId";
public static final String PARROT_NAME = "ParrotName";
public static final String PARROT_AGE = "ParrotAge";
public static final String COLOR = "Color";
public static final String TAMED = "Tamed";

private DataBaseConnection db;

public OwnedParrotFinder() {

}

public OwnedParrotFinder(DataBaseConnection db) {
this.db = db;
}

public OwnedParrotGateWay findById(Integer id) throws SQLException {
OwnedParrotGateWay ownedParrotGateWay = ParrotRegistry.getOwnedParrot(id);

if (ownedParrotGateWay != null) {
return ownedParrotGateWay;
}

if (db == null) {
db = new DataBaseConnection();
}
try (Connection connection = db.getConnection()) {
PreparedStatement getStmt;

if (connection != null) {

getStmt = connection.prepareStatement(FIND_BY_ID);
getStmt.setInt(1, id);

ResultSet rs = getStmt.executeQuery();

if (rs != null && rs.next()) {
ownedParrotGateWay = new OwnedParrotGateWay(rs.getInt(OWNED_PARROT_ID),
rs.getInt(PARROT_TYPE_ID),
rs.getString(PARROT_NAME),
rs.getInt(PARROT_AGE),
rs.getString(COLOR),
rs.getBoolean(TAMED));
ParrotRegistry.addOwnedParrot(ownedParrotGateWay);
db.closeConnection(connection);
return ownedParrotGateWay;
}
}
}
return null;
}

public List<OwnedParrotGateWay> findAll() throws SQLException {

if (db == null) {
db = new DataBaseConnection();
}
Connection connection = db.getConnection();
PreparedStatement getSizeStmt = null;
PreparedStatement getAllOwnedParrotsStmt = null;

List<OwnedParrotGateWay> ownedParrotGateWayList = new ArrayList<>();

if (connection != null) {

getSizeStmt = connection.prepareStatement(COUNT_FROM_OWNED_PARROT);
ResultSet rs = getSizeStmt.executeQuery();

if (rs != null && rs.next()) {
int numberOfOwnedParrots = rs.getInt("count");
if (ParrotRegistry.getParrotRegistrySize() == numberOfOwnedParrots) {
ownedParrotGateWayList = ParrotRegistry.getAllOwnedParrotsInRegistry();
} else {
getAllOwnedParrotsStmt = connection.prepareStatement(FIND_ALL);
ResultSet ownedParrotsResult = getAllOwnedParrotsStmt.executeQuery();

if (ownedParrotsResult != null) {
while (ownedParrotsResult.next()) {
OwnedParrotGateWay ownedParrotGateWay = new OwnedParrotGateWay(rs.getInt(OWNED_PARROT_ID),
rs.getInt(PARROT_TYPE_ID),
rs.getString(PARROT_NAME),
rs.getInt(PARROT_AGE),
rs.getString(COLOR),
rs.getBoolean(TAMED));
ParrotRegistry.addOwnedParrot(ownedParrotGateWay);
}
db.closeConnection(connection);
ownedParrotGateWayList = ParrotRegistry.getAllOwnedParrotsInRegistry();
}
}
}
}

return ownedParrotGateWayList;
}
private static final String FIND_ALL = "Select * from OwnedParrot";
private static final String FIND_BY_ID = "Select * from OwnedParrot where OwnedParrotId=?";
private static final String COUNT_FROM_OWNED_PARROT = "Select Count(*) as count from OwnedParrot";

public static final String OWNED_PARROT_ID = "OwnedParrotId";
public static final String PARROT_TYPE_ID = "ParrotTypeId";
public static final String PARROT_NAME = "ParrotName";
public static final String PARROT_AGE = "ParrotAge";
public static final String COLOR = "Color";
public static final String TAMED = "Tamed";

private DataBaseConnection db;

public OwnedParrotFinder() {
}

/**
* Class Constructor with db connection as param
*
* @param db
*/
public OwnedParrotFinder(DataBaseConnection db) {
this.db = db;
}

/**
* Find a OwnedParrot from Database by Id and return a gateway object
*
* @param id
* @return OwnedParrotGateWay
* @throws SQLException
*/
public OwnedParrotGateWay findById(Integer id) throws SQLException {
OwnedParrotGateWay ownedParrotGateWay = ParrotRegistry.getOwnedParrot(id);

if (ownedParrotGateWay != null) {
return ownedParrotGateWay;
}

if (db == null) {
db = new DataBaseConnection();
}
try (Connection connection = db.getConnection()) {
PreparedStatement getStmt;

if (connection != null) {

getStmt = connection.prepareStatement(FIND_BY_ID);
getStmt.setInt(1, id);

ResultSet rs = getStmt.executeQuery();

if (rs != null && rs.next()) {
ownedParrotGateWay = new OwnedParrotGateWay(rs.getInt(OWNED_PARROT_ID),
rs.getInt(PARROT_TYPE_ID),
rs.getString(PARROT_NAME),
rs.getInt(PARROT_AGE),
rs.getString(COLOR),
rs.getBoolean(TAMED));
ParrotRegistry.addOwnedParrot(ownedParrotGateWay);
getStmt.close();
db.closeConnection(connection);
return ownedParrotGateWay;
}
}
}
return null;
}

/**
* Find all entries in the database for OwnedParrot and return a list of gateway objects
*
* @return
* @throws SQLException
*/
public List<OwnedParrotGateWay> findAll() throws SQLException {

if (db == null) {
db = new DataBaseConnection();
}
Connection connection = db.getConnection();
PreparedStatement getSizeStmt = null;
PreparedStatement getAllOwnedParrotsStmt = null;

List<OwnedParrotGateWay> ownedParrotGateWayList = new ArrayList<>();

if (connection != null) {

getSizeStmt = connection.prepareStatement(COUNT_FROM_OWNED_PARROT);
ResultSet rs = getSizeStmt.executeQuery();

if (rs != null && rs.next()) {
int numberOfOwnedParrots = rs.getInt("count");
if (ParrotRegistry.getParrotRegistrySize() == numberOfOwnedParrots) {
ownedParrotGateWayList = ParrotRegistry.getAllOwnedParrotsInRegistry();
} else {
getAllOwnedParrotsStmt = connection.prepareStatement(FIND_ALL);
ResultSet ownedParrotsResult = getAllOwnedParrotsStmt.executeQuery();

if (ownedParrotsResult != null) {
while (ownedParrotsResult.next()) {
OwnedParrotGateWay ownedParrotGateWay = new OwnedParrotGateWay(rs.getInt(OWNED_PARROT_ID),
rs.getInt(PARROT_TYPE_ID),
rs.getString(PARROT_NAME),
rs.getInt(PARROT_AGE),
rs.getString(COLOR),
rs.getBoolean(TAMED));
ParrotRegistry.addOwnedParrot(ownedParrotGateWay);
}
rs.close();
ownedParrotsResult.close();
getSizeStmt.close();
getAllOwnedParrotsStmt.close();
db.closeConnection(connection);
ownedParrotGateWayList = ParrotRegistry.getAllOwnedParrotsInRegistry();
}
}
}
}

return ownedParrotGateWayList;
}

}
Loading

0 comments on commit fb45c8e

Please sign in to comment.