Author: obruce Date: 2014-05-06 17:21:10 +0200 (Tue, 06 May 2014) New Revision: 2964 Url: http://forge.chorem.org/projects/jtimer/repository/revisions/2964 Log: Ajout de push et pull task et time 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/controllers.js branches/ng-jtimer/src/main/webapp/js/entities.js 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-06 07:45:40 UTC (rev 2963) +++ branches/ng-jtimer/src/main/java/org/chorem/jtimer/storage/Storage.java 2014-05-06 15:21:10 UTC (rev 2964) @@ -144,8 +144,8 @@ statement.executeUpdate("CREATE TABLE " + TABLE_VERSION + "(version VARCHAR(10))"); statement.executeUpdate("CREATE TABLE " + TABLE_TASK + - "(id LONG NOT NULL AUTO_INCREMENT," + - " taskId VARCHAR(255) NOT NULL," + + "(id LONG NOT NULL UNIQUE AUTO_INCREMENT," + + " taskId VARCHAR(255) NOT NULL UNIQUE," + " name VARCHAR(255) NOT NULL," + " parent VARCHAR(255)," + " hidden BOOLEAN," + @@ -296,6 +296,25 @@ } } + public long getTaskNumber(String id) { + PreparedStatement statement = null; + long result= 0; + try { + statement = connection.prepareStatement("SELECT id FROM " +TABLE_TASK + + " WHERE taskId = '" + id+"'"); + + ResultSet rs = statement.executeQuery(); + while (rs.next()) { + result = rs.getLong(1); + } + } catch (SQLException ex) { + throw new StorageException("Read error", ex); + } finally { + closeStatement(statement); + } + return result; + } + public void addTaskTime(TimerTask task, Date date, String uuid, long duration) { PreparedStatement statement = null; try { @@ -315,38 +334,33 @@ } } - public void deleteTask(TimerTask task) { + public void addTaskTime(TimerTime time, long number) { PreparedStatement statement = null; try { - statement = connection.prepareStatement("DELETE FROM " + - TABLE_TASK + " WHERE id = ?"); - statement.setLong(1, task.getNumber()); + statement = connection.prepareStatement("INSERT INTO " + TABLE_TIME + + "(taskid, date, uuid, duration)" + + " VALUES(?, ?, ?, ?)"); + + statement.setLong(1, number); + statement.setDate(2, new java.sql.Date(time.getCreationDate().getTime())); + statement.setString(3, time.getTimeId()); + statement.setLong(4, time.getTime()); statement.executeUpdate(); } catch (SQLException ex) { - throw new StorageException("Can't delete project", ex); + throw new StorageException("Can't add task time", ex); } finally { closeStatement(statement); } } + + + public void setAnnotation(TimerTask task, Date date, String annotation) { } - public void deleteTaskWithId(String taskId) { - PreparedStatement statement = null; - try { - statement = connection.prepareStatement("DELETE FROM " + - TABLE_TASK + " WHERE taskId = ?"); - statement.setString(1, taskId); - statement.executeUpdate(); - } catch (SQLException ex) { - throw new StorageException("Can't delete project", ex); - } finally { - closeStatement(statement); - } - } /** @@ -379,4 +393,60 @@ } return times; } + + + /** Suppression de tuple **/ + + /** + * Methode qui va supprimer un timertime a l'aide de son identifiant + * @param timeId + */ + public void deleteTimeWithId(String timeId) { + PreparedStatement statement = null; + try { + statement = connection.prepareStatement("DELETE FROM " + + TABLE_TIME + " WHERE uuid = ?"); + statement.setString(1, timeId); + statement.executeUpdate(); + } catch (SQLException ex) { + throw new StorageException("Can't delete time", ex); + } finally { + closeStatement(statement); + } + + } + + /** + * Methode qui va supprimer un timertime a l'aide de son identifiant + * @param taskId + */ + public void deleteTaskWithId(String taskId) { + PreparedStatement statement = null; + try { + statement = connection.prepareStatement("DELETE FROM " + + TABLE_TASK + " WHERE taskId = ?"); + statement.setString(1, taskId); + statement.executeUpdate(); + } catch (SQLException ex) { + throw new StorageException("Can't delete project", ex); + } finally { + closeStatement(statement); + } + + } + + public void deleteTask(TimerTask task) { + PreparedStatement statement = null; + try { + statement = connection.prepareStatement("DELETE FROM " + + TABLE_TASK + " WHERE id = ?"); + statement.setLong(1, task.getNumber()); + statement.executeUpdate(); + } catch (SQLException ex) { + throw new StorageException("Can't delete project", ex); + } finally { + closeStatement(statement); + } + } + } \ 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-06 07:45:40 UTC (rev 2963) +++ branches/ng-jtimer/src/main/java/org/chorem/jtimer/web/TimeResource.java 2014-05-06 15:21:10 UTC (rev 2964) @@ -1,22 +1,37 @@ package org.chorem.jtimer.web; -import com.google.gson.Gson; +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; import org.restlet.representation.Representation; import org.restlet.representation.StringRepresentation; -import org.restlet.resource.Get; -import org.restlet.resource.ResourceException; -import org.restlet.resource.ServerResource; +import org.restlet.resource.*; +import java.io.IOException; +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 { + + private GsonBuilder builder; protected Storage storage; + private final static Logger LOGGER = Logger.getLogger(TasksResource.class.getName()); @Override protected void doInit() throws ResourceException { + //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()); + } + }); + storage = (Storage)getContext().getAttributes().get(Storage.class.getName()); } @@ -29,4 +44,26 @@ String json = gson.toJson(timerTimes); return new StringRepresentation(json, MediaType.APPLICATION_JSON); } + + @Delete + public void deleteTask() { + + String timeId = (String)getRequest().getAttributes().get("taskId"); + storage.deleteTimeWithId(timeId); + } + + @Post("json") + public void createTask(Representation representation) throws IOException { + String timeId = (String)getRequest().getAttributes().get("taskId"); + long number = storage.getTaskNumber(timeId); + + Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX").create(); + String repr1 = representation.getText(); + + TimerTime t = gson.fromJson(repr1, TimerTime.class); + + storage.addTaskTime(t,3); + } + + } Modified: branches/ng-jtimer/src/main/webapp/js/controllers.js =================================================================== --- branches/ng-jtimer/src/main/webapp/js/controllers.js 2014-05-06 07:45:40 UTC (rev 2963) +++ branches/ng-jtimer/src/main/webapp/js/controllers.js 2014-05-06 15:21:10 UTC (rev 2964) @@ -49,8 +49,8 @@ // {Array of TaskID} la file de tache attendant la synchronisation avec le serveur var stockedNewTasks = []; var stockedDeletedTasks = []; - // {Array of TaskID} la file de temps des taches attendant la synchronisation avec le serveur - var stockedNewTimes = []; + // {key: taskId, value: Array of TaskTime} la file de temps des taches attendant la synchronisation avec le serveur + var stockedNewTimes = {}; /** * Met a jour la tache selectionne, si la tache selectionnee est la courante @@ -113,7 +113,7 @@ /** * Récupère les nouveaux elements du serveur et les ajoute a l'arbre pour l'affichage */ - var getChangesFromServ = function(){ + var getTasksFromServ = function(){ serverTaskAccess.query(function (response) { angular.forEach(response, function (item) { @@ -136,13 +136,9 @@ //Un noeud avec un parent est ajoute à l'arbre de son parent $scope.data.tasks[newTask.taskId] = newTask; + //Les temps sont recuperes du serveur getTimesFromServer(newTask); - //On ajoute le temps du jour et global sur ce noeud - //$scope.getRecentTaskTime($scope.data.tasks[newTask.taskId]).addTime(item.todayTime); - //var global = new GlobalTime(newTask).addTime(parseInt(item.totalTime)); - //$scope.data.globalTimes[newTask.taskId] = global; - console.log(newTask); console.log("Import task "+ newTask.name); console.log($scope.data.times[newTask.taskId]) @@ -166,9 +162,11 @@ //On cree le tasktime taskTime = new TaskTime(task, item.timeId, item.creationDate, item.time); $scope.data.times[task.taskId].push(taskTime); + save(); } }); }); + } var pushChangesToServ= function(){ @@ -181,8 +179,19 @@ serverTaskAccess.create(angular.toJson(task)); console.log("ajout new task to serv "+ task.name); }); + angular.forEach(stockedNewTimes, function(times,task){ + angular.forEach(times, function(time){ + serverTimeAccess.create({taskId: task} , angular.toJson(time)); + }); + + console.log(times); + //serverTimeAccess.create({taskId: task} , angular.toJson(times)); + console.log("ajout new time to serv "); + }); + //TODO comportement de file avec shift stockedDeletedTasks = []; stockedNewTasks = []; + stockedNewTimes ={}; } /** @@ -214,11 +223,11 @@ if ( $scope.online == true) { pushChangesToServ(); - getChangesFromServ(); + getTasksFromServ(); //On change la date de dernier acces $scope.acces = moment().format("DD-MM-YYYY H:mm:ss a"); - console.log($scope.data.times); + } }, 30000); //TODO: for now 10s but for real put at least 2h: 200000000 @@ -258,7 +267,6 @@ node.$$open = true; save(); node.addChild($scope.createTreeNode(newTask)); - console.log($scope.data.times[newTask.taskId]); }; /** @@ -276,6 +284,7 @@ delete $scope.data.times[task.taskId]; stockedDeletedTasks.push(task.taskId); + delete stockedNewTimes[task.taskId]; var children = $scope.getChildren(task); removeRecurse(children); @@ -413,14 +422,15 @@ times = []; $scope.data.times[task.taskId]= times; } + if(!stockedNewTimes[task.taskId]){stockedNewTimes[task.taskId]=[];} var maxDate = new Date(0,0,0,0,0); var result; angular.forEach(times, function (t) { if (t.isToday() ) { - if(maxDate < t.creationTime){ - maxDate = t.creationTime; + if(maxDate < t.creationDate){ + maxDate = t.creationDate; result = t; } } @@ -429,6 +439,7 @@ if (!result) { result = new TaskTime(task); times.push(result); + stockedNewTimes[task.taskId].push(result); } return result; @@ -469,13 +480,14 @@ //si ce n'est la la tache courante actuel if ($scope.currentTask !== task) { //On ajoute un nouveau timeTask dans la liste des times - var times = $scope.data.times[task.taskId]; - if (!times) { - times = []; - $scope.data.times[task.taskId]= times; - } - times.push(new TaskTime(task)); + if (!$scope.data.times[task.taskId]) {$scope.data.times[task.taskId]= [];} + if(!stockedNewTimes[task.taskId]){stockedNewTimes[task.taskId]=[];} + + var temp =new TaskTime(task); + $scope.data.times[task.taskId].push(temp); + stockedNewTimes[task.taskId].push(temp); + //On change la task courante $scope.currentTask = task; $scope.currentTaskDate = now; @@ -485,7 +497,6 @@ $scope.currentTask = null; $scope.currentTaskDate = null; } - console.log($scope.data.times[task.taskId]); save(); }; @@ -558,7 +569,7 @@ //force the first server connection pushChangesToServ(); - getChangesFromServ(); + getTasksFromServ(); // force the first tree creation $scope.createTree(); Modified: branches/ng-jtimer/src/main/webapp/js/entities.js =================================================================== --- branches/ng-jtimer/src/main/webapp/js/entities.js 2014-05-06 07:45:40 UTC (rev 2963) +++ branches/ng-jtimer/src/main/webapp/js/entities.js 2014-05-06 15:21:10 UTC (rev 2964) @@ -360,9 +360,9 @@ } if(date == undefined){ - this.creationTime = new Date(); + this.creationDate = new Date(); }else{ - this.creationTime = new Date(date); + this.creationDate = new Date(date); } if(time == undefined){ @@ -378,7 +378,7 @@ * @returns {Boolean} */ TaskTime.prototype.isToday = function() { - var result = new Date(this.creationTime).sameDay(new Date()); + var result = new Date(this.creationDate).sameDay(new Date()); return result; };