package de.peacei.gae.foodsupplier.data.dao;
import java.util.List;
import javax.jdo.JDOUserException;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import com.google.inject.Singleton;
import de.peacei.gae.foodsupplier.data.Mensa;
import de.peacei.gae.foodsupplier.data.Weekplan;
import de.peacei.gae.foodsupplier.util.PMF;
@Singleton
public class WeekplanDAO {
public WeekplanDAO() { };
public Weekplan getWeekplanForMensaWeekAndYear(Mensa mensa, int week, int year) {
Weekplan weekplan = null;
PersistenceManager pm = PMF.get().getPersistenceManager();
try {
Query query = pm.newQuery(Weekplan.class, "mensa == m && week == w && year == y");
query.declareParameters("Mensa m, int w, int y");
query.setUnique(true);
weekplan = (Weekplan) query.execute(mensa, week, year);
} catch(Exception e) { }
return weekplan;
}
public void saveWeekplan(Weekplan weekplan) {
PersistenceManager pm = PMF.get().getPersistenceManager();
try {
pm.makePersistent(weekplan);
pm.flush();
} finally {
pm.close();
}
}
@SuppressWarnings("unchecked")
public void deleteWeekplansByWeekAndYear(int week, int year) {
PersistenceManager pm = PMF.get().getPersistenceManager();
Query query = pm.newQuery(Weekplan.class, "week == w && year == y");
query.declareParameters("int w, int y");
try {
List<Weekplan> weekplans = (List<Weekplan>) query.execute(week, year);
pm.deletePersistentAll(weekplans);
pm.flush();
} catch (JDOUserException e) { }
finally {
pm.close();
}
}
@SuppressWarnings("unchecked")
public void deleteWeekplansByMensaWeekAndYear(Mensa mensa, int week, int year) {
PersistenceManager pm = PMF.get().getPersistenceManager();
try {
Query query = pm.newQuery(Weekplan.class, "mensa == m && week == w && year == y");
query.declareParameters("Mensa m, int w, int y");
List<Weekplan> weekplans = (List<Weekplan>) query.execute(mensa, week, year);
pm.deletePersistentAll(weekplans);
pm.flush();
} catch (JDOUserException e) { }
finally {
pm.close();
}
}
}