Commit 0877448e7fe316bf1307ddcd93dca69f5027d254

Authored by Ali B
1 parent cd74ff65

Completed sqlite tables and database source so it now is ready to be used (not tested yet though)

Risiko/.idea/misc.xml
... ... @@ -24,7 +24,7 @@
24 24 </value>
25 25 </option>
26 26 </component>
27   - <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK">
  27 + <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
28 28 <output url="file://$PROJECT_DIR$/build/classes" />
29 29 </component>
30 30 <component name="ProjectType">
... ...
Risiko/app/src/main/java/database/DatabaseSource.java
1 1 package database;
2 2  
  3 +import android.content.ContentValues;
3 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;
4 12  
5 13 public class DatabaseSource {
6 14  
... ... @@ -10,4 +18,101 @@ public class DatabaseSource {
10 18 this.helper = DatabaseHelper.getInstance(context);
11 19 }
12 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 +
13 118 }
... ...
Risiko/app/src/main/java/database/EvaluationTable.java
... ... @@ -15,14 +15,13 @@ public class EvaluationTable {
15 15  
16 16 private static final String EVALUATION_TABLE_CREATE =
17 17 "CREATE TABLE IF NOT EXISTS " + EVALUATION_TABLE + " ("
18   - + EVALUATION_COL_ID + " INTEGER, "
  18 + + EVALUATION_COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
19 19 + EVALUATION_COL_ASSOCIATED_ACTIVITY + " INTEGER, "
20 20 + EVALUATION_COL_TITLE + " TEXT, "
21 21 + EVALUATION_COL_OUTCOME + " TEXT, "
22 22 + EVALUATION_COL_ACTION + " TEXT, "
23 23 + EVALUATION_COL_CONSEQUENCE + " INTEGER, "
24   - + EVALUATION_COL_PROBABILITY + " INTEGER, "
25   - + "PRIMARY KEY (" + EVALUATION_COL_ID + ")) ";
  24 + + EVALUATION_COL_PROBABILITY + " INTEGER)";
26 25  
27 26 public static void onCreate(SQLiteDatabase database){
28 27 database.execSQL(EVALUATION_TABLE_CREATE);
... ...
Risiko/app/src/main/java/database/IncidentTable.java
... ... @@ -8,21 +8,18 @@ public class IncidentTable {
8 8 public static final String INCIDENT_COL_ID = "ID";
9 9 public static final String INCIDENT_COL_ASSOCIATED_EVALUATION = "ASSOCIATED_EVALUATION";
10 10 public static final String INCIDENT_COL_TITLE = "TITLE";
11   - public static final String INCIDENT_COL_OUTCOME = "OUTCOME";
12   - public static final String INCIDENT_COL_ACTION = "PREVENTIVE_ACTION";
13   - public static final String INCIDENT_COL_CONSEQUENCE = "CONSEQUENCE";
14   - public static final String INCIDENT_COL_PROBABILITY = "PROBABILITY";
  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";
15 14  
16 15 private static final String INCIDENT_TABLE_CREATE =
17 16 "CREATE TABLE IF NOT EXISTS " + INCIDENT_TABLE + " ("
18   - + INCIDENT_COL_ID + " INTEGER, "
  17 + + INCIDENT_COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
19 18 + INCIDENT_COL_TITLE + " TEXT, "
20   - + INCIDENT_COL_OUTCOME + " TEXT, "
21   - + INCIDENT_COL_ACTION + " TEXT, "
22   - + INCIDENT_COL_CONSEQUENCE + " INTEGER, "
23   - + INCIDENT_COL_PROBABILITY + " INTEGER, "
24   - + "FOREIGN KEY (" + INCIDENT_COL_ASSOCIATED_EVALUATION + ") REFERENCES " + EvaluationTable.EVALUATION_TABLE + "(" + EvaluationTable.EVALUATION_COL_ID + "),"
25   - + "PRIMARY KEY (" + INCIDENT_COL_ID + ")) ";
  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 + ")) ";
26 23  
27 24 public static void onCreate(SQLiteDatabase database){
28 25 database.execSQL(INCIDENT_TABLE_CREATE);
... ...