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 e2106d5e24314ab00b9b93a1de2ac16f5e5a4520 Author: servantie <servantie.c@gmail.com> Date: Wed Jun 22 16:38:43 2016 +0200 added logs, made method synchronizeTaskOnURL static and returning an int (http response) --- .../java/org/chorem/jtimer/entities/SyncInfo.java | 4 +- .../java/org/chorem/jtimer/entities/TimerTask.java | 2 +- .../chorem/jtimer/io/TimerTaskSynchronizer.java | 43 +++--- .../jtimer/ui/report/TimerTaskUpdaterView.java | 160 +++++++++++---------- .../resources/TimerTaskUpdaterView.properties | 1 + .../resources/TimerTaskUpdaterView_fr.properties | 1 + 6 files changed, 110 insertions(+), 101 deletions(-) diff --git a/src/main/java/org/chorem/jtimer/entities/SyncInfo.java b/src/main/java/org/chorem/jtimer/entities/SyncInfo.java index f1fee01..1eb90c4 100644 --- a/src/main/java/org/chorem/jtimer/entities/SyncInfo.java +++ b/src/main/java/org/chorem/jtimer/entities/SyncInfo.java @@ -57,10 +57,10 @@ public class SyncInfo { } /** - * Sets the isActiveSync boolean + * Sets the setIsActiveSync boolean * @param isActive boolean */ - public void isActiveSync(boolean isActive) { + public void setIsActiveSync(boolean isActive) { isActiveSync = isActive; } diff --git a/src/main/java/org/chorem/jtimer/entities/TimerTask.java b/src/main/java/org/chorem/jtimer/entities/TimerTask.java index f782e55..070f3d0 100644 --- a/src/main/java/org/chorem/jtimer/entities/TimerTask.java +++ b/src/main/java/org/chorem/jtimer/entities/TimerTask.java @@ -372,7 +372,7 @@ public class TimerTask implements Cloneable, * @param syncInfo the SyncInfo */ public void setIsActive(boolean isActive, SyncInfo syncInfo) { - syncInfo.isActiveSync(isActive); + syncInfo.setIsActiveSync(isActive); } /** diff --git a/src/main/java/org/chorem/jtimer/io/TimerTaskSynchronizer.java b/src/main/java/org/chorem/jtimer/io/TimerTaskSynchronizer.java index ea3660c..5063ce9 100644 --- a/src/main/java/org/chorem/jtimer/io/TimerTaskSynchronizer.java +++ b/src/main/java/org/chorem/jtimer/io/TimerTaskSynchronizer.java @@ -112,18 +112,23 @@ public class TimerTaskSynchronizer implements DataEventListener { List<JsonObject> jsonObjectList = TimerTaskHelper.taskToJSONFormat(task, false, timezone); for (JsonObject object : jsonObjectList) { - boolean successfulSync = synchronizeTaskOnURL(object); + int SyncResult = synchronizeTaskOnURL(object); + boolean successfulSync = false; + String syncURL = object.get("URL").getAsString(); + if (SyncResult > 199 && SyncResult < 300) { + successfulSync = true; + } + else if (SyncResult == 0) { + log.debug("Error in connection " + syncURL); + } //sync successful -> change the last sync time if (successfulSync) { - log.debug("Sync successful on " + object.get("URL").getAsString()); - task.setLastSync(LocalDateTime.now(), object.get("URL").getAsString()); + log.debug("Sync successful on " + syncURL); + task.setLastSync(LocalDateTime.now(), syncURL); } //-> do not change last sync time else { - log.debug("Sync failed on " + object.get("URL").getAsString()); - //deactivate autosync if the sync failed - task.getSynchronizingInfo(object.get("URL").getAsString()).isActiveSync(false); - log.info("Adress deactivated"); + log.debug("Sync failed on " + syncURL); } } @@ -132,16 +137,16 @@ public class TimerTaskSynchronizer implements DataEventListener { /** * Sends one synchronization (one JSON object) * @param object the object to sync - * @return a boolean indicating failure or success + * @return an int (http response code or 0 in case of a problem) */ - public boolean synchronizeTaskOnURL(JsonObject object) { + public static int synchronizeTaskOnURL(JsonObject object) { String updateJsonString = object.toString(); String syncURL = object.get("URL").getAsString(); String charset = "UTF-8"; HttpURLConnection connection; URL url; byte[] postDataBytes; - boolean hasUpdated = false; + int upDateValue = 0; try { url = new URL(syncURL); connection = (HttpURLConnection) url.openConnection(); @@ -154,23 +159,19 @@ public class TimerTaskSynchronizer implements DataEventListener { connection.setRequestMethod("POST"); postDataBytes = updateJsonString.getBytes(charset); connection.getOutputStream().write(postDataBytes); - int code = connection.getResponseCode(); - if ((code == 200) || (code == 201) || (code == 202)){ - hasUpdated = true; - } - else { - log.info("Error"); - } + upDateValue = connection.getResponseCode(); } catch (MalformedURLException e) { - log.error("URL malformed"); + log.error("URL malformed : " + syncURL); } catch (ProtocolException e) { log.error("Protocol error."); } catch (UnsupportedEncodingException e) { - log.error("Problem with encoding"); + log.error("Problem with encoding " + syncURL); } catch (IOException e) { - log.error("Problem with the connection"); + log.error("Problem with the connection " + syncURL); + }catch (IllegalArgumentException e) { + log.error("Port value not valid " + syncURL); } - return hasUpdated; + return upDateValue; } @Override 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 e0be355..3844abb 100644 --- a/src/main/java/org/chorem/jtimer/ui/report/TimerTaskUpdaterView.java +++ b/src/main/java/org/chorem/jtimer/ui/report/TimerTaskUpdaterView.java @@ -31,6 +31,7 @@ import org.chorem.jtimer.JTimer; import org.chorem.jtimer.data.TimerCore; import org.chorem.jtimer.entities.SyncInfo; import org.chorem.jtimer.entities.TimerTask; +import org.chorem.jtimer.io.TimerTaskSynchronizer; import org.jdesktop.application.*; import org.jdesktop.application.Action; import org.jdesktop.swingx.JXDatePicker; @@ -43,9 +44,8 @@ import javax.swing.event.DocumentListener; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.net.*; +import java.awt.event.ItemEvent; +import java.awt.event.ItemListener; import java.text.DateFormat; import java.time.LocalDate; import java.time.LocalDateTime; @@ -59,7 +59,7 @@ import static org.chorem.jtimer.entities.TimerTaskHelper.taskToJSONFormat; * * Created by servantie on 13/05/16. */ -public class TimerTaskUpdaterView extends FrameView implements DocumentListener, ActionListener { +public class TimerTaskUpdaterView extends FrameView implements DocumentListener, ActionListener, ItemListener { /** Class logger */ protected static Log log = LogFactory.getLog(TimerTaskUpdaterView.class); @@ -113,10 +113,10 @@ public class TimerTaskUpdaterView extends FrameView implements DocumentListener, this.core = core; this.task = task; timezone = "+01:00"; + updateJson = new ArrayList<>(); setComponent(getMainComponent()); - updateJson = new ArrayList<>(); } @@ -170,6 +170,7 @@ public class TimerTaskUpdaterView extends FrameView implements DocumentListener, panelGeneral.add(previousWeekButton, new GridBagConstraints(2, 1, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(1, 1, 1, 1), 0, 0)); + // Option for period grouping JPanel typePanel = new JPanel(new GridLayout(0, 2)); panelGeneral.add(typePanel, new GridBagConstraints(0, 2, 3, 1, 0, 0, @@ -191,17 +192,19 @@ public class TimerTaskUpdaterView extends FrameView implements DocumentListener, JXTaskPane panelOption = new JXTaskPane(getResourceMap().getString("updateOptions")); panelOption.setLayout(new GridBagLayout()); + //checkbox to include annotations checkIncludesAnnotations = new JCheckBox(); checkIncludesAnnotations.setAction(getContext().getActionMap(this).get("isIncludingAnnotations")); - panelOption.add(checkIncludesAnnotations, new GridBagConstraints(1, 1, 1, 1, 0, 0, - GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(1, 1, 1, 1), 0, 0)); + panelOption.add(checkIncludesAnnotations, new GridBagConstraints(2, 4, 1, 1, 0, 0, + GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(4, 1, 1, 1), 0, 0)); //url combobox JLabel urlcomboBoxLabel = new JLabel(getResourceMap().getString("urlComboLabel")); urlComboBox = new JComboBox<>(); //make it editable to add new urls urlComboBox.setEditable(true); + urlComboBox.addItemListener(this); urlComboBox.addActionListener(this); //add the urls to the combobox for (String url : task.getSynchronizingURLList()) { @@ -209,10 +212,10 @@ public class TimerTaskUpdaterView extends FrameView implements DocumentListener, } urlComboBox.setActionCommand("comboBox"); - panelOption.add(urlcomboBoxLabel, new GridBagConstraints(1, 2, 1, 1, 0, 0, - GridBagConstraints.WEST, GridBagConstraints.VERTICAL, new Insets(2,1,1,1), 0, 0)); - panelOption.add(urlComboBox, new GridBagConstraints(2, 2, 1, 1, 0, 0, - GridBagConstraints.WEST, GridBagConstraints.VERTICAL, new Insets(1, 2, 1, 1), 0, 0)); + panelOption.add(urlcomboBoxLabel, new GridBagConstraints(1, 1, 1, 1, 0, 0, + GridBagConstraints.WEST, GridBagConstraints.VERTICAL, new Insets(1,1,1,1), 0, 0)); + panelOption.add(urlComboBox, new GridBagConstraints(2, 1, 1, 1, 0, 0, + GridBagConstraints.WEST, GridBagConstraints.VERTICAL, new Insets(1, 1, 1, 1), 0, 0)); //display last Sync time JLabel lastSyncDateLabel = new JLabel(getResourceMap().getString("lastUpdateLabel")); @@ -222,6 +225,7 @@ public class TimerTaskUpdaterView extends FrameView implements DocumentListener, lastUpdate = new JLabel(); //checkbox for active sync checkIsActiveSync = new JCheckBox(); + checkIsActiveSync.addActionListener(this); checkIsActiveSync.setAction(getContext().getActionMap(this).get("isActiveSync")); if (urlComboBox.getSelectedItem() != null) { if (task.getSynchronizingURLList().contains(urlComboBox.getSelectedItem())){ @@ -231,7 +235,10 @@ public class TimerTaskUpdaterView extends FrameView implements DocumentListener, if ((lastSyncTime != null) && lastSyncTime.isAfter(LocalDateTime.MIN)) { lastUpdate.setText(lastSyncTime.toString()); //logging change of lastSync - log.info(lastSyncTime.toString()); + log.info("Last Sync time loaded : " + lastSyncTime.toString()); + } + else { + log.info("No Last Sync time loaded"); } checkIsActiveSync.setSelected(task.getSynchronizingInfo((String) urlComboBox.getSelectedItem()).getActiveSync()); } @@ -422,11 +429,13 @@ public class TimerTaskUpdaterView extends FrameView implements DocumentListener, } /** - * Active sync checkbox checked + * Active sync checkbox checked, activates sync */ @Action public boolean isActiveSync() { boolean isActiveSyncURL = checkIsActiveSync.isSelected(); + task.getSynchronizingInfo((String) urlComboBox.getSelectedItem()).setIsActiveSync(isActiveSyncURL); + log.info("Activity of url changed "); return isActiveSyncURL; } @@ -471,75 +480,37 @@ public class TimerTaskUpdaterView extends FrameView implements DocumentListener, * @param object json of task info */ public void synchronizeTaskOnURL(TimerTask task, JsonObject object) { - String updateJsonString = object.toString(); + int syncAnswer = TimerTaskSynchronizer.synchronizeTaskOnURL(object); String syncURL = object.get("URL").getAsString(); - String charset = "UTF-8"; - HttpURLConnection connection; - URL url; - byte[] postDataBytes; + String message =""; + String title = ""; boolean hasUpdated = false; - String message = ""; - String title = getResourceMap().getString("action.updateErrorTitle"); - try { - url = new URL(syncURL); - connection = (HttpURLConnection) url.openConnection(); - connection.setUseCaches(false); - connection.setDoInput(true); - connection.setDoOutput(true); - connection.setRequestProperty("Content-Length", Integer.toString(updateJsonString.length())); - connection.setRequestProperty("Accept-Charset", charset); - connection.setRequestProperty("Content-Type", "application/json"); - connection.setRequestMethod("POST"); - //logging connection - log.info("Sending json to : " + syncURL); - - postDataBytes = updateJsonString.getBytes(charset); - connection.getOutputStream().write(postDataBytes); - int code = connection.getResponseCode(); - - //logging code return - log.info("Code return : " + code); - - if ((code == 200) || (code == 2001) || (code == 202)) { + if ((syncAnswer == 200) || (syncAnswer == 201) || (syncAnswer == 202)) { hasUpdated = true; + log.info("Update accepted for URL : " + syncURL); } - else if (code == 400) { - log.debug("Bad Request"); + else if (syncAnswer == 400) { + log.debug("Bad Request for URL : " + syncURL); message = getResourceMap().getString("action.update400"); } - else if (code == 404) { - log.debug("Not Found"); + else if (syncAnswer == 404) { + log.debug("URL Not Found : " + syncURL); message = getResourceMap().getString("action.update404"); } - else if (code == 500) { - log.debug("Server Error"); + else if (syncAnswer == 500) { + log.debug("Server Error on URL : " + syncURL); message = getResourceMap().getString("action.update500"); } else { log.debug("Error"); message = getResourceMap().getString("action.updateError"); } - connection.disconnect(); - - } catch (MalformedURLException e) { - log.debug("URL malformed"); - message = getResourceMap().getString("action.updateURLError"); - } catch (ProtocolException e) { - log.debug("Protocol error."); - message = getResourceMap().getString("action.updateProtocolError"); - } catch (UnsupportedEncodingException e) { - log.debug("Problem with encoding"); - message = getResourceMap().getString("action.updateEncodingError"); - } catch (IOException e) { - log.debug("Problem with the connection"); - message = getResourceMap().getString("action.updateError"); - } finally { if (hasUpdated) { - task.setLastSync(LocalDateTime.now(),syncURL); + task.setLastSync(LocalDateTime.now(), syncURL); + core.getData().changeSyncInfo(task, syncURL); } else if (!message.isEmpty()){ errorBox(message, title); } - } //advise that it's done, to grey-out the send sync button boolean oldValue = canUpdate; canUpdate = false; @@ -587,23 +558,58 @@ public class TimerTaskUpdaterView extends FrameView implements DocumentListener, public void itemAdded(){ String urlToAdd = (String) urlComboBox.getSelectedItem(); if ((!urlToAdd.isEmpty()) && (!task.getSynchronizingURLList().contains(urlToAdd))) { - urlComboBox.addItem(urlToAdd); - task.addSyncInfo(urlToAdd); - core.getData().changeSyncInfo(task, urlToAdd); + urlComboBox.addItem(urlToAdd); + task.addSyncInfo(urlToAdd); + core.getData().changeSyncInfo(task, urlToAdd); + log.info("New SyncInfo with URL : " + urlToAdd); } - if (lastUpdate != null) { - if (task.getSynchronizingInfo(urlToAdd).getLastSync().isAfter(LocalDateTime.MIN)) { - lastUpdate.setText(task.getSynchronizingInfo(urlToAdd).getLastSync().toString()); - core.getData().changeSyncInfo(task, urlToAdd); - } - else { - lastUpdate.setText(""); - } + } + + @Override + public void itemStateChanged(ItemEvent itemEvent) { + if (itemEvent.getStateChange()== ItemEvent.SELECTED) { + if (urlComboBox.getSelectedItem() != null) { + String urlToDisplay = urlComboBox.getSelectedItem().toString(); + if (!urlToDisplay.isEmpty()) { + log.info("Selected an object : " + urlComboBox.getSelectedItem().toString()); + //display last Sync Time + if (lastUpdate != null && task.getSynchronizingInfo(urlToDisplay).getLastSync() != null) { + if (task.getSynchronizingInfo(urlToDisplay).getLastSync().isAfter(LocalDateTime.MIN)) { + lastUpdate.setText(task.getSynchronizingInfo(urlToDisplay).getLastSync().toString()); + } + else { + lastUpdate.setText(""); + } + } + } + } } } + + @Override public void actionPerformed(ActionEvent actionEvent) { - itemAdded(); + String actionCommand = actionEvent.getActionCommand(); + if (actionCommand.equals("comboBox")) { + String urlToAdd = (String) urlComboBox.getSelectedItem(); + if (!task.getSynchronizingURLList().contains(urlToAdd)) { + log.info("New URL detected !"); + itemAdded(); + } + } + else if (actionCommand.equals("comboBoxChanged")) { + log.info("There has been a change in the comboBox"); + + } + else if (actionCommand.equals("comboBoxEdited")) { + log.info("The comboBox was edited"); + } + else if (actionCommand.equals(getResourceMap().getString("isActiveSync.Action.text"))) { + log.info("Checkbox ticked, unticked"); + } + else { + log.info("Action performed, not a new URL. Action: " + actionEvent.getActionCommand()); + } } -} +} \ No newline at end of file diff --git a/src/main/resources/org/chorem/jtimer/ui/report/resources/TimerTaskUpdaterView.properties b/src/main/resources/org/chorem/jtimer/ui/report/resources/TimerTaskUpdaterView.properties index 938b3ca..2b669a8 100644 --- a/src/main/resources/org/chorem/jtimer/ui/report/resources/TimerTaskUpdaterView.properties +++ b/src/main/resources/org/chorem/jtimer/ui/report/resources/TimerTaskUpdaterView.properties @@ -65,4 +65,5 @@ action.update500 = Server error action.updateErrorTitle = Synchronization error action.updateEncodingError = Encoding error action.updateURLError = URL error + isActiveSync.Action.text = Active synchronization \ No newline at end of file diff --git a/src/main/resources/org/chorem/jtimer/ui/report/resources/TimerTaskUpdaterView_fr.properties b/src/main/resources/org/chorem/jtimer/ui/report/resources/TimerTaskUpdaterView_fr.properties index 722dc83..db5d2a1 100644 --- a/src/main/resources/org/chorem/jtimer/ui/report/resources/TimerTaskUpdaterView_fr.properties +++ b/src/main/resources/org/chorem/jtimer/ui/report/resources/TimerTaskUpdaterView_fr.properties @@ -65,4 +65,5 @@ action.update404 = Mauvaise URL action.update500 = Erreur du serveur action.updateEncodingError = Erreur d'encodage action.updateURLError = Erreur d'URL + isActiveSync.Action.text = Synchronisation Active \ No newline at end of file -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.