Author: obruce Date: 2014-05-26 18:19:25 +0200 (Mon, 26 May 2014) New Revision: 2988 Url: http://forge.chorem.org/projects/jtimer/repository/revisions/2988 Log: storage changement pour la suppression 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/TaskResource.java branches/ng-jtimer/src/main/java/org/chorem/jtimer/web/TasksResource.java branches/ng-jtimer/src/main/java/org/chorem/jtimer/web/TimeResource.java branches/ng-jtimer/src/main/java/org/chorem/jtimer/web/TimesResource.java branches/ng-jtimer/src/main/webapp/js/controllers.js 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-26 09:26:00 UTC (rev 2987) +++ branches/ng-jtimer/src/main/java/org/chorem/jtimer/entities/TimerTask.java 2014-05-26 16:19:25 UTC (rev 2988) @@ -67,12 +67,16 @@ /** Total time. */ protected long totalTime; + /** if task is removed */ + protected boolean isremoved; + /** * Constructor. */ public TimerTask() { // wrong value to detect bug number = -1; + this.isremoved = false; } /** @@ -87,6 +91,7 @@ this.taskId = UUID.randomUUID().toString(); this.parent = ""; this.modificationDate = new Date(); + this.isremoved = false; } /** @@ -101,6 +106,7 @@ this.taskId = UUID.randomUUID().toString(); this.parent = ""; this.modificationDate = new Date(datetime); + this.isremoved = false; } @@ -224,4 +230,8 @@ public void setTotalTime(long totalTime) { this.totalTime = totalTime; } + + public boolean getRemoved() {return isremoved;} + + public void setRemoved(boolean removed) {this.isremoved = removed;} } \ No newline at end of file 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-26 09:26:00 UTC (rev 2987) +++ branches/ng-jtimer/src/main/java/org/chorem/jtimer/entities/TimerTime.java 2014-05-26 16:19:25 UTC (rev 2988) @@ -17,7 +17,7 @@ /** Task identifier it refers to */ protected String taskId; - /** Task identifier*/ + /** Time identifier*/ protected String timeId; /** Creation Date*/ @@ -29,8 +29,12 @@ /** Time Elapsed in the period */ protected long time; + /** */ + protected boolean isremoved; + public TimerTime() { + isremoved = false; } /** @@ -45,6 +49,7 @@ this.time = time; this.creationDate = creationDate; this.modificationDate = creationDate; + this.isremoved=false; } @@ -98,4 +103,12 @@ ", time=" + time + '}'; } + + public void setRemoved(boolean removed) { + this.isremoved = removed; + } + + public boolean getRemoved() { + return isremoved; + } } 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-26 09:26:00 UTC (rev 2987) +++ branches/ng-jtimer/src/main/java/org/chorem/jtimer/storage/Storage.java 2014-05-26 16:19:25 UTC (rev 2988) @@ -137,7 +137,7 @@ /** * Create tables - * @param conn + * @param conn la connection */ protected void createSchema(Connection conn) { Statement statement = null; @@ -153,6 +153,7 @@ " modificationDate LONG, " + " hidden BOOLEAN," + " note TEXT," + + "removed BOOLEAN," + " PRIMARY KEY (id, taskId))"); statement.executeUpdate("CREATE TABLE " + TABLE_TIME + "(taskid LONG NOT NULL," + @@ -160,6 +161,7 @@ " uuid varchar(255) unique," + " duration LONG," + " modificationDate LONG," + + "removed BOOLEAN," + " PRIMARY KEY (taskid, date, uuid)," + " FOREIGN KEY (taskid)" + " REFERENCES " + TABLE_TASK +"(id)" + @@ -184,7 +186,8 @@ int result = 0; PreparedStatement statement = null; try { - statement = connection.prepareStatement("SELECT count(*) FROM " + TABLE_TASK); + statement = connection.prepareStatement("SELECT count(*) FROM " + TABLE_TASK + + " WHERE removed = 0"); ResultSet rs = statement.executeQuery(); if (rs.next()) { result = rs.getInt(1); @@ -198,17 +201,18 @@ } /** - * Query that returns every tasks + * Query that returns not removed tasks * @return tasks arraylist of tasks */ public ArrayList<TimerTask> getTasks(Long date) { - ArrayList<TimerTask> tasks = new ArrayList<TimerTask>(); + ArrayList<TimerTask> tasks = new ArrayList<>(); PreparedStatement statement = null; PreparedStatement statement2 = null; try { statement = connection.prepareStatement("SELECT TA.*, sum(TI.duration) AS totalduration FROM " + TABLE_TASK + " TA, " + TABLE_TIME + " TI" + - " WHERE TA.id = TI.taskid AND TA.modificationDate >" +date + + " WHERE TA.id = TI.taskid" + + " AND TA.modificationDate >" +date + " GROUP BY TA.id"); ResultSet rs = statement.executeQuery(); while (rs.next()) { @@ -217,6 +221,7 @@ task.setTaskId(rs.getString("taskId")); task.setName(rs.getString("name")); task.setParent(rs.getString("parent")); + task.setRemoved(rs.getBoolean("removed")); task.setTodayTime(0); task.setModificationDate(new java.util.Date(rs.getLong("modificationDate"))); task.setTotalTime(rs.getLong("totalduration")); @@ -225,7 +230,8 @@ // not timed tasks statement2 = connection.prepareStatement("SELECT * FROM " + TABLE_TASK + - " WHERE (id not in (SELECT taskid FROM " + TABLE_TIME + ")) AND "+ TABLE_TASK +".modificationDate >" +date ); + " WHERE (id not in (SELECT taskid FROM " + TABLE_TIME + "))" + + " AND "+ TABLE_TASK +".modificationDate >" +date); rs = statement2.executeQuery(); while (rs.next()) { TimerTask task = new TimerTask(); @@ -234,6 +240,7 @@ task.setTaskId(rs.getString("taskId")); task.setParent(rs.getString("parent")); task.setModificationDate(new java.util.Date(rs.getLong("modificationDate"))); + task.setRemoved(rs.getBoolean("removed")); task.setTodayTime(0); task.setTotalTime(0); tasks.add(task); @@ -248,21 +255,21 @@ } - /* Insert, Update, Delete */ public void addTask(TimerTask task) { PreparedStatement statement = null; try { statement = connection.prepareStatement("INSERT INTO " + - TABLE_TASK + "(name, parent, taskId, hidden, note, modificationDate)" + - " VALUES (?, ?, ?, ?, ?,?)", Statement.RETURN_GENERATED_KEYS); + TABLE_TASK + "(name, parent, taskId, hidden, note, modificationDate, removed)" + + " VALUES (?, ?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS); statement.setString(1, task.getName()); 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.setBoolean(7,task.getRemoved()); statement.executeUpdate(); // get generated id @@ -281,7 +288,7 @@ PreparedStatement statement = null; try { statement = connection.prepareStatement("UPDATE " + - TABLE_TASK + " SET name=?, parent=?, hidden=?, note=?, modificationDate=?" + + TABLE_TASK + " SET name=?, parent=?, hidden=?, note=?, modificationDate=?, removed=?" + " WHERE taskId = ?"); statement.setString(1, task.getName()); //name statement.setString(2, task.getParent()); //parent @@ -289,6 +296,7 @@ statement.setString(4, null /*project.getNote()*/);//note statement.setLong(5, task.getModificationDate().getTime()); //modificationDate statement.setString(6, task.getTaskId()); //taskId + statement.setBoolean(7,task.getRemoved()); statement.executeUpdate(); } catch (SQLException ex) { @@ -302,12 +310,13 @@ PreparedStatement statement = null; try { statement = connection.prepareStatement("UPDATE " + - TABLE_TIME + " SET date=?, duration=?, modificationDate=?" + + TABLE_TIME + " SET date=?, duration=?, modificationDate=?, removed = ?" + " WHERE uuid = ?"); statement.setLong(1, t.getCreationDate().getTime()); statement.setLong(2, t.getTime()); statement.setLong(3, t.getModificationDate().getTime()); //modificationDate statement.setString(4, t.getTimeId() ); + statement.setBoolean(5, t.getRemoved()); statement.executeUpdate(); } catch (SQLException ex) { throw new StorageException("Can't modify time", ex); @@ -318,8 +327,8 @@ /** * Returns task number with ID - * @param id - * @return + * @param id identifiant de la tache + * @return le long representant la tache dans la base */ public long getTaskNumber(String id) { PreparedStatement statement = null; @@ -342,23 +351,24 @@ /** * Ajoute une periode pour une tache - * @param task - * @param date - * @param uuid - * @param duration + * @param task la tache qui va recevoir un temps + * @param date la date de creation + * @param uuid l'identifiant du temps + * @param duration le duree de la periode */ 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, modificationDate)" + - " VALUES(?, ?, ?, ?,?)"); + "(taskid, date, uuid, duration, modificationDate, removed)" + + " 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.setBoolean(6, false); statement.executeUpdate(); } catch (SQLException ex) { throw new StorageException("Can't add task time", ex); @@ -369,24 +379,22 @@ /** * Ajoute une periode pour une tache - * @param time - * @param number + * @param time le timertime a ajouter + * @param number le numero identidfiant la tache dans la base */ public void addTaskTime(TimerTime time, long number) { PreparedStatement statement = null; try { statement = connection.prepareStatement("INSERT INTO " + TABLE_TIME + - "(taskid, date, uuid, duration, modificationDate)" + - " VALUES(?, ?, ?, ?, ?)"); + "(taskid, date, uuid, duration, modificationDate, removed)" + + " VALUES(?, ?, ?, ?, ?, ?)"); statement.setLong(1, number); statement.setLong(2, time.getCreationDate().getTime()); statement.setString(3, time.getTimeId()); statement.setLong(4, time.getTime()); - if(log.isDebugEnabled()){ - log.info(time.getModificationDate().getTime()); - } statement.setLong(5, time.getModificationDate().getTime()); + statement.setBoolean(6, time.getRemoved()); statement.executeUpdate(); } catch (SQLException ex) { throw new StorageException("Can't add task time", ex); @@ -422,13 +430,13 @@ while (rs.next()) { - TimerTime time = new TimerTime(); time.setTaskId(rs.getString("mytask")); 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"))); + time.setRemoved(rs.getBoolean("removed")); times.add(time); @@ -455,6 +463,7 @@ statement = connection.prepareStatement("SELECT TI.* FROM " + TABLE_TASK + " TA, "+ TABLE_TIME + " TI" + " WHERE TA.id = TI.taskid AND TA.taskId = '" +taskid +"' " + + " AND TA.removed = 0" + " AND TI.modificationDate >" +date); ResultSet rs = statement.executeQuery(); while (rs.next()) { @@ -464,6 +473,7 @@ time.setTime(rs.getLong("duration")); time.setCreationDate(new java.util.Date(rs.getLong("date"))); time.setModificationDate(new java.util.Date(rs.getLong("modificationDate"))); + time.setRemoved(rs.getBoolean("removed")); times.add(time); } @@ -480,7 +490,7 @@ /** * Methode qui va supprimer un timertime a l'aide de son identifiant - * @param timeId + * @param timeId l'identifiant du temps */ public void deleteTimeWithId(String timeId) { @@ -500,7 +510,7 @@ /** * Methode qui va supprimer un timertime a l'aide de son identifiant - * @param taskId + * @param taskId l'identifiant de la tache */ public void deleteTaskWithId(String taskId) { PreparedStatement statement = null; @@ -517,18 +527,49 @@ } - public void deleteTask(TimerTask task) { + + /** Suppression virtuelle */ + /** + * Methode qui va modifier l'attribut removed d'un timertime a l'aide de son identifiant + * @param timeId l'identifiant du temps + */ + public void removeTimeWithId(String timeId) { + PreparedStatement statement = null; try { - statement = connection.prepareStatement("DELETE FROM " + - TABLE_TASK + " WHERE id = ?"); - statement.setLong(1, task.getNumber()); + statement = connection.prepareStatement("UPDATE " + + TABLE_TIME + " SET removed = 1" + + " 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 va modifier l'attribut removed d'un timertask a l'aide de son identifiant + * @param taskId l'identifiant de la tache + */ + public void removeTaskWithId(String taskId) { + PreparedStatement statement = null; + try { + statement = connection.prepareStatement("UPDATE " + + TABLE_TASK + " SET removed = 1" + + " WHERE taskId = '"+taskId +"'"); + + + statement.executeUpdate(); + } catch (SQLException ex) { throw new StorageException("Can't delete project", ex); } finally { closeStatement(statement); } + } Modified: branches/ng-jtimer/src/main/java/org/chorem/jtimer/web/TaskResource.java =================================================================== --- branches/ng-jtimer/src/main/java/org/chorem/jtimer/web/TaskResource.java 2014-05-26 09:26:00 UTC (rev 2987) +++ branches/ng-jtimer/src/main/java/org/chorem/jtimer/web/TaskResource.java 2014-05-26 16:19:25 UTC (rev 2988) @@ -37,6 +37,7 @@ String taskId = (String)getRequest().getAttributes().get("taskId"); Gson gson = new Gson(); String json = gson.toJson(Long.valueOf(taskId).longValue()); + return new StringRepresentation( json , MediaType.APPLICATION_JSON); } @@ -47,9 +48,8 @@ */ @Delete public void deleteTask() { - String taskId = (String)getRequest().getAttributes().get("taskId"); - storage.deleteTaskWithId(taskId); + storage.removeTaskWithId(taskId); } 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-26 09:26:00 UTC (rev 2987) +++ branches/ng-jtimer/src/main/java/org/chorem/jtimer/web/TasksResource.java 2014-05-26 16:19:25 UTC (rev 2988) @@ -5,6 +5,7 @@ import java.util.ArrayList; import java.util.Date; + import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -54,12 +55,11 @@ 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) { @@ -83,10 +83,12 @@ } catch (Exception e) { date = (long) 0; } - + ArrayList<TimerTask> timerTasks; Gson gson = builder.create(); - ArrayList<TimerTask> timerTasks = storage.getTasks(date); + + timerTasks = storage.getTasks(date); + //On ordonne les donnees, tache fille placee en début int tabSize = timerTasks.size(); @@ -98,10 +100,8 @@ i--; tabSize--; } - } - String json = gson.toJson(timerTasks); return new StringRepresentation(json, MediaType.APPLICATION_JSON); } @@ -115,11 +115,14 @@ Gson gson = builder.create(); String repr1 = representation.getText(); + if (log.isInfoEnabled()) { + log.info("La task suivante est cree : " + repr1); + } TimerTask t = gson.fromJson(repr1, TimerTask.class); if (log.isInfoEnabled()) { log.info("La task suivante est cree : " + t.toString()); } - + t.setRemoved(false); storage.addTask(t); } 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-26 09:26:00 UTC (rev 2987) +++ branches/ng-jtimer/src/main/java/org/chorem/jtimer/web/TimeResource.java 2014-05-26 16:19:25 UTC (rev 2988) @@ -55,6 +55,7 @@ } }); + builder.registerTypeAdapter(Date.class, new JsonSerializer<Date>() { @Override public JsonElement serialize(Date date, Type type, JsonSerializationContext jsonSerializationContext) { @@ -100,7 +101,7 @@ @Delete public void deleteTime() { String timeId = (String)getRequest().getAttributes().get("taskId"); - storage.deleteTimeWithId(timeId); + storage.removeTimeWithId(timeId); } /** Modified: branches/ng-jtimer/src/main/java/org/chorem/jtimer/web/TimesResource.java =================================================================== --- branches/ng-jtimer/src/main/java/org/chorem/jtimer/web/TimesResource.java 2014-05-26 09:26:00 UTC (rev 2987) +++ branches/ng-jtimer/src/main/java/org/chorem/jtimer/web/TimesResource.java 2014-05-26 16:19:25 UTC (rev 2988) @@ -49,6 +49,7 @@ } }); + builder.registerTypeAdapter(Date.class, new JsonSerializer<Date>() { @Override public JsonElement serialize(Date date, Type type, JsonSerializationContext jsonSerializationContext) { Modified: branches/ng-jtimer/src/main/webapp/js/controllers.js =================================================================== --- branches/ng-jtimer/src/main/webapp/js/controllers.js 2014-05-26 09:26:00 UTC (rev 2987) +++ branches/ng-jtimer/src/main/webapp/js/controllers.js 2014-05-26 16:19:25 UTC (rev 2988) @@ -14,8 +14,8 @@ //interval de conec var interval = 20000; //TODO: obruce 05-05-14 decider d'un intervalle interessant - // {Timestamp} periode de delais = 3 periode - var delayAccess = 30000; + // periode de delais = 3 periode +1msec + var delayAccess = 3 * interval +1; // {TreeNode} l'arbre regenere automatiquement lorsque les donnees changent $scope.tree; @@ -129,8 +129,7 @@ angular.forEach(response, function (item) { - //On verife si l'element n'est pas en cours de suppression - if(!(item.taskId in $scope.data.tasks)){ + if((!(item.taskId in $scope.data.tasks)) && item.isremoved == false){ console.log( "Un element non present " + item.name + item.modificationDate); var newTask = new Task( item.name, item.taskId, item.parent); @@ -181,22 +180,31 @@ serverTaskAccess.query({date : ($scope.taskAccess-delayAccess)}, function (response) { angular.forEach(response, function (item) { - console.log("Changement d'une tâche"); - console.log(item); + if(item.taskId in $scope.data.tasks){ + console.log("Tache existe"); + //console.log(item); + //On cherche le node et on le met à jour - $scope.tree.getNode(item.taskId).task.name = item.name; + var tmp = $scope.tree.getNode(item.taskId); - save(); + if(item.isremoved){ + $scope.removeTask(tmp); + }else{ + tmp.task.name = item.name; + } - //On change la date de dernier acces - $scope.taskAccess = Date.now(); - $scope.todo.lastTaskAccess= $scope.taskAccess; + save(); - }else{ + + }else if(!item.isremoved){ + + console.log("Nouvelle tache"); + //console.log(item); + var newTask = new Task( item.name, item.taskId, item.parent); if(item.parent == "" ){ @@ -215,7 +223,7 @@ //On ajoute le node à l'arbre if ($scope.tree) { - // $scope.tree.getNode(item.taskId).addChild($scope.createTreeNode(newTask)); + $scope.tree.getNode(item.parent).addChild($scope.createTreeNode(newTask)); } //Les temps sont recuperes du serveur @@ -224,8 +232,14 @@ save(); } } + }); }); + + //On change la date de dernier acces + $scope.taskAccess = Date.now(); + $scope.todo.lastTaskAccess= $scope.taskAccess; + save(); } /** @@ -234,7 +248,6 @@ */ var updateTimesFromServer = function(){ - serverTimeAccess.query({date : $scope.timeAccess-delayAccess}, function (response) { angular.forEach(response, function (item) { @@ -850,7 +863,7 @@ if ( $scope.online) { pushTodoListToServ(); - updateTasksFromServ(); + initTasksFromServ(); } }