/// <summary> /// This method will add a Suggestion with a RefreshEntity FieldName and State to the Suggestions /// for this ServerEntity. By convention, this will tell the UI to refresh the Entity. This method /// is called when an Activity changes the Item (e.g. a DueDate is parsed out of the Name, a Contacts /// FieldName is created, etc). /// </summary> /// <param name="workflowInstance"></param> /// <param name="entity"></param> protected void SignalEntityRefresh(WorkflowInstance workflowInstance, ServerEntity entity) { var sugg = new Suggestion() { ID = Guid.NewGuid(), EntityID = entity.ID, EntityType = entity.GetType().Name, WorkflowType = workflowInstance.WorkflowType, WorkflowInstanceID = workflowInstance.ID, State = SuggestionTypes.RefreshEntity, SuggestionType = SuggestionTypes.RefreshEntity, DisplayName = SuggestionTypes.RefreshEntity, GroupDisplayName = SuggestionTypes.RefreshEntity, Value = null, TimeSelected = null }; SuggestionsContext.Suggestions.Add(sugg); SuggestionsContext.SaveChanges(); }
private bool Update(Suggestion current, Suggestion original, Suggestion modified) { bool updated = false; // only allow update of TimeSelected and ReasonSelected if (original.TimeSelected == null && current.TimeSelected == null && modified.TimeSelected.HasValue) { // web client sets Date(0) to get server timestamp (ticks since 1970) if (modified.TimeSelected.Value.Year == 1970) { current.TimeSelected = DateTime.UtcNow; } else { current.TimeSelected = modified.TimeSelected; } updated = true; } if (original.TimeSelected.HasValue && current.TimeSelected.HasValue && (original.TimeSelected == current.TimeSelected)) { if (modified.TimeSelected.HasValue) { // web client sets Date(0) to get server timestamp (ticks since 1970) if (modified.TimeSelected.Value.Year == 1970) { current.TimeSelected = DateTime.UtcNow; updated = true; } else if (modified.TimeSelected.Value > current.TimeSelected.Value) { current.TimeSelected = modified.TimeSelected; updated = true; } } else { current.TimeSelected = null; updated = true; } } if (original.ReasonSelected == current.ReasonSelected && current.ReasonSelected != modified.ReasonSelected) { current.ReasonSelected = modified.ReasonSelected; updated = true; } return updated; }
/// <summary> /// Create a set of suggestions from the results of calling the suggestion function /// </summary> /// <param name="workflowInstance">Workflow instance to operate over</param> /// <param name="item">Item to process</param> /// <param name="suggestionFunction">Suggestion generation function</param> /// <returns>Complete if an exact match was found, Pending if multiple suggestions created</returns> protected Status CreateSuggestions( WorkflowInstance workflowInstance, ServerEntity entity, Func<WorkflowInstance, ServerEntity, Dictionary<string,string>, Status> suggestionFunction) { // analyze the item for possible suggestions var suggestions = new Dictionary<string, string>(); Status status = suggestionFunction.Invoke(workflowInstance, entity, suggestions); TraceLog.TraceDetail(String.Format("Retrieved {0} suggestions from activity {1}", suggestions.Count, Name)); // if the function completed with an error, or without generating any data, return (this is typically a fail-fast state) if (status == Status.Error || suggestions.Count == 0) return status; // if an "exact match" was discovered without user input, store it now and return if (status == Status.Complete && suggestions.Count == 1) { string s = null; foreach (var value in suggestions.Values) s = value; StoreInstanceData(workflowInstance, ActivityVariables.LastStateData, s); StoreInstanceData(workflowInstance, OutputParameterName, s); TraceLog.TraceDetail(String.Format("Exact match {0} was found for activity {1}", s, Name)); return status; } // construct the group display name string groupDisplayName = GroupDisplayName; if (groupDisplayName == null) groupDisplayName = workflowInstance.State; else groupDisplayName = FormatStringTemplate(workflowInstance, groupDisplayName); // get the suggestion parent ID if available var parentID = GetInstanceData(workflowInstance, ActivityVariables.ParentID); // add suggestions received from the suggestion function try { int num = 0; foreach (var s in suggestions.Keys) { // limit to four suggestions if (num++ == 4) break; var sugg = new Suggestion() { ID = Guid.NewGuid(), ParentID = parentID == null ? (Guid?) null : new Guid(parentID), EntityID = entity.ID, EntityType = entity.GetType().Name, WorkflowType = workflowInstance.WorkflowType, WorkflowInstanceID = workflowInstance.ID, State = workflowInstance.State, SuggestionType = SuggestionType, DisplayName = s, GroupDisplayName = groupDisplayName, SortOrder = num, Value = suggestions[s], TimeSelected = null }; SuggestionsContext.Suggestions.Add(sugg); TraceLog.TraceDetail(String.Format("Created suggestion {0} in group {1} for activity {2}", s, groupDisplayName, Name)); } SuggestionsContext.SaveChanges(); return status; } catch (Exception ex) { TraceLog.TraceException("Activity execution failed", ex); return Status.Error; } }