package model.daoModels;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.List;
import de.greenrobot.dao.DaoException;
import model.AudioBitrate;
import model.UWDatabaseModel;
import model.parsers.AudioBookParser;
// THIS CODE IS GENERATED BY greenDAO, EDIT ONLY INSIDE THE "KEEP"-SECTIONS
// KEEP INCLUDES - put your custom includes here
// KEEP INCLUDES END
/**
* Entity mapped to table "AUDIO_BOOK".
*/
public class AudioBook extends model.UWDatabaseModel implements java.io.Serializable {
private Long id;
private String uniqueSlug;
private String contributors;
private String revision;
private String textVersion;
private long bookId;
/** Used to resolve relations */
private transient DaoSession daoSession;
/** Used for active entity operations. */
private transient AudioBookDao myDao;
private Book book;
private Long book__resolvedKey;
private List<AudioChapter> audioChapters;
// KEEP FIELDS - put your custom fields here
// KEEP FIELDS END
public AudioBook() {
}
public AudioBook(Long id) {
this.id = id;
}
public AudioBook(Long id, String uniqueSlug, String contributors, String revision, String textVersion, long bookId) {
this.id = id;
this.uniqueSlug = uniqueSlug;
this.contributors = contributors;
this.revision = revision;
this.textVersion = textVersion;
this.bookId = bookId;
}
/** called by internal mechanisms, do not call yourself. */
public void __setDaoSession(DaoSession daoSession) {
this.daoSession = daoSession;
myDao = daoSession != null ? daoSession.getAudioBookDao() : null;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUniqueSlug() {
return uniqueSlug;
}
public void setUniqueSlug(String uniqueSlug) {
this.uniqueSlug = uniqueSlug;
}
public String getContributors() {
return contributors;
}
public void setContributors(String contributors) {
this.contributors = contributors;
}
public String getRevision() {
return revision;
}
public void setRevision(String revision) {
this.revision = revision;
}
public String getTextVersion() {
return textVersion;
}
public void setTextVersion(String textVersion) {
this.textVersion = textVersion;
}
public long getBookId() {
return bookId;
}
public void setBookId(long bookId) {
this.bookId = bookId;
}
/** To-one relationship, resolved on first access. */
public Book getBook() {
long __key = this.bookId;
if (book__resolvedKey == null || !book__resolvedKey.equals(__key)) {
if (daoSession == null) {
throw new DaoException("Entity is detached from DAO context");
}
BookDao targetDao = daoSession.getBookDao();
Book bookNew = targetDao.load(__key);
synchronized (this) {
book = bookNew;
book__resolvedKey = __key;
}
}
return book;
}
public void setBook(Book book) {
if (book == null) {
throw new DaoException("To-one property 'bookId' has not-null constraint; cannot set to-one to null");
}
synchronized (this) {
this.book = book;
bookId = book.getId();
book__resolvedKey = bookId;
}
}
/** To-many relationship, resolved on first access (and after reset). Changes to to-many relations are not persisted, make changes to the target entity. */
public List<AudioChapter> getAudioChapters() {
if (audioChapters == null) {
if (daoSession == null) {
throw new DaoException("Entity is detached from DAO context");
}
AudioChapterDao targetDao = daoSession.getAudioChapterDao();
List<AudioChapter> audioChaptersNew = targetDao._queryAudioBook_AudioChapters(id);
synchronized (this) {
if(audioChapters == null) {
audioChapters = audioChaptersNew;
}
}
}
return audioChapters;
}
/** Resets a to-many relationship, making the next get call to query for a fresh result. */
public synchronized void resetAudioChapters() {
audioChapters = null;
}
/** Convenient call for {@link AbstractDao#delete(Object)}. Entity must attached to an entity context. */
public void delete() {
if (myDao == null) {
throw new DaoException("Entity is detached from DAO context");
}
myDao.delete(this);
}
/** Convenient call for {@link AbstractDao#update(Object)}. Entity must attached to an entity context. */
public void update() {
if (myDao == null) {
throw new DaoException("Entity is detached from DAO context");
}
myDao.update(this);
}
/** Convenient call for {@link AbstractDao#refresh(Object)}. Entity must attached to an entity context. */
public void refresh() {
if (myDao == null) {
throw new DaoException("Entity is detached from DAO context");
}
myDao.refresh(this);
}
// KEEP METHODS - put your custom methods here
public AudioChapter getChapter(int chapterNumber){
if(getAudioChapters() != null) {
AudioChapterDao dao = daoSession.getAudioChapterDao();
return dao.queryBuilder()
.where(AudioChapterDao.Properties.AudioBookId.eq(this.getId()), AudioChapterDao.Properties.Chapter.eq(chapterNumber))
.unique();
}
else{
return null;
}
}
@Override
public UWDatabaseModel setupModelFromJson(JSONObject json) {
return null;
}
@Override
public UWDatabaseModel setupModelFromJson(JSONObject json, UWDatabaseModel parent) {
try{
return AudioBookParser.parseAudioBook(json, (Book) parent);
}
catch (JSONException e){
e.printStackTrace();
return null;
}
}
@Override
public boolean updateWithModel(UWDatabaseModel newModel) {
AudioBook newAudioBook = (AudioBook) newModel;
this.uniqueSlug = newAudioBook.uniqueSlug;
this.contributors = newAudioBook.contributors;
this.revision = newAudioBook.revision;
this.textVersion = newAudioBook.textVersion;
return true;
}
@Override
public void insertModel(DaoSession session) {
session.getAudioBookDao().insert(this);
this.refresh();
}
/**
* @param uniqueSlug Slug that is unique to only one model
* @param session Session to use
* @return Unique Model with the passed slug
*/
static public AudioBook getModelForUniqueSlug(String uniqueSlug, DaoSession session){
AudioBookDao dao = session.getAudioBookDao();
return dao.queryBuilder()
.where(AudioBookDao.Properties.UniqueSlug.eq(uniqueSlug))
.unique();
}
public long getAudioSize(AudioBitrate bitRate){
long size = 0;
for(AudioChapter chapter : getAudioChapters()){
for(AudioBitrate individualBitrate : chapter.getBitRates()){
if(individualBitrate.getBitrate() == bitRate.getBitrate()){
size += individualBitrate.getSize();
break;
}
}
}
return size;
}
// KEEP METHODS END
}