Exemplo n.º 1
0
        internal static void Validate(SearchInfoModel searchModel)
        {
            if (searchModel.MinAge > searchModel.MaxAge)
                throw new Exception("Min age cannot be bigger than max age");

            if (searchModel.MinHeight > searchModel.MaxHeight)
                throw new Exception("Min height cannot be bigger than max height");

            if (searchModel.MinWeight > searchModel.MaxWeight)
                throw new Exception("Min weight cannot be bigger than max weight");

            if (!searchModel.CanBeFemale && !searchModel.CanBeMale)
                throw new Exception("Please select at least one gender");

            if (TextUtils.IsStringEmpty(searchModel.Name) && TextUtils.IsStringEmpty(searchModel.Nationality) &&
                searchModel.MinAge == null && searchModel.MaxAge == null &&
                searchModel.MinHeight == null && searchModel.MaxHeight == null &&
                searchModel.MinWeight == null && searchModel.MaxWeight == null)
                throw new Exception("Please write any one parameter to search");

            if (!string.IsNullOrEmpty(searchModel.Name) && searchModel.Name.Length > MAX_NAME_LENGTH)//for service
                throw new Exception(string.Format("Name can not be bigger than {0} characters", MAX_NAME_LENGTH));

            if (!string.IsNullOrEmpty(searchModel.Nationality) && searchModel.Nationality.Length > MAX_NATIONALITY_LENGTH)//for service
                throw new Exception(string.Format("Nationality can not be bigger than {0} characters", MAX_NATIONALITY_LENGTH));

            if (searchModel.MinAge != null && searchModel.MinAge < MIN_AGE)
                throw new Exception(string.Format("Min age must be bigger than {0}", MIN_AGE));
        }
Exemplo n.º 2
0
        static void ProcessSearch(string email, SearchInfoModel searchModel)
        {
            CriminalsDataContext dc = new CriminalsDataContext();
            byte maleByte = (byte)CriminalInfo.SexType.Male;
            byte femaleByte = (byte)CriminalInfo.SexType.Female;
            var query =
                from a in dc.Criminals
                where (string.IsNullOrEmpty(searchModel.Name) || a.Name.Contains(searchModel.Name))
                where (string.IsNullOrEmpty(searchModel.Nationality) || a.Nationality.Equals(searchModel.Nationality, StringComparison.OrdinalIgnoreCase))
                where (searchModel.MinAge == null || a.Age >= searchModel.MinAge)
                where (searchModel.MaxAge == null || a.Age <= searchModel.MaxAge)
                where (searchModel.MinHeight == null || a.Height >= searchModel.MinHeight)
                where (searchModel.MaxHeight == null || a.Height <= searchModel.MaxHeight)
                where (searchModel.MinWeight == null || a.Weight >= searchModel.MinWeight)
                where (searchModel.MaxWeight == null || a.Weight <= searchModel.MaxWeight)
                where ((searchModel.CanBeMale && searchModel.CanBeFemale) || (searchModel.CanBeMale && a.Sex == maleByte)|| (searchModel.CanBeFemale && a.Sex == femaleByte))
                select a;

            var criminalsList = query.ToList();
            var pdfNamesList = new List<string>(criminalsList.Count);
            foreach (var criminal in criminalsList)
            {
                CriminalInfo ci = new CriminalInfo();
                ci.ID = criminal.Id;
                ci.Name = criminal.Name;
                ci.Age = criminal.Age;
                ci.Sex = (CriminalInfo.SexType)criminal.Sex;
                ci.Height = criminal.Height;
                ci.Weight = criminal.Weight;
                ci.Nationality = criminal.Nationality;
                ci.GeneratePDF();
                pdfNamesList.Add(ci.GetPdfName());
            }

            EmailUtils.SendPDFEmail(email, searchModel, pdfNamesList);
        }
Exemplo n.º 3
0
        public static void SendPDFEmail(string email, SearchInfoModel searchModel, List<string> pdfFileNames)
        {
            Queue<string> filesToAttach = new Queue<string>(pdfFileNames);
            while (filesToAttach.Count > 0)
            {
                var mailMessage = new MailMessage();
                mailMessage.To.Add(email);
                mailMessage.Subject = "Search Results";
                StringBuilder bodyBuilder = new StringBuilder();// bodyBuilder, oh yea..
                bodyBuilder.Append("Please check the attached files for the list<br/><br/><u>Search Info Used</u><br/>");

                if (!TextUtils.IsStringEmpty(searchModel.Name))
                    bodyBuilder.Append(string.Format(@"Name : Containing ""{0}""<br/>", searchModel.Name));

                if (searchModel.MinAge!= null && searchModel.MaxAge!= null)
                    bodyBuilder.Append(string.Format("Age : From {0} to {1} years <br/>", searchModel.MinAge, searchModel.MaxAge));
                else if (searchModel.MinAge != null)
                    bodyBuilder.Append(string.Format("Age : Min {0} years <br/>", searchModel.MinAge));
                else if (searchModel.MaxAge != null)
                    bodyBuilder.Append(string.Format("Age : Max {0} years <br/>", searchModel.MaxAge));

                if (searchModel.CanBeMale && searchModel.CanBeFemale)
                    bodyBuilder.Append("Sex : Both Males and Females<br/>");
                else if (searchModel.CanBeMale)
                    bodyBuilder.Append("Sex : Males only<br/>");
                else if (searchModel.CanBeFemale)
                    bodyBuilder.Append("Sex : Females only<br/>");

                if (searchModel.MinHeight != null && searchModel.MaxHeight != null)
                    bodyBuilder.Append(string.Format("Height : From {0} to {1} cms <br/>", searchModel.MinHeight, searchModel.MaxHeight));
                else if (searchModel.MinHeight != null)
                    bodyBuilder.Append(string.Format("Height : Min {0} cms <br/>", searchModel.MinHeight));
                else if (searchModel.MaxHeight != null)
                    bodyBuilder.Append(string.Format("Height : Max {0} cms <br/>", searchModel.MaxHeight));

                if (searchModel.MinWeight != null && searchModel.MaxWeight != null)
                    bodyBuilder.Append(string.Format("Weight : From {0} to {1} kgs <br/>", searchModel.MinWeight, searchModel.MaxWeight));
                else if (searchModel.MinWeight != null)
                    bodyBuilder.Append(string.Format("Weight : Min {0} kgs <br/>", searchModel.MinWeight));
                else if (searchModel.MaxWeight != null)
                    bodyBuilder.Append(string.Format("Weight : Max {0} kgs <br/>", searchModel.MaxWeight));

                if (!TextUtils.IsStringEmpty(searchModel.Nationality))
                    bodyBuilder.Append(string.Format(@"Nationality : Equals to ""{0}""<br/>", searchModel.Nationality));

                mailMessage.Body = bodyBuilder.ToString();
                mailMessage.IsBodyHtml = true;

                int filesAttached = 0;
                for (; filesAttached < MAX_FILES_CAN_ATTACH; filesAttached++)
                {
                    if (filesToAttach.Count == 0) break;
                    string pdfPath = CriminalUtils.MapFileName(filesToAttach.Dequeue());
                    mailMessage.Attachments.Add(new Attachment(pdfPath));
                }
                try
                {
                    var smtpClient = new SmtpClient();
                    smtpClient.Send(mailMessage);
                }
                catch (Exception)
                {
                    //log this exception, no way to show user now, it is too late as this is running on a background thread.
                }
            }
        }
 public ActionResult Search(SearchInfoModel searchModel)
 {
     if (ModelState.IsValid)
     {
         try
         {
             string email = HttpContext.User.Identity.Name;
             SearchUtils.InitiateSearch(email, searchModel);
             ViewBag.SuccessMessage = "Search request submitted, please check your email account for the results. Thank you.";
             ModelState.Clear();
             return View();
         }
         catch (Exception e)
         {
             ModelState.AddModelError("", e.Message);
         }
     }
     return View(searchModel);
 }
Exemplo n.º 5
0
        internal static void InitiateSearch(string email, SearchInfoModel searchModel)
        {
            Validate(searchModel);

            ThreadPool.QueueUserWorkItem(new WaitCallback((object state) => ProcessSearch(email, searchModel)));
        }