Author: obruce Date: 2014-05-20 11:31:49 +0200 (Tue, 20 May 2014) New Revision: 2979 Url: http://forge.chorem.org/projects/jtimer/repository/revisions/2979 Log: Base/UI ajout date de modification pour les taches Changement algo de synchro Modified: branches/ng-jtimer/src/main/java/org/chorem/jtimer/entities/TimerTask.java branches/ng-jtimer/src/main/java/org/chorem/jtimer/entities/TimerTime.java branches/ng-jtimer/src/main/java/org/chorem/jtimer/storage/Storage.java branches/ng-jtimer/src/main/java/org/chorem/jtimer/web/TasksResource.java branches/ng-jtimer/src/main/webapp/js/controllers.js branches/ng-jtimer/src/main/webapp/js/entities.js branches/ng-jtimer/src/main/webapp/js/service.js branches/ng-jtimer/src/main/webapp/partials/timeModal.html Modified: branches/ng-jtimer/src/main/java/org/chorem/jtimer/entities/TimerTask.java =================================================================== --- branches/ng-jtimer/src/main/java/org/chorem/jtimer/entities/TimerTask.java 2014-05-16 16:18:00 UTC (rev 2978) +++ branches/ng-jtimer/src/main/java/org/chorem/jtimer/entities/TimerTask.java 2014-05-20 09:31:49 UTC (rev 2979) @@ -56,7 +56,7 @@ protected String name; /** Creation date. */ - protected Date creationDate; + protected Date modificationDate; /** Closed task. */ protected boolean closed; @@ -86,7 +86,7 @@ this.name = name; this.taskId = UUID.randomUUID().toString(); this.parent = ""; - this.creationDate= new Date(); + this.modificationDate = new Date(); } /** @@ -100,7 +100,7 @@ this.name = name; this.taskId = UUID.randomUUID().toString(); this.parent = ""; - this.creationDate= new Date(datetime); + this.modificationDate = new Date(datetime); } @@ -178,17 +178,17 @@ * * @return task creation date */ - public Date getCreationDate() { - return creationDate; + public Date getModificationDate() { + return modificationDate; } /** * Set task creation date. * - * @param creationDate creation date + * @param modificationDate creation date */ - public void setCreationDate(Date creationDate) { - this.creationDate = creationDate; + public void setModificationDate(Date modificationDate) { + this.modificationDate = modificationDate; } /** Modified: branches/ng-jtimer/src/main/java/org/chorem/jtimer/entities/TimerTime.java =================================================================== --- branches/ng-jtimer/src/main/java/org/chorem/jtimer/entities/TimerTime.java 2014-05-16 16:18:00 UTC (rev 2978) +++ branches/ng-jtimer/src/main/java/org/chorem/jtimer/entities/TimerTime.java 2014-05-20 09:31:49 UTC (rev 2979) @@ -23,6 +23,9 @@ /** Creation Date*/ protected Date creationDate; + /** Modification Date */ + protected Date modificationDate; + /** Time Elapsed in the period */ protected long time; @@ -41,10 +44,17 @@ this.taskId = taskId; this.time = time; this.creationDate = creationDate; + this.modificationDate = creationDate; } + public Date getModificationDate() { + return modificationDate; + } + public void setModificationDate(Date modificationDate) { + this.modificationDate = modificationDate; + } public String getTaskId() { return taskId; @@ -83,7 +93,7 @@ return "TimerTime{" + ", taskId='" + taskId + '\'' + ", timeId='" + timeId + '\'' + - ", creationDate=" + creationDate + + ", modificationDate=" + creationDate + ", time=" + time + '}'; } 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-16 16:18:00 UTC (rev 2978) +++ branches/ng-jtimer/src/main/java/org/chorem/jtimer/storage/Storage.java 2014-05-20 09:31:49 UTC (rev 2979) @@ -61,8 +61,8 @@ protected Connection getConnection() throws SQLException { //TODO obruce 15-05-14 path a definir - //String url = "/tmp/jtimer/jtimer"; - String url = "/home/olivia/Bureau/jtimer/jtimer"; + String url = "/tmp/jtimer/jtimer"; + //String url = "/home/olivia/Bureau/jtimer/jtimer"; if (log.isInfoEnabled()) { log.info("Opening connection to database : " + url); } @@ -144,6 +144,7 @@ " taskId VARCHAR(255) NOT NULL UNIQUE," + " name VARCHAR(255) NOT NULL," + " parent VARCHAR(255)," + + " modificationDate LONG, " + " hidden BOOLEAN," + " note TEXT," + " PRIMARY KEY (id, taskId))"); @@ -152,6 +153,7 @@ " date LONG," + " uuid varchar(255) unique," + " duration LONG," + + " modificationDate LONG," + " PRIMARY KEY (taskid, date, uuid)," + " FOREIGN KEY (taskid)" + " REFERENCES " + TABLE_TASK +"(id)" + @@ -193,14 +195,14 @@ * Query that returns every tasks * @return tasks arraylist of tasks */ - public List<TimerTask> getTasks() { - List<TimerTask> tasks = new ArrayList<>(); + public ArrayList<TimerTask> getTasks(Long date) { + ArrayList<TimerTask> tasks = new ArrayList<TimerTask>(); PreparedStatement statement = null; PreparedStatement statement2 = null; try { - statement = connection.prepareStatement("SELECT TA.*, MIN(TI.date) as modifdate, sum(TI.duration) AS totalduration FROM " + + statement = connection.prepareStatement("SELECT TA.*, sum(TI.duration) AS totalduration FROM " + TABLE_TASK + " TA, " + TABLE_TIME + " TI" + - " WHERE TA.id = TI.taskid" + + " WHERE TA.id = TI.taskid AND TA.modificationDate >" +date + " GROUP BY TA.id"); ResultSet rs = statement.executeQuery(); while (rs.next()) { @@ -210,14 +212,14 @@ task.setName(rs.getString("name")); task.setParent(rs.getString("parent")); task.setTodayTime(0); - task.setCreationDate(new java.util.Date(rs.getLong("modifdate"))); + task.setModificationDate(new java.util.Date(rs.getLong("modificationDate"))); task.setTotalTime(rs.getLong("totalduration")); tasks.add(task); } // not timed tasks statement2 = connection.prepareStatement("SELECT * FROM " + TABLE_TASK + - " WHERE id not in (SELECT taskid FROM " + TABLE_TIME + ")"); + " WHERE (id not in (SELECT taskid FROM " + TABLE_TIME + ")) AND "+ TABLE_TASK +".modificationDate >" +date ); rs = statement2.executeQuery(); while (rs.next()) { TimerTask task = new TimerTask(); @@ -225,7 +227,7 @@ task.setName(rs.getString("name")); task.setTaskId(rs.getString("taskId")); task.setParent(rs.getString("parent")); - task.setCreationDate(new Date()); + task.setModificationDate(new java.util.Date(rs.getLong("modificationDate"))); task.setTodayTime(0); task.setTotalTime(0); tasks.add(task); @@ -247,17 +249,14 @@ PreparedStatement statement = null; try { statement = connection.prepareStatement("INSERT INTO " + - TABLE_TASK + "(name, parent, taskId, hidden, note)" + - " VALUES (?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS); + TABLE_TASK + "(name, parent, taskId, hidden, note, modificationDate)" + + " VALUES (?, ?, ?, ?, ?,?)", Statement.RETURN_GENERATED_KEYS); statement.setString(1, task.getName()); - if (task.getParent() == "" || task.getParent() == "0" ) { - statement.setString(2, ""); - } else { - statement.setString(2, task.getParent()); - } + statement.setString(2, task.getParent()); statement.setString(3, task.getTaskId()); statement.setBoolean(4, task.isClosed()); statement.setString(5, null /*project.getNote()*/); + statement.setLong(6, task.getModificationDate().getTime()); statement.executeUpdate(); // get generated id @@ -276,18 +275,15 @@ PreparedStatement statement = null; try { statement = connection.prepareStatement("UPDATE " + - TABLE_TASK + " SET name=?, parent=?, hidden=?, note=?" + + TABLE_TASK + " SET name=?, parent=?, hidden=?, note=?, modificationDate=?" + " WHERE taskId = ?"); statement.setString(1, task.getName()); //name - if (task.getParent() == "") { - statement.setString(2, ""); //parent - } else { - statement.setString(2, task.getParent()); //parent - } - + statement.setString(2, task.getParent()); //parent statement.setBoolean(3, task.isClosed()); //hidden statement.setString(4, null /*project.getNote()*/);//note - statement.setString(5, task.getTaskId()); //taskId + statement.setLong(5, task.getModificationDate().getTime()); //modificationDate + statement.setString(6, task.getTaskId()); //taskId + statement.executeUpdate(); } catch (SQLException ex) { throw new StorageException("Can't modify task", ex); @@ -300,11 +296,12 @@ PreparedStatement statement = null; try { statement = connection.prepareStatement("UPDATE " + - TABLE_TIME + " SET date=?, duration=?" + + TABLE_TIME + " SET date=?, duration=?, modificationDate=?" + " WHERE uuid = ?"); statement.setLong(1, t.getCreationDate().getTime()); statement.setLong(2, t.getTime()); - statement.setString(3, t.getTimeId() ); + statement.setLong(3, t.getModificationDate().getTime()); //modificationDate + statement.setString(4, t.getTimeId() ); statement.executeUpdate(); } catch (SQLException ex) { throw new StorageException("Can't modify time", ex); @@ -337,17 +334,25 @@ return result; } + /** + * Ajoute une periode pour une tache + * @param task + * @param date + * @param uuid + * @param duration + */ public void addTaskTime(TimerTask task, Date date, String uuid, long duration) { PreparedStatement statement = null; try { statement = connection.prepareStatement("INSERT INTO " + TABLE_TIME + - "(taskid, date, uuid, duration)" + - " VALUES(?, ?, ?, ?)"); + "(taskid, date, uuid, duration, modificationDate)" + + " VALUES(?, ?, ?, ?,?)"); statement.setLong(1, task.getNumber()); statement.setLong(2, date.getTime()); statement.setString(3, uuid); statement.setLong(4, duration); + statement.setLong(5, new Date().getTime()); statement.executeUpdate(); } catch (SQLException ex) { throw new StorageException("Can't add task time", ex); @@ -356,17 +361,23 @@ } } + /** + * Ajoute une periode pour une tache + * @param time + * @param number + */ public void addTaskTime(TimerTime time, long number) { PreparedStatement statement = null; try { statement = connection.prepareStatement("INSERT INTO " + TABLE_TIME + - "(taskid, date, uuid, duration)" + + "(taskid, date, uuid, duration, modificationDate)" + " VALUES(?, ?, ?, ?)"); statement.setLong(1, number); statement.setLong(2, time.getCreationDate().getTime()); statement.setString(3, time.getTimeId()); statement.setLong(4, time.getTime()); + statement.setLong(4, time.getModificationDate().getTime()); statement.executeUpdate(); } catch (SQLException ex) { throw new StorageException("Can't add task time", ex); @@ -404,7 +415,7 @@ time.setTimeId(rs.getString("uuid")); time.setTime(rs.getLong("duration")); time.setCreationDate(new java.util.Date(rs.getLong("date"))); - + time.setModificationDate(new java.util.Date(rs.getLong("modificationDate"))); times.add(time); } Modified: branches/ng-jtimer/src/main/java/org/chorem/jtimer/web/TasksResource.java =================================================================== --- branches/ng-jtimer/src/main/java/org/chorem/jtimer/web/TasksResource.java 2014-05-16 16:18:00 UTC (rev 2978) +++ branches/ng-jtimer/src/main/java/org/chorem/jtimer/web/TasksResource.java 2014-05-20 09:31:49 UTC (rev 2979) @@ -12,6 +12,7 @@ import java.io.IOException; import java.lang.reflect.Type; +import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.logging.Level; @@ -33,11 +34,24 @@ builder = new GsonBuilder(); // Register an adapter to manage the date types as long values + //GSON builder to format dates + + builder = new GsonBuilder(); builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { + public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + return new Date(json.getAsJsonPrimitive().getAsLong()); + } + }); + builder.registerTypeAdapter(Date.class, new JsonSerializer<Date>() { + @Override + public JsonElement serialize(Date date, Type type, JsonSerializationContext jsonSerializationContext) { + return new JsonPrimitive(date.getTime()); + } + }); } /** @@ -47,14 +61,26 @@ */ @Get("json") public Representation getTasks() { - Gson gson = new Gson(); - List<TimerTask> timerTasks = storage.getTasks(); + Long date = Long.valueOf(0); + try + { + date = Long.valueOf(getQuery().getValues("date")); + } + catch(Exception e) + { + date = Long.valueOf(0); + } + + Gson gson = builder.create(); + ArrayList<TimerTask> timerTasks = storage.getTasks(date); + //On ordonne les donnees, tache fille placee en début int tabSize = timerTasks.size(); - for(int i =0; i <= tabSize;i++){ + + for (int i = 0; i < tabSize; i++) { TimerTask task = timerTasks.get(i); - if(task.getParent() == ""){ + if (task.getParent() == "") { timerTasks.add(task); timerTasks.remove(i); i--; @@ -63,6 +89,7 @@ } + String json = gson.toJson(timerTasks); return new StringRepresentation(json, MediaType.APPLICATION_JSON); } Modified: branches/ng-jtimer/src/main/webapp/js/controllers.js =================================================================== --- branches/ng-jtimer/src/main/webapp/js/controllers.js 2014-05-16 16:18:00 UTC (rev 2978) +++ branches/ng-jtimer/src/main/webapp/js/controllers.js 2014-05-20 09:31:49 UTC (rev 2979) @@ -118,12 +118,18 @@ */ var getTasksFromServ = function(){ - serverTaskAccess.query(function (response) { + serverTaskAccess.query({date : $scope.access},function (response) { + //On change la date de dernier acces + $scope.access = new Date().getTime(); + $scope.todo.lastAccess= $scope.access; + save(); + angular.forEach(response, function (item) { - //On verife si l'element n'est pas deja present - if(!(item.taskId in $scope.data.tasks) && !(item.timeId in $scope.todo.stockedDeletedTask)){ + //On verife si l'element n'est pas en cours de suppression + if(!(item.taskId in $scope.data.tasks)){ + console.log( "Un element non present " + item.name + item.modificationDate); var newTask = new Task( item.name, item.taskId, item.parent); if(item.parent == "" ){ @@ -136,19 +142,28 @@ } }else{ + //TODO obruce ajout avec parent deja present //Un noeud avec un parent est ajoute à l'arbre de son parent $scope.data.tasks[newTask.taskId] = newTask; - + save(); //Les temps sont recuperes du serveur getTimesFromServer(newTask); save(); } + }else{ + console.log( "Un element deja present " + item.name + item.modificationDate); + var newTask = new Task( item.name, item.taskId, item.parent); + $scope.data.tasks[newTask.taskId] = newTask; + save(); + } }); + + console.log("Je suis dans le get tasks"); + }); - }; var getTimesFromServer = function(task){ @@ -158,7 +173,7 @@ serverTimeAccess.query({taskId : task.taskId}, function (response) { angular.forEach(response, function (item) { - if(!(item.timeId in $scope.data.times[task.taskId]) && !(item.timeId in $scope.todo.stockedDeletedTimes[task.taskId])){ + if(!(existInObject($scope.data.times,item.timeId,task.taskId)) && !(existInObject($scope.todo.stockedDeletedTimes, item.timeId, task.taskId))){ //On cree le tasktime taskTime = new TaskTime(task, item.timeId, item.creationDate, item.time); $scope.data.times[task.taskId].push(taskTime); @@ -169,6 +184,10 @@ } + var existInObject = function(object, item, pos){ + return (object[pos]) && (item in object[pos]); + }; + var pushChangesToServ= function(){ //On supprime de la base les taches sotckees pour suppression @@ -293,9 +312,6 @@ pushChangesToServ(); getTasksFromServ(); - - //On change la date de dernier acces - $scope.access = new Date().getTime(); } } @@ -307,8 +323,17 @@ var newTask = new Task($scope.name); $scope.data.tasks[newTask.taskId] = newTask; - //On ajoute à la file de synchro - $scope.todo.stockedNewTasks.push(newTask); + //On synchronise la tache creer + serverTaskAccess.create(angular.toJson(newTask), + function(){ + console.log("persist task success" + newTask); + $scope.todo.stockedNewTasks.shift(); + }, + function(){ + console.log("fail"); + //On ajoute à la file de synchro + $scope.todo.stockedNewTasks.push(newTask); + }); $scope.name = ""; save(); @@ -327,8 +352,17 @@ var newTask = new Task("New task", undefined,task.taskId); $scope.data.tasks[newTask.taskId] = newTask; - //On ajoute à la file de synchro - $scope.todo.stockedNewTasks.push(newTask); + //On synchronise la tache creer + serverTaskAccess.create(angular.toJson(newTask), + function(){ + console.log("persist task success" + newTask); + $scope.todo.stockedNewTasks.shift(); + }, + function(){ + console.log("fail"); + //On ajoute à la file de synchro + $scope.todo.stockedNewTasks.push(newTask); + }); node.$$open = true; save(); @@ -349,9 +383,18 @@ delete $scope.data.tasks[task.taskId]; delete $scope.data.times[task.taskId]; - $scope.todo.stockedDeletedTasks.push(task.taskId); delete $scope.todo.stockedNewTimes[task.taskId]; + serverTaskAccess.delete({taskId: task.taskId}, function(){ + console.log("delete success" + task.taskId); + }, + function(){ + console.log("fail"); + $scope.todo.stockedDeletedTasks.push(task.taskId); + + }); + + var children = $scope.getChildren(task); removeRecurse(children); save(); @@ -488,6 +531,7 @@ */ $scope.editTask = function(node, type) { node.edit = type; + save(); }; @@ -497,6 +541,8 @@ */ $scope.saveTask = function(node) { node.edit = null; + + node.task.setModificationDate(); $scope.todo.stockedEditedTasks.push(node.task); save(); }; @@ -652,9 +698,7 @@ $scope.todo.stockedDeletedTimes[id].push($scope.data.times[id][item.index].timeId); $scope.data.times[id].splice(item.index,1); - //Todo obruce 15-5-2014 ajout de suppression du temps du serveur - save(); } @@ -745,7 +789,7 @@ $scope.dateMaxPicker= new Date(); - $scope.alert = function(){ + $scope.changeDayDate = function(){ var year = $scope.dateobj.date.getFullYear(); var month = $scope.dateobj.date.getMonth(); Modified: branches/ng-jtimer/src/main/webapp/js/entities.js =================================================================== --- branches/ng-jtimer/src/main/webapp/js/entities.js 2014-05-16 16:18:00 UTC (rev 2978) +++ branches/ng-jtimer/src/main/webapp/js/entities.js 2014-05-20 09:31:49 UTC (rev 2979) @@ -60,7 +60,7 @@ * @returns {tdListData} */ var tdListData = function(){ - this.lastAccess = new Date().getTime(); + this.lastAccess = 0; // {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 @@ -140,7 +140,6 @@ }; - /** * Toutes les donnees de la fenetre qui sont a stocker entre 2 sessions * @returns {WebTimerData} @@ -371,8 +370,7 @@ this.taskId = taskId; } - this.creationDate = Date.now(); - this.modificationDate = this.creationDate; + this.modificationDate = Date.now(); this.removed = 0; if(parentTaskId == undefined){ Modified: branches/ng-jtimer/src/main/webapp/js/service.js =================================================================== --- branches/ng-jtimer/src/main/webapp/js/service.js 2014-05-16 16:18:00 UTC (rev 2978) +++ branches/ng-jtimer/src/main/webapp/js/service.js 2014-05-20 09:31:49 UTC (rev 2979) @@ -1,7 +1,7 @@ angular.module("serverAccessService", ["ngResource"]) .factory("serverTaskAccess", function ( $resource) { // Encapsule l'acces au server - return $resource("rest/tasks/:taskId",{}, + return $resource("http://localhost:8080/rest/tasks/:taskId",{}, { query : {method:'GET', isArray:true}, get : {method: 'GET',isArray: false}, @@ -18,7 +18,7 @@ angular.module("serverTimeService", ["ngResource"]) .factory("serverTimeAccess", function ( $resource) { // Encapsule l'acces au server - return $resource("rest/tasks/:taskId/time",{}, + return $resource("http://localhost:8080/rest/tasks/:taskId/time",{}, { query : {method:'GET', isArray:true}, get : {method: 'GET', isArray: false}, Modified: branches/ng-jtimer/src/main/webapp/partials/timeModal.html =================================================================== --- branches/ng-jtimer/src/main/webapp/partials/timeModal.html 2014-05-16 16:18:00 UTC (rev 2978) +++ branches/ng-jtimer/src/main/webapp/partials/timeModal.html 2014-05-20 09:31:49 UTC (rev 2979) @@ -39,7 +39,7 @@ <hr/> <div class ="timepick"> <div ng-controller="datePickerCtrl" style="display:inline-block; min-height:290px;"> - <datepicker ng-model="dateobj.date" ng-change="alert()" min-date="minDate" max-date="dateMaxPicker" show-weeks="true" class="well well-sm"></datepicker> + <datepicker ng-model="dateobj.date" ng-change="changeDayDate()" min-date="minDate" max-date="dateMaxPicker" show-weeks="true" class="well well-sm"></datepicker> </div> <div ng-controller="radioTimeCtrl">