Added ability to list persons, houses and apartments

Signed-off-by: Jim Martens <github@2martens.de>
This commit is contained in:
2017-04-24 16:17:37 +02:00
parent b695bc3657
commit 81fab4a99a

View File

@ -59,6 +59,17 @@ public class ORM {
case CONTRACT: case CONTRACT:
objects = processContracts(rs); objects = processContracts(rs);
break; break;
case PERSON:
objects = processPersons(rs);
break;
case HOUSE:
List<?> estates = getAll(Type.ESTATE);
objects = processHouses(rs, estates);
break;
case APARTMENT:
estates = getAll(Type.ESTATE);
objects = processApartments(rs, estates);
break;
} }
rs.close(); rs.close();
pstmt.close(); pstmt.close();
@ -95,6 +106,88 @@ public class ORM {
return estates; return estates;
} }
/**
* Process a select all query for houses.
*
* @param rs the result set of such a query
* @param estates a list of estates
* @return a list of houses
* @throws SQLException when an error occurs during the rs.next call
*/
private List<House> processHouses(ResultSet rs, List<?> estates) throws SQLException {
List<House> houses = new ArrayList<>();
Map<Integer, House> housesMap = new HashMap<>();
while (rs.next()) {
House house = new House();
house.setId(rs.getInt("ID"));
house.setPrice(rs.getInt("price"));
house.setGarden(rs.getBoolean("garden"));
house.setFloors(rs.getInt("floors"));
houses.add(house);
housesMap.put(house.getId(), house);
}
for (Object o : estates) {
Estate estate = (Estate) o;
if (!housesMap.containsKey(estate.getId())) {
continue;
}
House _house = housesMap.get(estate.getId());
_house.setCity(estate.getCity());
_house.setPostalCode(estate.getPostalCode());
_house.setStreet(estate.getStreet());
_house.setStreetNumber(estate.getStreetNumber());
_house.setSquareArea(estate.getSquareArea());
_house.setAgent(estate.getAgent());
}
return houses;
}
/**
* Process a select all query for houses.
*
* @param rs the result set of such a query
* @param estates a list of estates
* @return a list of houses
* @throws SQLException when an error occurs during the rs.next call
*/
private List<Apartment> processApartments(ResultSet rs, List<?> estates) throws SQLException {
List<Apartment> apartments = new ArrayList<>();
Map<Integer, Apartment> apartmentsMap = new HashMap<>();
while (rs.next()) {
Apartment apartment = new Apartment();
apartment.setId(rs.getInt("ID"));
apartment.setFloor(rs.getInt("floor"));
apartment.setRent(rs.getInt("rent"));
apartment.setRooms(rs.getInt("rooms"));
apartment.setBalcony(rs.getBoolean("balcony"));
apartment.setBuiltinKitchen(rs.getBoolean("builtInKitchen"));
apartments.add(apartment);
apartmentsMap.put(apartment.getId(), apartment);
}
for (Object o : estates) {
Estate estate = (Estate) o;
if (!apartmentsMap.containsKey(estate.getId())) {
continue;
}
Apartment _apartment = apartmentsMap.get(estate.getId());
_apartment.setCity(estate.getCity());
_apartment.setPostalCode(estate.getPostalCode());
_apartment.setStreet(estate.getStreet());
_apartment.setStreetNumber(estate.getStreetNumber());
_apartment.setSquareArea(estate.getSquareArea());
_apartment.setAgent(estate.getAgent());
}
return apartments;
}
/** /**
* Processes a select all query for estate agents. * Processes a select all query for estate agents.
* *
@ -142,6 +235,29 @@ public class ORM {
return contracts; return contracts;
} }
/**
* Process a select all query for persons.
*
* @param rs the result set of such a query
* @return a list of persons
* @throws SQLException when an error occurs during the rs.next call
*/
private List<Person> processPersons(ResultSet rs) throws SQLException {
List<Person> persons = new ArrayList<>();
while (rs.next()) {
Person person = new Person();
person.setId(rs.getInt("ID"));
person.setFirstName(rs.getString("firstName"));
person.setName(rs.getString("name"));
person.setAddress(rs.getString("address"));
persons.add(person);
}
return persons;
}
/** /**
* Loads the contract with the given ID from database and returns the corresponding object. * Loads the contract with the given ID from database and returns the corresponding object.
* *