Skip to content

Instantly share code, notes, and snippets.

@bubunyo
Created May 20, 2016 12:24
Show Gist options
  • Save bubunyo/d141fcd35aa23109085c23e6bf606579 to your computer and use it in GitHub Desktop.
Save bubunyo/d141fcd35aa23109085c23e6bf606579 to your computer and use it in GitHub Desktop.
package xyz.chanl.radio.data;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import java.util.List;
import de.greenrobot.dao.DaoMaster;
import de.greenrobot.dao.DaoSession;
import de.greenrobot.dao.Program;
import de.greenrobot.dao.ProgramDao;
import de.greenrobot.dao.Schedule;
import de.greenrobot.dao.ScheduleDao;
import de.greenrobot.dao.Station;
import de.greenrobot.dao.StationDao;
import de.greenrobot.dao.User;
public class DB {
private static final String TAG = DB.class.getSimpleName();
private SQLiteDatabase db;
private DaoSession daoSession;
private static DB dbHelper;
private static final int READ = 1;
private static final int WRITE = 2;
DaoMaster.DevOpenHelper helper;
private DB(Context context) {
helper = new DaoMaster.DevOpenHelper(context, "chanl-db", null);
}
public static void init(Context context) {
dbHelper = new DB(context);
}
public static synchronized DB get() {
if (dbHelper == null)
throw new RuntimeException("You must initialize DBhelper once in the application on create");
else return dbHelper;
}
private void open(int type) {
DaoMaster daoMaster;
if (dbHelper.db != null) {
if (!(dbHelper.db.isOpen() && type == READ && dbHelper.db.isReadOnly())) {
dbHelper.db = dbHelper.helper.getWritableDatabase();
daoMaster = new DaoMaster(dbHelper.db);
dbHelper.daoSession = daoMaster.newSession();
}
} else {
if (type == READ) dbHelper.db = dbHelper.helper.getReadableDatabase();
else dbHelper.db = dbHelper.helper.getWritableDatabase();
daoMaster = new DaoMaster(dbHelper.db);
dbHelper.daoSession = daoMaster.newSession();
}
}
public void save(User user) {
}
public void save(Program program) {
open(WRITE);
ProgramDao programDao = daoSession.getProgramDao();
Program existingProgram = programDao.queryBuilder()
.where(ProgramDao.Properties.Oid.eq(program.getOid())).unique();
program.getStationLocal().save();
program.setStationId(program.getStationLocal().getId());
if (existingProgram != null && program.getOid().equals(existingProgram.getOid())) {
program.setId(existingProgram.getId());
programDao.insertOrReplace(program);
} else
programDao.insert(program);
save(program, program.getSchedulesLocal());
}
public void delete(Program program) {
if (program.getId() != null) {
for (Schedule s : program.getSchedulesLocal())
delete(s);
program.delete();
}
}
public void delete(Schedule schedule) {
if (schedule.getId() != null)
schedule.delete();
}
public long save(Station station) {
open(WRITE);
StationDao stationDao = daoSession.getStationDao();
Station existingStation = stationDao.queryBuilder()
.where(StationDao.Properties.Oid.eq(station.getOid())).unique();
if (existingStation != null && station.getOid().equals(existingStation.getOid())) {
station.setId(existingStation.getId());
return stationDao.insertOrReplace(station);
} else {
return stationDao.insert(station);
}
}
public void save(Program program, List<Schedule> schedules) {
for (Schedule schedule : schedules) {
schedule.setProgram(program);
save(schedule);
}
}
public long save(Schedule schedule) {
open(WRITE);
ScheduleDao scheduleDao = daoSession.getScheduleDao();
return scheduleDao.insertOrReplace(schedule);
}
public List<Program> getPrograms() {
open(READ);
ProgramDao programDao = daoSession.getProgramDao();
return programDao.queryBuilder().list();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment