Push DataBase et Controller

Classes d'acces aux données, et les controleurs associés
master
Xineroks 8 years ago
parent 4c2c51e20f
commit 5fbd4910e6

@ -1 +1 @@
How To Drink
HTD

@ -0,0 +1,3 @@
<component name="ProjectDictionaryState">
<dictionary name="Administrateur" />
</component>

@ -5,7 +5,7 @@
<GradleProjectSettings>
<option name="distributionType" value="LOCAL" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleHome" value="D:\Android\Android Studio\gradle\gradle-2.10" />
<option name="gradleHome" value="E:\AndroidStudio\gradle\gradle-2.10" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$" />

@ -27,28 +27,6 @@
</value>
</option>
</component>
<component name="ProjectInspectionProfilesVisibleTreeState">
<entry key="Project Default">
<profile-state>
<expanded-state>
<State>
<id />
</State>
<State>
<id>Encapsulation issuesJava</id>
</State>
<State>
<id>Java</id>
</State>
</expanded-state>
<selected-state>
<State>
<id>Android</id>
</State>
</selected-state>
</profile-state>
</entry>
</component>
<component name="ProjectLevelVcsManager" settingsEditedManually="false">
<OptionsSetting value="true" id="Add" />
<OptionsSetting value="true" id="Remove" />

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
<mapping directory="$PROJECT_DIR$" vcs="" />
</component>
</project>

@ -12,6 +12,8 @@
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
#Mon May 30 15:08:46 CEST 2016
systemProp.http.proxyHost=
#Tue May 31 15:30:02 CEST 2016
systemProp.http.proxyPassword=MdppRD\!.1
systemProp.http.proxyHost=10.0.0.248
systemProp.http.proxyUser=d1412ducher
systemProp.http.proxyPort=3128

@ -21,7 +21,6 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:design:23.2.1'
}

@ -8,8 +8,15 @@
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"></activity>
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
>>>>>>> Stashed changes
</manifest>

@ -0,0 +1,43 @@
package htdinc.howtodrink.Controller;
import android.content.Context;
import java.util.ArrayList;
import htdinc.howtodrink.Objects.Bar;
import htdinc.howtodrink.Repository.BarRepository;
/**
* Created by Xineroks on 29/09/2015.
*/
public class BarController {
private BarRepository barRepository;
private static BarController _instance;
private ArrayList<Bar> allBars;
private Context context;
public BarController(Context applicationContext) {
this.barRepository = new BarRepository(applicationContext);
this.allBars = new ArrayList<>();
this.context = applicationContext;
loadAllActions();
}
public static BarController getInstance(Context applicationContext)
{
if (_instance == null)
{
_instance = new BarController(applicationContext);
}
return _instance;
}
public void loadAllActions(){
this.allBars.removeAll(this.allBars);
barRepository.Open();
this.allBars.addAll(/* TODO:a faire */ null);
barRepository.Close();
}
}

@ -0,0 +1,47 @@
package htdinc.howtodrink.Controller;
import android.content.Context;
import java.util.ArrayList;
import htdinc.howtodrink.Objects.Category;
import htdinc.howtodrink.Repository.CategoryRepository;
/**
* Created by Rudy on 31/05/2016.
*/
public class CategoryController {
private CategoryRepository categoryRepository;
private static CategoryController _instance;
ArrayList<Category> allCategories;
private Context context;
public CategoryController(Context applicationContext) {
this.categoryRepository = new CategoryRepository(applicationContext);
this.allCategories = new ArrayList<>();
this.context = applicationContext;
loadAllCategories();
}
public static CategoryController getInstance(Context applicationContext)
{
if (_instance == null)
{
_instance = new CategoryController(applicationContext);
}
return _instance;
}
// TODO : faire une requete par nom de catégorie
public void loadAllCategories(){
this.allCategories.removeAll(this.allCategories);
categoryRepository.Open();
this.allCategories.addAll(categoryRepository.GetAll());
categoryRepository.Close();
}
public String getCatByName(int id){
return this.allCategories.get(id).getNameCategory();
}
}

@ -0,0 +1,151 @@
package htdinc.howtodrink.DataBase;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Locale;
/**
* Created by RUDY on 31/05/2016.
*/
public class DataBaseOpenHelper extends SQLiteOpenHelper{
private static final String DATABASE_NAME = "HTDDatane.sqlite";
private static final int DATABASE_VERSION = 1;
private static final String SP_KEY_DB_VER = "db_ver";
private final Context mContext;
// Nom de la table
public static final String BAR_TABLE_NAME = "BAR";
// Description des colonnes
public static final String COLUMN_IDBAR = "IDBAR";
public static final int NUM_COLUMN_IDBAR = 0;
public static final String COLUMN_NAMEBAR = "NAMEBAR";
public static final int NUM_COLUMN_NAMEBAR = 1;
public static final String COLUMN_NOTEBAR = "NOTEBAR";
public static final int NUM_COLUMN_NOTEBAR = 2;
public static final String COLUMN_ADDRESSBAR = "ADDRESSBAR";
public static final int NUM_COLUMN_ADDRESSBAR = 3;
public static final String COLUMN_GPSLOCATIONLATITUDEBAR = "GPSLOCATIONLATITUDEBAR";
public static final int NUM_COLUMN_GPSLOCATIONLATITUDEBAR = 4;
public static final String COLUMN_GPSLOCATIONLONGITUDEBAR = "GPSLOCATIONLONGITUDEBAR";
public static final int NUM_COLUMN_GPSLOCATIONLONGITUDEBAR = 5;
// Nom de la table
public static final String CATEGORY_TABLE_NAME = "CATEGORY";
// Description des colonnes
public static final String COLUMN_IDCATEGORY = "IDCATEGORY";
public static final int NUM_COLUMN_IDCATEGORY = 0;
public static final String COLUMN_NAMECATEGORY = "NAMECATEGORY";
public static final int NUM_COLUMN_NAMECATEGORY = 1;
public DataBaseOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
mContext = context;
initialize();
}
/**
* Initializes database. Creates database if doesn't exist.
*/
private void initialize() {
if (databaseExists()) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(mContext);
int dbVersion = prefs.getInt(SP_KEY_DB_VER, 1);
if (DATABASE_VERSION != dbVersion) {
File dbFile = mContext.getDatabasePath(DATABASE_NAME);
if (!dbFile.delete()) {
Log.w("Failed", "Unable to update database");
}
}
}
if (!databaseExists()) {
createDatabase();
}
}
/**
* Returns true if database file exists, false otherwise.
* @return
*/
private boolean databaseExists() {
File dbFile = mContext.getDatabasePath(DATABASE_NAME);
return dbFile.exists();
}
/**
* Creates database by copying it from assets directory.
*/
private void createDatabase() {
String parentPath = mContext.getDatabasePath(DATABASE_NAME).getParent();
String path = mContext.getDatabasePath(DATABASE_NAME).getPath();
File file = new File(parentPath);
if (!file.exists()) {
if (!file.mkdir()) {
Log.w("FAILED", "Unable to create database directory");
return;
}
}
InputStream is = null;
OutputStream os = null;
try {
is = mContext.getAssets().open(DATABASE_NAME);
os = new FileOutputStream(path);
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
os.flush();
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(mContext);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(SP_KEY_DB_VER, DATABASE_VERSION);
editor.commit();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion) {
}
} // class

@ -13,18 +13,5 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button bouton = (Button) findViewById(R.id.button);
bouton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v)
{
Context context = getApplicationContext();
CharSequence text = "Ceci est un boutton bite !";
int duration = Toast.LENGTH_SHORT;
Toast.makeText(context,text,duration).show();
}
});
}
}

@ -0,0 +1,82 @@
package htdinc.howtodrink.Objects;
import android.location.Location;
/**
* Created by Rudy on 31/05/2016.
*/
public class Bar {
private int _idBar;
private String _nameBar;
private Float _noteBar;
private String _addressBar;
private Double _gpsLocationLatitudeBar;
private Double _gpsLocationLongitudeBar;
public Bar(int idBar, String nameBar, Float noteBar, String addressBar, Double gpsLocationLatitudeBar, Double gpsLocationLongitudeBar) {
this._idBar = idBar;
this._nameBar = nameBar;
this._noteBar = noteBar;
this._addressBar = addressBar;
this._gpsLocationLatitudeBar = gpsLocationLatitudeBar;
this._gpsLocationLongitudeBar = gpsLocationLongitudeBar;
}
public Bar(int idBar, String nameBar, String addressBar) {
this._idBar = idBar;
this._nameBar = nameBar;
this._addressBar = addressBar;
}
public Bar() {
}
public int getIdBar() {
return _idBar;
}
public void setIdBar(int idBar) {
this._idBar = idBar;
}
public String getNameBar() {
return _nameBar;
}
public void setNameBar(String nameBar) {
this._nameBar = nameBar;
}
public Float getNoteBar() {
return _noteBar;
}
public void setNoteBar(Float noteBar) {
this._noteBar = noteBar;
}
public String getAddressBar() {
return _addressBar;
}
public void setAddressBar(String addressBar) {
this._addressBar = addressBar;
}
public Double getGpsLocationLatitudeBar() {
return _gpsLocationLatitudeBar;
}
public void setGpsLocationLatitudeBar(Double gpsLocationLatitudeBar) {
this._gpsLocationLatitudeBar = gpsLocationLatitudeBar;
}
public Double getGpsLocationLongitudeBar() {
return _gpsLocationLongitudeBar;
}
public void setGpsLocationLongitudeBar(Double gpsLocationLongitudeBar) {
this._gpsLocationLongitudeBar = gpsLocationLongitudeBar;
}
}

@ -0,0 +1,34 @@
package htdinc.howtodrink.Objects;
/**
* Created by Rudy on 31/05/2016.
*/
public class Category {
private int _idCategory;
private String _nameCategory;
public Category(int idCategory, String nameCategory) {
this._idCategory = idCategory;
this._nameCategory = nameCategory;
}
public Category() {
}
public int getIdCategory() {
return _idCategory;
}
public void setIdCategory(int idCategory) {
this._idCategory = idCategory;
}
public String getNameCategory() {
return _nameCategory;
}
public void setNameCategory(String nameCategory) {
this._nameCategory = nameCategory;
}
}

@ -0,0 +1,121 @@
package htdinc.howtodrink.Repository;
import android.content.Context;
import android.database.Cursor;
import android.location.Location;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import htdinc.howtodrink.DataBase.DataBaseOpenHelper;
import htdinc.howtodrink.Objects.Bar;
/**
* Created by Rudy on 31/05/2016.
*/
public class BarRepository extends Repository{
public BarRepository(Context context) {
sqLiteOpenHelper = new DataBaseOpenHelper(context);
}
public List GetAllByName(String nameBar) {
String whereClause = DataBaseOpenHelper.COLUMN_NAMEBAR + " LIKE ?";
Cursor cursor = myDatabase.query(DataBaseOpenHelper.BAR_TABLE_NAME,
new String[]{DataBaseOpenHelper.COLUMN_IDBAR,
DataBaseOpenHelper.COLUMN_NAMEBAR,
DataBaseOpenHelper.COLUMN_NOTEBAR,
DataBaseOpenHelper.COLUMN_ADDRESSBAR,
DataBaseOpenHelper.COLUMN_GPSLOCATIONLATITUDEBAR,
DataBaseOpenHelper.COLUMN_GPSLOCATIONLONGITUDEBAR}, whereClause, new String[]{String.valueOf(nameBar)}, null,
null, null);
return ConvertCursorToListObject(cursor);
}
@Override
public List GetAll() {
Cursor cursor = myDatabase.query(DataBaseOpenHelper.BAR_TABLE_NAME,
new String[]{DataBaseOpenHelper.COLUMN_IDBAR,
DataBaseOpenHelper.COLUMN_NAMEBAR,
DataBaseOpenHelper.COLUMN_NOTEBAR,
DataBaseOpenHelper.COLUMN_ADDRESSBAR,
DataBaseOpenHelper.COLUMN_GPSLOCATIONLATITUDEBAR,
DataBaseOpenHelper.COLUMN_GPSLOCATIONLONGITUDEBAR}, null, null, null,
null, null);
return ConvertCursorToListObject(cursor);
}
@Override
public Object GetById(int id) {
return null;
}
@Override
public void Save(Object entite) {
}
@Override
public void Update(Object obj) {
}
@Override
public void Delete(int id) {
}
@Override
public List ConvertCursorToListObject(Cursor c) {
ArrayList<Bar> liste = new ArrayList<>();
// Si la liste est vide
if (c.getCount() == 0)
return liste;
// position sur le premeir item
c.moveToFirst();
// Pour chaque item
do {
Bar bar = ConvertCursorToObject(c);
liste.add(bar);
} while (c.moveToNext());
// Fermeture du curseur
c.close();
return liste;
}
@Override
public Bar ConvertCursorToObject(Cursor c) {
Bar bar = new Bar(c.getInt(DataBaseOpenHelper.NUM_COLUMN_IDBAR),
c.getString(DataBaseOpenHelper.NUM_COLUMN_NAMEBAR),
c.getFloat(DataBaseOpenHelper.NUM_COLUMN_NOTEBAR),
c.getString(DataBaseOpenHelper.NUM_COLUMN_ADDRESSBAR),
c.getDouble(DataBaseOpenHelper.NUM_COLUMN_GPSLOCATIONLATITUDEBAR),
c.getDouble(DataBaseOpenHelper.NUM_COLUMN_GPSLOCATIONLONGITUDEBAR));
return bar;
}
@Override
public Bar ConvertCursorToOneObject(Cursor c) {
c.moveToFirst();
Bar bar = ConvertCursorToObject(c);
c.close();
return bar;
}
}

@ -0,0 +1,95 @@
package htdinc.howtodrink.Repository;
import android.content.Context;
import android.database.Cursor;
import java.util.ArrayList;
import java.util.List;
import htdinc.howtodrink.DataBase.DataBaseOpenHelper;
import htdinc.howtodrink.Objects.Category;
/**
* Created by Rudy on 31/05/2016.
*/
public class CategoryRepository extends Repository{
public CategoryRepository(Context context) {
sqLiteOpenHelper = new DataBaseOpenHelper(context);
}
@Override
public List GetAll() {
Cursor cursor = myDatabase.query(DataBaseOpenHelper.CATEGORY_TABLE_NAME,
new String[] { DataBaseOpenHelper.COLUMN_IDCATEGORY,
DataBaseOpenHelper.COLUMN_NAMECATEGORY}, null, null, null,
null, null);
return ConvertCursorToListObject(cursor);
}
@Override
public Object GetById(int id) {
return null;
}
@Override
public void Save(Object entite) {
}
@Override
public void Update(Object obj) {
}
@Override
public void Delete(int id) {
}
@Override
public List ConvertCursorToListObject(Cursor c) {
ArrayList<Category> liste = new ArrayList<>();
// Si la liste est vide
if (c.getCount() == 0)
return liste;
// position sur le premeir item
c.moveToFirst();
// Pour chaque item
do {
Category cat = ConvertCursorToObject(c);
liste.add(cat);
} while (c.moveToNext());
// Fermeture du curseur
c.close();
return liste;
}
@Override
public Category ConvertCursorToObject(Cursor c) {
Category category = new Category(c.getInt(DataBaseOpenHelper.NUM_COLUMN_IDCATEGORY),
c.getString(DataBaseOpenHelper.NUM_COLUMN_NAMECATEGORY));
return category;
}
@Override
public Category ConvertCursorToOneObject(Cursor c) {
c.moveToFirst();
Category category = ConvertCursorToObject(c);
c.close();
return category;
}
}

@ -0,0 +1,20 @@
package htdinc.howtodrink.Repository;
import android.database.Cursor;
import java.util.List;
public interface IRepository<T> {
List GetAll();
T GetById(int id);
void Save(T entite);
void Update(T entite);
void Delete(int id);
List ConvertCursorToListObject(Cursor c);
T ConvertCursorToObject(Cursor c);
T ConvertCursorToOneObject(Cursor c);
}

@ -0,0 +1,32 @@
package htdinc.howtodrink.Repository;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public abstract class Repository implements IRepository {
// Base de donnees
protected SQLiteDatabase myDatabase;
protected SQLiteOpenHelper sqLiteOpenHelper;
/**
* Constructeur par defaut
*/
public Repository() {
}
/**
* Ouverture de la connection
*/
public void Open() {
myDatabase = sqLiteOpenHelper.getWritableDatabase();
}
/**
* Fermeture de la connection
*/
public void Close() {
myDatabase.close();
}
}

@ -9,11 +9,5 @@
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="htdinc.howtodrink.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bite"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>

@ -3,4 +3,5 @@
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="fab_margin">16dp</dimen>
</resources>

@ -1,15 +0,0 @@
package htdinc.howtodrink;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* To work on unit tests, switch the Test Artifact in the Build Variants view.
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() throws Exception {
assertEquals(4, 2 + 2);
}
}
Loading…
Cancel
Save