From 0d523ac74ba972cfc1784ed7865a08bba70e36c4 Mon Sep 17 00:00:00 2001 From: Panagiotis Kompis Date: Wed, 25 May 2016 21:31:02 +0300 Subject: [PATCH] Update ScoreList.java --- .../org/isoron/uhabits/models/ScoreList.java | 63 ++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) 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..a593128c0 100644 --- a/app/src/main/java/org/isoron/uhabits/models/ScoreList.java +++ b/app/src/main/java/org/isoron/uhabits/models/ScoreList.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Álinson Santos Xavier + * Copyright (C) 2016 Álinson Santos Xavier * * This file is part of Loop Habit Tracker. * @@ -39,6 +39,7 @@ import java.io.IOException; import java.io.Writer; import java.text.SimpleDateFormat; import java.util.Date; +import java.util.List; public class ScoreList { @@ -55,6 +56,10 @@ public class ScoreList this.habit = habit; } + public ScoreList() + { + } + protected From select() { return new Select() @@ -342,4 +347,60 @@ public class ScoreList 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 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 void writeCSVMultipleHabits(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)); + } + do + { + String timestamp = dateFormat.format(new Date(cursor.getLong(0))); + out.write(String.format("\n%s", timestamp)); + for(Habit habit : habits) + { + out.write(String.format(",%s", getHabitScore(habit.getId().toString(), Long.toString(cursor.getLong(0))))); + } + } while(cursor.moveToNext()); + cursor.close(); + out.close(); + } + + /** + * Returns the habit score for a given timestamp and habit. + * + * @param name habit for the query + * @param out the writer where the CSV will be output + * + * @return habit value for a specific timestamp and habit + */ + public String getHabitScore(String name, String date){ + String query = "select score from Score where timestamp = ? and habit = ?"; + SQLiteDatabase db = Cache.openDatabase(); + Cursor cursor = db.rawQuery(query, new String[] {date, name}); + String habitScore = " "; + if(cursor != null && cursor.moveToFirst()) { + habitScore = String.format("%.4f", ((float) cursor.getInt(0)) / Score.MAX_VALUE); + cursor.close(); + } + return habitScore; + } }