From 81fab4a99ad8ba936a84de9ec5b6424298324625 Mon Sep 17 00:00:00 2001 From: Jim Martens Date: Mon, 24 Apr 2017 16:17:37 +0200 Subject: [PATCH] Added ability to list persons, houses and apartments Signed-off-by: Jim Martens --- 02/src/de/dis2017/data/db/ORM.java | 116 +++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) diff --git a/02/src/de/dis2017/data/db/ORM.java b/02/src/de/dis2017/data/db/ORM.java index debf17e..1475892 100644 --- a/02/src/de/dis2017/data/db/ORM.java +++ b/02/src/de/dis2017/data/db/ORM.java @@ -59,6 +59,17 @@ public class ORM { case CONTRACT: objects = processContracts(rs); 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(); pstmt.close(); @@ -95,6 +106,88 @@ public class ORM { 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 processHouses(ResultSet rs, List estates) throws SQLException { + List houses = new ArrayList<>(); + Map 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 processApartments(ResultSet rs, List estates) throws SQLException { + List apartments = new ArrayList<>(); + Map 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. * @@ -142,6 +235,29 @@ public class ORM { 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 processPersons(ResultSet rs) throws SQLException { + List 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. *