connection = DAO.getDataSource().getConnection();
pstmt = connection.prepareStatement(query);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
AnimalType animal = new AnimalType(rs.getInt("animal_type_id"));
animal.setSpeciesName(rs.getString("species_name"));
animal.setCost(rs.getInt("cost"));
animal.setDescription(rs.getString("description"));
animal.setCategory(rs.getString("category"));
animal.setAvgBiomass(rs.getInt("max_biomass"));
animal.setWaterBiomassLoss(rs.getInt("water_biomass_loss"));
animal.setDietType(rs.getShort("diet_type"));
animal.setWaterNeedFrequency(rs.getInt("water_need_frequency"));
animal.setMetabolism(rs.getFloat("metabolism"));
String[] nodeList = rs.getString("node_list").split(",");
int[] newNodeList = new int[nodeList.length];
HashMap<Integer, Integer> nodeAmountList = new HashMap<Integer, Integer>();
for (int i = 0; i < nodeList.length; i++) {
String[] nodePair = nodeList[i].split(":");
newNodeList[i] = Integer.parseInt(nodePair[0]);
if (nodePair.length == 1) {
nodeAmountList.put(newNodeList[i], 1);
} else {
nodeAmountList.put(newNodeList[i], Integer.parseInt(nodePair[1]));
}
}
animal.setNodeList(newNodeList);
animal.setNodeAmountList(nodeAmountList);
animal.setTrophicLevel(rs.getFloat("trophic_level"));
animal.setModelID(rs.getInt("model_id"));
animal.setMass(rs.getInt("mass"));
animal.setMovtForce(rs.getInt("movt_force"));
animal.setMaxForce(rs.getInt("max_force"));
animal.setHealChance(rs.getFloat("heal_chance"));
animal.setAnimalCategory(rs.getString("animal_category"));
animal.setGroupCapacity(rs.getInt("group_capacity"));
animalList.add(animal);
}
rs.close();
pstmt.close();
HashMap<Integer, List<Integer>> cPredatorMap = CarnivoreDAO.getPreyToPredatorTable();
HashMap<Integer, List<Integer>> cPreyMap = CarnivoreDAO.getPredatorToPreyTable();
HashMap<Integer, List<Integer>> hPreyMap = HerbivoreDAO.getPredatorToPreyTable();
for (AnimalType animal : animalList) {
int animal_id = animal.getID();
if (cPredatorMap.containsKey(animal_id)) {
for (int predator_id : cPredatorMap.get(animal_id)) {
animal.addPredatorID(predator_id, Constants.ORGANISM_TYPE_ANIMAL);
}
}
if (cPreyMap.containsKey(animal_id)) {
for (int prey_id : cPreyMap.get(animal_id)) {
animal.addPreyID(prey_id, Constants.ORGANISM_TYPE_ANIMAL);
}
}
if (hPreyMap.containsKey(animal_id)) {
for (int prey_id : hPreyMap.get(animal_id)) {
animal.addPreyID(prey_id, Constants.ORGANISM_TYPE_PLANT);
}
}
}
} finally {
if (connection != null) {