org.chorem.jtimer.io
Class GTimerIncrementalSaver

java.lang.Object
  extended by java.util.TimerTask
      extended by org.chorem.jtimer.io.AbstractSaver
          extended by org.chorem.jtimer.io.GTimerIncrementalSaver
All Implemented Interfaces:
Runnable, EventListener, DataEventListener, VetoableDataEventListener, Saver

public class GTimerIncrementalSaver
extends AbstractSaver
implements Saver, DataEventListener

Charge et sauve les fichiers au format gTimer. Le format de fichier utilisé ici est 1.2. => Format: 1.2 Changes: - contrairement à la premiere version, ce Saver sauve les projets/taches/annotations seulement si elles ont été modifiées.

Version:
$Revision: 2687 $ Last update : $Date: 2009-11-03 11:06:13 +0100 (mar., 03 nov. 2009) $ By : $Author: echatellier $
Author:
chatellier

Field Summary
protected  long autoSaveDelay
          Auto save delay.
protected  Timer autoSaveTimer
          Timer used for autosave.
protected  FileChannel channel
          Resource used by lock.
protected static String GTIMER_ALERT_EXTENSION
          Extension alert.
protected static String GTIMER_ANNOTATION_EXTENSION
          Extension annotation.
protected static String GTIMER_EMPTY_PROJECT_NAME
          Empty gtimer project name.
protected static String GTIMER_FILE_VERSION
          Version du format de fichier.
protected static String GTIMER_PROJECT_EXTENSION
          Extension project.
protected static String GTIMER_SUBTASK_SEPARATOR
          Separateur pour les sous taches.
protected static String GTIMER_TASK_EXTENSION
          Extension task.
protected  FileLock lock
          Resource used by lock.
protected static String LOCK_FILE_NAME
          Lock filename.
protected  RandomAccessFile raf
          Resource used by lock.
protected  Collection<TimerTask> runningTasks
          Running task list.
protected  String saveDirectory
          Save directory.
 
Fields inherited from class org.chorem.jtimer.io.AbstractSaver
BACKUP_EXTENSION
 
Constructor Summary
GTimerIncrementalSaver()
          Constructor.
 
Method Summary
 void addProject(TimerProject project)
          Add project event.
 void addTask(TimerTask task)
          Add task event.
protected  void backupGTimerFiles()
          For development.
 void checkAddProject(TimerProject project)
          Check add project.
 void checkAddTask(TimerTask parent, TimerTask task)
          Check add task.
 void checkChangeClosedState(TimerTask task)
          Check change task state.
 void checkDeleteProject(TimerProject project)
          Check delete project.
 void checkDeleteTask(TimerTask task)
          Check delete task.
 void checkMergeTasks(TimerTask destinationTask, List<TimerTask> otherTasks)
          Check merge task.
 void checkModifyProject(TimerProject project)
          Check modify project.
 void checkModifyTask(TimerTask task)
          Check modify task.
 void checkMoveTask(TimerTask destination, Collection<TimerTask> tasksToMove)
          Check move task.
protected  void checkName(TimerTask task)
          Check task name.
protected  void checkSaveDirectory()
          Check that save directory exists.
 void checkSetAnnotation(TimerTask task, Date date, String value)
          Check update task annotation.
 void checkSetTaskTime(TimerTask task, Date date, Long value)
          Check update task time.
 void dataLoaded(Collection<TimerProject> projects)
          All data loaded.
 void deleteProject(TimerProject project)
          Delete project.
 void deleteTask(TimerTask task)
          Delete task.
protected  void deleteTaskOrProject(TimerTask taskOrProject, String extension)
          Delete associated file or project files on disk.
protected  TimerTask findTask(TimerTask parentTask, String[] taskNames)
          Find a task in task's subtask
protected  TimerProject getProjectFromFile(File projectFile)
          Load a project in his file.
protected  int getUnusedNumber(String extension)
          Explore directory and find a non used number for project.
protected  Collection<TimerProject> gTimerLoad()
          Load from gtimer file format.
protected  boolean isGTimerFile(String filename)
          Check if a file denoted by his name is a gtimer file.
 Collection<TimerProject> load()
          Load a project list.
 void lock()
          Try to lock current saver directory.
 void modifyProject(TimerProject project)
          Modify project.
 void modifyTask(TimerTask task)
          Modify task.
 void moveTask(TimerTask task)
          Post move task.
protected  void parseAlerts(TimerTask task)
          Find task alert and load it.
protected  void parseAnnotations(TimerTask task)
          Try to find and load annotations from task.
protected  TimerTask parseTaskFromFile(Map<String,TimerProject> mapNumberProject, File taskFile, SortedMap<TimerTask,TimerProject> taskToManage)
          Parse task file.
protected  void parseTaskFromSavedMap(SortedMap<TimerTask,TimerProject> taskToManage)
          Post process task to manage subtask loaded before parent task.
 void postChangeClosedState(TimerTask task)
          Post change task state.
 void postMergeTasks(TimerTask destinationTask, List<TimerTask> otherTasks)
          Post merge task.
 void preChangeClosedState(TimerTask task)
          Pre change task state.
 void preDeleteProject(TimerProject project)
          Before delete project.
 void preDeleteTask(TimerTask task)
          Before delete task.
 void preMergeTasks(TimerTask destinationTask, List<TimerTask> otherTasks)
          Pre merge task.
 void preMoveTask(TimerTask task)
          Pre move task.
 void run()
           
protected  void saveAlerts(TimerTask task)
          Save task alerts.
protected  void saveProject(TimerProject project)
          Save a unique project.
protected  void saveRunningTasks()
          Save runing tasks.
protected  void saveTask(TimerTask task)
          Save task, found correct prefix, and project.
protected  void saveTask(TimerTask task, int associatedToProject, String taskPrefixName)
          Save a task.
protected  void saveTaskAnnotation(TimerTask task)
          Save annotation for a task.
 void setAnnotation(TimerTask task, Date date, String annotation)
          Add annotation.
 void setAutoSaveDelay(long autoSaveDelay)
          Set auto save delay (in milliseconds).
 void setSaveDirectory(String directory)
          Set file directory.
 void setTaskTime(TimerTask task, Date date, Long time)
          Set task time.
 void startTask(TimerTask task)
          Start task.
 void stopTask(TimerTask task)
          Stop task.
 void unlock()
          Try to unlock current saver directory.
 
Methods inherited from class org.chorem.jtimer.io.AbstractSaver
deleteBackupFile, makeBackupFile, restoreBackupFile
 
Methods inherited from class java.util.TimerTask
cancel, scheduledExecutionTime
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

GTIMER_SUBTASK_SEPARATOR

protected static final String GTIMER_SUBTASK_SEPARATOR
Separateur pour les sous taches. Utilise pour assurer une compatibilite avec gTimer, qui ne permet pas de stocker des sous taches.

See Also:
Constant Field Values

GTIMER_FILE_VERSION

protected static final String GTIMER_FILE_VERSION
Version du format de fichier.

See Also:
Constant Field Values

GTIMER_PROJECT_EXTENSION

protected static final String GTIMER_PROJECT_EXTENSION
Extension project.

See Also:
Constant Field Values

GTIMER_TASK_EXTENSION

protected static final String GTIMER_TASK_EXTENSION
Extension task.

See Also:
Constant Field Values

GTIMER_ANNOTATION_EXTENSION

protected static final String GTIMER_ANNOTATION_EXTENSION
Extension annotation.

See Also:
Constant Field Values

GTIMER_ALERT_EXTENSION

protected static final String GTIMER_ALERT_EXTENSION
Extension alert.

See Also:
Constant Field Values

GTIMER_EMPTY_PROJECT_NAME

protected static final String GTIMER_EMPTY_PROJECT_NAME
Empty gtimer project name.

See Also:
Constant Field Values

LOCK_FILE_NAME

protected static final String LOCK_FILE_NAME
Lock filename.

See Also:
Constant Field Values

saveDirectory

protected String saveDirectory
Save directory. Init with $user.home/.gtimer


lock

protected FileLock lock
Resource used by lock.


raf

protected RandomAccessFile raf
Resource used by lock.


channel

protected FileChannel channel
Resource used by lock.


autoSaveTimer

protected Timer autoSaveTimer
Timer used for autosave.


autoSaveDelay

protected long autoSaveDelay
Auto save delay. (default value is 5min)


runningTasks

protected Collection<TimerTask> runningTasks
Running task list.

Constructor Detail

GTimerIncrementalSaver

public GTimerIncrementalSaver()
Constructor.

Method Detail

setSaveDirectory

public void setSaveDirectory(String directory)
Description copied from interface: Saver
Set file directory.

Specified by:
setSaveDirectory in interface Saver
Parameters:
directory - directory

setAutoSaveDelay

public void setAutoSaveDelay(long autoSaveDelay)
Description copied from interface: Saver
Set auto save delay (in milliseconds).

Specified by:
setAutoSaveDelay in interface Saver
Parameters:
autoSaveDelay - delay in milliseconds

checkSaveDirectory

protected void checkSaveDirectory()
Check that save directory exists. Create it if not.


lock

public void lock()
          throws DataLockingException
Description copied from interface: Saver
Try to lock current saver directory.

Specified by:
lock in interface Saver
Throws:
DataLockingException - if can't obtain lock

unlock

public void unlock()
            throws DataLockingException
Description copied from interface: Saver
Try to unlock current saver directory.

Specified by:
unlock in interface Saver
Throws:
DataLockingException - if there is no lock

load

public Collection<TimerProject> load()
Description copied from interface: Saver
Load a project list.

Specified by:
load in interface Saver
Returns:
a collection of projects

gTimerLoad

protected Collection<TimerProject> gTimerLoad()
Load from gtimer file format.

Returns:
projects loaded

getProjectFromFile

protected TimerProject getProjectFromFile(File projectFile)
                                   throws IOException
Load a project in his file.

Parameters:
projectFile - project file
Returns:
a project
Throws:
IOException - if ioexception occurs

parseTaskFromFile

protected TimerTask parseTaskFromFile(Map<String,TimerProject> mapNumberProject,
                                      File taskFile,
                                      SortedMap<TimerTask,TimerProject> taskToManage)
                               throws IOException
Parse task file.

Parameters:
mapNumberProject - map entre les projets reels et leur numero gtimer
taskFile - gtimer task file
taskToManage - sorted map to remember task to post process
Returns:
parsed task
Throws:
IOException - if ioexception occurs

parseAnnotations

protected void parseAnnotations(TimerTask task)
                         throws IOException
Try to find and load annotations from task.

Parameters:
task - task to load annotations
Throws:
IOException - if can't read ann file

parseAlerts

protected void parseAlerts(TimerTask task)
                    throws IOException
Find task alert and load it. This file can't be loaded by Properties#load(java.io.InputStream). Constains duplicated keys.

Parameters:
task - task to load alert
Throws:
IOException - if can't read ann file

parseTaskFromSavedMap

protected void parseTaskFromSavedMap(SortedMap<TimerTask,TimerProject> taskToManage)
Post process task to manage subtask loaded before parent task.

Parameters:
taskToManage - sorted map of those task

findTask

protected TimerTask findTask(TimerTask parentTask,
                             String[] taskNames)
Find a task in task's subtask

Parameters:
parentTask - task
taskNames - names
Returns:
a task or null

backupGTimerFiles

protected void backupGTimerFiles()
For development. Save gtimer files (because project collection is sometimes erased)


isGTimerFile

protected boolean isGTimerFile(String filename)
Check if a file denoted by his name is a gtimer file. Check that the name end with .project, .task or .ann

Parameters:
filename - filename
Returns:
answer

saveProject

protected void saveProject(TimerProject project)
Save a unique project.

Parameters:
project - project to save

saveTask

protected void saveTask(TimerTask task)
Save task, found correct prefix, and project. Then call saveTask(TimerTask, int, String)

Parameters:
task - task to save

saveTask

protected void saveTask(TimerTask task,
                        int associatedToProject,
                        String taskPrefixName)
Save a task. taskPrefixName represents the task prefix is case, it is represented in jTimer as a subtask. It is saved in gTimer format in a new task, which name is composed of parents task name.

Parameters:
task - task to save
associatedToProject - associated project number
taskPrefixName - task prefix name

saveTaskAnnotation

protected void saveTaskAnnotation(TimerTask task)
Save annotation for a task.

Parameters:
task - task to save annotation

saveAlerts

protected void saveAlerts(TimerTask task)
Save task alerts.

Parameters:
task - task to save alert

addProject

public void addProject(TimerProject project)
Description copied from interface: DataEventListener
Add project event.

Specified by:
addProject in interface DataEventListener
Parameters:
project - project to add

addTask

public void addTask(TimerTask task)
Description copied from interface: DataEventListener
Add task event.

Specified by:
addTask in interface DataEventListener
Parameters:
task - task to add

getUnusedNumber

protected int getUnusedNumber(String extension)
Explore directory and find a non used number for project.

Parameters:
extension - to check (ie ".project" or ".task")
Returns:
a non used project number

dataLoaded

public void dataLoaded(Collection<TimerProject> projects)
Description copied from interface: DataEventListener
All data loaded.

Specified by:
dataLoaded in interface DataEventListener
Parameters:
projects - projects collection

preDeleteProject

public void preDeleteProject(TimerProject project)
Description copied from interface: DataEventListener
Before delete project. TODO only usefull for UI (can't known on to delete if already deleted)

Specified by:
preDeleteProject in interface DataEventListener
Parameters:
project - deleted project

preDeleteTask

public void preDeleteTask(TimerTask task)
Description copied from interface: DataEventListener
Before delete task. TODO only usefull for UI (can't known on to delete if already deleted)

Specified by:
preDeleteTask in interface DataEventListener
Parameters:
task - deleted task

deleteProject

public void deleteProject(TimerProject project)
Description copied from interface: DataEventListener
Delete project.

Specified by:
deleteProject in interface DataEventListener
Parameters:
project - deleted project

deleteTask

public void deleteTask(TimerTask task)
Description copied from interface: DataEventListener
Delete task.

Specified by:
deleteTask in interface DataEventListener
Parameters:
task - deleted task

deleteTaskOrProject

protected void deleteTaskOrProject(TimerTask taskOrProject,
                                   String extension)
Delete associated file or project files on disk.

Parameters:
taskOrProject - task or project to delete file
extension - extension without .

modifyProject

public void modifyProject(TimerProject project)
Description copied from interface: DataEventListener
Modify project.

Specified by:
modifyProject in interface DataEventListener
Parameters:
project - modified project

modifyTask

public void modifyTask(TimerTask task)
Description copied from interface: DataEventListener
Modify task.

Specified by:
modifyTask in interface DataEventListener
Parameters:
task - modified task

setAnnotation

public void setAnnotation(TimerTask task,
                          Date date,
                          String annotation)
Description copied from interface: DataEventListener
Add annotation.

Specified by:
setAnnotation in interface DataEventListener
Parameters:
task - task where annotation is changed
date - date
annotation - new annotation value

setTaskTime

public void setTaskTime(TimerTask task,
                        Date date,
                        Long time)
Description copied from interface: DataEventListener
Set task time.

Specified by:
setTaskTime in interface DataEventListener
Parameters:
task - task where time is changed
date - date
time - task time

postChangeClosedState

public void postChangeClosedState(TimerTask task)
Description copied from interface: DataEventListener
Post change task state.

Specified by:
postChangeClosedState in interface DataEventListener
Parameters:
task - task

preChangeClosedState

public void preChangeClosedState(TimerTask task)
Description copied from interface: DataEventListener
Pre change task state.

Specified by:
preChangeClosedState in interface DataEventListener
Parameters:
task - task

preMoveTask

public void preMoveTask(TimerTask task)
Description copied from interface: DataEventListener
Pre move task.

Specified by:
preMoveTask in interface DataEventListener
Parameters:
task - task

moveTask

public void moveTask(TimerTask task)
Description copied from interface: DataEventListener
Post move task.

Specified by:
moveTask in interface DataEventListener
Parameters:
task - task

postMergeTasks

public void postMergeTasks(TimerTask destinationTask,
                           List<TimerTask> otherTasks)
Description copied from interface: DataEventListener
Post merge task.

Specified by:
postMergeTasks in interface DataEventListener
Parameters:
destinationTask - task where all other task will be merged
otherTasks - other tasks to merge

preMergeTasks

public void preMergeTasks(TimerTask destinationTask,
                          List<TimerTask> otherTasks)
Description copied from interface: DataEventListener
Pre merge task.

Specified by:
preMergeTasks in interface DataEventListener
Parameters:
destinationTask - task where all other task will be merged
otherTasks - other tasks to merge

startTask

public void startTask(TimerTask task)
Description copied from interface: DataEventListener
Start task.

Specified by:
startTask in interface DataEventListener
Parameters:
task - started task

stopTask

public void stopTask(TimerTask task)
Description copied from interface: DataEventListener
Stop task.

Specified by:
stopTask in interface DataEventListener
Parameters:
task - stoped task

run

public void run()
Specified by:
run in interface Runnable
Specified by:
run in class TimerTask

saveRunningTasks

protected void saveRunningTasks()
Save runing tasks.


checkSetAnnotation

public void checkSetAnnotation(TimerTask task,
                               Date date,
                               String value)
Check update task annotation.

Specified by:
checkSetAnnotation in interface VetoableDataEventListener
Parameters:
task - task to update
date - day of change
value - new annotation

checkAddProject

public void checkAddProject(TimerProject project)
Check add project.

Specified by:
checkAddProject in interface VetoableDataEventListener
Parameters:
project - project to add

checkAddTask

public void checkAddTask(TimerTask parent,
                         TimerTask task)
Check add task.

Specified by:
checkAddTask in interface VetoableDataEventListener
Parameters:
parent - parent task
task - task to add

checkName

protected void checkName(TimerTask task)
Check task name.

Parameters:
task - task to check

checkChangeClosedState

public void checkChangeClosedState(TimerTask task)
Check change task state.

Specified by:
checkChangeClosedState in interface VetoableDataEventListener
Parameters:
task - task

checkDeleteProject

public void checkDeleteProject(TimerProject project)
Check delete project.

Specified by:
checkDeleteProject in interface VetoableDataEventListener
Parameters:
project - deleted project

checkDeleteTask

public void checkDeleteTask(TimerTask task)
Check delete task.

Specified by:
checkDeleteTask in interface VetoableDataEventListener
Parameters:
task - deleted task

checkModifyProject

public void checkModifyProject(TimerProject project)
Check modify project.

Specified by:
checkModifyProject in interface VetoableDataEventListener
Parameters:
project - modified project

checkModifyTask

public void checkModifyTask(TimerTask task)
Check modify task.

Specified by:
checkModifyTask in interface VetoableDataEventListener
Parameters:
task - modified task

checkMoveTask

public void checkMoveTask(TimerTask destination,
                          Collection<TimerTask> tasksToMove)
Check move task.

Specified by:
checkMoveTask in interface VetoableDataEventListener
Parameters:
destination - task to move to
tasksToMove - tasks to move

checkSetTaskTime

public void checkSetTaskTime(TimerTask task,
                             Date date,
                             Long value)
Check update task time.

Specified by:
checkSetTaskTime in interface VetoableDataEventListener
Parameters:
task - task to update
date - day of change
value - new time in seconds

checkMergeTasks

public void checkMergeTasks(TimerTask destinationTask,
                            List<TimerTask> otherTasks)
Check merge task.

Specified by:
checkMergeTasks in interface VetoableDataEventListener
Parameters:
destinationTask - task result of merge
otherTasks - other task to merge


Copyright © 2007-2012 CodeLutin. All Rights Reserved.