package hirondelle.fish.main.resto;
import java.util.*;
import hirondelle.web4j.model.Id;
import hirondelle.web4j.database.DAOException;
import hirondelle.web4j.database.DuplicateException;
import hirondelle.web4j.util.Util;
import hirondelle.web4j.database.Db;
import static hirondelle.fish.main.resto.RestoAction.LIST_RESTOS;
import static hirondelle.fish.main.resto.RestoAction.FETCH_RESTO;
import static hirondelle.fish.main.resto.RestoAction.CHANGE_RESTO;
import static hirondelle.fish.main.resto.RestoAction.ADD_RESTO;
import static hirondelle.fish.main.resto.RestoAction.DELETE_RESTO;
/** Data Access Object (DAO) for {@link Resto} objects. */
final class RestoDAO {
/** Return a <tt>List</tt> of all {@link Resto} objects. */
List<Resto> list() throws DAOException {
return Db.list(Resto.class, LIST_RESTOS);
}
/** Return a single {@link Resto} identified by its id. */
Resto fetch(Id aRestoId) throws DAOException {
return Db.fetch(Resto.class, FETCH_RESTO, aRestoId);
}
/**
Add a new {@link Resto} to the database.
<P>The name of the restaurant must be unique. If there is a name conflict, then
a {@link DuplicateException} is thrown.
@return the autogenerated database id.
*/
Id add(Resto aResto) throws DAOException, DuplicateException {
Id result = Db.add(ADD_RESTO, baseParamsFrom(aResto));
return result;
}
/** Update an existing {@link Resto}. */
boolean change(Resto aResto) throws DAOException, DuplicateException {
int result = Db.edit(CHANGE_RESTO, Db.addIdTo(baseParamsFrom(aResto), aResto.getId()) );
return Util.isSuccess(result);
}
/**
Delete a {@link Resto}.
<P>If an item is linked to this {@link Resto}, then deletion will fail, and a
{@link DAOException} is thrown.
*/
void delete(Id aRestoId) throws DAOException {
Db.delete(DELETE_RESTO, aRestoId);
}
// PRIVATE //
private Object[] baseParamsFrom(Resto aResto){
return new Object[]{
aResto.getName(), aResto.getLocation(), aResto.getPrice(), aResto.getComment()
};
}
}