public override void Execute(INotification notification) { base.Execute(notification); Console.WriteLine("NewTimeEntryCommand.Execute"); TaskProxy taskProxy = Facade.RetrieveProxy(TaskProxy.NAME) as TaskProxy; TimerProxy timerProxy = Facade.RetrieveProxy(TimerProxy.NAME) as TimerProxy; taskProxy.Comments = ""; //The required fields are StartTime, EndTime, Duration, and RelatedTask. TimeEntry timeEntry = new TimeEntry(taskProxy.CurrentTask); timeEntry.StartTime = DateTime.Now; taskProxy.CurrentTimeEntry = timeEntry; timerProxy.Timer.Start(); }
/// <summary> /// Parses a single TimeEntry from an XmlNode /// </summary> /// <param name="n">XmlNode to parse TimeEntry from</param> /// <returns>TimeEntry parsed from input XmlNode</returns> private static TimeEntry ParseTimeEntry(XmlNode n) { XmlNode taskNode = n.SelectSingleNode("task"); TimeEntry te = new TimeEntry(ParseTask(taskNode)); te.Id = GetNodeText(n, "id"); te.Duration = float.Parse(GetNodeText(n, "duration-in-seconds"), CultureInfo.InvariantCulture); te.Comments = GetNodeText(n, "comments"); if (GetNodeText(n, "tags").Length > 0) te.Tags.AddRange(GetNodeText(n, "tags").Split(',')); te.UpdatedTime = GetNodeText(n, "updated-at").Length > 0 ? DateTime.Parse(GetNodeText(n, "updated-at"), CultureInfo.InvariantCulture) : DateTime.MinValue; te.CreatedTime = GetNodeText(n, "created-at").Length > 0 ? DateTime.Parse(GetNodeText(n, "created-at"), CultureInfo.InvariantCulture) : DateTime.MinValue; te.EndTime = GetNodeText(n, "end-time").Length > 0 ? DateTime.Parse(GetNodeText(n, "end-time"), CultureInfo.InvariantCulture) : DateTime.MinValue; te.StartTime = GetNodeText(n, "start-time").Length > 0 ? DateTime.Parse(GetNodeText(n, "start-time"), CultureInfo.InvariantCulture) : DateTime.MinValue; te.InProgress = bool.Parse(GetNodeText(n, "in-progress")); return te; }
/// <summary> /// Updates or Inserts a Time Entry to SlimTimer /// The method determines if an insert is needed by looking at the ID field. /// If the ID is null, an insert will be performed, otherwise it will do an update. /// </summary> /// <param name="entry">TimeEntry to update/insert</param> /// <returns>TimeEntry returned from SlimTimer, this will return the new ID for inserted entries</returns> public TimeEntry UpdateTimeEntry(TimeEntry entry) { // verify the required fields have data so we don't get an error back from SlimTimer TimeSpan ts = entry.EndTime.Subtract(entry.StartTime); if (ts.TotalSeconds < entry.Duration) { throw new ArgumentOutOfRangeException("Entry.Duration", entry.Duration, "Duration cannot be longer than difference between the StartTime and EndTime"); } if (entry.Duration <= 0) { throw new ArgumentNullException("TimeEntry.Duration", "Duration must be greater than 0"); } if (entry.RelatedTask == null || entry.RelatedTask.Id == null || entry.RelatedTask.Id.Length == 0) { throw new ArgumentNullException("TimeEntry.RelatedTask", "The related task is either null or doesn't have a SlimTimer ID"); } if (!PrepareRequestCall()) return null; string url = GetBaseURL() + "/time_entries"; RequestType request = RequestType.Post; // if the task has an Id we are updating if (entry.Id != null && entry.Id.Length > 0) { url += "/" + entry.Id; request = RequestType.Put; } url += GetURLParams(); Console.WriteLine(url); // Create the XML data to send to SlimTimer XmlDocument data = new XmlDocument(); data.CreateXmlDeclaration("1.0", "UTF-8", ""); XmlNode root = data.CreateNode(XmlNodeType.Element, "time-entry", ""); data.AppendChild(root); XmlNode n = data.CreateNode(XmlNodeType.Element, "start-time", ""); n.InnerText = entry.StartTime.ToString("s", CultureInfo.InvariantCulture); root.AppendChild(n); n = data.CreateNode(XmlNodeType.Element, "end-time", ""); n.InnerText = entry.EndTime.ToString("s", CultureInfo.InvariantCulture); root.AppendChild(n); n = data.CreateNode(XmlNodeType.Element, "duration-in-seconds", ""); n.InnerText = entry.Duration.ToString(CultureInfo.InvariantCulture); root.AppendChild(n); n = data.CreateNode(XmlNodeType.Element, "task-id", ""); n.InnerText = entry.RelatedTask.Id; root.AppendChild(n); if (entry.Tags.Count > 0) { n = data.CreateNode(XmlNodeType.Element, "tags", ""); foreach (string s in entry.Tags) { n.InnerText += s + ","; } n.InnerText = n.InnerText.TrimEnd(','); root.AppendChild(n); } n = data.CreateNode(XmlNodeType.Element, "comments", ""); n.InnerText = entry.Comments; root.AppendChild(n); n = data.CreateNode(XmlNodeType.Element, "in-progress", ""); n.InnerText = entry.InProgress.ToString(CultureInfo.InvariantCulture); root.AppendChild(n); string Response = GetHttpPage(new Uri(url), request, ContentType.Xml, data.InnerXml); Collection<TimeEntry> result = ParseTimeEntries(Response); if (result.Count > 0) return result[0]; _Error = "Unable to update/insert TimeEntry"; return null; }