/// <summary> /// Creates the new element. /// </summary> /// <param name="elementDefinition">The element definition.</param> /// <returns></returns> /// <exception cref="EngineException"> /// Error creating new element. The parent task not exists. /// or /// Error creating new element. The element type not exists. /// </exception> public Guid CreateNewElement(IElementDefinitionCreate elementDefinition) { IElementEntry elementEntry = null; using (var transaction = uow.BeginTransaction()) { // Load parent task var taskEntry = engineRepository.GetTaskById(elementDefinition.ParentTaskId); // Check if parent task exists if (taskEntry == null) { throw new EngineException(logger, "Error creating new element. The parent task not exists."); } // get the engine element IEngineElement engineElement = GetEngineElement(elementDefinition.ElementTypeKey); // Get the right entry model for this engine element type elementEntry = (IElementEntry)engineElement.GetEmptyEntity(); // Populate the entry element whit data provided by user elementEntry.ParentTask = taskEntry; elementEntry.Name = elementDefinition.Name; elementEntry.Description = elementDefinition.Description; elementEntry.IsEnabled = true; // create the element in db engineRepository.CreateElement(elementEntry); } return(elementEntry.Id); }
/// <summary> /// Executes the element. /// </summary> /// <param name="element">The element.</param> /// <exception cref="EngineException"> /// The element '{element.Name}' cannot be executed. This task was aborted. /// or /// The element {element.Name} (id: {element.InstanceID}) was not executed cause one or more errors occurs. This task was aborted. /// or /// The element {element.Name} (id: {element.InstanceID}) was executed but one or more warning occurs. This task was aborted. /// </exception> public void ExecuteElement(IEngineElement element) { // Check if element can be executed if (!element.CanBeExecuted()) { element.AddMessage($"The element cannot be executed.", MessageSeverity.Error); if (element.ParentTask.FailIfAnyElementHasError) { throw new EngineException(logger, $"The element cannot be executed. This task was aborted."); } else { return; } } // Execute element if (ElementStarted != null) { ElementStarted.Invoke((Guid)element.EntityId, (Guid)element.InstanceID); } element.Start(); // stop element element.Stop(); if (element.HasErrors) { element.AddMessage($"The element (instance: {element.InstanceID}) was not executed cause one or more errors occurs.", MessageSeverity.Error); if (element.ParentTask.FailIfAnyElementHasError) { if (ElementFinished != null) { ElementFinished.Invoke((Guid)element.EntityId, (Guid)element.InstanceID, false); } throw new EngineException(logger, $"The element (instance: {element.InstanceID}) was not executed cause one or more errors occurs. This task was aborted."); } } else if (element.HasWarnings) { element.AddMessage($"The element (instance: {element.InstanceID}) was executed but one or more warning occurs.", MessageSeverity.Warning); if (element.ParentTask.FailIfAnyElementHasWarning) { if (ElementFinished != null) { ElementFinished.Invoke((Guid)element.EntityId, (Guid)element.InstanceID, false); } throw new EngineException(logger, $"The element (instance: {element.InstanceID}) was executed but one or more warning occurs. This task was aborted."); } } if (ElementFinished != null) { ElementFinished.Invoke((Guid)element.EntityId, (Guid)element.InstanceID, true); } }