package com.tubeilike.model;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import com.tubeilike.entity.Tube;
public class TubeModel {
static PersistenceManager pm = PMF.get().getPersistenceManager();
public static void initPm() {
if (pm.isClosed()) {
pm = PMF.get().getPersistenceManager();
}
}
public static boolean add(Tube tube) {
initPm();
try {
pm.makePersistent(tube);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public static boolean addAll(List<Tube> listTube) {
initPm();
try {
pm.makePersistentAll(listTube);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public static boolean update(Tube tube) {
initPm();
pm.makePersistent(tube);
return true;
}
@SuppressWarnings("unchecked")
public static Tube getByTubeId(String tubeId) {
initPm();
Tube tube = null;
Query query = pm.newQuery(Tube.class);
query.setFilter("tubeId=='" + tubeId + "' && status==1");
query.setRange(0, 1);
List<Tube> listResult = (List<Tube>) query.execute();
if (listResult.size() > 0) {
tube = listResult.get(0);
}
return tube;
}
public static void closePM() {
pm.close();
}
}