public bool SaveWorkflow(HRPaidTimeOffDataContext db, ref ENTValidationErrors validationErrors, ENTBaseEO item, int userAccountId) { WFItem.ItemId = item.ID; ValidateWorkflow(db, ref validationErrors, item); if (validationErrors.Count == 0) { //Set the ID for all the child owner objects foreach (ENTWFItemOwnerEO entWFItemOwner in WFOwners) { entWFItemOwner.ENTWFItemId = item.ID; } foreach (ENTWFItemStateHistoryEO entWFItemStateHistory in WFStateHistory) { entWFItemStateHistory.ENTWFItemId = item.ID; } if (WFItem.Save(db, ref validationErrors, userAccountId)) { foreach (ENTWFItemOwnerEO wfItemOwner in WFOwners) { wfItemOwner.ENTWFItemId = WFItem.ID; if (wfItemOwner.Save(db, ref validationErrors, userAccountId) == false) { return(false); } } foreach (ENTWFItemStateHistoryEO wfItemStateHistory in WFStateHistory) { if (wfItemStateHistory.IsNewRecord()) { //A state history is only added if an item changes state or a different person becomes the owner. //Send notification if user became owner, Chapter 8. //Check if the new owner is registered to recieve a notification when they become the owner of an item. ENTNotificationEO myNotification = new ENTNotificationEO(); if (myNotification.Load(db, ENTNotificationEO.NotificationType.IBecameOwnerOfIssue, wfItemStateHistory.ENTUserAccountId)) { //Get the new owner's email address ENTUserAccountEO newOwner = new ENTUserAccountEO(); newOwner.Load(db, wfItemStateHistory.ENTUserAccountId); ENTEmailEO email = new ENTEmailEO { FromEmailAddress = myNotification.FromEmailAddress, Subject = ReplaceTokens(myNotification.Subject, item), Body = ReplaceTokens(myNotification.Body, item), EmailStatusFlag = ENTEmailEO.EmailStatusFlagEnum.NotSent, ToEmailAddress = newOwner.Email }; email.Save(db, ref validationErrors, userAccountId); } } wfItemStateHistory.ENTWFItemId = WFItem.ID; if (wfItemStateHistory.Save(db, ref validationErrors, userAccountId) == false) { return(false); } } //Call any methods the transition requires if (ENTWFTransitionId != 0) { ENTWFTransitionEO entWFTransition = WFTransitions.Get(ENTWFTransitionId); if (entWFTransition.PostTransitionMethodName != null) { //Create an instance of the object Type objectType = Type.GetType(Workflow.ENTWorkflowObjectName); //object listObject = Activator.CreateInstance(objectType); //Call the method to load the object objectType.InvokeMember(entWFTransition.PostTransitionMethodName, BindingFlags.InvokeMethod, null, item, new object[] { db }); } //Send notifications if user requests to be notified when their issue changes state, Chapter 8. ENTNotificationEO issueChangedStateNotification = new ENTNotificationEO(); if (issueChangedStateNotification.Load(db, ENTNotificationEO.NotificationType.MyRequestChangedState, WFItem.SubmitterENTUserAccountId)) { //Get the submitters email address. ENTUserAccountEO submitter = new ENTUserAccountEO(); submitter.Load(db, WFItem.SubmitterENTUserAccountId); ENTEmailEO email = new ENTEmailEO { FromEmailAddress = issueChangedStateNotification.FromEmailAddress, Subject = ReplaceTokens(issueChangedStateNotification.Subject, item), Body = ReplaceTokens(issueChangedStateNotification.Body, item), EmailStatusFlag = ENTEmailEO.EmailStatusFlagEnum.NotSent, ToEmailAddress = submitter.Email }; email.Save(db, ref validationErrors, userAccountId); } //Check if anyone registered for this notification for the current state. ENTNotificationENTUserAccountEOList goesToStateNotification = new ENTNotificationENTUserAccountEOList(); goesToStateNotification.Load(db, WFItem.CurrentWFStateId, ENTNotificationEO.NotificationType.IssueIOwnedGoesToState); if (goesToStateNotification.Count > 0) { //Get the notification details to send the email. ENTNotificationEO notification = new ENTNotificationEO(); notification.Load(db, (int)ENTNotificationEO.NotificationType.IssueIOwnedGoesToState); //Send notifications if user requests to be notified if they owned an issue an it reaches a specific state. foreach (ENTWFItemOwnerEO owner in WFOwners) { ENTNotificationENTUserAccountEO notifyForState = goesToStateNotification.GetByENTUserAccountId((int)owner.ENTUserAccountId); if (notifyForState != null) { //Get the owner's email address. ENTUserAccountEO ownerUserAccount = new ENTUserAccountEO(); ownerUserAccount.Load(db, (int)owner.ENTUserAccountId); ENTEmailEO email = new ENTEmailEO { FromEmailAddress = notification.FromEmailAddress, Subject = ReplaceTokens(notification.Subject, item), Body = ReplaceTokens(notification.Body, item), EmailStatusFlag = ENTEmailEO.EmailStatusFlagEnum.NotSent, ToEmailAddress = ownerUserAccount.Email }; email.Save(db, ref validationErrors, userAccountId); } } } } return(true); } else { //Failed item save. return(false); } } else { //Failed Validation return(false); } }
public void LoadByENTUserAccountId(int entUserAccountId) { _userNotifications.Load(entUserAccountId); }