Added ability to load all estates

Signed-off-by: Jim Martens <github@2martens.de>
This commit is contained in:
2017-04-19 11:26:05 +02:00
parent b276dea83f
commit acf0d884d2

View File

@ -18,6 +18,7 @@ public class ORM {
private Map<Integer, EstateAgent> _agents;
private Map<String, EstateAgent> _agentsUsername;
private Map<Integer, Estate> _estates;
/**
* Initializes the ORM.
@ -27,6 +28,7 @@ public class ORM {
_connection = _dbManager.getConnection();
_agents = new HashMap<>();
_agentsUsername = new HashMap<>();
_estates = new HashMap<>();
}
/**
@ -48,6 +50,9 @@ public class ORM {
case ESTATEAGENT:
objects = processAgents(rs);
break;
case ESTATE:
objects = processEstates(rs);
break;
}
rs.close();
pstmt.close();
@ -58,15 +63,42 @@ public class ORM {
return objects;
}
/**
* Process a select all query for estates.
*
* @param rs the result set of such a query
* @return a list of estates
* @throws SQLException when an error occurs during the rs.next call
*/
private List<Estate> processEstates(ResultSet rs) throws SQLException {
List<Estate> estates = new ArrayList<>();
while (rs.next()) {
Estate estate = new Estate();
estate.setId(rs.getInt("ID"));
estate.setCity(rs.getString("city"));
estate.setPostalCode(rs.getString("postalCode"));
estate.setStreet(rs.getString("street"));
estate.setStreetNumber(rs.getInt("streetNumber"));
estate.setSquareArea(rs.getInt("squareArea"));
estate.setAgent(rs.getInt("agent"));
_estates.put(estate.getId(), estate);
estates.add(estate);
}
return estates;
}
/**
* Processes a select all query for estate agents.
*
* @param rs the result set of such a query
* @return a list of agents
* @throws SQLException when an error occurs during the rs.next call
*/
private List<EstateAgent> processAgents(ResultSet rs) {
private List<EstateAgent> processAgents(ResultSet rs) throws SQLException {
List<EstateAgent> agents = new ArrayList<>();
try {
while (rs.next()) {
EstateAgent agent = new EstateAgent();
agent.setId(rs.getInt("ID"));
@ -79,9 +111,6 @@ public class ORM {
_agentsUsername.put(agent.getLogin(), agent);
agents.add(agent);
}
} catch (SQLException e) {
e.printStackTrace();
}
return agents;
}