Commit 0eaaa40ead2dedc1689008900d2bc3a8a0dc6ed5

Authored by Ali B
2 parents 60f887eb 0877448e

Merge branch 'sqlite_ali' into master_application

Risiko/.idea/caches/build_file_checksums.ser
No preview for this file type
Risiko/.idea/misc.xml
... ... @@ -30,20 +30,4 @@
30 30 <component name="ProjectType">
31 31 <option name="id" value="Android" />
32 32 </component>
33   - <component name="masterDetails">
34   - <states>
35   - <state key="ProjectJDKs.UI">
36   - <settings>
37   - <last-edited>1.8</last-edited>
38   - <splitter-proportions>
39   - <option name="proportions">
40   - <list>
41   - <option value="0.2" />
42   - </list>
43   - </option>
44   - </splitter-proportions>
45   - </settings>
46   - </state>
47   - </states>
48   - </component>
49 33 </project>
50 34 \ No newline at end of file
... ...
Risiko/app/src/main/java/database/DatabaseHelper.java 0 → 100644
  1 +package database;
  2 +
  3 +
  4 +import android.content.Context;
  5 +import android.database.sqlite.SQLiteDatabase;
  6 +import android.database.sqlite.SQLiteOpenHelper;
  7 +
  8 +public class DatabaseHelper extends SQLiteOpenHelper {
  9 +
  10 + private static DatabaseHelper instance;
  11 + private static final String TAG = "DatabaseHelper";
  12 + private static final String DATABASE_NAME = "risk_eval.db";
  13 + private static final int DATABASE_VERSION = 1;
  14 +
  15 + private DatabaseHelper(Context context){
  16 + super(context, DATABASE_NAME, null, DATABASE_VERSION);
  17 + }
  18 +
  19 + public static synchronized DatabaseHelper getInstance(Context context){
  20 + if(instance == null){
  21 + instance = new DatabaseHelper(context.getApplicationContext());
  22 + }
  23 + return instance;
  24 + }
  25 +
  26 + @Override
  27 + public void onCreate(SQLiteDatabase db) {
  28 + EvaluationTable.onCreate(db);
  29 + IncidentTable.onCreate(db);
  30 + }
  31 +
  32 + @Override
  33 + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  34 + EvaluationTable.onUpgrade(db, oldVersion, newVersion);
  35 + IncidentTable.onUpgrade(db, oldVersion, newVersion);
  36 + }
  37 +
  38 +}
... ...
Risiko/app/src/main/java/database/DatabaseSource.java 0 → 100644
  1 +package database;
  2 +
  3 +import android.content.ContentValues;
  4 +import android.content.Context;
  5 +import android.database.Cursor;
  6 +import android.database.sqlite.SQLiteDatabase;
  7 +
  8 +import java.util.ArrayList;
  9 +import java.util.Date;
  10 +
  11 +import dataclasses.Event;
  12 +
  13 +public class DatabaseSource {
  14 +
  15 + private DatabaseHelper helper;
  16 +
  17 + public DatabaseSource(Context context){
  18 + this.helper = DatabaseHelper.getInstance(context);
  19 + }
  20 +
  21 + public boolean insertEvaluation (Evaluation evaluation) {
  22 + ContentValues values = new ContentValues();
  23 + values.put(EvaluationTable.EVALUATION_COL_ASSOCIATED_ACTIVITY, evaluation.getAssociatedActivity());
  24 + values.put(EvaluationTable.EVALUATION_COL_TITLE, evaluation.getTitle());
  25 + values.put(EvaluationTable.EVALUATION_COL_OUTCOME, evaluation.getOutcome());
  26 + values.put(EvaluationTable.EVALUATION_COL_ACTION, evaluation.getAction());
  27 + values.put(EvaluationTable.EVALUATION_COL_CONSEQUENCE, evaluation.getConsequence());
  28 + values.put(EvaluationTable.EVALUATION_COL_PROBABILITY, evaluation.getProbability());
  29 +
  30 + SQLiteDatabase database = helper.getWritableDatabase();
  31 + long insertId = database.insert(EvaluationTable.EVALUATION_TABLE, null, values);
  32 + database.close();
  33 +
  34 + return insertId > -1;
  35 + }
  36 +
  37 + public boolean insertIncident(Event incident) {
  38 + ContentValues values = new ContentValues();
  39 + values.put(IncidentTable.INCIDENT_COL_ASSOCIATED_EVALUATION, incident.getAssociatedEvaluation);
  40 + values.put(IncidentTable.INCIDENT_COL_TITLE, incident.getHeadline());
  41 + values.put(IncidentTable.INCIDENT_COL_DATE, incident.getDate().toString());
  42 + values.put(IncidentTable.INCIDENT_COL_LOCATION, incident.getPlaceOfEvent());
  43 + values.put(IncidentTable.INCIDENT_COL_DESCRIPTION, incident.getDescription());
  44 +
  45 + SQLiteDatabase database = helper.getWritableDatabase();
  46 + long insertId = database.insert(IncidentTable.INCIDENT_TABLE, null, values);
  47 + database.close();
  48 +
  49 + return insertId > -1;
  50 + }
  51 +
  52 + public void getAllEvaluation(ArrayList<Evaluation> evaluations) {
  53 + SQLiteDatabase database = helper.getWritableDatabase();
  54 + Cursor cursor = database.rawQuery("select * from " + EvaluationTable.EVALUATION_TABLE, null);
  55 + cursor.moveToFirst();
  56 + evaluations.clear();
  57 + while (!cursor.isAfterLast()) {
  58 + evaluations.add(cursorToEvaluation(cursor));
  59 + cursor.moveToNext();
  60 + }
  61 + cursor.close();
  62 + database.close();
  63 + }
  64 +
  65 + public void getAllIncidents(ArrayList<Event> incidents) {
  66 + SQLiteDatabase database = helper.getWritableDatabase();
  67 + Cursor cursor = database.rawQuery("select * from " + IncidentTable.INCIDENT_TABLE, null);
  68 + cursor.moveToFirst();
  69 + incidents.clear();
  70 + while (!cursor.isAfterLast()) {
  71 + incidents.add(cursorToIncident(cursor));
  72 + cursor.moveToNext();
  73 + }
  74 + cursor.close();
  75 + database.close();
  76 + }
  77 +
  78 + private Evaluation cursorToEvaluation(Cursor cursor) {
  79 + Evaluation data = new Evaluation();
  80 + int idIndex = cursor.getColumnIndexOrThrow(EvaluationTable.EVALUATION_COL_ID);
  81 + int associatedActivityIndex = cursor.getColumnIndexOrThrow(EvaluationTable.EVALUATION_COL_ASSOCIATED_ACTIVITY);
  82 + int titleIndex = cursor.getColumnIndexOrThrow(EvaluationTable.EVALUATION_COL_TITLE);
  83 + int outcomeIndex = cursor.getColumnIndexOrThrow(EvaluationTable.EVALUATION_COL_OUTCOME);
  84 + int actionIndex = cursor.getColumnIndexOrThrow(EvaluationTable.EVALUATION_COL_ACTION);
  85 + int consequenceIndex = cursor.getColumnIndexOrThrow(EvaluationTable.EVALUATION_COL_CONSEQUENCE);
  86 + int probabilityIndex = cursor.getColumnIndexOrThrow(EvaluationTable.EVALUATION_COL_PROBABILITY);
  87 +
  88 + data.setId(cursor.getInt(idIndex));
  89 + data.setAssociatedActivity(cursor.getInt(associatedActivityIndex));
  90 + data.setTitle(cursor.getString(titleIndex));
  91 + data.setOutcome(cursor.getString(outcomeIndex));
  92 + data.setAction(cursor.getString(actionIndex));
  93 + data.setConsequence(cursor.getInt(consequenceIndex));
  94 + data.setPrbability(cursor.getInt(probabilityIndex));
  95 +
  96 + return data;
  97 + }
  98 +
  99 + private Event cursorToIncident(Cursor cursor) {
  100 + Event data = new Event();
  101 + int idIndex = cursor.getColumnIndexOrThrow(IncidentTable.INCIDENT_COL_ID);
  102 + int associatedEvaluationIndex = cursor.getColumnIndexOrThrow(IncidentTable.INCIDENT_COL_ASSOCIATED_EVALUATION);
  103 + int titleIndex = cursor.getColumnIndexOrThrow(IncidentTable.INCIDENT_COL_TITLE);
  104 + int dateIndex = cursor.getColumnIndexOrThrow(IncidentTable.INCIDENT_COL_DATE);
  105 + int locationIndex = cursor.getColumnIndexOrThrow(IncidentTable.INCIDENT_COL_LOCATION);
  106 + int descriptionIndex = cursor.getColumnIndexOrThrow(IncidentTable.INCIDENT_COL_DESCRIPTION);
  107 +
  108 + data.setId(cursor.getInt(idIndex));
  109 + data.setAssociatedEvaluation(cursor.getInt(associatedEvaluationIndex));
  110 + data.setHeadline(cursor.getString(titleIndex));
  111 + data.setDate(cursor.getString(dateIndex));
  112 + data.setPlaceOfEvent(cursor.getString(locationIndex));
  113 + data.setDescription(cursor.getString(descriptionIndex));
  114 +
  115 + return data;
  116 + }
  117 +
  118 +}
... ...
Risiko/app/src/main/java/database/EvaluationTable.java 0 → 100644
  1 +package database;
  2 +
  3 +import android.database.sqlite.SQLiteDatabase;
  4 +
  5 +public class EvaluationTable {
  6 +
  7 + public static final String EVALUATION_TABLE = "EVALUATION";
  8 + public static final String EVALUATION_COL_ID = "ID";
  9 + public static final String EVALUATION_COL_ASSOCIATED_ACTIVITY = "ASSOCIATED_ACTIVITY";
  10 + public static final String EVALUATION_COL_TITLE = "TITLE";
  11 + public static final String EVALUATION_COL_OUTCOME = "OUTCOME";
  12 + public static final String EVALUATION_COL_ACTION = "PREVENTIVE_ACTION";
  13 + public static final String EVALUATION_COL_CONSEQUENCE = "CONSEQUENCE";
  14 + public static final String EVALUATION_COL_PROBABILITY = "PROBABILITY";
  15 +
  16 + private static final String EVALUATION_TABLE_CREATE =
  17 + "CREATE TABLE IF NOT EXISTS " + EVALUATION_TABLE + " ("
  18 + + EVALUATION_COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
  19 + + EVALUATION_COL_ASSOCIATED_ACTIVITY + " INTEGER, "
  20 + + EVALUATION_COL_TITLE + " TEXT, "
  21 + + EVALUATION_COL_OUTCOME + " TEXT, "
  22 + + EVALUATION_COL_ACTION + " TEXT, "
  23 + + EVALUATION_COL_CONSEQUENCE + " INTEGER, "
  24 + + EVALUATION_COL_PROBABILITY + " INTEGER)";
  25 +
  26 + public static void onCreate(SQLiteDatabase database){
  27 + database.execSQL(EVALUATION_TABLE_CREATE);
  28 + }
  29 +
  30 + public static void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion){
  31 + database.execSQL("drop table if exists " + EVALUATION_TABLE);
  32 + onCreate(database);
  33 + }
  34 +
  35 +}
... ...
Risiko/app/src/main/java/database/IncidentTable.java 0 → 100644
  1 +package database;
  2 +
  3 +import android.database.sqlite.SQLiteDatabase;
  4 +
  5 +public class IncidentTable {
  6 +
  7 + public static final String INCIDENT_TABLE = "INCIDENT";
  8 + public static final String INCIDENT_COL_ID = "ID";
  9 + public static final String INCIDENT_COL_ASSOCIATED_EVALUATION = "ASSOCIATED_EVALUATION";
  10 + public static final String INCIDENT_COL_TITLE = "TITLE";
  11 + public static final String INCIDENT_COL_DATE = "DATE";
  12 + public static final String INCIDENT_COL_LOCATION = "LOCATION";
  13 + public static final String INCIDENT_COL_DESCRIPTION = "DESCRIPTION";
  14 +
  15 + private static final String INCIDENT_TABLE_CREATE =
  16 + "CREATE TABLE IF NOT EXISTS " + INCIDENT_TABLE + " ("
  17 + + INCIDENT_COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
  18 + + INCIDENT_COL_TITLE + " TEXT, "
  19 + + INCIDENT_COL_DATE + " TEXT, "
  20 + + INCIDENT_COL_LOCATION + " TEXT, "
  21 + + INCIDENT_COL_DESCRIPTION + " TEXT, "
  22 + + "FOREIGN KEY (" + INCIDENT_COL_ASSOCIATED_EVALUATION + ") REFERENCES " + EvaluationTable.EVALUATION_TABLE + "(" + EvaluationTable.EVALUATION_COL_ID + ")) ";
  23 +
  24 + public static void onCreate(SQLiteDatabase database){
  25 + database.execSQL(INCIDENT_TABLE_CREATE);
  26 + }
  27 +
  28 + public static void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion){
  29 + database.execSQL("drop table if exists " + INCIDENT_TABLE);
  30 + onCreate(database);
  31 + }
  32 +
  33 +}
... ...