using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Text; using System.Runtime.CompilerServices; using umbraco.BusinessLogic; using umbraco.DataLayer; namespace umbraco.cms.businesslogic.task { /// /// An umbraco task is currently only used with the translation workflow in umbraco. But is extendable to cover other taskbased system as well. /// A task represent a simple job, it will always be assigned to a user, related to a node, and contain a comment about the task. /// The user attached to the task can complete the task, and the author of the task can reopen tasks that are not complete correct. /// /// Tasks can in umbraco be used for setting up simple workflows, and contains basic controls structures to determine if the task is completed or not. /// public class Task { #region Properties private int _id; /// /// Gets or sets the id. /// /// The id. public int Id { get { return _id; } set { _id = value; } } private bool _closed; /// /// Gets or sets a value indicating whether this is closed. /// /// true if closed; otherwise, false. public bool Closed { get { return _closed; } set { _closed = value; } } private CMSNode _node; /// /// Gets or sets the node. /// /// The node. public CMSNode Node { get { return _node; } set { _node = value; } } private TaskType _type; /// /// Gets or sets the type. /// /// The type. public TaskType Type { get { return _type; } set { _type = value; } } private User _parentUser; /// /// Gets or sets the parent user. /// /// The parent user. public User ParentUser { get { return _parentUser; } set { _parentUser = value; } } private string _comment; /// /// Gets or sets the comment. /// /// The comment. public string Comment { get { return _comment; } set { _comment = value; } } private DateTime _date; /// /// Gets or sets the date. /// /// The date. public DateTime Date { get { return _date; } set { _date = value; } } private User _user; /// /// Gets or sets the user. /// /// The user. public User User { get { return _user; } set { _user = value; } } /// /// Gets the SQL helper. /// /// The SQL helper. protected static ISqlHelper SqlHelper { get { return Application.SqlHelper; } } #endregion #region Constructors /// /// Initializes a new instance of the class. /// public Task() { } /// /// Initializes a new instance of the class. /// /// The task id. public Task(int TaskId) { using (IRecordsReader dr = SqlHelper.ExecuteReader( "select id, taskTypeId, nodeId, parentUserId, userId, DateTime, comment, closed from cmsTask where id = @id", SqlHelper.CreateParameter("@id", TaskId))) { if (dr.Read()) { PopulateTaskFromReader(dr); } else { throw new ArgumentException("Task with id: '" + TaskId + "' not found"); } } } #endregion #region Public Methods /// /// Deletes the current task. /// Generally tasks should not be deleted and closed instead. /// public void Delete() { SqlHelper.ExecuteNonQuery("DELETE FROM cmsTask WHERE id = @id", SqlHelper.CreateParameter("@id", this._id)); } /// /// Saves this instance. /// [MethodImpl(MethodImplOptions.Synchronized)] public void Save() { //error checking if (Node == null) throw new ArgumentNullException("Node"); if (ParentUser == null) throw new ArgumentNullException("ParentUser"); if (User == null) throw new ArgumentNullException("User"); if (Id == 0) { // The method is synchronized SqlHelper.ExecuteNonQuery( "insert into cmsTask (closed, taskTypeId, nodeId, parentUserId, userId, comment) values (@closed, @taskTypeId, @nodeId, @parentUserId, @userId, @comment)", SqlHelper.CreateParameter("@closed", Closed), SqlHelper.CreateParameter("@taskTypeId", Type.Id), SqlHelper.CreateParameter("@nodeId", Node.Id), SqlHelper.CreateParameter("@parentUserId", ParentUser.Id), SqlHelper.CreateParameter("@userId", User.Id), SqlHelper.CreateParameter("@comment", Comment)); Id = SqlHelper.ExecuteScalar("SELECT MAX(id) FROM cmsTask"); } else { SqlHelper.ExecuteNonQuery( "update cmsTask set closed = @closed, taskTypeId = @taskTypeId, nodeId = @nodeId, parentUserId = @parentUserId, userId = @userId, comment = @comment where id = @id", SqlHelper.CreateParameter("@closed", Closed), SqlHelper.CreateParameter("@taskTypeId", Type.Id), SqlHelper.CreateParameter("@nodeId", Node.Id), SqlHelper.CreateParameter("@parentUserId", ParentUser.Id), SqlHelper.CreateParameter("@userId", User.Id), SqlHelper.CreateParameter("@comment", Comment), SqlHelper.CreateParameter("@id", Id)); } } #endregion #region Private methods private void PopulateTaskFromReader(IRecordsReader dr) { _id = dr.GetInt("id"); Type = new TaskType((int)dr.GetByte("taskTypeId")); Node = new CMSNode(dr.GetInt("nodeId")); ParentUser = User.GetUser(dr.GetInt("parentUserId")); User = User.GetUser(dr.GetInt("userId")); Date = dr.GetDateTime("DateTime"); Comment = dr.GetString("comment"); Closed = dr.GetBoolean("closed"); } #endregion #region static methods /// /// Returns all tasks by type /// /// /// public static Tasks GetTasksByType(int taskType) { var sql = "select id, taskTypeId, nodeId, parentUserId, userId, DateTime, comment, closed from cmsTask where taskTypeId = @taskTypeId"; Tasks tasks = new Tasks(); using (IRecordsReader dr = SqlHelper.ExecuteReader(sql, SqlHelper.CreateParameter("@taskTypeId", taskType))) { while (dr.Read()) { var t = new Task(); t.PopulateTaskFromReader(dr); tasks.Add(t); } } return tasks; } /// /// Get all tasks assigned to a node /// /// /// public static Tasks GetTasks(int nodeId) { var sql = "select id, taskTypeId, nodeId, parentUserId, userId, DateTime, comment, closed from cmsTask where nodeId = @nodeId"; Tasks tasks = new Tasks(); using (IRecordsReader dr = SqlHelper.ExecuteReader(sql, SqlHelper.CreateParameter("@nodeId", nodeId))) { while (dr.Read()) { var t = new Task(); t.PopulateTaskFromReader(dr); tasks.Add(t); } } return tasks; } /// /// Retrieves a collection of open tasks assigned to the user /// /// The User who have the tasks assigned /// If true both open and closed tasks will be returned /// A collections of tasks public static Tasks GetTasks(User User, bool IncludeClosed) { string sql = "select id, taskTypeId, nodeId, parentUserId, userId, DateTime, comment, closed from cmsTask where userId = @userId"; if (!IncludeClosed) sql += " and closed = 0"; sql += " order by DateTime desc"; Tasks tasks = new Tasks(); using (IRecordsReader dr = SqlHelper.ExecuteReader( sql, SqlHelper.CreateParameter("@userId", User.Id))) { while (dr.Read()) { var t = new Task(); t.PopulateTaskFromReader(dr); tasks.Add(t); } } return tasks; } /// /// Retrieves a collection of open tasks assigned to the user /// /// The User who have the tasks assigned /// If true both open and closed tasks will be returned /// A collections of tasks public static Tasks GetOwnedTasks(User User, bool IncludeClosed) { string sql = "select id, taskTypeId, nodeId, parentUserId, userId, DateTime, comment, closed from cmsTask where parentUserId = @userId"; if (!IncludeClosed) sql += " and closed = 0"; sql += " order by DateTime desc"; Tasks tasks = new Tasks(); using (IRecordsReader dr = SqlHelper.ExecuteReader( sql, SqlHelper.CreateParameter("@userId", User.Id))) { while (dr.Read()) { var t = new Task(); t.PopulateTaskFromReader(dr); tasks.Add(t); } } return tasks; } #endregion } }