Exemple #1
0
 /// <summary>
 /// Map a Dog View Model object to a dataDog Model object
 /// </summary>
 /// <param name="item">presentation view model object</param>
 /// <returns>dataDog model object</returns>
 public static Entity_Person ToModel(this ViewModel.Entity.Person item)
 {
     return(Mapper.Map <Entity_Person>(item));
 }
        /// <summary>
        /// Add new Applicant to the database
        /// </summary>
        /// <param name="obj">Applicant object to be saved to the database</param>
        /// <returns>success or failure</returns>
        public override ServiceResultEnum Create(ref Applicant obj)
        {
            // Number of changes as a result of the database change
            NumberChanges = 0;
            try
            {
                // Perform data access using the context
                using (var context = new HuskyRescueEntities())
                {
                    #region Save Application to Database
                    try
                    {
                        // Create ViewModel of adopter
                        var appPersonVm = new ViewModel.Entity.Person
                        {
                            Base =
                            {
                                EmailAddresses = new List <ViewModel.Entity.EmailAddress>(),
                                PhoneNumbers   = new List <ViewModel.Entity.PhoneNumber>()
                            }
                        };
                        if (!string.IsNullOrEmpty(obj.AppEmail))
                        {
                            appPersonVm.Base.EmailAddresses.Add(new ViewModel.Entity.EmailAddress
                            {
                                Address = obj.AppEmail,
                                Type    = "0"
                            });
                        }
                        if (!string.IsNullOrEmpty(obj.AppCellPhone))
                        {
                            appPersonVm.Base.PhoneNumbers.Add(
                                new ViewModel.Entity.PhoneNumber
                            {
                                Number = obj.AppCellPhone,
                                Type   = "3"
                            }
                                );
                        }
                        if (!string.IsNullOrEmpty(obj.AppHomePhone))
                        {
                            appPersonVm.Base.PhoneNumbers.Add(
                                new ViewModel.Entity.PhoneNumber
                            {
                                Number = obj.AppHomePhone,
                                Type   = "1"
                            }
                                );
                        }

                        appPersonVm.FirstName = obj.AppNameFirst;
                        appPersonVm.LastName  = obj.AppNameLast;
                        if (obj.DateSubmitted == null)
                        {
                            obj.DateSubmitted = DateTime.Now;
                        }
                        appPersonVm.Base.Comments = "applied for: " + obj.FilterAppDogsInterestedIn + " on " + obj.DateSubmitted.Value.ToShortDateString() + ". ";

                        // Save primary adopter to database
                        var personHandler = new PersonHandler();
                        personHandler.Create(ref appPersonVm);

                        // Associate the application with the adopter in the database
                        obj.PersonID = appPersonVm.ID;

                        // Remove "owned animals" that are blank (no name)
                        obj.ApplicantOwnedAnimal.RemoveAll(pet => pet.Name.IsNullOrEmpty());

                        // Associate the adopter's "owned animals" in the database to the adopter
                        foreach (var animalVm in obj.ApplicantOwnedAnimal)
                        {
                            animalVm.PersonID = obj.PersonID;
                            if (string.IsNullOrEmpty(animalVm.Breed))
                            {
                                animalVm.Breed = "n/a";
                            }
                        }

                        // convert to database object
                        var dbObj = obj.ToModel();

                        // StateId is only 2 char in database and will error if there is whitespace after that.
                        dbObj.AppAddressStateId = dbObj.AppAddressStateId.Trim();

                        // add to the database and retrieve the updated object back (namely the GUID generated into the Id)
                        dbObj = context.Applicants.Add(dbObj);

                        // commit changes to the database
                        NumberChanges = context.SaveChanges();

                        // convert the database object back to a presentation object with included changes from the database (if any)
                        obj = dbObj.ToViewModel();
                    }
                    catch (DbEntityValidationException dex)
                    {
                        _logger.Error("ApplicantError", dex);
                        var validationErrors = "";
                        foreach (var failure in dex.EntityValidationErrors)
                        {
                            validationErrors  = failure.ValidationErrors.Aggregate(validationErrors, (current, error) => current + (error.PropertyName + "  " + error.ErrorMessage));
                            validationErrors += Environment.NewLine + Environment.NewLine;
                        }
                        if (validationErrors != "")
                        {
                            _logger.Error("ApplicantError - Data Validation Errors " + Environment.NewLine + validationErrors, dex);
                        }
                        if (dex.InnerException != null)
                        {
                            _logger.Error("ApplicantError - Data Validation Inner", dex.InnerException);
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.Error("ApplicantError", ex);
                        if (ex.InnerException != null)
                        {
                            _logger.Error("ApplicantError Inner", ex.InnerException);
                        }
                    }
                    finally
                    {
                        _logger.Information("Application App Submitted" + Environment.NewLine + obj);
                    }
                    #endregion

                    #region generate application pdf
                    var newPdfPath = string.Empty;
                    try
                    {
                        // Generate the pdf of the application and email it
                        var genPdf = new GenerateAppPdf(_logger);
                        newPdfPath = genPdf.CreatePdf(obj, obj.ApplicantType);
                    }
                    catch (Exception ex)
                    {
                        _logger.Error(obj.ApplicantType + " App PDF Gen Ex", ex);
                        if (ex.InnerException != null)
                        {
                            _logger.Error(obj.ApplicantType + " App PDF Gen Inner Ex", ex.InnerException);
                        }
                    }
                    #endregion

                    #region Email Application

                    var appTypeDesc = (obj.ApplicantType.Equals("A")) ? "Adoption" : "Foster";

                    var          subject     = "Online " + appTypeDesc + " Application for " + obj.AppNameFirst + " " + obj.AppNameLast;
                    const string bodyAppHtml = @" Thank you for your application.
								See attachment for a copy of your application sent to Texas Husky Rescue, Inc.
								You can respond back to this email if you have any further questions or comments for us.
								We will get back to you within the next 5 days regarding your application."                                ;
                    var          bodyGroup   = @" Dogs interested in: " + obj.FilterAppDogsInterestedIn;

                    var message = new EmailMessage
                    {
                        BodyTextExternal     = bodyAppHtml,
                        BodyTextInternal     = bodyGroup,
                        Subject              = subject,
                        EmailAddressExternal = (string.IsNullOrEmpty(obj.AppEmail)) ? string.Empty : obj.AppEmail,
                        EmailAddressInternal = Settings.Default.ContactEmail,
                        NameInternal         = "Texas Husky Rescue",
                        NameExternal         = string.Empty
                    };

                    var newPdfFile = new FileInfo(newPdfPath);
                    if (newPdfFile.Exists)
                    {
                        var attachment  = new Attachment(newPdfFile.FullName, MediaTypeNames.Application.Octet);
                        var disposition = attachment.ContentDisposition;
                        disposition.CreationDate     = newPdfFile.CreationTime;
                        disposition.ModificationDate = newPdfFile.CreationTime;
                        disposition.ReadDate         = newPdfFile.CreationTime;
                        disposition.FileName         = newPdfFile.Name;
                        disposition.Size             = newPdfFile.Length;
                        disposition.DispositionType  = DispositionTypeNames.Attachment;
                        message.Attachments.Add(attachment);
                    }
                    message.SendMessage();
                    #endregion
                }
            }
            catch (InvalidOperationException ex)
            {
                Trace.WriteLine(ex.Message);
            }
            catch (ValidationException ex)
            {
                Trace.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }

            return(NumberChanges > 0 ? ServiceResultEnum.Success : ServiceResultEnum.Failure);
        }