branch feature/sync updated (b8e6eb5 -> 431e815)
This is an automated email from the git hooks/post-receive script. New change to branch feature/sync in repository jtimer. See https://gitlab.nuiton.org/chorem/jtimer.git from b8e6eb5 removed a useless comment new 431e815 added the gson library to the project to enable easier generation of json output for the updates, adapted the update functions to accomodate (removed useless prettyfier) The 1 revisions listed above as "new" are entirely new to this repository and will be described in separate emails. The revisions listed as "adds" were already present in the repository and have only been added to this reference. Detailed log of new commits: commit 431e815b1cf2d6a06756b02cab311c8d15ba283c Author: servantie <servantie.c@gmail.com> Date: Mon May 23 17:15:04 2016 +0200 added the gson library to the project to enable easier generation of json output for the updates, adapted the update functions to accomodate (removed useless prettyfier) Summary of changes: pom.xml | 7 +++ .../chorem/jtimer/entities/TimerTaskHelper.java | 66 +++++++++------------- .../jtimer/ui/report/TimerTaskUpdaterView.java | 63 ++++++--------------- 3 files changed, 50 insertions(+), 86 deletions(-) -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.
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 431e815b1cf2d6a06756b02cab311c8d15ba283c Author: servantie <servantie.c@gmail.com> Date: Mon May 23 17:15:04 2016 +0200 added the gson library to the project to enable easier generation of json output for the updates, adapted the update functions to accomodate (removed useless prettyfier) --- pom.xml | 7 +++ .../chorem/jtimer/entities/TimerTaskHelper.java | 66 +++++++++------------- .../jtimer/ui/report/TimerTaskUpdaterView.java | 63 ++++++--------------- 3 files changed, 50 insertions(+), 86 deletions(-) diff --git a/pom.xml b/pom.xml index 7d8f2d2..a83716a 100644 --- a/pom.xml +++ b/pom.xml @@ -293,6 +293,13 @@ <version>1.17</version> </dependency> + <!-- gson lib --> + <dependency> + <groupId>com.google.code.gson</groupId> + <artifactId>gson</artifactId> + <version>2.6.2</version> + </dependency> + <!-- commons-xxx lib --> <dependency> diff --git a/src/main/java/org/chorem/jtimer/entities/TimerTaskHelper.java b/src/main/java/org/chorem/jtimer/entities/TimerTaskHelper.java index 94bb7f8..c513ae0 100644 --- a/src/main/java/org/chorem/jtimer/entities/TimerTaskHelper.java +++ b/src/main/java/org/chorem/jtimer/entities/TimerTaskHelper.java @@ -22,6 +22,9 @@ package org.chorem.jtimer.entities; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; + import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -338,49 +341,33 @@ public class TimerTaskHelper { * @param task the task to make a JSON from * @return result the string in JSON */ - public static String taskToJSONFormat(TimerTask task, boolean withAnnotations) { + public static JsonObject taskToJSONFormat(TimerTask task, boolean withAnnotations) { Date startDate = task.getCreationDate(); Date endDate = new Date(); - String resultingJSON = taskToJSONFormat(task, startDate, endDate, withAnnotations); + JsonObject resultingJSON = taskToJSONFormat(task, startDate, endDate, withAnnotations); return resultingJSON; } /** - * Returns a String in JSON format (cf schema (simplified) - * with all times and annotations of a task (without subtasks) + * Returns a JSONObject (cf schema (simplified) + * with all times of a task (without subtasks) * in a given period between two dates + * with annotations if enabled * @param task the task to make a JSON from * @param startDate the beginning date * @param endDate the end date + * @param withAnnotations true if annotations included * @return result the string in JSON */ - public static String taskToJSONFormat(TimerTask task, Date startDate, Date endDate, boolean withAnnotations) { - + public static JsonObject taskToJSONFormat(TimerTask task, Date startDate, Date endDate, boolean withAnnotations) { + JsonObject responseJSON = new JsonObject(); LocalDate startPeriodDate = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); LocalDate endPeriodDate = endDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); - - String resultingJSON = "{\"URL\":\"" + task.getSynchronisingURL()+ "\",\"startDate\":\"" + - startPeriodDate.toString() + "\",\"endDate\":\"" + endPeriodDate.toString() + "\",\"periods\":" - + getTimesAndCommentsJSON(task, startDate, endDate, withAnnotations) +"}"; - return resultingJSON; - } - - /** - * Returns a String with the times and comments of a task as : - * [{"id":"..","startDate":"..","duration":..,"info":".."},..] - * over a defined period from startDate to endDate - * @param task : task to make a json of - * @param startDate : beginning of period to work with - * @param endDate : end of period - * @return a string in json format - */ - public static String getTimesAndCommentsJSON(TimerTask task, Date startDate, Date endDate, boolean withAnnotations) { - String result = ""; + JsonArray periodArray = new JsonArray(); SortedMap<Date, Long> dates = task.getAllDaysAndTimes().subMap(startDate, endDate); if (dates.size() != 0) { - result = result + "["; for (SortedMap.Entry<Date, Long> entry : dates.entrySet()) { //adding id, startDate and duration //converting Date to LocalDate (to ease the .toString()) @@ -388,25 +375,26 @@ public class TimerTaskHelper { LocalDate date = entry.getKey().toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); String dateString = date.toString(); //as jtimer has time entries only for a day, the id of the times will be the date in yyyy-mm-dd format - result = result + "{\"id\":\""+ dateString + "\",\"startDate\":\"" + dateString + "T00:00:00" + - "\",\"duration\":" + Long.toString(entry.getValue()) + ",\"info\":\""; - //adding comments (if there are any) - if (getAnnotation(task, entry.getKey()).size() != 0 && withAnnotations ) { + JsonObject periodElement = new JsonObject(); + periodElement.addProperty("id", dateString); + periodElement.addProperty("startDate", dateString + "T00:00:00"); + periodElement.addProperty("duration", entry.getValue()); + if (withAnnotations && getAnnotation(task, entry.getKey()).size() != 0 ) { + String annotations = ""; for (String s : getAnnotation(task, entry.getKey())) { - result = result + s + ","; + annotations = annotations + s + ","; } //remove trailing comma - result = result.substring(0, result.length()-1); + annotations = annotations.substring(0, annotations.length()-1); + periodElement.addProperty("info", annotations); } - result = result + "\"},"; + periodArray.add(periodElement); } - //deleting trailing ',' because of the loop - result = result.substring(0, result.length()-1); - result = result + "]"; - } - else { - result ="[]"; } - return result; + responseJSON.addProperty("URL", task.getSynchronisingURL()); + responseJSON.addProperty("startDate", startPeriodDate.toString()); + responseJSON.addProperty("endDate", endPeriodDate.toString()); + responseJSON.add("periods", periodArray ); + return responseJSON; } } diff --git a/src/main/java/org/chorem/jtimer/ui/report/TimerTaskUpdaterView.java b/src/main/java/org/chorem/jtimer/ui/report/TimerTaskUpdaterView.java index 606b5e8..73936b4 100644 --- a/src/main/java/org/chorem/jtimer/ui/report/TimerTaskUpdaterView.java +++ b/src/main/java/org/chorem/jtimer/ui/report/TimerTaskUpdaterView.java @@ -22,7 +22,9 @@ */ package org.chorem.jtimer.ui.report; -import org.apache.commons.lang3.StringUtils; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonObject; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.chorem.jtimer.JTimer; @@ -71,8 +73,8 @@ public class TimerTaskUpdaterView extends FrameView implements DocumentListener /** update output view*/ protected JTextArea updateArea; - /** update output (as json) */ - protected String updateJson; + + protected JsonObject updateJson; /** task to update */ protected TimerTask task; @@ -315,43 +317,6 @@ public class TimerTaskUpdaterView extends FrameView implements DocumentListener } /** - * Prettify the JSON String to make it - * human readable - * @param jsonString : the JSON string to prettify - * @return an easier to read String - */ - public static String prettifyJSON(String jsonString) { - int indent = 0; - String prettyJson; - String prettiest = ""; - //return line after each { - prettyJson = jsonString.replace("{", "{\n\t"); - prettyJson = prettyJson.replace("}", "\n}"); - //fix { \n } (useless space} - prettyJson = prettyJson.replace("{\n\t\n}", "{}"); - // return after each , - prettyJson = prettyJson.replace(",", ",\n\t"); - //change ":" into " : " - prettyJson = prettyJson.replace("\":\"", "\" : \""); - prettyJson = prettyJson.replace("\":", "\" : "); - for (int i = 0 ; i < prettyJson.length(); ++i) { - prettiest = prettiest + prettyJson.charAt(i); - if ((indent != 0) && (prettyJson.charAt(i) == '\n')) { - for (int j = 0 ; j < indent ; ++j) { - prettiest = prettiest + "\t"; - } - } - if (prettyJson.charAt(i) == '{') { - ++indent; - } - else if (prettyJson.charAt(i) == '}') { - --indent; - } - } - return prettiest; - } - - /** * Close action. */ @org.jdesktop.application.Action @@ -367,16 +332,20 @@ public class TimerTaskUpdaterView extends FrameView implements DocumentListener */ @org.jdesktop.application.Action public void generateUpdate() { - updateJson = ""; - // make String of information + + // make the JSON Object of information updateJson = taskToJSONFormat(task, datePickerFrom.getDate(), - datePickerTo.getDate(), isIncludingAnnotations()); + datePickerTo.getDate(), isIncludingAnnotations()); + //make it human readable + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + String jsonOutput = gson.toJson(updateJson); - if (updateJson != null && !updateJson.isEmpty()) { - updateArea.setText(prettifyJSON(updateJson)); + if (updateJson != null) { + updateArea.setText(jsonOutput); } else { updateArea.setText(""); } + } /** @@ -419,13 +388,13 @@ public class TimerTaskUpdaterView extends FrameView implements DocumentListener String charset = "UTF-8"; String query=""; try { - query = String.format("json=%s", URLEncoder.encode(updateJson, charset)); + query = String.format("json=%s", URLEncoder.encode(updateJson.toString(), charset)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } try { - query = String.format("json=%s", URLEncoder.encode(updateJson, charset)); + query = String.format("json=%s", URLEncoder.encode(updateJson.toString(), charset)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.
participants (1)
-
chorem.org scm