From c5caf9681d5795488356ed3b7d2f64634b913c3c Mon Sep 17 00:00:00 2001 From: Panagiotis Kompis Date: Sat, 28 May 2016 15:58:59 +0300 Subject: [PATCH 1/3] Update HabitsCSVExporter.java --- .../org/isoron/uhabits/io/HabitsCSVExporter.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/src/main/java/org/isoron/uhabits/io/HabitsCSVExporter.java b/app/src/main/java/org/isoron/uhabits/io/HabitsCSVExporter.java index f84ad666c..9eb711982 100644 --- a/app/src/main/java/org/isoron/uhabits/io/HabitsCSVExporter.java +++ b/app/src/main/java/org/isoron/uhabits/io/HabitsCSVExporter.java @@ -60,6 +60,18 @@ public class HabitsCSVExporter FileWriter out = new FileWriter(exportDirName + filename); generateFilenames.add(filename); Habit.writeCSV(habits, out); + + filename = "AllCheckmarks.csv"; + new File(exportDirName).mkdirs(); + out = new FileWriter(exportDirName + filename); + generateFilenames.add(filename); + CheckmarkList.writeCSV(habits, out); + + filename = "AllScores.csv"; + new File(exportDirName).mkdirs(); + out = new FileWriter(exportDirName + filename); + generateFilenames.add(filename); + ScoreList.writeCSV(habits, out); out.close(); for(Habit h : habits) From a88ea4740f021b080a3223511c67d66acb3d6293 Mon Sep 17 00:00:00 2001 From: Panagiotis Kompis Date: Sat, 28 May 2016 16:00:18 +0300 Subject: [PATCH 2/3] Update CheckmarkList.java --- .../isoron/uhabits/models/CheckmarkList.java | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/app/src/main/java/org/isoron/uhabits/models/CheckmarkList.java b/app/src/main/java/org/isoron/uhabits/models/CheckmarkList.java index 80b565c9c..9290da0cd 100644 --- a/app/src/main/java/org/isoron/uhabits/models/CheckmarkList.java +++ b/app/src/main/java/org/isoron/uhabits/models/CheckmarkList.java @@ -36,7 +36,9 @@ import java.io.IOException; import java.io.Writer; import java.text.SimpleDateFormat; import java.util.Date; +import java.util.HashMap; import java.util.List; +import java.util.Map; public class CheckmarkList { @@ -299,4 +301,67 @@ public class CheckmarkList cursor.close(); out.close(); } + + /** + * Writes the checkmark of all habits for each date to the given writer, in CSV format. There is one + * line for each date. Each line contains two fields: timestamp and checkmark value. + * + * @param habits the list of habits to write + * @param out the writer where the CSV will be output + * @throws IOException in case write operations fail + */ + public static void writeCSV(List habits, Writer out) throws IOException { + SimpleDateFormat dateFormat = DateHelper.getCSVDateFormat(); + String query = "select DISTINCT timestamp from checkmarks order by timestamp"; + SQLiteDatabase db = Cache.openDatabase(); + Cursor cursor = db.rawQuery(query, null); + + if (!cursor.moveToFirst()) return; + + out.write(String.format("%s", "Date")); + for (Habit habit : habits) { + out.write(String.format(",%s", habit.name)); + } + Map values; + do { + String timestamp = dateFormat.format(new Date(cursor.getLong(0))); + out.write(String.format("\n%s", timestamp)); + values = getValuesPerTimestamp(Long.toString(cursor.getLong(0))); + int i =0; + for (Habit habit : habits) { + if(values.containsKey(habit.getId().toString())) { + out.write(String.format(",%s", values.get(habit.getId().toString()))); + } else if(++i == habits.size()) { + out.write(String.format("")); + } else { + out.write(String.format(",")); + } + i++; + } + + } while (cursor.moveToNext()); + cursor.close(); + out.close(); + } + + /** + * Returns Map with habits and their value for a given timestamp. + * + * @param timestamp the timestamp where the values must me returned + * @return Map with habits and their score + */ + public static Map getValuesPerTimestamp(String timestamp) { + String query = "select habit, value from checkmarks where timestamp = ? order by habit"; + SQLiteDatabase db = Cache.openDatabase(); + String args[] = {timestamp}; + Cursor cursor = db.rawQuery(query, args); + + if (!cursor.moveToFirst()) return null; + + Map values = new HashMap(); + do { + values.put(cursor.getString(0), cursor.getInt(1)); + } while (cursor.moveToNext()); + return values; + } } From de5a3434dcedf43427255e8cfb2b82c9c4f14b71 Mon Sep 17 00:00:00 2001 From: Panagiotis Kompis Date: Sat, 28 May 2016 16:01:10 +0300 Subject: [PATCH 3/3] Update ScoreList.java --- .../org/isoron/uhabits/models/ScoreList.java | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/app/src/main/java/org/isoron/uhabits/models/ScoreList.java b/app/src/main/java/org/isoron/uhabits/models/ScoreList.java index eca89b9e0..7af265a27 100644 --- a/app/src/main/java/org/isoron/uhabits/models/ScoreList.java +++ b/app/src/main/java/org/isoron/uhabits/models/ScoreList.java @@ -39,6 +39,9 @@ import java.io.IOException; import java.io.Writer; import java.text.SimpleDateFormat; import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; public class ScoreList { @@ -342,4 +345,67 @@ public class ScoreList cursor.close(); out.close(); } + + /** + * Writes the score of all habits for each date to the given writer, in CSV format. There is one + * line for each date. Each line contains two fields: timestamp and score. + * + * @param habits the list of habits to write + * @param out the writer where the CSV will be output + * @throws IOException in case write operations fail + */ + public static void writeCSV(List habits, Writer out) throws IOException { + SimpleDateFormat dateFormat = DateHelper.getCSVDateFormat(); + String query = "select DISTINCT timestamp from score order by timestamp"; + SQLiteDatabase db = Cache.openDatabase(); + Cursor cursor = db.rawQuery(query, null); + + if (!cursor.moveToFirst()) return; + + out.write(String.format("%s", "Date")); + for (Habit habit : habits) { + out.write(String.format(",%s", habit.name)); + } + Map scores; + do { + String timestamp = dateFormat.format(new Date(cursor.getLong(0))); + out.write(String.format("\n%s", timestamp)); + scores = getValuesPerTimestamp(Long.toString(cursor.getLong(0))); + int i =0; + for (Habit habit : habits) { + if(scores.containsKey(habit.getId().toString())) { + out.write(String.format(",%s", scores.get(habit.getId().toString()))); + } else if(++i == habits.size()) { + out.write(String.format("")); + } else { + out.write(String.format(",")); + } + i++; + } + + } while (cursor.moveToNext()); + cursor.close(); + out.close(); + } + + /** + * Returns Map with habits and their score for a given timestamp. + * + * @param timestamp the timestamp where the values must me returned + * @return Map with habits and their score + */ + public static Map getValuesPerTimestamp(String timestamp) { + String query = "select habit, score from Score where timestamp = ? order by habit"; + SQLiteDatabase db = Cache.openDatabase(); + String args[] = {timestamp}; + Cursor cursor = db.rawQuery(query, args); + + if (!cursor.moveToFirst()) return null; + + Map scores = new HashMap(); + do { + scores.put(cursor.getString(0), String.format("%.4f", ((float) cursor.getLong(1)) / Score.MAX_VALUE)); + } while (cursor.moveToNext()); + return scores; + } }