예제 #1
0
        //Add Data From Linked Tables for Display
        public void EditItemForDisplay(PolicyCountryGroupItem policyCountryGroupItem)
        {
            //PolicyGroupName
            PolicyGroupRepository policyGroupRepository = new PolicyGroupRepository();
            PolicyGroup           policyGroup           = policyGroupRepository.GetGroup(policyCountryGroupItem.PolicyGroupId);

            policyCountryGroupItem.PolicyGroupName = policyGroup.PolicyGroupName;

            //PolicyCountryStatus
            if (policyCountryGroupItem.PolicyCountryStatusId != null)
            {
                int policyCountryStatusId = (int)policyCountryGroupItem.PolicyCountryStatusId;
                PolicyCountryStatusRepository policyCountryStatusRepository = new PolicyCountryStatusRepository();
                PolicyCountryStatus           policyCountryStatus           = new PolicyCountryStatus();
                policyCountryStatus = policyCountryStatusRepository.GetPolicyCountryStatus(policyCountryStatusId);
                if (policyCountryStatus != null)
                {
                    policyCountryGroupItem.PolicyCountryStatusDescription = policyCountryStatus.PolicyCountryStatusDescription;
                }
            }

            //CountryName
            CountryRepository countryRepository = new CountryRepository();
            Country           country           = new Country();

            country = countryRepository.GetCountry(policyCountryGroupItem.CountryCode);
            if (country != null)
            {
                policyCountryGroupItem.CountryName = country.CountryName;
            }
        }
예제 #2
0
        public PolicyCountryImportStep2VM PreImportCheck(HttpPostedFileBase file, int policyGroupId)
        {
            //convert file to string so that we can parse
            int length = file.ContentLength;

            byte[] tempFile = new byte[length];
            file.InputStream.Read(tempFile, 0, length);
            byte[] array = tempFile.ToArray();
            System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
            string fileToText            = fileToText = enc.GetString(array);

            // Create the xml document container, this will be used to store the data after the checks
            XmlDocument    doc = new XmlDocument();
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", null, null);

            doc.AppendChild(dec);
            XmlElement root = doc.CreateElement("PAVIGs");

            doc.AppendChild(root);

            List <string> returnMessages = new List <string>();
            string        returnMessage;
            int           i = 0;

            //Split the CSV into lines
            string[] lines = fileToText.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

            //loop through CSV lines
            foreach (string line in lines)
            {
                i++;

                if (i > 1) //ignore first line with titles
                {
                    string[] cells = line.Split(';');

                    //extract the data items from the file

                    string CountryCode = cells[0];                    //Required: (eg.Preferred)
                    string PolicyCountryStatusDescription = cells[1]; //Required: (eg.DL)
                    string enabledFlag         = cells[2];            //(True / False)(1, 0)
                    string enabledDate         = cells[3];            //(YYYY / MM / DD)
                    string expiryDate          = cells[4];            //(YYYY / MM / DD)
                    string travelDateValidFrom = cells[5];            //(YYYY / MM / DD)
                    string travelDateValidTo   = cells[6];            //(YYYY / MM / DD)

                    //Build the XML Element for items

                    XmlElement xmlPAVItem = doc.CreateElement("PAVIG");

                    XmlElement xmlCountryCode = doc.CreateElement("CountryCode");
                    xmlCountryCode.InnerText = CountryCode;
                    xmlPAVItem.AppendChild(xmlCountryCode);

                    XmlElement xmlPolicyCountryStatusDescription = doc.CreateElement("PolicyCountryStatusDescription");
                    xmlPolicyCountryStatusDescription.InnerText = PolicyCountryStatusDescription;
                    xmlPAVItem.AppendChild(xmlPolicyCountryStatusDescription);

                    XmlElement xmlEnabledDate = doc.CreateElement("EnabledDate");
                    xmlEnabledDate.InnerText = enabledDate;
                    xmlPAVItem.AppendChild(xmlEnabledDate);

                    XmlElement xmlExpiryDate = doc.CreateElement("ExpiryDate");
                    xmlExpiryDate.InnerText = expiryDate;
                    xmlPAVItem.AppendChild(xmlExpiryDate);

                    XmlElement xmlTravelDateValidFrom = doc.CreateElement("TravelDateValidFrom");
                    xmlTravelDateValidFrom.InnerText = travelDateValidFrom;
                    xmlPAVItem.AppendChild(xmlTravelDateValidFrom);

                    XmlElement xmlTravelDateValidTo = doc.CreateElement("TravelDateValidTo");
                    xmlTravelDateValidTo.InnerText = travelDateValidTo;
                    xmlPAVItem.AppendChild(xmlTravelDateValidTo);


                    //Validate data

                    //Validate data country status
                    if (string.IsNullOrEmpty(PolicyCountryStatusDescription) == true)
                    {
                        returnMessage = "Row " + i + ": Policy Country Status Description is missing. Please provide a valid Country Status";
                        if (!returnMessages.Contains(returnMessage))
                        {
                            returnMessages.Add(returnMessage);
                        }
                    }

                    PolicyCountryStatusRepository policyCountryStatusRepository = new PolicyCountryStatusRepository();
                    PolicyCountryStatus           policyCountry = policyCountryStatusRepository.GetPolicyCountryStatusByDescription(PolicyCountryStatusDescription);
                    if (policyCountry == null)
                    {
                        returnMessage = "Row " + i + ": Country Status Description is invalid. Please provide a valid Country Status";
                        if (!returnMessages.Contains(returnMessage))
                        {
                            returnMessages.Add(returnMessage);
                        }
                    }
                    else
                    {
                        PolicyCountryStatusDescription = policyCountry.PolicyCountryStatusId.ToString();
                    }

                    //Validate data country code
                    if (string.IsNullOrEmpty(CountryCode) == true)
                    {
                        returnMessage = "Row " + i + ": Country Code is missing. Please provide a valid Country Code";
                        if (!returnMessages.Contains(returnMessage))
                        {
                            returnMessages.Add(returnMessage);
                        }
                    }
                    else
                    {
                        CountryRepository countryRepository = new CountryRepository();
                        Country           country           = countryRepository.GetCountry(CountryCode);
                        if (country == null)
                        {
                            returnMessage = "Row " + i + ": Country Code is invalid. Please provide a valid Country Code";
                            if (!returnMessages.Contains(returnMessage))
                            {
                                returnMessages.Add(returnMessage);
                            }
                        }
                    }

                    enabledFlag = enabledFlag.ToUpper();
                    if (enabledFlag != "TRUE" && enabledFlag != "FALSE" && enabledFlag != "1" && enabledFlag != "0" && enabledFlag != "")
                    {
                        returnMessage = "Row " + i + ": EnabledFlag value is not valid. Please provide a valid Enabled Flag";
                        if (!returnMessages.Contains(returnMessage))
                        {
                            returnMessages.Add(returnMessage);
                        }
                    }
                    if (enabledFlag == "TRUE" || string.IsNullOrEmpty(enabledFlag))
                    {
                        enabledFlag = "1";
                    }
                    if (enabledFlag == "FALSE")
                    {
                        enabledFlag = "0";
                    }

                    XmlElement xmlEnabledFlag = doc.CreateElement("EnabledFlag");
                    xmlEnabledFlag.InnerText = enabledFlag;
                    xmlPAVItem.AppendChild(xmlEnabledFlag);

                    if (enabledDate != "" && enabledDate != "NULL")
                    {
                        DateTime enabledDateDT;
                        if (DateTime.TryParseExact(enabledDate, "yyyy/MM/dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out enabledDateDT) == false)
                        {
                            returnMessage = "Row " + i + ": EnabledDate must be in the format YYYY/MM/DD. Please provide a valid date format";
                            if (!returnMessages.Contains(returnMessage))
                            {
                                returnMessages.Add(returnMessage);
                            }
                        }
                    }

                    if (expiryDate != "" && expiryDate != "NULL")
                    {
                        DateTime enabledDateDT;
                        if (DateTime.TryParseExact(expiryDate, "yyyy/MM/dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out enabledDateDT) == false)
                        {
                            returnMessage = "Row " + i + ": ExpiryDate must be in the format YYYY/MM/DD. Please provide a valid date format";
                            if (!returnMessages.Contains(returnMessage))
                            {
                                returnMessages.Add(returnMessage);
                            }
                        }
                    }

                    if (travelDateValidFrom != "" && travelDateValidFrom != "NULL")
                    {
                        DateTime enabledDateDT;
                        if (DateTime.TryParseExact(travelDateValidFrom, "yyyy/MM/dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out enabledDateDT) == false)
                        {
                            returnMessage = "Row " + i + ": TravelDateFrom must be in the format YYYY/MM/DD. Please provide a valid date format";
                            if (!returnMessages.Contains(returnMessage))
                            {
                                returnMessages.Add(returnMessage);
                            }
                        }
                    }

                    if (travelDateValidTo != "" && travelDateValidTo != "NULL")
                    {
                        DateTime enabledDateDT;
                        if (DateTime.TryParseExact(travelDateValidTo, "yyyy/MM/dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out enabledDateDT) == false)
                        {
                            returnMessage = "Row " + i + ": TravelDateTo must be in the format YYYY/MM/DD. Please provide a valid date format";
                            if (!returnMessages.Contains(returnMessage))
                            {
                                returnMessages.Add(returnMessage);
                            }
                        }
                    }

                    //Attach the XML Element for an item to the Document
                    root.AppendChild(xmlPAVItem);
                }
            }
            if (i == 0)
            {
                returnMessage = "There is no data in the file";
                returnMessages.Add(returnMessage);
            }

            PolicyCountryImportStep2VM preImportCheckResult = new PolicyCountryImportStep2VM();

            preImportCheckResult.ReturnMessages = returnMessages;

            if (returnMessages.Count != 0)
            {
                //preImportCheckResult.FileBytes = array;

                preImportCheckResult.IsValidData = false;
            }
            else
            {
                //DB Check
                string adminUserGuid = HttpContext.Current.User.Identity.Name.Split(new[] { '|' })[0];

                var output = (
                    from n in dbHierarchyDC.spDesktopDataAdmin_UpdatePolicyCountryGroupItemCount_v1(
                        policyGroupId,
                        System.Xml.Linq.XElement.Parse(doc.OuterXml),
                        adminUserGuid
                        )
                    select n).ToList();

                foreach (spDesktopDataAdmin_UpdatePolicyCountryGroupItemCount_v1Result message in output)
                {
                    returnMessages.Add(message.MessageText.ToString());
                }

                preImportCheckResult.FileBytes = array;

                preImportCheckResult.IsValidData = true;
            }

            return(preImportCheckResult);
        }