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 66dee368627d529a1e70998a4995ee88ae463692 Author: servantie <servantie.c@gmail.com> Date: Mon Jun 6 11:25:44 2016 +0200 added the timezone as option in JTimerConfig, updated json creation accordingly --- src/main/java/org/chorem/jtimer/JTimerConfig.java | 9 ++- src/main/java/org/chorem/jtimer/JTimerFactory.java | 3 + .../chorem/jtimer/entities/TimerTaskHelper.java | 8 +-- .../chorem/jtimer/io/TimerTaskSynchronizer.java | 21 ++++-- .../jtimer/ui/report/TimerTaskUpdaterView.java | 78 ++++++++++++++-------- 5 files changed, 81 insertions(+), 38 deletions(-) diff --git a/src/main/java/org/chorem/jtimer/JTimerConfig.java b/src/main/java/org/chorem/jtimer/JTimerConfig.java index 5d52f63..0bc8dc1 100644 --- a/src/main/java/org/chorem/jtimer/JTimerConfig.java +++ b/src/main/java/org/chorem/jtimer/JTimerConfig.java @@ -181,6 +181,12 @@ public class JTimerConfig { public long getIOSyncAutoSyncDelay() { return appConfig.getOptionAsLong(JTimerOption.IO_SYNC_AUTOSYNCDELAY.key);} /** + * Returns timezone for sync + * @return timezone + */ + public String getIOSyncTimeZone() { return appConfig.getOption(JTimerOption.IO_SYNC_TIMEZONE.key);} + + /** * Return user idle time threshold in seconds. * * @return idle time threshold @@ -293,7 +299,8 @@ public class JTimerConfig { IO_SAVER_AUTOSAVEDELAY("jtimer.io.saver.autosavedelay", "300"), IO_SYNC_CLASS("jtimer.io.synchronizer.class", "org.chorem.jtimer.io.TimerTaskSynchronizer"), - IO_SYNC_AUTOSYNCDELAY("jtimer.io.synchronizer.autosyncdelay", "3600"), + IO_SYNC_AUTOSYNCDELAY("jtimer.io.synchronizer.autosyncdelay", "10"), + IO_SYNC_TIMEZONE("jtimer.io.synchronizer.timezone", "+0600"), UI_IDLE_TIME("jtimer.ui.idletime", "300"), UI_SHOW_CLOSED("jtimer.ui.showclosed", "false"), diff --git a/src/main/java/org/chorem/jtimer/JTimerFactory.java b/src/main/java/org/chorem/jtimer/JTimerFactory.java index 4a100d5..b0329a5 100644 --- a/src/main/java/org/chorem/jtimer/JTimerFactory.java +++ b/src/main/java/org/chorem/jtimer/JTimerFactory.java @@ -114,6 +114,9 @@ public class JTimerFactory { // set delay to saver synchronizer.setAutoSyncDelay(JTimer.config.getIOSyncAutoSyncDelay() * 1000); + //set timezone to synchronizer + synchronizer.setTimezone(JTimer.config.getIOSyncTimeZone()); + } catch (InstantiationException e) { if (log.isErrorEnabled()) { log.error("Can't instanciate class : " + synchronizerClass, e); diff --git a/src/main/java/org/chorem/jtimer/entities/TimerTaskHelper.java b/src/main/java/org/chorem/jtimer/entities/TimerTaskHelper.java index ffd7c99..a640223 100644 --- a/src/main/java/org/chorem/jtimer/entities/TimerTaskHelper.java +++ b/src/main/java/org/chorem/jtimer/entities/TimerTaskHelper.java @@ -341,10 +341,10 @@ public class TimerTaskHelper { * @param task the task to make a JSON from * @return result the string in JSON */ - public static JsonObject taskToJSONFormat(TimerTask task, boolean withAnnotations) { + public static JsonObject taskToJSONFormat(TimerTask task, boolean withAnnotations, String timezone) { Date startDate = task.getAllDaysAndTimes().firstKey(); Date endDate = new Date(); - JsonObject resultingJSON = taskToJSONFormat(task, startDate, endDate, withAnnotations); + JsonObject resultingJSON = taskToJSONFormat(task, startDate, endDate, withAnnotations, timezone); return resultingJSON; } @@ -360,7 +360,7 @@ public class TimerTaskHelper { * @param withAnnotations true if annotations included * @return result the string in JSON */ - public static JsonObject taskToJSONFormat(TimerTask task, Date startDate, Date endDate, boolean withAnnotations) { + public static JsonObject taskToJSONFormat(TimerTask task, Date startDate, Date endDate, boolean withAnnotations, String timezone) { JsonObject responseJSON = new JsonObject(); LocalDate startPeriodDate = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); LocalDate endPeriodDate = endDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); @@ -377,7 +377,7 @@ public class TimerTaskHelper { //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("periodId", dateString); - periodElement.addProperty("periodStartDate", dateString + "T00:00:00.000+0100"); + periodElement.addProperty("periodStartDate", dateString + "T00:00:00.000" + timezone); periodElement.addProperty("periodDuration", entry.getValue()); if (withAnnotations && getAnnotation(task, entry.getKey()).size() != 0 ) { String annotations = ""; diff --git a/src/main/java/org/chorem/jtimer/io/TimerTaskSynchronizer.java b/src/main/java/org/chorem/jtimer/io/TimerTaskSynchronizer.java index a15f05f..6a1f577 100644 --- a/src/main/java/org/chorem/jtimer/io/TimerTaskSynchronizer.java +++ b/src/main/java/org/chorem/jtimer/io/TimerTaskSynchronizer.java @@ -26,7 +26,7 @@ public class TimerTaskSynchronizer implements DataEventListener { private static Log log = LogFactory.getLog(TimerTaskSynchronizer.class); /** auto sync delay */ - protected long autoSyncDelay = 1000 * 60 * 60 * 2 ; //every 2 hours, check for modifications to sync + protected long autoSyncDelay = 1000 * 10; //every 2 hours, check for modifications to sync /** timer to schedule syncs */ protected Timer timer; @@ -34,6 +34,9 @@ public class TimerTaskSynchronizer implements DataEventListener { /** Tasks to sync */ protected Collection<TimerTask> tasksToSync; + /** Timezone */ + protected String timezone = "+0100"; + public TimerTaskSynchronizer() { timer = new Timer(); timer.schedule(new UpdateTask(), autoSyncDelay, autoSyncDelay); @@ -53,6 +56,16 @@ public class TimerTaskSynchronizer implements DataEventListener { } /** + * Change the timezone + * @param timezone + */ + public void setTimezone(String timezone) { + if (!timezone.isEmpty() && !timezone.equals(null)) { + this.timezone = timezone; + } + } + + /** * Inner Task class to make the automatic sync */ protected class UpdateTask extends java.util.TimerTask { @@ -102,11 +115,11 @@ public class TimerTaskSynchronizer implements DataEventListener { * @return boolean for failure of sync (maybe make it return an int for better treatment of failure) */ public boolean synchronizeSingleTask(TimerTask task) { - String updateJsonString = TimerTaskHelper.taskToJSONFormat(task, false).toString(); + String updateJsonString = TimerTaskHelper.taskToJSONFormat(task, false, timezone).toString(); String syncURL = task.getSynchronisingURL(); String charset = "UTF-8"; - HttpURLConnection connection = null; - URL url = null; + HttpURLConnection connection; + URL url; if (syncURL != null) { try { url = new URL(syncURL); 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 df85ba6..66e165c 100644 --- a/src/main/java/org/chorem/jtimer/ui/report/TimerTaskUpdaterView.java +++ b/src/main/java/org/chorem/jtimer/ui/report/TimerTaskUpdaterView.java @@ -82,6 +82,9 @@ public class TimerTaskUpdaterView extends FrameView implements DocumentListener /** can update */ protected boolean canUpdate; + /** timezone */ + protected String timezone; + /** * UpdaterView constructor. * @@ -98,6 +101,8 @@ public class TimerTaskUpdaterView extends FrameView implements DocumentListener this.core = core; this.task = task; setComponent(getMainComponent()); + //default the timezone + timezone = "+0100"; } @@ -335,7 +340,7 @@ public class TimerTaskUpdaterView extends FrameView implements DocumentListener // make the JSON Object of information updateJson = taskToJSONFormat(task, datePickerFrom.getDate(), - datePickerTo.getDate(), isIncludingAnnotations()); + datePickerTo.getDate(), isIncludingAnnotations(), timezone); //make it human readable Gson gson = new GsonBuilder().setPrettyPrinting().create(); String jsonOutput = gson.toJson(updateJson); @@ -384,45 +389,59 @@ public class TimerTaskUpdaterView extends FrameView implements DocumentListener */ @Action(enabledProperty = "updatingEnabled") public void sendUpdate() { - String syncURl = task.getSynchronisingURL(); + String syncURL = task.getSynchronisingURL(); String charset = "UTF-8"; - String query=""; - try { - query = String.format("json=%s", URLEncoder.encode(updateJson.toString(), charset)); - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - } + if (syncURL != null) { + HttpURLConnection connection= null; + URL url = null; + try { + url = new URL(syncURL); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + try { + connection = (HttpURLConnection) url.openConnection(); + } catch (IOException e) { + e.printStackTrace(); + } + + connection.setUseCaches(false); + connection.setDoInput(true); + connection.setRequestProperty("Content-Length", "" + updateJson.toString().length()); + connection.setDoOutput(true); + connection.setRequestProperty("Accept-Charset", charset); + connection.setRequestProperty("Content-Type", "application/json" + charset); + try { + connection.setRequestMethod("POST"); + } catch (ProtocolException e) { + e.printStackTrace(); + } + byte[] postDataBytes = new byte[0]; + try { + postDataBytes = updateJson.toString().getBytes("UTF-8"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + try { + connection.getOutputStream().write(postDataBytes); + } catch (IOException 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(); - } //get the header fields Map<String, List<String>> map = connection.getHeaderFields(); boolean hasUpdated = false; //check for positive answer from server for (Map.Entry<String, List<String>> entry : map.entrySet()) { - for (String s : entry.getValue()) { - if (s.equals("HTTP/1.1 200 OK")) { - hasUpdated = true; - } + for (String s : entry.getValue()) { + if (s.equals("HTTP/1.1 200 OK")) { + hasUpdated = true; } } + } if (hasUpdated) { core.getData().editTaskLastSync(task, LocalDateTime.now()); - } - else { + } else { log.error("Update error, wrong URL?"); String message = getResourceMap().getString("action.updateError"); String title = getResourceMap().getString("action.updateErrorTitle"); @@ -430,6 +449,7 @@ public class TimerTaskUpdaterView extends FrameView implements DocumentListener JOptionPane.ERROR_MESSAGE); } + } } @Override -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.