package model.daoModels;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
import org.unfoldingword.mobile.R;
import java.util.List;
import de.greenrobot.dao.DaoException;
import model.DataFileManager;
import model.UWDatabaseModel;
import model.parsers.MediaType;
import model.parsers.VersionParser;
// 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 "VERSION".
*/
public class Version extends model.UWDatabaseModel implements java.io.Serializable {
private Long id;
private String uniqueSlug;
private String slug;
private String name;
private String statusCheckingEntity;
private String statusCheckingLevel;
private String statusComments;
private String statusContributors;
private String statusPublishDate;
private String statusSourceText;
private String statusSourceTextVersion;
private String statusVersion;
private java.util.Date modified;
private long languageId;
/** Used to resolve relations */
private transient DaoSession daoSession;
/** Used for active entity operations. */
private transient VersionDao myDao;
private Language language;
private Long language__resolvedKey;
private List<Book> books;
// KEEP FIELDS - put your custom fields here
static private final String TAG = "Version";
private int verificationStatus = -1;
// KEEP FIELDS END
public Version() {
}
public Version(Long id) {
this.id = id;
}
public Version(Long id, String uniqueSlug, String slug, String name, String statusCheckingEntity, String statusCheckingLevel, String statusComments, String statusContributors, String statusPublishDate, String statusSourceText, String statusSourceTextVersion, String statusVersion, java.util.Date modified, long languageId) {
this.id = id;
this.uniqueSlug = uniqueSlug;
this.slug = slug;
this.name = name;
this.statusCheckingEntity = statusCheckingEntity;
this.statusCheckingLevel = statusCheckingLevel;
this.statusComments = statusComments;
this.statusContributors = statusContributors;
this.statusPublishDate = statusPublishDate;
this.statusSourceText = statusSourceText;
this.statusSourceTextVersion = statusSourceTextVersion;
this.statusVersion = statusVersion;
this.modified = modified;
this.languageId = languageId;
}
/** called by internal mechanisms, do not call yourself. */
public void __setDaoSession(DaoSession daoSession) {
this.daoSession = daoSession;
myDao = daoSession != null ? daoSession.getVersionDao() : 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 getSlug() {
return slug;
}
public void setSlug(String slug) {
this.slug = slug;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStatusCheckingEntity() {
return statusCheckingEntity;
}
public void setStatusCheckingEntity(String statusCheckingEntity) {
this.statusCheckingEntity = statusCheckingEntity;
}
public String getStatusCheckingLevel() {
return statusCheckingLevel;
}
public void setStatusCheckingLevel(String statusCheckingLevel) {
this.statusCheckingLevel = statusCheckingLevel;
}
public String getStatusComments() {
return statusComments;
}
public void setStatusComments(String statusComments) {
this.statusComments = statusComments;
}
public String getStatusContributors() {
return statusContributors;
}
public void setStatusContributors(String statusContributors) {
this.statusContributors = statusContributors;
}
public String getStatusPublishDate() {
return statusPublishDate;
}
public void setStatusPublishDate(String statusPublishDate) {
this.statusPublishDate = statusPublishDate;
}
public String getStatusSourceText() {
return statusSourceText;
}
public void setStatusSourceText(String statusSourceText) {
this.statusSourceText = statusSourceText;
}
public String getStatusSourceTextVersion() {
return statusSourceTextVersion;
}
public void setStatusSourceTextVersion(String statusSourceTextVersion) {
this.statusSourceTextVersion = statusSourceTextVersion;
}
public String getStatusVersion() {
return statusVersion;
}
public void setStatusVersion(String statusVersion) {
this.statusVersion = statusVersion;
}
public java.util.Date getModified() {
return modified;
}
public void setModified(java.util.Date modified) {
this.modified = modified;
}
public long getLanguageId() {
return languageId;
}
public void setLanguageId(long languageId) {
this.languageId = languageId;
}
/** To-one relationship, resolved on first access. */
public Language getLanguage() {
long __key = this.languageId;
if (language__resolvedKey == null || !language__resolvedKey.equals(__key)) {
if (daoSession == null) {
throw new DaoException("Entity is detached from DAO context");
}
LanguageDao targetDao = daoSession.getLanguageDao();
Language languageNew = targetDao.load(__key);
synchronized (this) {
language = languageNew;
language__resolvedKey = __key;
}
}
return language;
}
public void setLanguage(Language language) {
if (language == null) {
throw new DaoException("To-one property 'languageId' has not-null constraint; cannot set to-one to null");
}
synchronized (this) {
this.language = language;
languageId = language.getId();
language__resolvedKey = languageId;
}
}
/** 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<Book> getBooks() {
if (books == null) {
if (daoSession == null) {
throw new DaoException("Entity is detached from DAO context");
}
BookDao targetDao = daoSession.getBookDao();
List<Book> booksNew = targetDao._queryVersion_Books(id);
synchronized (this) {
if(books == null) {
books = booksNew;
}
}
}
return books;
}
/** Resets a to-many relationship, making the next get call to query for a fresh result. */
public synchronized void resetBooks() {
books = 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 boolean isDownloaded(){
return getBooks().get(0).isDownloaded();
}
public boolean hasAudio(){
for(Book book : getBooks()){
if(book.getAudioBook() != null){
return true;
}
}
return false;
}
public boolean hasVideo(){
return false;
}
//region UWDatabaseModel
@Override
public UWDatabaseModel setupModelFromJson(JSONObject json) {
return null;
}
@Override
public UWDatabaseModel setupModelFromJson(JSONObject json, UWDatabaseModel parent) {
try {
return VersionParser.parseVersion(json, parent);
}
catch (JSONException e){
e.printStackTrace();
return null;
}
}
@Override
public boolean updateWithModel(UWDatabaseModel newModel) {
Version newVersion = (Version) newModel;
this.uniqueSlug = newVersion.uniqueSlug;
this.name = newVersion.name;
this.statusCheckingEntity = newVersion.statusCheckingEntity;
this.statusCheckingLevel = newVersion.statusCheckingLevel;
this.statusComments = newVersion.statusComments;
this.statusContributors = newVersion.statusContributors;
this.statusPublishDate = newVersion.statusPublishDate;
this.statusSourceText = newVersion.statusSourceText;
this.statusSourceTextVersion = newVersion.statusSourceTextVersion;
this.statusVersion = newVersion.statusVersion;
this.languageId = newVersion.languageId;
boolean wasUpdated = (newVersion.modified.compareTo(this.modified) > 0);
this.modified = newVersion.modified;
update();
return wasUpdated;
}
@Override
public void insertModel(DaoSession session) {
session.getVersionDao().insert(this);
this.refresh();
}
//endregion
// region convenience methods
public String getTitle(){
return getSlug().substring(0, 3).toUpperCase() + " (" + getLanguage().getLanguageAbbreviation() + ")";
}
/**
* @param uniqueSlug Slug unique to only one model
* @param session Session to use
* @return Unique model with the passed slug
*/
static public Version getModelForUniqueSlug(String uniqueSlug, DaoSession session){
VersionDao dao = session.getVersionDao();
return dao.queryBuilder()
.where(VersionDao.Properties.UniqueSlug.eq(uniqueSlug))
.unique();
}
/**
* @param id Unique ID of desired Version
* @param session Session to use
* @return Unique model with the passed id
*/
static public Version getVersionForId(long id, DaoSession session){
return session.getVersionDao().queryBuilder()
.where(VersionDao.Properties.Id.eq(id))
.unique();
}
/**
* @param book Current book of which you want the next book
* @return The next Book following the passed book
*/
@Nullable
public Book getNextBook(Book book){
int index = getBooks().indexOf(book);
if(getBooks().size() > (index + 1)){
return getBooks().get(index + 1);
}
else{
return null;
}
}
/**
* @return The verification status of this version
*/
public int getVerificationStatus(){
if(verificationStatus > -1){
return verificationStatus;
}
else {
verificationStatus = 0;
for (Book book : getBooks()) {
int bookStatus = book.getVerificationStatus();
if (bookStatus > verificationStatus) {
verificationStatus = bookStatus;
}
}
}
return verificationStatus;
}
/**
* @return Verification Text for this version
*/
public String getVerificationText(){
String text = "";
for(Book book : getBooks()){
text += book.getVerificationText();
}
return (text.length() == 0)? "All Content is Verified" : text;
}
/**
* @param slug Slug of a book belonging to this version
* @param session Session to use
* @return Book belonging to this Version that has the passed slug
*/
public Book getBookForBookSlug(String slug, DaoSession session){
BookDao dao = session.getBookDao();
return dao.queryBuilder()
.where(BookDao.Properties.VersionId.eq(getId()), BookDao.Properties.Slug.like("%" + slug + "%"))
.unique();
}
/**
* @param session Session to use
* @return List of all Version
*/
static public List<Version> getAllModels(DaoSession session){
return session.getVersionDao().queryBuilder().list();
}
/**
* Deletes all downloaded content
*/
public void deleteContent(Context context){
for(Book book : getBooks()){
book.deleteBookContent(context);
}
DataFileManager.deleteContentForBook(context, this, MediaType.MEDIA_TYPE_TEXT);
if(hasAudio()) {
deleteAudio(context);
}
update();
}
/**
* @param context Context to use
* @return JSONObject with all data necessary for sharing or saving this version
*/
public JSONObject getAsSideLoadJson(Context context){
return VersionParser.getAsSideLoadJson(context, this);
}
//endregion
public void deleteAudio(Context context){
// for(Book book : getBooks()){
// book.deleteAudio(context);
// }
boolean result = DataFileManager.deleteContentForBook(context, this, MediaType.MEDIA_TYPE_AUDIO);
Log.i(TAG, "Result of audio deletion: " + result);
}
public boolean isObs(Context context){
return getLanguage().getProject().getUniqueSlug().equalsIgnoreCase(context.getString(R.string.open_bible_stories_slug));
}
@Nullable
public BibleChapter getFirstBibleChapter() {
List<Book> books = getBooks();
if (books != null && !books.isEmpty()) {
List<BibleChapter> chapters = books.get(0).getBibleChapters(true);
if(chapters != null && !chapters.isEmpty()) {
return chapters.get(0);
}
}
return null;
}
@Override
public String toString() {
return "Version{" +
"uniqueSlug='" + uniqueSlug + '\'' +
", name='" + name + '\'' +
", slug='" + slug + '\'' +
", statusCheckingEntity='" + statusCheckingEntity + '\'' +
", statusCheckingLevel='" + statusCheckingLevel + '\'' +
", statusComments='" + statusComments + '\'' +
", statusContributors='" + statusContributors + '\'' +
", statusPublishDate='" + statusPublishDate + '\'' +
", statusSourceText='" + statusSourceText + '\'' +
", statusSourceTextVersion='" + statusSourceTextVersion + '\'' +
", statusVersion='" + statusVersion + '\'' +
", modified=" + modified +
", id=" + id +
'}';
}
// KEEP METHODS END
}