Added README.txt and GPL Header to Source Files
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / database / DatabaseManager.java
index 6a515b42a3c97ed0d6676c42928ab1d9bcf85f9a..d042bd0810a3e4b6fc1f121b96deb1b2477ce56b 100644 (file)
@@ -1,5 +1,19 @@
+/* DatabaseManager.java
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
 package de.dhbwloe.campusapp.database;
-
 import android.app.Activity;
 import android.content.ContentValues;
 import android.database.Cursor;
@@ -32,7 +46,8 @@ import de.dhbwloe.campusapp.vorlesungen.CourseGroup;
  */
 public class DatabaseManager {
     private static final String DATABASE_NAME = "DHBWLoe.CampusApp.db";
-    private static final int DATABASE_VERSION = 2;
+    private static final int DATABASE_VERSION = 2; // Datenbank Version - muss bei strukturellen Anpassungen erhöht werden
+
     private CampusAppContext AppContext;
     private SQLiteDatabase database;
     private NewsDatabaseHelper newsDBHelper;
@@ -55,6 +70,7 @@ public class DatabaseManager {
         openDatabase();
         database.execSQL("CREATE TABLE IF NOT EXISTS Version(Version INT);");
 
+        // Abfragen der Datenbank Version
         Cursor resultSet = database.rawQuery("Select * from Version", null);
         int version;
         if(resultSet.moveToFirst()) {
@@ -65,15 +81,12 @@ public class DatabaseManager {
         }
 
         resultSet.close();
-        if(version < DATABASE_VERSION)
+        if(version < DATABASE_VERSION) // Upgrade der Datenbank Struktur (oder auch Initialisierung)
             upgradeTables(version, DATABASE_VERSION);
-
-        //database.execSQL("DELETE FROM CourseCalendar");
-        //database.execSQL("DELETE FROM CourseCalendarEvent");
     }
 
     private void upgradeTables(int oldVersion, int newVersion) {
-        if(oldVersion == 0 && newVersion > 0) {
+        if(oldVersion == 0 && newVersion > 0) { // Initialisierung (noch keine Datenbank)
             database.execSQL("CREATE TABLE IF NOT EXISTS RuntimeCache " +
                     "(" +
                     "Reference TEXT, " +
@@ -90,6 +103,8 @@ public class DatabaseManager {
                     "StaticEntry INT, " +
                     "UpdateTime INT, " +
                     "TargetPage TEXT);");
+
+            // Vorlesungsplan
             database.execSQL("CREATE TABLE IF NOT EXISTS CourseCalendar " +
                     "(" +
                     "Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " +
@@ -114,14 +129,6 @@ public class DatabaseManager {
                     "PRIMARY KEY (EventId, EventFrom, EventTo)" +
                     ");");
             database.execSQL("CREATE INDEX CourseCalendarEventIdx ON CourseCalendarEvent (EventFrom, EventTo);");
-            database.execSQL("CREATE TABLE IF NOT EXISTS NfcCardStore " +
-                    "(" +
-                    "CardId INT, " +
-                    "UpdateTime INT," +
-                    "CardBalance INT, " +
-                    "CardLastTransaction INT, " +
-                    "PRIMARY KEY (CardId, UpdateTime)" +
-                    ");");
             database.execSQL("CREATE TABLE IF NOT EXISTS CourseCalendarGroup " +
                     "(" +
                     "GroupId INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " +
@@ -131,6 +138,18 @@ public class DatabaseManager {
                     "UNIQUE (GroupName)" +
                     ");");
             database.execSQL("CREATE INDEX CourseCalendarGroupIdx ON CourseCalendarGroup (CourseName, GroupName);");
+
+            // Mensa Karten History is this actually used?)
+            database.execSQL("CREATE TABLE IF NOT EXISTS NfcCardStore " +
+                    "(" +
+                    "CardId INT, " +
+                    "UpdateTime INT," +
+                    "CardBalance INT, " +
+                    "CardLastTransaction INT, " +
+                    "PRIMARY KEY (CardId, UpdateTime)" +
+                    ");");
+
+            // Mensaplan
             database.execSQL("CREATE TABLE IF NOT EXISTS MensaPlan " +
                     "(" +
                     "PlanDate INT, " +
@@ -146,6 +165,8 @@ public class DatabaseManager {
                     "PriceSchool INT, " +
                     "PRIMARY KEY (PlanDate, MenuName)" +
                     ");");
+
+            // News
             database.execSQL("CREATE TABLE IF NOT EXISTS News " +
                     "(" +
                     "Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " +
@@ -175,6 +196,7 @@ public class DatabaseManager {
     }
 
     public void addSearchIndices(SearchIndices[] indices) {
+        // Hinzufügen oder Updaten mehrerer SearchIndice Objekte
         openDatabase();
         for(int i = 0; i < indices.length; i++) {
             String[] whereArgs = new String[] {
@@ -209,6 +231,7 @@ public class DatabaseManager {
     }
 
     public SearchIndices[] performSearchRequest(String query, int maxResults) {
+        // App Suche
         openDatabase();
         String[] whereArgs = new String[] {
                 "%" + query + "%"
@@ -243,6 +266,7 @@ public class DatabaseManager {
     }
 
     public void setRuntimeCache(String name, String value) {
+        // Simple cache for runtime options
         openDatabase();
         long now = (new Date()).getTime() / 1000;
         String[] whereArgs = new String[] {