/// <summary>
/// Updates a communication model with the user-entered values
/// </summary>
/// <param name="communicationService">The service.</param>
/// <returns></returns>
private Rock.Model.Communication UpdateCommunication(RockContext rockContext)
{
var communicationService = new CommunicationService(rockContext);
var recipientService = new CommunicationRecipientService(rockContext);
Rock.Model.Communication communication = null;
if ( CommunicationId.HasValue )
{
communication = communicationService.Get( CommunicationId.Value );
// Remove any deleted recipients
foreach(var recipient in recipientService.GetByCommunicationId( CommunicationId.Value ) )
{
if (!Recipients.Any( r => recipient.PersonAlias != null && r.PersonId == recipient.PersonAlias.PersonId))
{
recipientService.Delete(recipient);
communication.Recipients.Remove( recipient );
}
}
}
if (communication == null)
{
communication = new Rock.Model.Communication();
communication.Status = CommunicationStatus.Transient;
communication.SenderPersonAliasId = CurrentPersonAliasId;
communicationService.Add( communication );
}
// Add any new recipients
foreach(var recipient in Recipients )
{
if ( !communication.Recipients.Any( r => r.PersonAlias != null && r.PersonAlias.PersonId == recipient.PersonId ) )
{
var person = new PersonService( rockContext ).Get( recipient.PersonId );
if ( person != null )
{
var communicationRecipient = new CommunicationRecipient();
communicationRecipient.PersonAlias = person.PrimaryAlias;
communication.Recipients.Add( communicationRecipient );
}
}
}
communication.IsBulkCommunication = cbBulk.Checked;
communication.MediumEntityTypeId = MediumEntityTypeId;
communication.MediumData.Clear();
GetMediumData();
foreach ( var keyVal in MediumData )
{
if ( !string.IsNullOrEmpty( keyVal.Value ) )
{
communication.MediumData.Add( keyVal.Key, keyVal.Value );
}
}
if ( communication.MediumData.ContainsKey( "Subject" ) )
{
communication.Subject = communication.MediumData["Subject"];
communication.MediumData.Remove( "Subject" );
}
DateTime? futureSendDate = dtpFutureSend.SelectedDateTime;
if ( futureSendDate.HasValue && futureSendDate.Value.CompareTo( RockDateTime.Now ) > 0 )
{
communication.FutureSendDateTime = futureSendDate;
}
else
{
communication.FutureSendDateTime = null;
}
return communication;
}