This is an automated email from the git hooks/post-receive script. New commit to branch feature/1364_sous_taches in repository jtimer. See https://gitlab.nuiton.org/chorem/jtimer.git commit e23494731fad75aab8199577fdb5455b663f1f0d Author: servantie <servantie.c@gmail.com> Date: Fri Jul 29 16:35:08 2016 +0200 adds subtasks times to task sync if they have no syncInfo id of times are : date + task_id in format : 'yyyy-mm-dd_id' (task id is number) --- .../chorem/jtimer/entities/TimerTaskHelper.java | 141 +++++++++------------ .../chorem/jtimer/io/TimerTaskSynchronizer.java | 8 +- .../jtimer/ui/report/TimerTaskSyncInfoEditor.java | 2 +- .../jtimer/entities/TimerTaskHelperTest.java | 32 +++-- 4 files changed, 89 insertions(+), 94 deletions(-) diff --git a/src/main/java/org/chorem/jtimer/entities/TimerTaskHelper.java b/src/main/java/org/chorem/jtimer/entities/TimerTaskHelper.java index ed9af7b..a3343b0 100644 --- a/src/main/java/org/chorem/jtimer/entities/TimerTaskHelper.java +++ b/src/main/java/org/chorem/jtimer/entities/TimerTaskHelper.java @@ -336,85 +336,88 @@ public class TimerTaskHelper { } /** - * Returns a list of JSONObject (1/SyncURL) (follows the schema) - * with all times and annotations of a task (without subtasks) - * with annotations if enabled - * @param task the task to make a JSON from - * @param withAnnotations true if annotations included - * @param timezone represents the timezone - * @return result the string in JSON + * makes a JSONObject corresponding to one sync with all the times since lastSync + * @param task + * @param sync + * @param timezone + * @return */ - public static List<JsonObject> taskToJSONFormat(TimerTask task, boolean withAnnotations, String timezone) { - Date startDate = task.getAllDaysAndTimes().firstKey(); + public static JsonObject taskToJsonObject(TimerTask task, SyncInfo sync, String timezone) { + JsonObject resultingObject = new JsonObject(); + JsonArray periodArray = new JsonArray(); + Date startDate = sync.getLastSync(); Date endDate = DateUtils.ceiling(new Date(), Calendar.DAY_OF_MONTH); - List<JsonObject> resultingJSON = taskToJSONFormat(task, startDate, endDate, withAnnotations, timezone); - return resultingJSON; - } + String timestamp = "T00:00:00" + timezone; + SortedMap<Date, Long> dates = task.getAllDaysAndTimes().subMap(startDate, endDate); + LocalDate startPeriodDate = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); + String startPeriodString = startPeriodDate.toString() + timestamp; + LocalDate endPeriodDate = endDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); + String endPeriodString = endPeriodDate.toString() + timestamp; + //get the times of the task + periodArray.addAll(getTimesAsJsonArray(task, startDate, endDate, sync.isWithAnnotations(), timestamp)); + //if there are subtasks, get the times of the subtasks that have no syncInfo + if (!task.getSubTasks().isEmpty()) { + for (TimerTask subtask : getAllSubTasks(task)) { + if (subtask.getSynchronizingInfoList().isEmpty()) { + periodArray.addAll(getTimesAsJsonArray(subtask, startDate, endDate, sync.isWithAnnotations(), timestamp)); + } + } + } + resultingObject.addProperty("URL", sync.getSyncURL()); + resultingObject.addProperty("startDate", startPeriodString); + resultingObject.addProperty("endDate", endPeriodString); + resultingObject.add("periods", periodArray); + + return resultingObject; + } /** - * Returns a list of JSONObject (one for each sync URL) - * 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 - * @param timezone represents the timezone - * @return result the string in JSON + * Returns all the subtasks of a task (including the subtaks of subtasks) + * @param task + * @return a List<TimerTask> */ - public static List<JsonObject> taskToJSONFormat(TimerTask task, Date startDate, Date endDate, boolean withAnnotations, String timezone) { - ArrayList<JsonObject> jsonObjectList = new ArrayList<>(); - SortedMap<Date, Long> dates = task.getAllDaysAndTimes().subMap(startDate, endDate); - //only create jsons if there are times to send - if (dates.size() != 0) { - //iterate over active urls only - for (String url : task.getActiveSynchronizingURLList()) { - JsonObject responseJSON = taskURLToJSONObject(task, url, startDate, endDate, withAnnotations, timezone); - jsonObjectList.add(responseJSON); + public static List<TimerTask> getAllSubTasks(TimerTask task) { + List<TimerTask> subTasksList = new ArrayList<>(); + if (!task.getSubTasks().isEmpty()) { + for (TimerTask subTask : task.getSubTasks()) { + subTasksList.add(subTask); + if (!subTask.getSubTasks().isEmpty()) { + subTasksList.addAll(getAllSubTasks(subTask)); + } } } - - return jsonObjectList; + return subTasksList; } - /** - * Makes only one object for a task associated to a URL - * @param task : the task to sync - * @param url : the url to sync to - * @param startDate : the start of the period to sync - * @param endDate : the end of the period to sync - * @param withAnnotations : if true add annotations to sync - * @param timezone : the timezone - * - * adds a timestamp ("T00:00:00") because jTimer stores task times only associated to a day, the json - * Format that was agreed on includes the time (to separate time periods more precisely - * over a day). + /*** + * Returns a JsonArray of all the times of a task, according to format + * the id is made of the date of the time and the id of the task (as in the number given + * by jTimer) + * id, startdate, duration, info (if annotations is true) * - * @return JSonObject : the object to send + * @param task + * @param startDate + * @param endDate + * @param withAnnotations + * @param timestamp + * @return a JsonArray of the periods */ - public static JsonObject taskURLToJSONObject(TimerTask task, String url, Date startDate, Date endDate, boolean withAnnotations, String timezone) { - JsonObject resultingObject = new JsonObject(); + public static JsonArray getTimesAsJsonArray(TimerTask task, Date startDate, Date endDate, boolean withAnnotations, String timestamp) { JsonArray periodArray = new JsonArray(); - String timestamp = "T00:00:00" + timezone; SortedMap<Date, Long> dates = task.getAllDaysAndTimes().subMap(startDate, endDate); - LocalDate startPeriodDate = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); - String startPeriodString = startPeriodDate.toString() + timestamp; - LocalDate endPeriodDate = endDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); - String endPeriodString = endPeriodDate.toString() + timestamp; for (SortedMap.Entry<Date, Long> entry : dates.entrySet()) { //adding id, startDate and duration //converting Date to LocalDate (to ease the .toString()) 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 - JsonObject periodElement = new JsonObject(); - periodElement.addProperty("id", dateString); + JsonObject periodElement = new JsonObject(); + periodElement.addProperty("id", dateString + "_" + task.getNumber()); periodElement.addProperty("startDate", dateString + timestamp); //TimerTaskHelper.getTotalTime(task, entry.getKey()) to get the total time of the task and all the subtasks - //entry.getValue() to get only the time of the task - periodElement.addProperty("duration", TimerTaskHelper.getTotalTime(task, entry.getKey())/1000); + //entry.getValue() to get only the time of the task divided by 1000 (jtimer stores milliseconds) + periodElement.addProperty("duration", entry.getValue()/1000); if (withAnnotations && !(getAnnotation(task, entry.getKey()).isEmpty())) { StringBuilder annotationBuilder = new StringBuilder(); for (String s : getAnnotation(task, entry.getKey())) { @@ -425,29 +428,7 @@ public class TimerTaskHelper { } periodArray.add(periodElement); } - resultingObject.addProperty("URL", url); - resultingObject.addProperty("startDate", startPeriodString); - resultingObject.addProperty("endDate", endPeriodString); - resultingObject.add("periods", periodArray); - - return resultingObject; - } + return periodArray; - /** - * Makes a single object with all the times - * @param task - * @param url - * @param withAnnotations - * @param timezone - * @return a JsonObject of the task times - */ - public static JsonObject taskURLToJSONObject(TimerTask task, String url, boolean withAnnotations, String timezone) { - JsonObject resultingObject = new JsonObject(); - if (task.getAllDaysAndTimes().size() > 0) { - Date startDate = task.getAllDaysAndTimes().firstKey(); - Date endDate = DateUtils.ceiling(new Date(), Calendar.DAY_OF_MONTH); - resultingObject = taskURLToJSONObject(task, url, startDate, endDate, withAnnotations, timezone); - } - return resultingObject; } } diff --git a/src/main/java/org/chorem/jtimer/io/TimerTaskSynchronizer.java b/src/main/java/org/chorem/jtimer/io/TimerTaskSynchronizer.java index 54f04b8..158ee15 100644 --- a/src/main/java/org/chorem/jtimer/io/TimerTaskSynchronizer.java +++ b/src/main/java/org/chorem/jtimer/io/TimerTaskSynchronizer.java @@ -127,7 +127,7 @@ public class TimerTaskSynchronizer implements DataEventListener { if (log.isDebugEnabled()) { log.debug("Sync hasn't been done today for " + syncURL + " " + task.getName() ); } - JsonObject syncObject = TimerTaskHelper.taskURLToJSONObject(task, syncInfo.getSyncURL(), syncInfo.getLastSync(), new Date(), syncInfo.isWithAnnotations(), timezone); + JsonObject syncObject = TimerTaskHelper.taskToJsonObject(task, syncInfo, timezone); int syncResult = synchronizeTaskOnURL(syncObject); if (syncResult > 199 && syncResult < 300) { if (log.isDebugEnabled()) { @@ -155,8 +155,8 @@ public class TimerTaskSynchronizer implements DataEventListener { public void synchronizeSingleTask(TimerTask task) { List<JsonObject> jsonObjectList = new ArrayList<>(); - for (SyncInfo sync : task.getSynchronizingInfoList()) { - JsonObject syncObject = TimerTaskHelper.taskURLToJSONObject(task, sync.getSyncURL(), sync.getLastSync(), new Date(), sync.isWithAnnotations(), timezone); + for (SyncInfo syncInfo : task.getSynchronizingInfoList()) { + JsonObject syncObject = TimerTaskHelper.taskToJsonObject(task, syncInfo, timezone); jsonObjectList.add(syncObject); } @@ -175,7 +175,7 @@ public class TimerTaskSynchronizer implements DataEventListener { log.debug(LocalDateTime.now()); } Calendar cal = Calendar.getInstance(); - task.setLastSync(cal.getTime(), syncURL); + task.getSynchronizingInfo(syncURL).setLastSync(cal.getTime()); } else { if (log.isDebugEnabled()) { log.debug("Error in connection " + syncURL); diff --git a/src/main/java/org/chorem/jtimer/ui/report/TimerTaskSyncInfoEditor.java b/src/main/java/org/chorem/jtimer/ui/report/TimerTaskSyncInfoEditor.java index 8d903da..81b8701 100644 --- a/src/main/java/org/chorem/jtimer/ui/report/TimerTaskSyncInfoEditor.java +++ b/src/main/java/org/chorem/jtimer/ui/report/TimerTaskSyncInfoEditor.java @@ -426,7 +426,7 @@ public class TimerTaskSyncInfoEditor extends FrameView implements ActionListener //if the test button has been clicked, test the sync on the URL and returns a message to the user //tests only if the info exists if (task.getSynchronizingInfoList().contains(infoToUse)) { - JsonObject testObject = TimerTaskHelper.taskURLToJSONObject(task, infoToUse.getSyncURL(), infoToUse.getLastSync(), new Date(), infoToUse.isWithAnnotations(), timezone); + JsonObject testObject = TimerTaskHelper.taskToJsonObject(task, infoToUse, timezone); int responseCode = TimerTaskSynchronizer.synchronizeTaskOnURL(testObject); if (responseCode > 199 && responseCode < 300) { diff --git a/src/test/java/org/chorem/jtimer/entities/TimerTaskHelperTest.java b/src/test/java/org/chorem/jtimer/entities/TimerTaskHelperTest.java index b75f59d..47c8e18 100644 --- a/src/test/java/org/chorem/jtimer/entities/TimerTaskHelperTest.java +++ b/src/test/java/org/chorem/jtimer/entities/TimerTaskHelperTest.java @@ -23,9 +23,13 @@ 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; +import java.util.Calendar; import java.util.Date; import java.util.List; +import org.apache.commons.lang3.time.DateUtils; import org.chorem.jtimer.AbstractJTimerTest; import org.testng.Assert; import org.testng.annotations.Test; @@ -60,38 +64,48 @@ public class TimerTaskHelperTest extends AbstractJTimerTest { public void taskToJSONFormatTest() { TimerTask task = new TimerTask(); task.setName("JsonBuilder Test"); - task.addSyncInfo("http://localhost:3000"); + SyncInfo sync = new SyncInfo("http://localhost:3000"); + task.addSyncInfo(sync); + //set the creation date at same date as first timing date (to fit test object) + task.setCreationDate(new Date(1462831200000L)); + sync.setLastSync(new Date(1462831200000L)); //date : 2016-05-10 task.setTime(new Date(1462831200000L), 452000L); //date : 2016-05-12 task.setTime(new Date(1463004000000L), 4533000L); - //create json object list to get - List<JsonObject> listOfObjects = new ArrayList<>(); + //create json object JsonObject objectToHave = new JsonObject(); JsonArray periodArray = new JsonArray(); JsonObject periodElement1 = new JsonObject(); - periodElement1.addProperty("id", "2016-05-10"); + //id is date+_+task.number, here -1 + periodElement1.addProperty("id", "2016-05-10_-1"); periodElement1.addProperty("startDate", "2016-05-10T00:00:00+01:00"); periodElement1.addProperty("duration", 452); periodArray.add(periodElement1); JsonObject periodElement2 = new JsonObject(); - periodElement2.addProperty("id", "2016-05-12"); + periodElement2.addProperty("id", "2016-05-12_-1"); periodElement2.addProperty("startDate", "2016-05-12T00:00:00+01:00"); periodElement2.addProperty("duration", 4533); periodArray.add(periodElement2); objectToHave.addProperty("URL", "http://localhost:3000"); + //startDate corresponds to lastSync in taksToJsonObject objectToHave.addProperty("startDate", "2016-05-10T00:00:00+01:00"); - objectToHave.addProperty("endDate", "2016-05-20T00:00:00+01:00"); + //endDate is nextday (from currenttime) + Date endDate = DateUtils.ceiling(new Date(), Calendar.DAY_OF_MONTH); + String timestamp = "T00:00:00+01:00"; + LocalDate endPeriodDate = endDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); + String endPeriodString = endPeriodDate.toString() + timestamp; + + objectToHave.addProperty("endDate", endPeriodString); objectToHave.add("periods", periodArray); - listOfObjects.add(objectToHave); //make a list of json objects from the task - List<JsonObject> taskJsonList = TimerTaskHelper.taskToJSONFormat(task, new Date(1462831200000L), new Date(1463695200000L), false, "+01:00"); + JsonObject jsonObject = TimerTaskHelper.taskToJsonObject(task, sync, "+01:00"); //compare it - Assert.assertEquals(taskJsonList, listOfObjects); + Assert.assertEquals(jsonObject, objectToHave); } } -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.