+/* 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;
import android.database.sqlite.SQLiteDatabase;
-import android.util.Log;
import net.fortuna.ical4j.model.Component;
-import net.fortuna.ical4j.model.DateList;
-import net.fortuna.ical4j.model.DateTime;
-import net.fortuna.ical4j.model.Period;
-import net.fortuna.ical4j.model.Recur;
-import net.fortuna.ical4j.model.parameter.Value;
-import net.fortuna.ical4j.model.property.RRule;
-
-import java.lang.reflect.Array;
-import java.text.ParseException;
+
import java.util.ArrayList;
import java.util.Date;
-import java.util.ListIterator;
import de.dhbwloe.campusapp.CampusAppContext;
+import de.dhbwloe.campusapp.coursenames.CourseName;
import de.dhbwloe.campusapp.mensaplan.MensaTagesplan;
import de.dhbwloe.campusapp.news.NewsItem;
import de.dhbwloe.campusapp.search.SearchIndices;
* Created by pk910 on 19.01.2016.
*/
public class DatabaseManager {
- private static final String DATABASE_NAME = "DHBWLoe.CampusApp.DEV";
- private static final int DATABASE_VERSION = 2;
+ private static final String DATABASE_NAME = "DHBWLoe.CampusApp.db";
+ private static final int DATABASE_VERSION = 3; // Datenbank Version - muss bei strukturellen Anpassungen erhöht werden
+
private CampusAppContext AppContext;
private SQLiteDatabase database;
private NewsDatabaseHelper newsDBHelper;
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()) {
}
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, " +
"StaticEntry INT, " +
"UpdateTime INT, " +
"TargetPage TEXT);");
+
+ // Vorlesungsplan
database.execSQL("CREATE TABLE IF NOT EXISTS CourseCalendar " +
"(" +
"Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " +
"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, " +
"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, " +
"PriceSchool INT, " +
"PRIMARY KEY (PlanDate, MenuName)" +
");");
+
+ // News
database.execSQL("CREATE TABLE IF NOT EXISTS News " +
"(" +
"Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " +
}
if(oldVersion < 3 && newVersion >= 3) {
// Version 3
+ database.execSQL("CREATE TABLE IF NOT EXISTS CourseNames " +
+ "(" +
+ "CourseName TEXT, " +
+ "Status TEXT, " +
+ "LastUpdate INT" +
+ ");");
+ }
+ if(oldVersion < 4 && newVersion >= 4) {
+ // Version 4
}
}
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[] {
}
public SearchIndices[] performSearchRequest(String query, int maxResults) {
+ // App Suche
openDatabase();
String[] whereArgs = new String[] {
"%" + query + "%"
}
public void setRuntimeCache(String name, String value) {
+ // Simple cache for runtime options
openDatabase();
long now = (new Date()).getTime() / 1000;
String[] whereArgs = new String[] {
return nfcCardData;
}
+ public CourseName[] getCourseNames() {
+ openDatabase();
+ String value = null;
+ Cursor resultSet;
+ resultSet = database.rawQuery("SELECT CourseName, Status, LastUpdate FROM CourseNames ORDER BY CourseName ASC", null);
+
+ ArrayList<CourseName> courseNames = new ArrayList<CourseName>();
+ if(resultSet.moveToFirst()) {
+ do {
+ CourseName cname = new CourseName(resultSet.getString(0), resultSet.getString(1), resultSet.getLong(2));
+ courseNames.add(cname);
+ } while (resultSet.moveToNext());
+ }
+ resultSet.close();
+ CourseName[] resultsArr = new CourseName[courseNames.size()];
+ resultsArr = courseNames.toArray(resultsArr);
+ return resultsArr;
+ }
+
+ public CourseName getLatestCourseName() {
+ openDatabase();
+ CourseName lastCourse = null;
+ Cursor resultSet;
+ resultSet = database.rawQuery("SELECT CourseName, Status, LastUpdate FROM CourseNames ORDER BY LastUpdate DESC LIMIT 1", null);
+
+ if(resultSet.moveToFirst()) {
+ lastCourse = new CourseName(resultSet.getString(0), resultSet.getString(1), resultSet.getLong(2));
+ }
+ resultSet.close();
+ return lastCourse;
+ }
+
+ public void addCourseName(CourseName courseName) {
+ openDatabase();
+ String[] whereArgs = new String[] {
+ courseName.getName()
+ };
+ Cursor resultSet = database.rawQuery("SELECT CourseName FROM CourseNames WHERE CourseName = ?", whereArgs);
+ if(resultSet.moveToFirst()) {
+ if(courseName.isActiveCourse()) {
+ try {
+ ContentValues updateValues = new ContentValues();
+ updateValues.put("Status", courseName.getStatus());
+ updateValues.put("LastUpdate", courseName.getLastUpdate());
+
+ database.update("CourseNames", updateValues, "CourseName = ?", whereArgs);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ } else {
+ database.delete("CourseNames", "CourseName = ?", whereArgs);
+ }
+ } else if(courseName.isActiveCourse()) {
+ try {
+ ContentValues indexValues = new ContentValues();
+ indexValues.put("CourseName", courseName.getName());
+ indexValues.put("Status", courseName.getStatus());
+ indexValues.put("LastUpdate", courseName.getLastUpdate());
+
+ database.insertOrThrow("CourseNames", null, indexValues);
+ } catch(Exception e) {
+ e.printStackTrace();
+ }
+ }
+ resultSet.close();
+ }
+
+ public boolean haveCourseName(String courseName) {
+ openDatabase();
+ String[] whereArgs = new String[] {
+ courseName
+ };
+ Cursor resultSet = database.rawQuery("SELECT CourseName FROM CourseNames WHERE CourseName = ?", whereArgs);
+ boolean result;
+ if(resultSet.moveToFirst()) {
+ result = true;
+ } else {
+ result = false;
+ }
+ return result;
+ }
+
public void updateCourseCalendar(CourseEvent event, Component cevent) {
openDatabase();
if(vorlesungsplanDBHelper == null)
return vorlesungsplanDBHelper.getCourseCalendarEvents(coursename, timeFrom, timeTo);
}
+ public CourseEvent[] getCourseEventsByGroup(CourseGroup group) {
+ openDatabase();
+ if(vorlesungsplanDBHelper == null)
+ vorlesungsplanDBHelper = new VorlesungsplanDatabaseHelper(AppContext, database);
+ return vorlesungsplanDBHelper.getCourseEventsByGroup(group);
+ }
+
public CourseEvent[] getCourseCalendarTimetable(String coursename, long timeFrom, int days) {
openDatabase();
if(vorlesungsplanDBHelper == null)
return vorlesungsplanDBHelper.getCourseGroups(coursename, notBefore);
}
+ public CourseEvent[] getCourseExamEvents(String coursename, long timeFrom, long timeTo) {
+ openDatabase();
+ if(vorlesungsplanDBHelper == null)
+ vorlesungsplanDBHelper = new VorlesungsplanDatabaseHelper(AppContext, database);
+ return vorlesungsplanDBHelper.getCourseExamEvents(coursename, timeFrom, timeTo);
+ }
+
public void updateMensaTagesplan(MensaTagesplan plan) {
openDatabase();
if(mensaplanDBHelper == null)