pull/114/merge
Panagiotis Kompis 9 years ago
commit 1741b05c2c

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
@ -60,6 +60,20 @@ 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 check = new CheckmarkList();
check.writeCSVMultipleHabits (habits, out);
filename = "AllScores.csv";
new File(exportDirName).mkdirs();
out = new FileWriter(exportDirName + filename);
generateFilenames.add(filename);
ScoreList score = new ScoreList();
score.writeCSVMultipleHabits (habits, out);
out.close();
for(Habit h : habits)

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
@ -42,6 +42,10 @@ public class CheckmarkList
{
private Habit habit;
public CheckmarkList()
{
}
public CheckmarkList(Habit habit)
{
this.habit = habit;
@ -299,4 +303,62 @@ 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 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<Habit> 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", getHabitValue(habit.getId().toString(), Long.toString(cursor.getLong(0)))));
}
} while(cursor.moveToNext());
cursor.close();
out.close();
}
/**
* Returns the habit value 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 getHabitValue(String name, String date)
{
String query = "select value from checkmarks where timestamp = ? and habit = ? ";
SQLiteDatabase db = Cache.openDatabase();
Cursor cursor = db.rawQuery(query, new String[] {date, name});
String habitValue = " ";
if(cursor != null && cursor.moveToFirst()) {
habitValue = Integer.toString(cursor.getInt(0));
cursor.close();
}
return habitValue;
}
}

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
*
* 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<Habit> 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;
}
}

@ -26,4 +26,4 @@ subprojects {
project.android.dexOptions.preDexLibraries = rootProject.ext.preDexLibs
}
}
}
}

Loading…
Cancel
Save