Bugfixes for persist(Person) and isApartment()

Bugfixes for persist(Person) and isApartment()
This commit is contained in:
Togepy95
2017-04-22 12:43:24 +02:00
parent 5de082b7f5
commit 5151567386
3 changed files with 30 additions and 4 deletions

View File

@ -167,7 +167,7 @@ public class Main {
person.setName(FormUtil.readString("Name")); person.setName(FormUtil.readString("Name"));
person.setAddress(FormUtil.readString("Address")); person.setAddress(FormUtil.readString("Address"));
//_orm.persist(person); _orm.persist(person);
System.out.println("Person with the Name " + person.getFirstName() +" "+person.getName() + " was created."); System.out.println("Person with the Name " + person.getFirstName() +" "+person.getName() + " was created.");
} }

View File

@ -4,7 +4,7 @@ public class Person {
private String firstName; private String firstName;
private String name; private String name;
private String address; private String address;
private int id; private int id=-1;
public int getId() { public int getId() {

View File

@ -25,6 +25,7 @@ public class ORM {
private Map<String, EstateAgent> _agentsUsername; private Map<String, EstateAgent> _agentsUsername;
private Map<Integer, Estate> _estates; private Map<Integer, Estate> _estates;
private Map<Integer, Person> _persons; private Map<Integer, Person> _persons;
private Map<Integer, Contract> _contracts;
/** /**
* Initializes the ORM. * Initializes the ORM.
@ -35,6 +36,8 @@ public class ORM {
_agents = new HashMap<>(); _agents = new HashMap<>();
_agentsUsername = new HashMap<>(); _agentsUsername = new HashMap<>();
_estates = new HashMap<>(); _estates = new HashMap<>();
_persons = new HashMap<>();
_contracts = new HashMap<>();
} }
/** /**
@ -597,6 +600,7 @@ public class ORM {
*/ */
public void persist(Person person) public void persist(Person person)
{ {
System.out.println(person.getId());
boolean changeFinished = false; boolean changeFinished = false;
try { try {
_connection.setAutoCommit(false); _connection.setAutoCommit(false);
@ -660,7 +664,29 @@ public class ORM {
* @return true if apartment, false if house * @return true if apartment, false if house
*/ */
public boolean isApartment(int estateID){ public boolean isApartment(int estateID){
//TODO check in the DB if the Estate with the ID is an Apartment then return true. If it is a house return false. if(_estates.containsKey(estateID) && _estates.get(estateID) instanceof Apartment)
return true; return true;
boolean isApartment = false;
try {
_connection.setAutoCommit(false);
String insertSQL = "SELECT ID FROM INTO APARTMENT WHERE ID=?";
PreparedStatement pstmt = _connection.prepareStatement(insertSQL, Statement.RETURN_GENERATED_KEYS);
pstmt.setInt(1, estateID);
ResultSet result = pstmt.executeQuery();
if(result.first())isApartment=true;
result.close();
pstmt.close();
_connection.setAutoCommit(true);
} catch (SQLException e) {
try {
_connection.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
return isApartment;
} }
} }