This is an automated email from the git hooks/post-receive script. New commit to branch sync-timebundle in repository jtimer. See https://gitlab.nuiton.org/chorem/jtimer.git commit 534767e281d7db20e262cd1da83dbf470fb4d525 Author: Eric Chatellier <chatellier@codelutin.com> Date: Tue Mar 7 15:34:45 2017 +0100 Manage sub task times and project total sync --- .../plugins/timebundle/RemoteSynchronizer.java | 54 +++++++++------------- .../jtimer/plugins/timebundle/SyncIOSaver.java | 41 +++++++++------- .../chorem/jtimer/plugins/timebundle/SyncView.java | 7 +-- 3 files changed, 47 insertions(+), 55 deletions(-) diff --git a/src/main/java/org/chorem/jtimer/plugins/timebundle/RemoteSynchronizer.java b/src/main/java/org/chorem/jtimer/plugins/timebundle/RemoteSynchronizer.java index 1e85162..7ab4853 100644 --- a/src/main/java/org/chorem/jtimer/plugins/timebundle/RemoteSynchronizer.java +++ b/src/main/java/org/chorem/jtimer/plugins/timebundle/RemoteSynchronizer.java @@ -41,8 +41,6 @@ import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.nio.charset.Charset; -import java.util.ArrayList; -import java.util.Collections; import java.util.Date; import java.util.List; import java.util.Map; @@ -198,6 +196,9 @@ public class RemoteSynchronizer implements Synchronizer { } protected void syncTask(RemoteTask remoteTask) { + if (log.isDebugEnabled()) { + log.debug("Syncing timebundle task " + remoteTask.getName()); + } String url = remoteTask.getUrl(); Set<TimerTask> syncTasks = remoteTask.getSyncTasks(); @@ -205,19 +206,22 @@ public class RemoteSynchronizer implements Synchronizer { jsonObject.put("startDate", DateFormatUtils.format(new Date(0), DATE_MIDNIGHT_PATTERN)); jsonObject.put("endDate", DateFormatUtils.format(new Date(), DATE_MIDNIGHT_PATTERN)); JSONArray periodArray = new JSONArray(); - syncTasks.forEach(timerTask -> { - timerTask.getAllDaysAndTimes().entrySet().forEach(dateLongEntry -> { - JSONObject period = new JSONObject(); - period.put("id", String.valueOf(timerTask.getNumber()) + DateFormatUtils.format(dateLongEntry.getKey(), DATE_MIDNIGHT_PATTERN)); - period.put("startDate", DateFormatUtils.format(dateLongEntry.getKey(), DATE_MIDNIGHT_PATTERN)); - period.put("duration", dateLongEntry.getValue() / 1000L); - periodArray.put(period); - }); - }); + syncTasks.forEach(timerTask -> addTaskTime(periodArray, timerTask)); jsonObject.put("periods", periodArray); post(url, jsonObject); } + protected void addTaskTime(JSONArray periodArray, TimerTask timerTask) { + timerTask.getAllDaysAndTimes().entrySet().forEach(dateLongEntry -> { + JSONObject period = new JSONObject(); + period.put("id", String.valueOf(timerTask.getNumber()) + DateFormatUtils.format(dateLongEntry.getKey(), DATE_MIDNIGHT_PATTERN)); + period.put("startDate", DateFormatUtils.format(dateLongEntry.getKey(), DATE_MIDNIGHT_PATTERN)); + period.put("duration", dateLongEntry.getValue() / 1000L); + periodArray.put(period); + }); + timerTask.getSubTasks().forEach(subTask -> addTaskTime(periodArray, subTask)); + } + protected static void post(String url, JSONObject data) { try { URI uri = new URI(url); @@ -228,30 +232,14 @@ public class RemoteSynchronizer implements Synchronizer { OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream()); out.write(data.toString(2)); out.close(); - System.out.println(conn.getResponseCode()); + if (log.isDebugEnabled()) { + log.debug("Sync url " + url + "(" + conn.getResponseCode() + ")"); + } } catch (IOException | URISyntaxException ex) { log.error("ex", ex); } - System.out.println("will send : " + data.toString(2)); - } - - public static void main(String... args) { - TimerProject project = new TimerProject("jTimer"); - List<RemoteProject> tbprojects = new ArrayList<>(); - RemoteProject tbProject = new RemoteProject(); - tbProject.setUrl("http://localhost:8080/api/v1/codelutin/contribute/3a526a69-59b9-45d2-bde2-09..."); - RemoteProject tbTask = new RemoteProject(); - tbTask.setUrl("http://localhost:8080/api/v1/codelutin/contribute/3a526a69-59b9-45d2-bde2-09..."); - - TimerTask task1 = new TimerTask(); - task1.setCreationDate(new Date()); - task1.setTime(new Date(), 42L); - tbTask.setSyncTasks(Collections.singleton(task1)); - tbProject.setSubTasks(Collections.singletonList(tbTask)); - tbprojects.add(tbProject); - project.getMeta().put(Constants.META, tbprojects); - - RemoteSynchronizer r = new RemoteSynchronizer(); - r.sync(project); + /*if (log.isDebugEnabled()) { + log.debug("will send : " + data.toString(2)); + }*/ } } diff --git a/src/main/java/org/chorem/jtimer/plugins/timebundle/SyncIOSaver.java b/src/main/java/org/chorem/jtimer/plugins/timebundle/SyncIOSaver.java index 3fea5e3..716d566 100644 --- a/src/main/java/org/chorem/jtimer/plugins/timebundle/SyncIOSaver.java +++ b/src/main/java/org/chorem/jtimer/plugins/timebundle/SyncIOSaver.java @@ -23,6 +23,7 @@ package org.chorem.jtimer.plugins.timebundle; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; import com.google.gson.stream.JsonReader; import org.apache.commons.collections4.CollectionUtils; @@ -47,6 +48,7 @@ import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Properties; import java.util.Set; import java.util.function.Function; @@ -56,9 +58,7 @@ public class SyncIOSaver { protected static Log log = LogFactory.getLog(SyncIOSaver.class); - protected static final String TIMEBUNDLE_DATA_DIRECTORY = "timebundledatabeta1"; - - protected static final String TIMEBUNDLE_CACHE_DIRECTORY = "timebundlecachebeta1"; + protected static final String TIMEBUNDLE_DATA_DIRECTORY = "timebundlebeta1"; protected File dataDirectory; @@ -68,8 +68,6 @@ public class SyncIOSaver { File homeDirectory = config.getHomeDirectory(); dataDirectory = new File(homeDirectory, TIMEBUNDLE_DATA_DIRECTORY); dataDirectory.mkdirs(); - cacheDirectory = new File(homeDirectory, TIMEBUNDLE_CACHE_DIRECTORY); - cacheDirectory.mkdirs(); } public void saveProject(TimerProject project) { @@ -90,7 +88,7 @@ public class SyncIOSaver { } protected void saveSyncInfo(TimerProject project, List<RemoteProject> remoteProjects) throws IOException { - Gson gson = new Gson(); + Gson gson = new GsonBuilder().setPrettyPrinting().create(); String json = gson.toJson(remoteProjects); File jsonFile = new File(dataDirectory, project.getNumber() + ".json"); FileUtils.writeStringToFile(jsonFile, json, StandardCharsets.UTF_8); @@ -99,8 +97,7 @@ public class SyncIOSaver { protected void saveTaskLinks(TimerProject project, List<RemoteProject> remoteProjects) throws IOException { Properties props = new Properties(); - remoteProjects.forEach(remoteProject -> saveTaskLinks(props, remoteProject.getSubTasks())); - + remoteProjects.forEach(remoteProject -> saveTaskLinks(props, remoteProject.getSubTasks(), project)); File syncFile = new File(dataDirectory, project.getNumber() + ".sync"); if (props.isEmpty()) { @@ -112,18 +109,26 @@ public class SyncIOSaver { } } - private void saveTaskLinks(Properties props, List<RemoteTask> remoteTasks) { + private void saveTaskLinks(Properties props, List<RemoteTask> remoteTasks, TimerProject project) { if (remoteTasks != null) { remoteTasks.forEach(remoteTask -> { - String values = remoteTask.getSyncTasks() - .stream() - .mapToInt(TimerTask::getNumber) - .mapToObj(Integer::toString) - .collect(Collectors.joining(",")); - if (!values.isEmpty()) { - props.put(remoteTask.getUuid(), values); + if (remoteTask.getSyncTasks() != null) { + Set<TimerTask> syncTasks = remoteTask.getSyncTasks(); + String values; + if (syncTasks.contains(project)) { + values = "-1"; // FIXME HACK + } else { + values = syncTasks + .stream() + .mapToInt(TimerTask::getNumber) + .mapToObj(Integer::toString) + .collect(Collectors.joining(",")); + } + if (!values.isEmpty()) { + props.put(remoteTask.getUuid(), values); + } } - saveTaskLinks(props, remoteTask.getSubTasks()); + saveTaskLinks(props, remoteTask.getSubTasks(), project); }); } } @@ -168,6 +173,7 @@ public class SyncIOSaver { props.load(syncReader); Map<Integer, TimerTask> timerTaskByNumber = new HashMap<>(); loadTimerTaskMap(timerTaskByNumber, project.getSubTasks()); + timerTaskByNumber.put(-1, project); // FIXME HACK // match remote and sync remoteTaskById.entrySet().forEach(entry -> { @@ -178,6 +184,7 @@ public class SyncIOSaver { Set<TimerTask> syncTasks = Arrays.stream(syncList.split("\\s*,\\s*")) .map(Integer::valueOf) .map(timerTaskByNumber::get) + .filter(Objects::nonNull) // HACK .collect(Collectors.toSet()); value.setSyncTasks(syncTasks); } diff --git a/src/main/java/org/chorem/jtimer/plugins/timebundle/SyncView.java b/src/main/java/org/chorem/jtimer/plugins/timebundle/SyncView.java index e923067..2c31658 100644 --- a/src/main/java/org/chorem/jtimer/plugins/timebundle/SyncView.java +++ b/src/main/java/org/chorem/jtimer/plugins/timebundle/SyncView.java @@ -141,11 +141,8 @@ public class SyncView extends FrameView implements TreeSelectionListener, Window } public void addRemoteProject() { - String url = (String)JOptionPane.showInputDialog(this.getFrame(), - "New remote project url", "Add remote project", - JOptionPane.INFORMATION_MESSAGE, null, - new Object[] {"http://localhost:8080/api/v1/codelutin/contribute/9370ee7e-7767-482c-b798-7a7262bc417f/#jTimer"}, - "http://localhost:8080/api/v1/codelutin/contribute/9370ee7e-7767-482c-b798-7a..."); + String url = JOptionPane.showInputDialog(this.getFrame(), + "New remote project url", "Add remote project"); RemoteProject project = synchronizer.fetchProject(url); getRemoteProjects().add(project); -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.