Ejemplo n.º 1
0
    /// <summary>
    /// Get Data From LineString
    /// </summary>
    /// <param name="lineContent"></param>
    /// <param name="lineNumber"></param>
    private void GetDataFromLineString(OrginalDataField orginalDataField, List<DataItem> listPType, List<DataItem> listPSize, List<DataItem> listCountry)
    {
        try
        {
            //parse line
            //string[] valueOffields = ImportExportUltility.GetCsvRecord(lineContent);//input line and return value of fields of that line

            //set value field
            string errorDescriptionRow = string.Empty;
            string errorDescriptionField = string.Empty;
            bool fieldOK = true;
            bool rowOK = true;
            string itemID = string.Empty;

            //create a new datarow
            ImportExportFields dataRow = new ImportExportFields();
            dataRow.LineNumber = orginalDataField.LineNumber;//0
            dataRow.Premium = memberLevel;
            dataRow.isApproved = 1;

            //ID
            if (!string.IsNullOrEmpty(orginalDataField.ID))
            {
                fieldOK = CheckValidateField(orginalDataField.ID, "ID", "Guid", false, 0, -1, null, null, out itemID, out errorDescriptionField);
                if (fieldOK)
                    dataRow.UserId = new Guid(orginalDataField.ID);//1
                else
                {
                    rowOK = false;
                    errorDescriptionRow += errorDescriptionField;
                }
            }

            //FirstName
            fieldOK = CheckValidateField(orginalDataField.FirstName, "First Name", null, true, 0, 100, @"[^{}]*", null, out itemID, out errorDescriptionField);
            if (fieldOK)
                dataRow.FirstName = orginalDataField.FirstName;//csv field 1
            else
            {
                rowOK = false;
                errorDescriptionRow += errorDescriptionField;
            }

            //LastName
            fieldOK = CheckValidateField(orginalDataField.LastName, "Last Name", null, false, 0, 60, null, null, out itemID, out errorDescriptionField);
            if (fieldOK)
                dataRow.LastName = orginalDataField.LastName;//csv field 2
            else
            {
                rowOK = false;
                errorDescriptionRow += errorDescriptionField;
            }

            //csv field OrganizationName
            fieldOK = CheckValidateField(orginalDataField.OrganizationName, "Organization Name", null, true, 0, 100, null, null, out itemID, out errorDescriptionField);
            if (fieldOK)
                dataRow.OrganizationName = orginalDataField.OrganizationName;//csv field 3
            else
            {
                rowOK = false;
                errorDescriptionRow += errorDescriptionField;
            }

            //Website
            fieldOK = CheckValidateField(orginalDataField.Website, "Website", null, false, 0, 255, @"(http(s)?://)?([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?", null, out itemID, out errorDescriptionField);
            if (fieldOK)
                dataRow.Website = orginalDataField.Website;//csv field 4
            else
            {
                rowOK = false;
                errorDescriptionRow += errorDescriptionField;
            }

            //PhoneNumber
            fieldOK = CheckValidateField(orginalDataField.PhoneNumber, "Phone Number", null, false, 0, 24, @"([()\d+-.\s])*", null, out itemID, out errorDescriptionField);
            if (fieldOK)
                dataRow.PhoneNumber = orginalDataField.PhoneNumber;//csv field 5
            else
            {
                rowOK = false;
                errorDescriptionRow += errorDescriptionField;
            }

            //Email
            fieldOK = CheckValidateField(orginalDataField.Email, "Email", null, false, 0, 256, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", null, out itemID, out errorDescriptionField);
            if (fieldOK)
                dataRow.Email = orginalDataField.Email;//csv field 6
            else
            {
                rowOK = false;
                errorDescriptionRow += errorDescriptionField;
            }

            //PollinatorType
            fieldOK = CheckValidateField(orginalDataField.PollinatorType, "Type of Pollinator", null, false, 0, 30, null, listPType, out itemID, out errorDescriptionField);
            if (fieldOK)
            {
                dataRow.PollinatorTypeName = orginalDataField.PollinatorType;//csv field 7
                if (!string.IsNullOrEmpty(itemID))
                    dataRow.PollinatorType = Int32.Parse(itemID);
                else
                    dataRow.PollinatorType = 0;
            }
            else
            {
                rowOK = false;
                errorDescriptionRow += errorDescriptionField;
            }

            //OrganizationDescription
            fieldOK = CheckValidateField(orginalDataField.OrganizationDescription, "Organization Description", null, false, 0, -1, null, null, out itemID, out errorDescriptionField);
            if (fieldOK)
                dataRow.OrganizationDescription = orginalDataField.OrganizationDescription;//csv field 8
            else
            {
                rowOK = false;
                errorDescriptionRow += errorDescriptionField;
            }

            //PollinatorSize
            fieldOK = CheckValidateField(orginalDataField.PollinatorSize, "Size of Pollinator Location", null, false, 0, 60, null, listPSize, out itemID, out errorDescriptionField);
            if (fieldOK)
            {
                dataRow.PollinatorSizeName = orginalDataField.PollinatorSize;//csv field 9
                if (!string.IsNullOrEmpty(itemID))
                    dataRow.PollinatorSize = Int32.Parse(itemID);
                else
                    dataRow.PollinatorSize = 9;//Other
            }
            else
            {
                rowOK = false;
                errorDescriptionRow += errorDescriptionField;
            }

            //check Address(Street, City, State)
            if (string.IsNullOrEmpty(orginalDataField.Address) && string.IsNullOrEmpty(orginalDataField.City) && string.IsNullOrEmpty(orginalDataField.State))
            {
                rowOK = false;
                errorDescriptionField += string.Format("<br> - {0}", "Address (or City/State) is required");
                errorDescriptionRow += errorDescriptionField;
            }
            else
            {
                //Address
                fieldOK = CheckValidateField(orginalDataField.Address, "Address", null, false, 0, 100, null, null, out itemID, out errorDescriptionField);
                if (fieldOK)
                    dataRow.LandscapeStreet = orginalDataField.Address;//csv field 10
                else
                {
                    rowOK = false;
                    errorDescriptionRow += errorDescriptionField;
                }

                //City
                fieldOK = CheckValidateField(orginalDataField.City, "City", null, false, 0, 50, null, null, out itemID, out errorDescriptionField);
                if (fieldOK)
                    dataRow.LandscapeCity = orginalDataField.City;//csv field 11
                else
                {
                    rowOK = false;
                    errorDescriptionRow += errorDescriptionField;
                }

                //State
                fieldOK = CheckValidateField(orginalDataField.State, "State", null, false, 0, 30, null, null, out itemID, out errorDescriptionField);
                if (fieldOK)
                    dataRow.LandscapeState = orginalDataField.State;//csv field 12
                else
                {
                    rowOK = false;
                    errorDescriptionRow += errorDescriptionField;
                }
            }

            //ZIP
            fieldOK = CheckValidateField(orginalDataField.ZIP, "ZIP", null, false, 0, 15, @"[A-Za-z0-9\s-]*", null, out itemID, out errorDescriptionField);
            if (fieldOK)
                dataRow.LandscapeZipcode = orginalDataField.ZIP;//csv field 13
            else
            {
                rowOK = false;
                errorDescriptionRow += errorDescriptionField;
            }

            //Country
            fieldOK = CheckValidateField(orginalDataField.Country, "Country", null, false, 0, 100, null, listCountry, out itemID, out errorDescriptionField);
            if (fieldOK)
            {
                dataRow.LandscapeCountryName = orginalDataField.Country;//csv field 14
                dataRow.LandscapeCountry = itemID;
            }
            else
            {
                rowOK = false;
                errorDescriptionRow += errorDescriptionField;
            }

            //Geolocation(lat;lng)
            if (!string.IsNullOrEmpty(orginalDataField.Geolocation) && orginalDataField.Geolocation.Contains(';'))
            {
                dataRow.Geolocation = orginalDataField.Geolocation;
                string[] geolocation = orginalDataField.Geolocation.Split(new string[] { ";" }, StringSplitOptions.None);
                if (geolocation.Length == 2)
                {
                    string latitude = geolocation[0].Trim();
                    string longitude = geolocation[1].Trim();

                    //latitude
                    fieldOK = CheckValidateField(latitude, "Latitude of Geolocation", "decimal", true, 0, 18, null, null, out itemID, out errorDescriptionField);
                    if (fieldOK)
                        dataRow.Latitude = decimal.Parse(latitude);
                    else
                    {
                        rowOK = false;
                        errorDescriptionRow += errorDescriptionField;
                    }

                    //Longitude
                    fieldOK = CheckValidateField(latitude, "Longitude of Geolocation", "decimal", true, 0, 18, null, null, out itemID, out errorDescriptionField);
                    if (fieldOK)
                        dataRow.Longitude = decimal.Parse(longitude);
                    else
                    {
                        rowOK = false;
                        errorDescriptionRow += errorDescriptionField;
                    }
                }
            }

            //row is OK-> add to list OK
            if (rowOK)
                listRowOK.Add(dataRow);//add to list row OK
            else//row is failed -> add to list
            {
                OrginalDataField dataRowFailed = new OrginalDataField();
                dataRowFailed.LineNumber = orginalDataField.LineNumber;//0
                dataRowFailed.ErrorDescription = errorDescriptionRow.Substring(4);//error

                dataRowFailed.ID = orginalDataField.ID;
                dataRowFailed.FirstName = orginalDataField.FirstName;
                dataRowFailed.LastName = orginalDataField.LastName;
                dataRowFailed.OrganizationName = orginalDataField.OrganizationName;
                dataRowFailed.Website = orginalDataField.Website;
                dataRowFailed.PhoneNumber = orginalDataField.PhoneNumber;
                dataRowFailed.Email = orginalDataField.Email;
                dataRowFailed.PollinatorType = orginalDataField.PollinatorType;
                dataRowFailed.OrganizationDescription = orginalDataField.OrganizationDescription;
                dataRowFailed.PollinatorSize = orginalDataField.PollinatorSize;
                dataRowFailed.Address = orginalDataField.Address;
                dataRowFailed.City = orginalDataField.City;
                dataRowFailed.State = orginalDataField.State;
                dataRowFailed.ZIP = orginalDataField.ZIP;
                dataRowFailed.Country = orginalDataField.Country;
                dataRowFailed.Geolocation = orginalDataField.Geolocation;

                //add to list failed rows
                listRowFailed.Add(dataRowFailed);
            }

        }
        catch (Exception ex)
        {
            lblErrorMess.Text = ex.Message;
            Pollinator.Common.Logger.Error("Error occured at " + typeof(Admin_ImportData).Name + " GetDataFromLineString(). ", ex);
        }
    }
Ejemplo n.º 2
0
    private static void SetLalngByAddress(ImportExportFields writedata, PolinatorInformation polinatorInformation)
    {
        string physicalLocation;
        GoogleGeoCoder geoCoder = new GoogleGeoCoder();
        bool getLocation = false;
        Address[] matchAddress = null;
        int numTry = 0;
        while (!getLocation && numTry < 5)
        {
            try
            {
                numTry++;
                physicalLocation = writedata.LandscapeStreet + " " +
                    writedata.LandscapeZipcode + " " +
                    writedata.LandscapeCity + " " +
                    writedata.LandscapeState + " " +
                    writedata.LandscapeCountry;
                matchAddress = geoCoder.GeoCode(physicalLocation).ToArray();
                getLocation = true;
                break;
            }
            catch (Exception ex)
            {
                System.Threading.Thread.Sleep(2000);
            }

        }
        if (matchAddress != null && matchAddress.Length > 0)
        {
            polinatorInformation.Latitude = (decimal)matchAddress[0].Coordinates.Latitude;
            polinatorInformation.Longitude = (decimal)matchAddress[0].Coordinates.Longitude;
            if (matchAddress.Length > 1)
                polinatorInformation.FuzyLocation = true;

        }
    }