package com.tubeonfire.model;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import com.tubeonfire.entity.Playlist;
public class PlaylistModel {
static PersistenceManager pm = PMF.get().getPersistenceManager();
public static void initPm() {
if (pm.isClosed()) {
pm = PMF.get().getPersistenceManager();
}
}
@SuppressWarnings("unchecked")
public static List<Playlist> all() {
initPm();
List<Playlist> playList = null;
Query query = pm.newQuery(Playlist.class);
query.setFilter("status==1");
query.setRange(0, 10);
playList = (List<Playlist>) query.execute();
return playList;
}
public static boolean add(Playlist playList) {
initPm();
try {
pm.makePersistent(playList);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public static boolean addAll(List<Playlist> playLists) {
initPm();
try {
pm.makePersistentAll(playLists);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public static boolean update(Playlist playList) {
initPm();
pm.makePersistent(playList);
return true;
}
@SuppressWarnings("unchecked")
public static Playlist getById(String id) {
initPm();
Playlist playList = null;
Query query = pm.newQuery(Playlist.class);
query.setFilter("id=='" + id + "' && status==1");
query.setRange(0, 1);
List<Playlist> listResult = (List<Playlist>) query.execute();
if (listResult.size() > 0) {
playList = listResult.get(0);
}
return playList;
}
@SuppressWarnings("unchecked")
public static List<Playlist> getByUserId(String userId) {
initPm();
Query query = pm.newQuery(Playlist.class);
System.out.println("User id : " + userId);
query.setFilter("userFederatedId=='" + userId + "' && status==1");
List<Playlist> listResult = (List<Playlist>) query.execute();
return listResult;
}
@SuppressWarnings("unchecked")
public static boolean isExits(String id) {
initPm();
Query query = pm.newQuery(Playlist.class);
query.setFilter("id==idPara");
query.declareParameters("java.lang.String idPara");
query.setRange(0, 1);
List<Playlist> listResult = (List<Playlist>) query.execute(id);
if (listResult.size() > 0) {
return true;
}
return false;
}
public static void closePM() {
pm.close();
}
}