#region License
/**
* Ingenious MVC : An MVC framework for .NET 2.0
* Copyright (C) 2006, JDP Group
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Authors: - Kent Boogaart (kentcb@internode.on.net)
*/
#endregion
using System;
using Ingenious.Mvc.Util;
namespace Ingenious.Mvc{
/// <include file='Task.doc.xml' path='/doc/member[@name="T:Task"]/*'/>
[Serializable]
public class Task : IEquatable<Task>
{
private Task _owner;
private Id<Task> _id;
private TaskManager _taskManager;
private DateTime _timestamp;
[field: NonSerialized]
private object _data;
private TaskState _state;
private static readonly Log _log = Log.CreateForType(typeof(Task));
/// <include file='Task.doc.xml' path='/doc/member[@name="P:Owner"]/*'/>
public Task Owner
{
get
{
return _owner;
}
}
/// <include file='Task.doc.xml' path='/doc/member[@name="P:Id"]/*'/>
public Id<Task> Id
{
get
{
return _id;
}
}
/// <include file='Task.doc.xml' path='/doc/member[@name="P:Manager"]/*'/>
public TaskManager Manager
{
get
{
return _taskManager;
}
}
/// <include file='Task.doc.xml' path='/doc/member[@name="P:Timestamp"]/*'/>
public DateTime Timestamp
{
get
{
return _timestamp;
}
}
/// <include file='Task.doc.xml' path='/doc/member[@name="P:Data"]/*'/>
public object Data
{
get
{
return _data;
}
}
/// <include file='Task.doc.xml' path='/doc/member[@name="P:State"]/*'/>
public TaskState State
{
get
{
return _state;
}
}
/// <include file='Task.doc.xml' path='/doc/member[@name="E:Starting"]/*'/>
[field: NonSerialized]
public event EventHandler<TaskEventArgs> Starting;
/// <include file='Task.doc.xml' path='/doc/member[@name="E:Started"]/*'/>
[field: NonSerialized]
public event EventHandler<TaskEventArgs> Started;
/// <include file='Task.doc.xml' path='/doc/member[@name="E:Ending"]/*'/>
[field: NonSerialized]
public event EventHandler<TaskEndingEventArgs> Ending;
/// <include file='Task.doc.xml' path='/doc/member[@name="E:Ended"]/*'/>
[field: NonSerialized]
public event EventHandler<TaskEventArgs> Ended;
/// <include file='Task.doc.xml' path='/doc/member[@name="M:.ctor(Ingenious.Mvc.Task,Ingenious.Mvc.Id`1,Ingenious.Mvc.TaskManager,System.Object)"]/*'/>
public Task(Task owner, Id<Task> id, TaskManager taskManager, object data)
{
ArgumentHelper.AssertNotEmpty(id, "id");
ArgumentHelper.AssertNotNull(taskManager, "taskManager");
_owner = owner;
_id = id;
_taskManager = taskManager;
_timestamp = DateTime.Now;
_data = data;
_log.Verbose("Constructed Task with ID '{0}' and owner task ID '{1}'.", id, (owner == null) ? string.Empty : owner.Id.ToString());
}
/// <include file='Task.doc.xml' path='/doc/member[@name="M:Start()"]/*'/>
internal void Start()
{
_log.Information("Starting task with ID '{0}'.", _id);
TaskEventArgs taskEventArgs = new TaskEventArgs(this);
OnStarting(taskEventArgs);
_state = TaskState.Started;
OnStarted(taskEventArgs);
_log.Information("Started task with ID '{0}'.", _id);
}
/// <include file='Task.doc.xml' path='/doc/member[@name="M:End()"]/*'/>
public void End()
{
ExceptionHelper.ThrowIf(_state != TaskState.Started, "End.taskNotStarted", Id);
_log.Information("Ending task with ID '{0}'.", _id);
TaskEndingEventArgs endingEventArgs = new TaskEndingEventArgs(this);
OnEnding(endingEventArgs);
//allows the ending to be cancelled
if (!endingEventArgs.Cancel)
{
_state = TaskState.Ended;
OnEnded(new TaskEventArgs(this));
_log.Information("Ended task with ID '{0}'.", _id);
}
else
{
_log.Information("Ending of task with ID '{0}' was cancelled.", _id);
}
}
/// <include file='Task.doc.xml' path='/doc/member[@name="M:Equals(Ingenious.Mvc.Task)"]/*'/>
public bool Equals(Task task)
{
if (task == null)
{
return false;
}
return _id.Equals(task._id);
}
/// <include file='Task.doc.xml' path='/doc/member[@name="M:Equals(System.Object)"]/*'/>
public override bool Equals(object obj)
{
Task task = obj as Task;
if (task != null)
{
return Equals(task);
}
return false;
}
/// <include file='Task.doc.xml' path='/doc/member[@name="M:GetHashCode()"]/*'/>
public override int GetHashCode()
{
return _id.GetHashCode();
}
/// <summary>
/// Raises the <see cref="Starting"/> event.
/// </summary>
protected virtual void OnStarting(TaskEventArgs e)
{
EventHelper.Raise(Starting, this, e);
}
/// <summary>
/// Raises the <see cref="Started"/> event.
/// </summary>
protected virtual void OnStarted(TaskEventArgs e)
{
EventHelper.Raise(Started, this, e);
}
/// <summary>
/// Raises the <see cref="Ending"/> event.
/// </summary>
protected virtual void OnEnding(TaskEndingEventArgs e)
{
EventHelper.Raise(Ending, this, e);
}
/// <summary>
/// Raises the <see cref="Ended"/> event.
/// </summary>
protected virtual void OnEnded(TaskEventArgs e)
{
EventHelper.Raise(Ended, this, e);
}
}
}
|