Exemplo n.º 1
0
		public string Send(ManagedItem managedItem, ITATSystem system, string sEnvironment, List<int> owningFacilityIDs)
		{
			//If the subject or body or recipients are empty, then do not send anything
			if ((string.IsNullOrEmpty(Subject)) || (string.IsNullOrEmpty(this.Text)) || (this.Recipients == null) || (this.Recipients.Count == 0))
				return string.Empty;

			string sError = string.Empty;

			string from = null;
			string subject = null;
			string text = null;
			Kindred.Common.Security.NameEmailPair[] emailRecipients = null;

			try
			{
				Kindred.Common.Email.Email email = new Kindred.Common.Email.Email();
				from = XMLNames._M_EmailFrom;
				subject = string.IsNullOrEmpty(Subject) ? "Subject Missing" : Subject;
				text = string.IsNullOrEmpty(Text) ? "Text Missing" : Text;

				Term.SubstituteBasicTerms(managedItem, ref text);
				Term.SubstituteSpecialTerms(ref text, managedItem);

				//Now complete the 'ManagedItemReference' email links - fill in the dynamically supplied info
				if (!string.IsNullOrEmpty(system.Name))
					text = text.Replace(XMLNames._M_SystemNameHolder, system.Name);
				if (!string.IsNullOrEmpty(sEnvironment))
					text = text.Replace(XMLNames._M_EnvironmentHolder, sEnvironment);
				text = text.Replace(XMLNames._M_ManagedItemIdHolder, managedItem.ManagedItemID.ToString());

				List<int> facilityIDs = null;
				if (owningFacilityIDs != null)
					facilityIDs = new List<int>(owningFacilityIDs);
				else
					facilityIDs = new List<int>();
				bool overrideFacilities = false;
				//Note - HasOwningFacility trumps use of filterFacilityTerm
				if (!(system.HasOwningFacility ?? false))
				{
					if (FilterFacilityTermID.HasValue)
					{
						Term filterFacilityTerm = managedItem.FindTerm(FilterFacilityTermID.Value);
						if (filterFacilityTerm != null)
						{
							FacilityTerm facilityTerm = null;
							if (filterFacilityTerm.TermType == TermType.Facility)
								facilityTerm = filterFacilityTerm as FacilityTerm;
							else
								facilityTerm = new FacilityTerm(false, managedItem, filterFacilityTerm);
							if (facilityTerm != null)
							{
								facilityIDs = facilityTerm.SelectedFacilityIDs;
								overrideFacilities = true;
							}
						}
					}
				}

				List<int> allRecipientFacilityIDs = new List<int>();
				
				if ((system.HasOwningFacility ?? false) || overrideFacilities)
				{
					foreach (string recipient in Recipients)
					{
						List<int> absoluteLevels = null;
						List<int> relativeLevels = null;
						system.GetFacilityLevels(recipient, ref absoluteLevels, ref relativeLevels);
						List<int> recipientFacilityIDs = FacilityCollection.FacilityAncestry(facilityIDs, absoluteLevels, relativeLevels);
						foreach (int facilityID in recipientFacilityIDs)
							if (!allRecipientFacilityIDs.Contains(facilityID))
								allRecipientFacilityIDs.Add(facilityID);
					}
					emailRecipients = SecurityHelper.GetEmailRecipients(system, Recipients, allRecipientFacilityIDs);
				}
				else
				{
					emailRecipients = SecurityHelper.GetEmailRecipients(system, Recipients);
				}

				EmailHelper.SendEmail(from, subject, text, emailRecipients, system, Recipients, allRecipientFacilityIDs);
			}
			catch (Exception e)
			{
				sError = string.Format("Email={0} Exception={1}", EmailInfo(from, emailRecipients, subject, text), e.ToString());
			}
			return sError;
		}