12/75: 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)
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 16cb34765159069459e814f84b940f3f67bcbe2b 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 9f17c4b..01bc02e 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 19643d3..af83918 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.time.LocalDate; import java.time.ZoneId; import java.util.ArrayList; @@ -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