package hirondelle.fish.exercise.html5;
import hirondelle.web4j.database.DAOException;
import hirondelle.web4j.database.DuplicateException;
import hirondelle.web4j.model.Id;
import hirondelle.web4j.model.ModelCtorException;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
final class PersonDAO {
Collection<Person> list() throws DAOException {
return fPersons.values();
}
public Person fetch(Id aPersonId) {
return fPersons.get(aPersonId);
}
void add(Person aPerson) throws ModelCtorException {
if (! fPersons.containsKey(aPerson.getId())){
++ID_COUNTER;
Id id = Id.from(ID_COUNTER.toString());
Person personWithId = new Person(
id, aPerson.getName(), aPerson.getEmail(), aPerson.getWebsite(), aPerson.getWeight(),
aPerson.getPhone(), aPerson.getColor(), aPerson.getRating(), aPerson.getBorn()
);
fPersons.put(id, personWithId);
}
}
void change(Person aPerson) throws DAOException, DuplicateException {
if(fPersons.containsKey(aPerson.getId())){
fPersons.put(aPerson.getId(), aPerson);
}
}
void delete(Id aPersonId) throws DAOException {
fPersons.remove(aPersonId);
}
private static final Map<Id, Person> fPersons = new LinkedHashMap<Id, Person>();
private static Integer ID_COUNTER = 0;
}