public HttpResponseMessage AdvanceProcess([Metadata("SequenceId", "Unique identifier of the processflow")] long sequenceId) { try { using (EntityModelCommon contextCommon = new EntityModelCommon()) { ProcessFlow currentStep = (from PF in contextCommon.ProcessFlows where PF.SequenceId == sequenceId select PF).FirstOrDefault(); if (currentStep != null) { if (currentStep.IsLastStep == false) { ProcessFlow nextStep = (from PF in contextCommon.ProcessFlows where PF.EntityId == currentStep.EntityId && PF.ProcessId == currentStep.ProcessId && PF.Sequence == currentStep.Sequence + 1 select PF).FirstOrDefault(); if (nextStep != null) { contextCommon.ProcessFlows.Attach(nextStep); nextStep.Status = "Pending"; } else { throw new ApplicationException("Process step is indicated not to be the last step, but the next step is not found. Check the process definition in the database."); } } contextCommon.ProcessFlows.Attach(currentStep); currentStep.Status = "Completed"; currentStep.EndDate = DateTime.Now; contextCommon.SaveChanges(); } else { throw new ApplicationException("Process step is not found based on the supplied ProcessFlowId."); } } response = new HttpResponseMessage(); response.StatusCode = HttpStatusCode.Accepted; return response; } catch (Exception ex) { string error = "Exception occurred when advancing the process."; ServiceBusException serviceBusException = new ServiceBusException("ProcessManager", "ProcessFlow", sequenceId.ToString(), error, ex.Message); ErrorHandler handler = new ErrorHandler(); handler.LogError(serviceBusException); serviceBusException = null; handler = null; response = new HttpResponseMessage(); response.StatusCode = HttpStatusCode.InternalServerError; string errorMsg = ex.Message.Replace("\r\n", string.Empty); errorMsg = errorMsg.Replace("\n", string.Empty); response.ReasonPhrase = errorMsg; return response; } }
public bool ResolveProcess(string messageType, string entityId, string sourceSystem) { try { using (EntityModelCommon contextCommon = new EntityModelCommon()) { //Not all fields of the ProcessDefinition are required //Select PD (the full selection) is used in order to be able to return a typed list List<ProcessDefinition> entries = (from PD in contextCommon.ProcessDefinitions where PD.Process.SourceSystem == sourceSystem && PD.Process.MessageType == messageType && PD.Process.ResolveProcess == true select PD).ToList<ProcessDefinition>(); if (entries.Count >= 1) { foreach (ProcessDefinition entry in entries) { ProcessFlow processFlow = new ProcessFlow(); processFlow.ProcessId = entry.ProcessId; processFlow.StepId = entry.StepId; processFlow.Sequence = entry.Sequence; processFlow.Status = entry.Status; processFlow.Notification = entry.Notification; processFlow.IsLastStep = entry.IsLastStep; processFlow.Entity = messageType; processFlow.EntityId = entityId; contextCommon.ProcessFlows.Add(processFlow); SaveChanges(contextCommon); } } else { GatewayException gatewayException = new GatewayException(String.Format("{0}Gateway", sourceSystem), messageType, entityId, "The process cannot be resolved. Please contact the administrator."); ErrorHandler handler = new ErrorHandler(); handler.LogError(gatewayException); gatewayException = null; handler = null; return false; } } return true; } catch (DbEntityValidationException dbEx) { string detailedException = ""; foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { detailedException = detailedException + validationError.ErrorMessage + "; "; } } //Log exception GatewayException gatewayException = new GatewayException(String.Format("{0}Gateway", sourceSystem), messageType, entityId, dbEx.Message, detailedException); ErrorHandler handler = new ErrorHandler(); handler.LogError(gatewayException); gatewayException = null; handler = null; return false; } catch (Exception ex) { //Log exception GatewayException gatewayException = new GatewayException(String.Format("{0}Gateway", sourceSystem), messageType, entityId, ex.Message, String.IsNullOrEmpty(ex.InnerException.ToString()) ? "" : ex.InnerException.ToString()); ErrorHandler handler = new ErrorHandler(); handler.LogError(gatewayException); gatewayException = null; handler = null; return false; } }
public HttpResponseMessage TriggerProcessStep(string triggerState, [Metadata("ProcessStep", "Name of the process step")] string processStep) { try { ProcessStepTrigger trigger; using (EntityModelCommon contextCommon = new EntityModelCommon()) { trigger = (from PF in contextCommon.ProcessFlows join PS in contextCommon.ProcessSteps on PF.StepId equals PS.StepId where PF.Status == "Pending" && PS.Name == processStep && PF.StartDate == null orderby PF.SequenceId ascending select new ProcessStepTrigger { ProcessFlowId = PF.SequenceId, EntityId = PF.EntityId }).FirstOrDefault(); // Add startdate to the processstep // Set polling action of trigger if (trigger != null) { ProcessFlow currentStep = (from PF in contextCommon.ProcessFlows where PF.SequenceId == trigger.ProcessFlowId select PF).FirstOrDefault(); if (currentStep != null) { contextCommon.ProcessFlows.Attach(currentStep); currentStep.StartDate = DateTime.Now; contextCommon.SaveChanges(); } else { throw new ApplicationException("Cannot set the startdate of the process step. Process step is not found based on the supplied ProcessFlowId."); } //If data -> Call EventTriggered -> triggerState = "ProcessStep is kicked off" triggerState = "ProcessStep is kicked off"; return Request.EventTriggered(trigger, triggerState: triggerState, pollAgain: new TimeSpan(0, 1, 0)); } else { //If no data -> Call EventWaitPoll -> triggerState = "ProcessStep is not kicked off" triggerState = "ProcessStep is not kicked off"; return Request.EventWaitPoll(retryDelay: new TimeSpan(0, 1, 0), triggerState: triggerState); } } } catch (Exception ex) { string error = String.Format("Exception occurred when triggering process step {0}", processStep); ServiceBusException serviceBusException = new ServiceBusException("ProcessManager", "ProcessFlow", "", error, ex.Message); ErrorHandler handler = new ErrorHandler(); handler.LogError(serviceBusException); serviceBusException = null; handler = null; response = new HttpResponseMessage(); response.StatusCode = HttpStatusCode.InternalServerError; string errorMsg = ex.Message.Replace("\r\n", string.Empty); errorMsg = errorMsg.Replace("\n", string.Empty); response.ReasonPhrase = errorMsg; return response; } }