This is an automated email from the git hooks/post-receive script. New commit to branch feature/sync in repository jtimer. See https://gitlab.nuiton.org/chorem/jtimer.git commit 1f186b5720811363e3162993869f5250673d616d Author: servantie <servantie.c@gmail.com> Date: Tue May 10 11:30:43 2016 +0200 "use of simpler json format, sends update request to url when url is changed with the json of the task." --- .../chorem/jtimer/entities/TimerTaskHelper.java | 197 +++++++++++++++------ .../chorem/jtimer/io/GTimerIncrementalSaver.java | 2 + 2 files changed, 149 insertions(+), 50 deletions(-) diff --git a/src/main/java/org/chorem/jtimer/entities/TimerTaskHelper.java b/src/main/java/org/chorem/jtimer/entities/TimerTaskHelper.java index 2edf112..a6de757 100644 --- a/src/main/java/org/chorem/jtimer/entities/TimerTaskHelper.java +++ b/src/main/java/org/chorem/jtimer/entities/TimerTaskHelper.java @@ -22,7 +22,16 @@ package org.chorem.jtimer.entities; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.UnsupportedEncodingException; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLEncoder; +import java.time.LocalDate; import java.time.LocalDateTime; +import java.time.ZoneId; import java.util.*; /** @@ -323,13 +332,13 @@ public class TimerTaskHelper { return components; } - /** - * Returns a String in JSON format (cf schema) + /* /** + * Returns a String in JSON format (cf schema (v1)) * with all the times and annotations of one task (and * its subtasks) * @param task the task to make a JSON from * @return subtaskString the String representing the task - */ + *//* public static String tasktoJSONFormat(TimerTask task) { String taskNumber = Integer.toString(task.getNumber()); //task time ? or all times (including subtasks) @@ -345,68 +354,115 @@ public class TimerTaskHelper { subtaskString = "[]"; } - String res = "{\"path\":\"" + pathForJSON(task) + "\",\"name\":\"" + taskNumber + + String res = "{\"id\":\"" + pathForJSON(task) + "\",\"annotations\":{" + annotationsForJSON(task) + "},\"times\":" + - timesForJSON(task) + - ",\"taskTime\":" + taskTotalTime + ",\"subTasks\":" +subtaskString + "}"; + timesForJSON(task) + ",\"taskTime\":" + taskTotalTime + + "\",\"name\":\"" + taskNumber + ",\"subTasks\":" + subtaskString + "}"; return res; - } + }*/ /** - * Returns a String with the task's path - * @param task : the task - * @return path : the task's path - */ - public static String pathForJSON(TimerTask task) { - String path = ""; - TimerTask clonetask = task; - while (clonetask != null) { - path = clonetask.getName()+ "/" + path; - clonetask = clonetask.getParent(); - } - return path; - } - - /** - * Returns the annotations of the task as - * "Date" : "annotation" - * @param task : the task - * @return annotations : the task's annotations + * Returns a String in JSON format (cf schema (simplified) + * with all times and annotations of a task (without subtasks) + * @param task the task to make a JSON from + * @return result the string in JSON */ - public static String annotationsForJSON(TimerTask task) { - String annotations=""; - if (task.getAllDaysAnnotations().size() != 0) { - SortedMap<Date, String> annotationsMap = task.getAllDaysAnnotations(); - //toString pas au bon format fixme (do it manually) - for (SortedMap.Entry<Date, String> entry : annotationsMap.entrySet()) { - annotations = annotations + "\"" + entry.getKey().toString() + "\":\"" + entry.getValue() + "\","; - } - //fix for last , in the String - annotations = annotations.substring(0,annotations.length()-1); - } - return annotations; + public static String tasktoJSONFormat(TimerTask task) { + String taskID = Integer.toString(task.getNumber()); + String taskTotalTime = Long.toString(getAllTotalTime(task)); + String res = "{\"uuid\":\"" + taskID + "\",\"periods\":" + getTimesAndCommentsJSON(task) +"}"; + return res; } /** - * Returns the times of the task as - * "Date" : time - * @param task the task - * @return times : the task's times + * Returns a String with the times and comments of a task as : + * [{"id":"..","startDate":"..","duration":..,"info":".."},..] */ - public static String timesForJSON(TimerTask task) { - String times = "{"; - if (task.getAllDaysAndTimes().size() !=0) { + public static String getTimesAndCommentsJSON(TimerTask task) { + String result =""; + //todo:deal with ids (currently a silly sequence) + int sequence = 0; + if (task.getAllDaysAndTimes().size() != 0) { SortedMap<Date, Long> timesMap = task.allDaysTimes; for (SortedMap.Entry<Date, Long> entry : timesMap.entrySet()) { - times = times + "\"" + entry.getKey().toString() + "\":\"" + entry.getValue() + "\","; + //adding id, startDate and duration + //converting Date to LocalDate (to ease the .toString()) + //todo:deal with timezones + LocalDate date = entry.getKey().toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); + + result = result + "{\"id\":"+ sequence + "\",\"startDate\":\"" + date.toString() + "T00:00:00" + + "\",\"duration\":\"" + Long.toString(entry.getValue()) + ",\"info\":\""; + //adding comments (if there are any) + if (getAnnotation(task, entry.getKey()).size() != 0) { + for (String s : getAnnotation(task, entry.getKey())) { + result = result + s + ","; + } + } + result = result + "\"},"; + ++sequence; } - times = times.substring(0, times.length()-1); - } - times += "}"; - return times; + //deleting trailing ',' because of the loop + result = result.substring(0, result.length()-1); + } + + return result; } +// /** +// * Returns a String with the task's path +// * @param task : the task +// * @return path : the task's path +// */ +// public static String pathForJSON(TimerTask task) { +// String path = ""; +// TimerTask clonetask = task; +// while (clonetask != null) { +// path = clonetask.getName() + "/" + path; +// clonetask = clonetask.getParent(); +// } +// return path; +// } + +// /** +// * Returns the annotations of the task as +// * "Date" : "annotation" +// * @param task : the task +// * @return annotations : the task's annotations +// */ +// public static String annotationsForJSON(TimerTask task) { +// String annotations=""; +// if (task.getAllDaysAnnotations().size() != 0) { +// SortedMap<Date, String> annotationsMap = task.getAllDaysAnnotations(); +// //toString pas au bon format fixme (do it manually) +// for (SortedMap.Entry<Date, String> entry : annotationsMap.entrySet()) { +// annotations = annotations + "\"" + entry.getKey().toString() + "\":\"" + entry.getValue() + "\","; +// } +// //fix for last , in the String +// annotations = annotations.substring(0,annotations.length()-1); +// } +// return annotations; +// } +// +// /** +// * Returns the times of the task as +// * "Date" : time +// * @param task the task +// * @return times : the task's times +// */ +// public static String timesForJSON(TimerTask task) { +// String times = "["; +// if (task.getAllDaysAndTimes().size() !=0) { +// SortedMap<Date, Long> timesMap = task.allDaysTimes; +// for (SortedMap.Entry<Date, Long> entry : timesMap.entrySet()) { +// times = times + "{\"" + "\"startDate\":" + entry.getKey().toString() + "\",\"duration\":\"" + Long.toString(entry.getValue()) + "\"},"; +// } +// times = times.substring(0, times.length()-1); +// } +// times += "]"; +// return times; +// } + /** * Prettify the JSON String to make it * human readable @@ -443,4 +499,45 @@ public class TimerTaskHelper { } return prettiest; } + + /** + * Update a task completely + * @param task : the task to update + * + */ + public static void updateTask(TimerTask task) { + String syncURl = task.getSynchronisingURL(); + String charset = "UTF-8"; + String json = tasktoJSONFormat(task); + String query=""; + try { + query = String.format("json=%s", URLEncoder.encode(json, charset)); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + + URLConnection connection = null; + try { + connection = new URL("http://"+ syncURl).openConnection(); + } catch (IOException e) { + e.printStackTrace(); + } + connection.setDoOutput(true); + connection.setRequestProperty("Accept-Charset", charset); + connection.setRequestProperty("Content-Type", "application/json" + charset); + + try (OutputStream output = connection.getOutputStream()) { + output.write(query.getBytes(charset)); + } catch (IOException e) { + e.printStackTrace(); + } + + try { + InputStream response = connection.getInputStream(); + + } catch (IOException e) { + e.printStackTrace(); + } + + } } diff --git a/src/main/java/org/chorem/jtimer/io/GTimerIncrementalSaver.java b/src/main/java/org/chorem/jtimer/io/GTimerIncrementalSaver.java index 4408bd0..ea077d1 100644 --- a/src/main/java/org/chorem/jtimer/io/GTimerIncrementalSaver.java +++ b/src/main/java/org/chorem/jtimer/io/GTimerIncrementalSaver.java @@ -1186,6 +1186,8 @@ public class GTimerIncrementalSaver extends AbstractSaver implements Saver, synchronisationTaskFile.delete(); } + + TimerTaskHelper.updateTask(task); } /** -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.