Commit 82e410f0 authored by Mathieu Féry (Mathius)'s avatar Mathieu Féry (Mathius)
Browse files

Add Doc to api.*.models

parent 01e6fa7a
Pipeline #23322 failed with stages
in 2 minutes and 36 seconds
......@@ -8,18 +8,32 @@ import androidx.annotation.NonNull;
import java.util.List;
/**
* Class used as a basis for our databases
*/
public abstract class BaseDB extends SQLiteOpenHelper {
protected Context ctx;
protected int currentVersion;
protected String dbName;
/**
* Method for generate sql query for creation table
* @param allStringAttrs : List of text fields
* @param intAttrs : List of int fields
* @param tableName : Base table name
* @param versionDatabase : Version of database
* @param primaryKey : Primary key
* @return sqlQuery
*/
@NonNull
public static String getSQL_CREATE_ENTRIES(@NonNull List<String> allStringAttrs,
@NonNull List<String> intAttrs,
@NonNull String tableName,
@NonNull Integer versionTable,
@NonNull Integer versionDatabase,
@NonNull String primaryKey) {
String tempCreation = String.format(
"CREATE TABLE IF NOT EXISTS %s (%s INTEGER PRIMARY KEY,", tableName(tableName,
versionTable), primaryKey);
versionDatabase), primaryKey);
for (String field : allStringAttrs)
tempCreation = String.format("%s%s TEXT,", tempCreation, field);
for (String field : intAttrs)
......@@ -27,11 +41,25 @@ public abstract class BaseDB extends SQLiteOpenHelper {
tempCreation = String.format("%s)", tempCreation.substring(0, tempCreation.length() - 1));
return tempCreation;
}
/**
* Method for generate sql query for drop table
* @param tableName : Base table name
* @param dbVersion : Version of database
* @return sqlQuery
*/
@NonNull
public static String getSQL_DELETE_ENTRIES(@NonNull String tableName,
@NonNull Integer dbVersion) {
return String.format("DROP TABLE IF EXISTS %s", tableName(tableName, dbVersion));
}
/**
* Default Constructor
* @param context : Context for access database
* @param dbName : Name of database
* @param dbVersion : Version of database
*/
public BaseDB(@NonNull Context context, @NonNull String dbName, int dbVersion) {
super(context, dbName, null, dbVersion);
this.ctx = context;
......@@ -40,47 +68,100 @@ public abstract class BaseDB extends SQLiteOpenHelper {
this.onCreate(this.getWritableDatabase(), dbVersion);
}
/**
* Method for upgrade table in database
* @param db : sql connector linked to database
* @param oldVersion : Integer represent old version
* @param newVersion : Integer represent new version
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion, tableName());
}
/**
* Method for generate name of table based to base Table name and version of model
* @param baseTableName : Base name for table
* @param dbVersion : Version of database
* @return name
*/
@NonNull
public static String tableName(@NonNull String baseTableName, int dbVersion) {
return String.format("%s_%d", baseTableName, dbVersion);
}
/**
* Method for generate name of table based to base Table name and version of model
* @return name
*/
@NonNull
public String tableName() {
return tableName(dbName, currentVersion);
}
public abstract void onCreate(@NonNull SQLiteDatabase db, @NonNull Integer dbVersion);
/**
* Method for create table in database
* @param db : sql connector linked to database
* @param allStringAttrs : List of text fields
* @param intAttrs : List of int fields
* @param tableName : Base table name
* @param versionDatabase : Version of database
* @param primaryKey : Primary key
*/
public void onCreate(@NonNull SQLiteDatabase db, @NonNull List<String> allStringAttrs,
@NonNull List<String> intAttrs, @NonNull String tableName,
@NonNull Integer versionTable, @NonNull String primaryKey) {
db.execSQL(getSQL_CREATE_ENTRIES(allStringAttrs, intAttrs, tableName, versionTable,
@NonNull Integer versionDatabase, @NonNull String primaryKey) {
db.execSQL(getSQL_CREATE_ENTRIES(allStringAttrs, intAttrs, tableName, versionDatabase,
primaryKey));
}
/**
* Method for clear content of current Table
*/
public void onClear() {
onClear(this.currentVersion);
}
/**
* Method for clear content of specific version
* @param version : Integer represent version of database
*/
public void onClear(int version) {
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL(getSQL_DELETE_ENTRIES(dbName, version));
onCreate(db, version);
}
/**
* Method for upgrade table in database
* @param db : sql connector linked to database
* @param oldVersion : Integer represent old version
* @param newVersion : Integer represent new version
* @param tableName : Name of table
*/
public void onUpgrade(@NonNull SQLiteDatabase db, int oldVersion, int newVersion,
@NonNull String tableName) {
db.execSQL(getSQL_DELETE_ENTRIES(tableName, oldVersion));
onCreate(db, newVersion);
}
/**
* Method for create table in database
* @param db : sql connector linked to database
*/
@Override
public void onCreate(SQLiteDatabase db) {
onCreate(db, this.currentVersion);
}
/**
* Method for downgrade table in database
* @param db : sql connector linked to database
* @param oldVersion : Integer represent old version
* @param newVersion : Integer represent new version
*/
@Override
public void onDowngrade(@NonNull SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
......
......@@ -7,6 +7,7 @@ import android.database.sqlite.SQLiteDatabase;
import android.provider.BaseColumns;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
......@@ -14,14 +15,31 @@ import java.util.List;
import fr.iutnantes.miar.dndgenerator.api.BaseDB;
import fr.iutnantes.miar.dndgenerator.api.dnd.DnDSaves;
/**
* Class representing the storage of the Saves of a bookmarks.
*/
public class DnDSavesDB extends BaseDB {
/**
* Method for create table in database
* @param db : sql connector linked to database
* @param dbVersion : Version of database
*/
@Override
public void onCreate(@NonNull SQLiteDatabase db, @NonNull Integer dbVersion) {
onCreate(db, allStringAttrs, intAttrs, DATABASE_NAME, dbVersion, PRIMARY_KEY);
}
/**
* Constructor for build database in specific version
* @param context : Context for access database
* @param dbVersion : Version of database
*/
public DnDSavesDB(@NonNull Context context, int dbVersion) {
super(context, DATABASE_NAME, dbVersion);
}
/**
* Constructor for build default database
* @param context : Context for access database
*/
public DnDSavesDB(@NonNull Context context) {
this(context, DATABASE_VERSION);
}
......@@ -33,6 +51,9 @@ public class DnDSavesDB extends BaseDB {
private static List<String> stringAttrs = new ArrayList<>();
private static List<String> allStringAttrs = new ArrayList<>();
private static List<String> listStringAttrs = new ArrayList<>();
/**
* Class represent fields in current model
*/
public static class Vars implements BaseColumns {
public static final String TABLE_NAME = "DnDSaves";
public static final String WANDS = "wands";
......@@ -54,6 +75,12 @@ public class DnDSavesDB extends BaseDB {
allStringAttrs.addAll(stringAttrs);
allStringAttrs.addAll(listStringAttrs);
}
/**
* Method for get saves
* @param id : HashCode of saves
* @return saves
*/
@Nullable
public DnDSaves getDnDSaves(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(tableName(), null, String.format("%s = ?", Vars._ID),
......@@ -70,6 +97,11 @@ public class DnDSavesDB extends BaseDB {
return null;
}
/**
* Method for insert Saves if not already in database
* @param saves : Saves to save
* @return isInserted
*/
public boolean insertDnDSaves(DnDSaves saves) {
if (getDnDSaves(saves.hashCode()) != null)
return false;
......
......@@ -7,6 +7,7 @@ import android.database.sqlite.SQLiteDatabase;
import android.provider.BaseColumns;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
......@@ -22,14 +23,31 @@ import fr.iutnantes.miar.dndgenerator.api.dnd.DnDSystem;
import fr.iutnantes.miar.dndgenerator.api.tenor.GIFObject;
import fr.iutnantes.miar.dndgenerator.api.tenor.models.GIFArtifactDB;
/**
* Class representing the storage of favorites
*/
public class DnDSheetDB extends BaseDB {
/**
* Method for create table in database
* @param db : sql connector linked to database
* @param dbVersion : Version of database
*/
@Override
public void onCreate(@NonNull SQLiteDatabase db, @NonNull Integer dbVersion) {
onCreate(db, allStringAttrs, intAttrs, DATABASE_NAME, dbVersion, PRIMARY_KEY);
}
/**
* Constructor for build database in specific version
* @param context : Context for access database
* @param dbVersion : Version of database
*/
public DnDSheetDB(@NonNull Context context, int dbVersion) {
super(context, DATABASE_NAME, dbVersion);
}
/**
* Constructor for build default database
* @param context : Context for access database
*/
public DnDSheetDB(@NonNull Context context) {
this(context, DATABASE_VERSION);
}
......@@ -42,6 +60,9 @@ public class DnDSheetDB extends BaseDB {
private static List<String> allStringAttrs = new ArrayList<>();
private static List<String> listStringAttrs = new ArrayList<>();
/**
* Class represent fields in current model
*/
public static class Vars implements BaseColumns {
public static final String TABLE_NAME = "DnDSheet";
public static final String HP = "hp";
......@@ -84,6 +105,11 @@ public class DnDSheetDB extends BaseDB {
allStringAttrs.addAll(listStringAttrs);
}
/**
* Method for remove a sheet
* @param sheet : Sheet to remove
* @return isRemoved
*/
public boolean removeDnDSheet(DnDSheet sheet) {
if (getDnDSheet(sheet.hashCode()) == null)
return false;
......@@ -93,6 +119,11 @@ public class DnDSheetDB extends BaseDB {
return true;
}
/**
* Method for update GIF of sheet
* @param sheet : Sheet to update
* @return isUpdated
*/
public boolean updateGIF(DnDSheet sheet) {
if (getDnDSheet(sheet.hashCode()) == null)
return false;
......@@ -106,6 +137,10 @@ public class DnDSheetDB extends BaseDB {
return true;
}
/**
* Method for get all sheets save in database
* @return sheets
*/
public List<DnDSheet> getAllDnDSheet() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(tableName(), null, null, null,
......@@ -117,6 +152,12 @@ public class DnDSheetDB extends BaseDB {
return sheets;
}
/**
* Method for transform record in database to DnDSheet
* @param cursor : Cursor element point a record
* @return sheet
*/
@NonNull
private DnDSheet transformRecordToDnDSheet(Cursor cursor) {
GIFObject gif = GIFObject.getGIFObjectById(ctx, cursor.getString(
cursor.getColumnIndex(Vars.GIF_ID)), new GIFArtifactDB(ctx, currentVersion)
......@@ -165,6 +206,12 @@ public class DnDSheetDB extends BaseDB {
);
}
/**
* Method for get sheet
* @param id : HashCode of sheet
* @return sheet
*/
@Nullable
public DnDSheet getDnDSheet(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(tableName(), null, String.format("%s = ?", Vars._ID),
......@@ -175,6 +222,11 @@ public class DnDSheetDB extends BaseDB {
return null;
}
/**
* Method for insert Sheet if not already in database
* @param sheet : Sheet to save
* @return isInserted
*/
public boolean insertDnDSheet(DnDSheet sheet) {
if (getDnDSheet(sheet.hashCode()) != null)
return false;
......
......@@ -7,6 +7,7 @@ import android.database.sqlite.SQLiteDatabase;
import android.provider.BaseColumns;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
......@@ -16,15 +17,34 @@ import fr.iutnantes.miar.dndgenerator.api.tenor.FilterContent;
import fr.iutnantes.miar.dndgenerator.api.tenor.GIFArtifact;
import fr.iutnantes.miar.dndgenerator.api.tenor.MediaFilter;
/**
* Class representing GIFArtifact storage for bookmarks.
*/
public class GIFArtifactDB extends BaseDB {
/**
* Method for create table in database
* @param db : sql connector linked to database
* @param dbVersion : Version of database
*/
@Override
public void onCreate(@NonNull SQLiteDatabase db, @NonNull Integer dbVersion) {
onCreate(db, allStringAttrs, intAttrs, DATABASE_NAME, dbVersion, PRIMARY_KEY);
}
/**
* Constructor for build database in specific version
* @param context : Context for access database
* @param dbVersion : Version of database
*/
public GIFArtifactDB(@NonNull Context context, int dbVersion) {
super(context, DATABASE_NAME, dbVersion);
}
/**
* Constructor for build default database
* @param context : Context for access database
*/
public GIFArtifactDB(@NonNull Context context) {
this(context, DATABASE_VERSION);
}
......@@ -37,6 +57,9 @@ public class GIFArtifactDB extends BaseDB {
private static List<String> allStringAttrs = new ArrayList<>();
private static List<String> listStringAttrs = new ArrayList<>();
/**
* Class represent fields in current model
*/
public static class Vars implements BaseColumns {
public static final String TABLE_NAME = "GIFArtifact";
public static final String QUERY = "query";
......@@ -54,6 +77,13 @@ public class GIFArtifactDB extends BaseDB {
allStringAttrs.addAll(stringAttrs);
allStringAttrs.addAll(listStringAttrs);
}
/**
* Method for get artifact
* @param id : HashCode of artifact
* @return artifact
*/
@Nullable
public GIFArtifact getGIFArtifact(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(tableName(), null, String.format("%s = ?", Vars._ID),
......@@ -71,6 +101,11 @@ public class GIFArtifactDB extends BaseDB {
return null;
}
/**
* Method for insert artifact if not already in database
* @param artifact : Artifact to save
* @return isInserted
*/
public boolean insertGIFArtifact(GIFArtifact artifact) {
if (getGIFArtifact(artifact.hashCode()) != null)
return false;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment