PageRenderTime 56ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/DesktopEntities/Entities/Task.cs

#
C# | 399 lines | 256 code | 48 blank | 95 comment | 40 complexity | 07182263aba05c90246fecee8da7db9e MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Globalization;
  6. using Business.Logic;
  7. using Newtonsoft.Json;
  8. using System.Xml.Serialization;
  9. namespace Business.Entities
  10. {
  11. /// <summary>
  12. /// Represents a Google/ Task
  13. /// </summary>
  14. [Serializable]
  15. public class Task : NilzBusinessBase<Task>
  16. {
  17. public const int LengthOfGuid = 36;
  18. /// <summary>
  19. /// yyyyMMdd
  20. /// </summary>
  21. private const string GoogleStringDateFormat = "yyyyMMdd";
  22. #region Json Properties
  23. /// <summary>
  24. /// The task text
  25. /// </summary>
  26. public string name { get; set; }
  27. /// <summary>
  28. /// Detailed notes
  29. /// </summary>
  30. public string notes { get; set; }
  31. /// <summary>
  32. /// List IDs of the children tasks of this task
  33. /// </summary>
  34. public string[] child_id;
  35. /// <summary>
  36. /// List IDs this one's a part of (dont know if it actually can be member of more than one, but it's defined
  37. /// in goolge as an array
  38. /// </summary>
  39. public string[] list_id;
  40. /// <summary>
  41. /// Deleted?
  42. /// </summary>
  43. public bool deleted;
  44. /// <summary>
  45. /// Last modified unbknown format
  46. /// </summary>
  47. public long last_modified;
  48. /// <summary>
  49. /// Checked, done, complete?
  50. /// </summary>
  51. public bool completed;
  52. /// <summary>
  53. /// Date due in yyyyMMdd format
  54. /// </summary>
  55. public string task_date;
  56. /// <summary>
  57. /// The ID in the Google DB
  58. /// </summary>
  59. public string id;
  60. #endregion
  61. #region Extra properties
  62. [JsonIgnore]
  63. [XmlIgnore]
  64. public string ListId
  65. {
  66. get
  67. {
  68. if (list_id == null) return null;
  69. return list_id[0];
  70. }
  71. set
  72. {
  73. list_id = new string[1];
  74. list_id[0] = value;
  75. }
  76. }
  77. /// <summary>
  78. /// True if this task has a Google ID (i.e. not temporary guid)
  79. /// </summary>
  80. [JsonIgnore]
  81. [XmlIgnore]
  82. public bool HasGoogleId
  83. {
  84. get { return id != null && id.Length != LengthOfGuid && id.Length > 0; }
  85. }
  86. /// <summary>
  87. /// Returns google id. Null if temp id.
  88. /// </summary>
  89. [JsonIgnore]
  90. [XmlIgnore]
  91. public string GoogleId
  92. {
  93. get
  94. {
  95. if (!HasGoogleId) return null;
  96. return id;
  97. }
  98. }
  99. /// <summary>
  100. /// Children tasks in Google
  101. /// </summary>
  102. [JsonIgnore]
  103. [XmlIgnore]
  104. public List<Task> Children = new List<Task>();
  105. /// <summary>
  106. /// The unique EntryId in outlook
  107. /// </summary>
  108. [JsonIgnore]
  109. [XmlIgnore]
  110. public string OutlookId;
  111. [XmlIgnore]
  112. [JsonIgnore]
  113. public TaskList ParentList;
  114. /// <summary>
  115. /// Timestamp for last updated
  116. /// </summary>
  117. [JsonIgnore]
  118. [XmlIgnore]
  119. public DateTime CreatedAt;
  120. /// <summary>
  121. /// Timestamp for last modified. Mirrors last_modified
  122. /// </summary>
  123. [JsonIgnore]
  124. [XmlIgnore]
  125. public DateTime LastModified
  126. {
  127. get
  128. {
  129. // Parse _lastmodified
  130. return StringUtil.GooglTimeToDateTime(last_modified);
  131. }
  132. set
  133. {
  134. last_modified = StringUtil.DateTimeToGoogleTime(value);
  135. }
  136. }
  137. /// <summary>
  138. /// Index/Ordinal
  139. /// </summary>
  140. [JsonIgnore]
  141. [XmlIgnore]
  142. public int Ordinal;
  143. /// <summary>
  144. /// 0 = not indented
  145. /// </summary>
  146. [JsonIgnore]
  147. [XmlIgnore]
  148. public int IndentLevel;
  149. [JsonIgnore]
  150. [XmlIgnore]
  151. public Task ParentTask;
  152. /// <summary>
  153. /// Task visible directly above this one
  154. /// </summary>
  155. //[JsonIgnore]
  156. //public Task PriorTask;
  157. /// <summary>
  158. /// Id of parent task (if indented) or list
  159. /// </summary>
  160. [JsonIgnore]
  161. [XmlIgnore]
  162. public string ParentElementId
  163. {
  164. get
  165. {
  166. if (ParentTask != null) return ParentTask.id;
  167. return ListId;
  168. }
  169. }
  170. /// <summary>
  171. /// Deadline for task. Mirrors task_date.
  172. /// </summary>
  173. [JsonIgnore]
  174. [XmlIgnore]
  175. public DateTime TaskDate
  176. {
  177. get
  178. {
  179. if (String.IsNullOrEmpty(task_date)) return DateTime.MinValue;
  180. try
  181. {
  182. var date = DateTime.ParseExact(task_date, GoogleStringDateFormat, new DateTimeFormatInfo());
  183. return date;
  184. }
  185. catch (Exception)
  186. {
  187. return DateTime.MinValue;
  188. }
  189. }
  190. set
  191. {
  192. if (value.Year > 4000) value = DateTime.MinValue;
  193. if (value == DateTime.MinValue) task_date = "";
  194. else task_date = value.ToString(GoogleStringDateFormat);
  195. }
  196. }
  197. [JsonIgnore]
  198. [XmlIgnore]
  199. public bool HasTaskDate
  200. {
  201. get { return TaskDate != DateTime.MinValue; }
  202. set
  203. {
  204. if (value)
  205. {
  206. if (TaskDate == DateTime.MinValue) TaskDate = DateTime.Today;
  207. }
  208. else
  209. {
  210. TaskDate = DateTime.MinValue;
  211. }
  212. }
  213. }
  214. [JsonIgnore]
  215. [XmlIgnore]
  216. public string DateStringShort
  217. {
  218. get
  219. {
  220. if (!HasTaskDate) return "N/A";
  221. return TaskDate.ToShortDateString();
  222. }
  223. }
  224. [JsonIgnore]
  225. [XmlIgnore]
  226. public object OutlookTask;
  227. #endregion
  228. #region Constructors
  229. public Task()
  230. {
  231. id = Guid.NewGuid().ToString(); // Temp ID before stored to google
  232. }
  233. #endregion
  234. #region Business methods
  235. public override string ToString()
  236. {
  237. string cp = completed ? "[X]" : "[ ]";
  238. return Ordinal + ":"+ cp + " " + name;
  239. }
  240. /// <summary>
  241. /// Copies properties form given task
  242. /// </summary>
  243. /// <param name="source"></param>
  244. public void CopyProperties(Task source, bool includeOrdinal)
  245. {
  246. name = source.name;
  247. notes = source.notes;
  248. deleted = source.deleted;
  249. completed = source.completed;
  250. //child_id = source.child_id;
  251. TaskDate = source.TaskDate;
  252. if (includeOrdinal) Ordinal = source.Ordinal;
  253. }
  254. /// <summary>
  255. /// returns true if id,completed,name,notes and task date equals
  256. /// </summary>
  257. public bool DataEquals(Task other)
  258. {
  259. bool equals = true;
  260. equals &= other.id == id;
  261. equals &= other.completed == completed;
  262. //equals &= ArrayEquals(child_id, other.child_id);
  263. equals &= StringUtil.EqualsOrNull(other.name, name);
  264. equals &= StringUtil.EqualsOrNull(other.notes, notes);
  265. equals &= StringUtil.EqualsOrNull(other.task_date, task_date);
  266. return equals;
  267. }
  268. #endregion
  269. public void AddChild(Task task)
  270. {
  271. Children.Add(task);
  272. SetChildIdFromList();
  273. }
  274. public void RemoveChild(Task task)
  275. {
  276. Children.Remove(task);
  277. SetChildIdFromList();
  278. }
  279. private void SetChildIdFromList()
  280. {
  281. child_id = new string[Children.Count];
  282. for (int i = 0; i < child_id.Length; i++)
  283. {
  284. child_id[i] = Children[i].id;
  285. }
  286. }
  287. internal static bool ParentTaskEquals(Task one, Task two)
  288. {
  289. if (one == null || two == null) throw new ArgumentException("Null input in parentTaskEquals");
  290. if (one.ParentTask == null && two.ParentTask == null) return true;
  291. if (one.ParentTask != null && two.ParentTask == null) return false;
  292. if (one.ParentTask == null && two.ParentTask != null) return false;
  293. return one.ParentTask.id == two.ParentTask.id;
  294. }
  295. public Task GetPriorTask()
  296. {
  297. return ParentList.GetPriorSibling(this);
  298. }
  299. /// <summary>
  300. /// Sets new ID and updates all parent objects refering to this
  301. /// </summary>
  302. /// <param name="newId"></param>
  303. public void SetNewId(string newId)
  304. {
  305. ParentList.ReplaceChildIds(id, newId);
  306. id = newId;
  307. }
  308. /// <summary>
  309. /// Creates a new GUID as ID and updates all parent objects refering to this
  310. /// </summary>
  311. public void SetNewId()
  312. {
  313. SetNewId(Guid.NewGuid().ToString());
  314. }
  315. public int GetChildCountDeep()
  316. {
  317. return GetChildCountDeepRec(0);
  318. }
  319. private int GetChildCountDeepRec(int count)
  320. {
  321. count += Children.Count;
  322. foreach (Task child in Children) count += child.GetChildCountDeepRec(count);
  323. return count;
  324. }
  325. /// <summary>
  326. /// Adds ordinal recursively by N and saves each item
  327. /// </summary>
  328. /// <param name="n">What to add ordinal with</param>
  329. public void AddOrdinalDeep(int n)
  330. {
  331. Ordinal += n;
  332. //outlook.InsertOrUpdateTask(this, null);
  333. foreach (Task child in Children) child.AddOrdinalDeep(n);
  334. }
  335. /// <summary>
  336. /// Returns this task with all children recursively.
  337. /// </summary>
  338. /// <returns></returns>
  339. public List<Task> GetThisAndChildren()
  340. {
  341. var list = new List<Task>();
  342. GetThisAndChildren(list);
  343. return list;
  344. }
  345. private void GetThisAndChildren(List<Task> list)
  346. {
  347. list.Add(this);
  348. foreach (Task task in Children) task.GetThisAndChildren(list);
  349. }
  350. }
  351. }