Author: obruce Date: 2014-05-15 14:50:25 +0200 (Thu, 15 May 2014) New Revision: 2977 Url: http://forge.chorem.org/projects/jtimer/repository/revisions/2977 Log: UI: ajout de la modification des periodes, changement todolist en entities enregistrement todolist dans le localstorage serveur : ajout modification des periodes Modified: branches/ng-jtimer/src/main/java/org/chorem/jtimer/storage/Storage.java branches/ng-jtimer/src/main/java/org/chorem/jtimer/web/TimeResource.java branches/ng-jtimer/src/main/webapp/js/app.js branches/ng-jtimer/src/main/webapp/js/controllers.js branches/ng-jtimer/src/main/webapp/js/entities.js branches/ng-jtimer/src/main/webapp/partials/timeModal.html Modified: branches/ng-jtimer/src/main/java/org/chorem/jtimer/storage/Storage.java =================================================================== --- branches/ng-jtimer/src/main/java/org/chorem/jtimer/storage/Storage.java 2014-05-14 16:22:39 UTC (rev 2976) +++ branches/ng-jtimer/src/main/java/org/chorem/jtimer/storage/Storage.java 2014-05-15 12:50:25 UTC (rev 2977) @@ -1,23 +1,15 @@ package org.chorem.jtimer.storage; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; - import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.chorem.jtimer.entities.TimerTask; import org.chorem.jtimer.entities.TimerTime; -import org.chorem.jtimer.web.TasksResource; +import java.sql.*; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + /** * Implementation du stockage des taches en base de données basée sur h2. * @@ -32,8 +24,6 @@ */ public class Storage { - private final static Logger LOGGER = Logger.getLogger(TasksResource.class.getName()); - private static final Log log = LogFactory.getLog(Storage.class); protected static final String TABLE_TASK = "task"; @@ -70,8 +60,8 @@ } protected Connection getConnection() throws SQLException { - - String url = "/home/olivia/Bureau/jtimer/jtimer"; + //TODO path a definir + String url = "/tmp/jtimer/jtimer"; if (log.isInfoEnabled()) { log.info("Opening connection to database : " + url); } @@ -305,6 +295,28 @@ } } + public void modifyTime(TimerTime t) { + PreparedStatement statement = null; + try { + statement = connection.prepareStatement("UPDATE " + + TABLE_TIME + " SET date=?, duration=?" + + " WHERE uuid = ?"); + statement.setLong(1, t.getCreationDate().getTime()); + statement.setLong(2, t.getTime()); + statement.setString(3, t.getTimeId() ); + statement.executeUpdate(); + } catch (SQLException ex) { + throw new StorageException("Can't modify time", ex); + } finally { + closeStatement(statement); + } + } + + /** + * Returns task number with ID + * @param id + * @return + */ public long getTaskNumber(String id) { PreparedStatement statement = null; long result= 0; @@ -352,8 +364,6 @@ statement.setLong(1, number); statement.setLong(2, time.getCreationDate().getTime()); - - LOGGER.log(Level.WARNING, "Storage add times datetime " +time.getCreationDate().getTime()); statement.setString(3, time.getTimeId()); statement.setLong(4, time.getTime()); statement.executeUpdate(); @@ -460,4 +470,5 @@ } } + } \ No newline at end of file Modified: branches/ng-jtimer/src/main/java/org/chorem/jtimer/web/TimeResource.java =================================================================== --- branches/ng-jtimer/src/main/java/org/chorem/jtimer/web/TimeResource.java 2014-05-14 16:22:39 UTC (rev 2976) +++ branches/ng-jtimer/src/main/java/org/chorem/jtimer/web/TimeResource.java 2014-05-15 12:50:25 UTC (rev 2977) @@ -1,7 +1,6 @@ package org.chorem.jtimer.web; import com.google.gson.*; -import org.chorem.jtimer.entities.TimerTask; import org.chorem.jtimer.entities.TimerTime; import org.chorem.jtimer.storage.Storage; import org.restlet.data.MediaType; @@ -13,8 +12,6 @@ import java.lang.reflect.Type; import java.util.Date; import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; public class TimeResource extends ServerResource { @@ -96,5 +93,18 @@ storage.addTaskTime(t,number); } + /** + * Methode qui met à jour une tache + * suite d'une requête de type PUT + */ + @Put("json") + public void updateTime(Representation representation) throws IOException{ + Gson gson = builder.create(); + String repr1 = representation.getText(); + TimerTime t = gson.fromJson(repr1, TimerTime.class); + + storage.modifyTime(t); + } + } Modified: branches/ng-jtimer/src/main/webapp/js/app.js =================================================================== --- branches/ng-jtimer/src/main/webapp/js/app.js 2014-05-14 16:22:39 UTC (rev 2976) +++ branches/ng-jtimer/src/main/webapp/js/app.js 2014-05-15 12:50:25 UTC (rev 2977) @@ -272,8 +272,22 @@ var item = window.localStorage.getItem(key); var result = new WebTimerData(); result.fromJson(item); - + return result; + }, + setTodo: function(key, value) { + key = webtimerPrefix + key; + // add version, to check version during read (get) + var item = value.toJson(); + window.localStorage.setItem(key, item); + }, + getTodo: function(key) { + key = webtimerPrefix + key; + var item = window.localStorage.getItem(key); + var result = new tdListData(); + result.fromJson(item); + + return result; } }; }) Modified: branches/ng-jtimer/src/main/webapp/js/controllers.js =================================================================== --- branches/ng-jtimer/src/main/webapp/js/controllers.js 2014-05-14 16:22:39 UTC (rev 2976) +++ branches/ng-jtimer/src/main/webapp/js/controllers.js 2014-05-15 12:50:25 UTC (rev 2977) @@ -5,6 +5,9 @@ // {WebTimerData} toutes les donnees $scope.data = $localStorage.get("data"); + //{tdListData} toutes les donnees non envoyees + $scope.todo = $localStorage.getTodo("todo"); + // {Date} last update to server $scope.acces = moment().format("DD-MM-YYYY H:mm:ss"); @@ -43,16 +46,6 @@ // {boolean} boolean qui indique si une inactivite a ete detectee $scope.webSocketClientIdle = false; - - // {Array of TaskID} la file de tache attendant la synchronisation avec le serveur - var stockedDeletedTasks = []; - // {Array of Task} - var stockedNewTasks = []; - var stockedEditedTasks = []; - // {key: taskId, value: Array of TaskTime} la file de temps des taches attendant la synchronisation avec le serveur - var stockedNewTimes = {}; - var stockedEditedTimes = {}; - /** * Met a jour la tache selectionne, si la tache selectionnee est la courante * alors on met la selection a null @@ -109,6 +102,7 @@ */ var save = function() { $localStorage.set("data", $scope.data); + $localStorage.setTodo("todo", $scope.todo); }; /** @@ -168,34 +162,35 @@ } var pushChangesToServ= function(){ + //On supprime de la base les taches sotckees pour suppression - angular.forEach(stockedDeletedTasks, function(task){ + angular.forEach($scope.todo.stockedDeletedTasks, function(task){ serverTaskAccess.delete({taskId: task}, function(){ console.log("delete success" + task); - stockedDeletedTasks.shift(); + $scope.todo.stockedDeletedTasks.shift(); }, function(){ console.log("fail"); }); }); //On ajoute au serveur les taches stockees pour l'ajout - angular.forEach(stockedNewTasks, function(task){ + angular.forEach($scope.todo.stockedNewTasks, function(task){ serverTaskAccess.create(angular.toJson(task), function(){ console.log("persist task success" + task); - stockedNewTasks.shift(); + $scope.todo.stockedNewTasks.shift(); }, function(){ console.log("fail"); }); }); //On ajoute au serveur les taches stockees pour l'ajout - angular.forEach(stockedEditedTasks, function(task){ + angular.forEach($scope.todo.stockedEditedTasks, function(task){ serverTaskAccess.update(angular.toJson(task), function(){ console.log("update task success" + task); - stockedEditedTasks.shift(); + $scope.todo.stockedEditedTasks.shift(); }, function(){ console.log("fail"); @@ -203,18 +198,34 @@ }); //On ajoute les nouveaux temps au serveur - angular.forEach(stockedNewTimes, function(times,task){ + angular.forEach($scope.todo.stockedNewTimes, function(times,task){ angular.forEach(times, function(time){ serverTimeAccess.create({taskId: task} , angular.toJson(time), function(){ console.log("persist time success" + task); - stockedNewTimes[task].shift(); + $scope.todo.stockedNewTimes[task].shift(); }, function(){ console.log("fail"); }); }); }); + + //On update les temps sur le serveur + angular.forEach($scope.todo.stockedEditedTimes, function(times,task){ + angular.forEach(times, function(time){ + serverTimeAccess.update({taskId: task}, angular.toJson(time), + function(){ + console.log("update time success" + task); + $scope.todo.stockedEditedTimes[task].shift(); + }, + function(){ + console.log("fail"); + }); + }); + }); + + save(); } @@ -273,7 +284,7 @@ $scope.data.tasks[newTask.taskId] = newTask; //On ajoute à la file de synchro - stockedNewTasks.push(newTask); + $scope.todo.stockedNewTasks.push(newTask); $scope.name = ""; save(); @@ -293,7 +304,7 @@ $scope.data.tasks[newTask.taskId] = newTask; //On ajoute à la file de synchro - stockedNewTasks.push(newTask); + $scope.todo.stockedNewTasks.push(newTask); node.$$open = true; save(); @@ -314,13 +325,14 @@ delete $scope.data.tasks[task.taskId]; delete $scope.data.times[task.taskId]; - stockedDeletedTasks.push(task.taskId); - delete stockedNewTimes[task.taskId]; + $scope.todo.stockedDeletedTasks.push(task.taskId); + delete $scope.todo.stockedNewTimes[task.taskId]; var children = $scope.getChildren(task); removeRecurse(children); + save(); }); - save(); + }; // mark all task as removed removeRecurse([node.task]); @@ -461,7 +473,7 @@ */ $scope.saveTask = function(node) { node.edit = null; - stockedEditedTasks.push(node.task); + $scope.todo.stockedEditedTasks.push(node.task); save(); }; @@ -478,8 +490,8 @@ var taskTime = $scope.getRecentTaskTime($scope.currentTask); taskTime.addTime(now - $scope.currentTaskDate); - if(!stockedNewTimes[task.taskId]){stockedNewTimes[task.taskId]=[];} - stockedNewTimes[task.taskId].push(taskTime); + if(!$scope.todo.stockedNewTimes[task.taskId]){$scope.todo.stockedNewTimes[task.taskId]=[];} + $scope.todo.stockedNewTimes[task.taskId].push(taskTime); } //si ce n'est la la tache courante actuel if ($scope.currentTask !== task) { @@ -583,15 +595,17 @@ modalInstance.result.then(function (item) { if(item.index != -1){ - //changement + //On met la valeur à jour $scope.data.times[id][item.index].changeCreationDate(item.creationDate); $scope.data.times[id][item.index].changeDuration(item.time); //init - if(!stockedEditedTimes[id]){stockedEditedTimes[id]=[]; } + if(!$scope.todo.stockedEditedTimes[id]){$scope.todo.stockedEditedTimes[id]=[]; } - stockedEditedTimes[id].push($scope.data.times[id][item.index]); - console.log(stockedEditedTimes); + //On ajoute dans la tdList + $scope.todo.stockedEditedTimes[id].push($scope.data.times[id][item.index]); + + save(); } }); } @@ -644,6 +658,7 @@ $scope.getStartObjTime = function(){return moment($scope.obj.creationDate).format("DD-MM-YYYY H:mm:ss");} $scope.getStopObjTime = function(){return moment($scope.obj.creationDate + $scope.obj.time).format("DD-MM-YYYY H:mm:ss");} + $scope.getObjDuration = function(){return $scope.obj.time;} //selectionne un element dans le dropdown $scope.select = function(item, ind){ @@ -654,14 +669,15 @@ $scope.obj.creationDate=item.creationDate; $scope.obj.time=item.time; - $scope.aTime = new Date(item.creationDate); + $scope.aTime = item.creationDate; } - // Methode a la fermeture + // Methode a la fermeture avec ok $scope.ok = function () { $modalInstance.close($scope.obj); }; + //Methode a la fermeture avec cancel $scope.cancel = function () { $modalInstance.dismiss('cancel'); }; @@ -671,38 +687,53 @@ //Activated button radio $scope.radioModel = 'Left'; - $scope.aTime = new Date($scope.activTime.creationDate); //Le pas des heures et minutes $scope.hstep = 1; $scope.mstep = 1; + //La date max + var maxDate = new Date($scope.activTime.creationDate); + maxDate.setHours(23,59,59,999); + + + /* + * Méthode déclancher quand l'heure change dans le timepicker + * Change l'objet du controller principal + */ $scope.changeModel = function(){ - //TODO obruce : ici il faut mettre des contraintes pour pas depasser 24h - //TODO pas depasser dans l'autre sens + + //Si On modifie sur la date de debut if(($scope.radioModel || 'null') == 'Left'){ $scope.obj.creationDate = $scope.aTime.getTime(); - console.log($scope.obj.creationDate); - }else{ + //si crea+temps superieur à 23:59 + if(($scope.obj.creationDate+$scope.obj.time) > maxDate.getTime()){ + $scope.aTime=maxDate.getTime()-$scope.obj.time; + $scope.obj.creationDate = maxDate.getTime()-$scope.obj.time; + } + + }else{//On modifie la date de fin + if($scope.aTime.getTime() > $scope.activTime.creationDate){ - var duree = $scope.aTime.getTime() - $scope.activTime.creationDate ; + //Si l'heure de fin est superieur a l'heure de depart + var duree = $scope.aTime.getTime() - $scope.obj.creationDate ; $scope.obj.time = duree; }else{ - $scope.aTime = new Date($scope.obj.creationDate ); - duree = 0; + //Empeche de depasser 23:59 + $scope.aTime = maxDate; + $scope.obj.time = maxDate.getTime() - $scope.obj.creationDate; } - console.log($scope.obj.time); } } //Change l'heure dans le timePicker selon debut/fin $scope.changeFonc=function(){ if(($scope.radioModel || 'null') == 'Left'){ - $scope.aTime = new Date($scope.obj.creationDate); + $scope.aTime = $scope.obj.creationDate; }else{ - $scope.aTime = new Date($scope.obj.creationDate + $scope.activTime.time); + $scope.aTime = $scope.obj.creationDate + $scope.obj.time; } } }; Modified: branches/ng-jtimer/src/main/webapp/js/entities.js =================================================================== --- branches/ng-jtimer/src/main/webapp/js/entities.js 2014-05-14 16:22:39 UTC (rev 2976) +++ branches/ng-jtimer/src/main/webapp/js/entities.js 2014-05-15 12:50:25 UTC (rev 2977) @@ -55,9 +55,82 @@ } }; +/** + * Toutes les donnees qui demandent d'etre traitees par le serveur + * @returns {tdListData} + */ +var tdListData = function(){ + // {Array of TaskID} la file de tache attendant la synchronisation avec le serveur + this.stockedDeletedTasks = []; + // {Array of Task} file de tache attendant la synchronisation avec le serveur + this.stockedNewTasks = []; + this.stockedEditedTasks = []; + // {key: taskId, value: Array of TaskTime} la file de temps attendant la synchronisation avec le serveur + this.stockedNewTimes = {}; + this.stockedEditedTimes = {}; +}; + + /** - * Toutes les donnees qui sont a stocker entre 2 sessions + * Converti l'objet tdListData en Json + * @returns {JSON} + */ +tdListData.prototype.toJson = function() { + var result = angular.toJson(this); + return result; +}; + +/** + * Charge des donnees au format JSON les convertit dans le bon type d'objet, + * fait la migration des data si necessaire + * @param {JSON or JsonString} json + * @returns {tdListData} retourne lui meme + */ +tdListData.prototype.fromJson = function(json) { + if (angular.isString(json)) { + json = angular.fromJson(json); + } + if (json) { + + angular.forEach(json.stockedDeletedTasks, function (t) { + this.stockedDeletedTasks.push(t); + }, this); + + angular.forEach(json.stockedNewTasks, function (t) { + t = jQuery.extend(new Task(), t); + this.stockedNewTasks.push(t); + }, this); + + angular.forEach(json.stockedEditedTasks, function (t) { + t = jQuery.extend(new Task(), t); + this.stockedEditedTasks.push(t); + }, this); + + angular.forEach(json.stockedNewTimes, function (times, k) { + this.stockedNewTimes[k] = []; + angular.forEach(times, function (t) { + t = jQuery.extend(new TaskTime(), t); + this.stockedNewTimes[k].push(t); + }, this); + }, this); + + angular.forEach(json.stockedEditedTimes, function (times, k) { + this.stockedEditedTimes[k] = []; + angular.forEach(times, function (t) { + t = jQuery.extend(new TaskTime(), t); + this.stockedEditedTimes[k].push(t); + }, this); + }, this); + } + + return this; +}; + + + +/** + * Toutes les donnees de la fenetre qui sont a stocker entre 2 sessions * @returns {WebTimerData} */ var WebTimerData = function() { Modified: branches/ng-jtimer/src/main/webapp/partials/timeModal.html =================================================================== --- branches/ng-jtimer/src/main/webapp/partials/timeModal.html 2014-05-14 16:22:39 UTC (rev 2976) +++ branches/ng-jtimer/src/main/webapp/partials/timeModal.html 2014-05-15 12:50:25 UTC (rev 2977) @@ -46,7 +46,7 @@ </div> - <alert type="info" >Début : {{getStartObjTime()}} - Fin : {{getStopObjTime(activTime)}}</alert> + <alert type="info" >Début : {{getStartObjTime()}} - Fin : {{getStopObjTime()}} - Duree : {{getObjDuration() | time}} </alert> </div>