Alex Lonyay

Alex Lonyay

  • 1.9k
  • 67
  • 1.3k

How to map excel column value to custom defined object class

Jul 4 2020 11:52 AM

I have mapper method which maps different types of property like bool,enum,string,int

But now I want to map my one of the custom class which is being inherited in several places

What Required: Currently, it is not mapping values of Payment Options, and also I do not know how will I access param.paymentoptions which will have delivery and payLater information 

Back Story:

Earlier we were creating instance and assigning values in POM only, now we want to use Excel to pass value and map and then use in our methods

Object classes Which I want to map
 
  1. namespace Framework.Model.PaymentOptions  
  2. {  
  3.     public class PaymentOptions  
  4.     {  
  5.         public PaymentPortal portal;  
  6.         public DeliveryMethod delivery = DeliveryMethod.Billing;  
  7.   
  8.         public PaymentOptions()  
  9.         {  
  10.         }  
  11.         public PaymentOptions(Site site)  
  12.         {  
  13.   
  14.         }  
  15.     }  
  16. }  
Which is inherited in below class
  1. namespace Framework.Model.PaymentOptions  
  2. {  
  3.     public class KlarnaOptions : PaymentOptions  
  4.     {  
  5.         //default - don't use card payment by deffault  
  6.         public bool byCard = false;  
  7.         public bool payLater = false;  
  8.         public bool sliceIt = false;  
  9.         public KlarnaOptions()  
  10.         {  
  11.             portal = PaymentPortal.Klarna;  
  12.         }  
  13.     }  
  14. }  

Earlier we used to access and assign the values as below

Assigning Values

  1. public class WorkflowParameters  
  2.     {  
  3.   
  4.         public Site _site = default(Site);  
  5.         public PaymentOptions paymentOptions = default(PaymentOptions);  
  6.     }  
  7.   
  8. var param = new WorkflowParameters()  
  9.             {  
  10.                   
  11.                 paymentOptions = new KlarnaOptions()  
  12.                 {  
  13.                     delivery = DeliveryMethod.Billing,  
  14.                     payLater = true  
  15.                 }  
  16.             };  
Use in Methods would be as below
  1. Checkout(param.paymentoptions); // Which would pass delivery and paylater values  
  • Now we want to pass values from Excel Sheet 
  • Test Data Model 
    1. namespace Framework.Model.Excel  
    2. {  
    3.     public partial class TestDataModel  
    4.     {  
    5.           
    6.         public TestDataModel() {  
    7.   
    8.               
    9.         }  
    10.           
    11.           
    12.         [DataNames("TestName")]  
    13.         public string TestName { getset; }  
    14.   
    15.           
    16.   
    17.   
    18.         [DataNames("paymentOptions")]  
    19.         public PaymentOptions paymentOptions { getset; }  
    20.         //public PaymentOptions paymentOptions = default(PaymentOptions);  
    21.   
    22.         [DataNames("SiteGroupId")]  
    23.         public SiteGroup SiteGroupId { getset; }  
    24.   
    25.         [DataNames("NickName")]  
    26.         public string NickName { getset; }  
    27.   
    28.         [DataNames("byCard")]  
    29.         public bool byCard { getset; }  
    30.   
    31.         [DataNames("payLater")]  
    32.         public bool payLater { getset; }  
    33.   
    34.         [DataNames("sliceIt")]  
    35.         public bool sliceIt { getset; }  
    36.   
    37.         [DataNames("portal")]  
    38.         public PaymentPortal portal { getset; }  
    39.   
    40.         [DataNames("delivery")]  
    41.         public DeliveryMethod delivery{get;set;}  
    42.     }  
    43.       
    44. }  
    1. public static class PropertyMapHelper  
    2.     {  
    3.   
    4.         public static void Map(Type type, DataRow row, PropertyInfo prop, object entity)  
    5.         {  
    6.             List<string> columnNames = AttributeHelper.GetDataNames(type, prop.Name);  
    7.   
    8.             foreach (var columnName in columnNames)  
    9.             {  
    10.                 if (!String.IsNullOrWhiteSpace(columnName) && row.Table.Columns.Contains(columnName))  
    11.                 {  
    12.                     var propertyValue = row[columnName];  
    13.                     if (propertyValue != DBNull.Value)  
    14.                     {  
    15.                         ParsePrimitive(prop, entity, row[columnName]);  
    16.                         break;  
    17.                     }  
    18.                 }  
    19.             }  
    20.         }  
    21.   
    22.         private static void ParsePrimitive(PropertyInfo prop, object entity, object value)  
    23.         {  
    24.             if (prop.PropertyType == typeof(string))  
    25.             {  
    26.                 prop.SetValue(entity, value.ToString().Trim(), null);  
    27.             }  
    28.             else if (prop.PropertyType == typeof(bool) || prop.PropertyType == typeof(bool?))  
    29.             {  
    30.                 if (value == null)  
    31.                 {  
    32.                     prop.SetValue(entity, nullnull);  
    33.                 }  
    34.                 else  
    35.                 {  
    36.                     prop.SetValue(entity, ParseBoolean(value.ToString()), null);  
    37.                 }  
    38.             }  
    39.             else if (prop.PropertyType == typeof(long))  
    40.             {  
    41.                 prop.SetValue(entity, long.Parse(value.ToString()), null);  
    42.             }  
    43.             else if (prop.PropertyType == typeof(int) || prop.PropertyType == typeof(int?))  
    44.             {  
    45.                 if (value == null)  
    46.                 {  
    47.                     prop.SetValue(entity, nullnull);  
    48.                 }  
    49.                 else  
    50.                 {  
    51.                     prop.SetValue(entity, int.Parse(value.ToString()), null);  
    52.                 }  
    53.             }  
    54.             else if (prop.PropertyType == typeof(decimal))  
    55.             {  
    56.                 prop.SetValue(entity, decimal.Parse(value.ToString()), null);  
    57.             }  
    58.             else if (prop.PropertyType == typeof(double) || prop.PropertyType == typeof(double?))  
    59.             {  
    60.                 double number;  
    61.                 bool isValid = double.TryParse(value.ToString(), out number);  
    62.                 if (isValid)  
    63.                 {  
    64.                     prop.SetValue(entity, double.Parse(value.ToString()), null);  
    65.                 }  
    66.             }  
    67.             else if (prop.PropertyType == typeof(DateTime) || prop.PropertyType == typeof(Nullable))  
    68.             {  
    69.                 DateTime date;  
    70.                 bool isValid = DateTime.TryParse(value.ToString(), out date);  
    71.                 if (isValid)  
    72.                 {  
    73.                     prop.SetValue(entity, date, null);  
    74.                 }  
    75.                 else  
    76.                 {  
    77.                     isValid = DateTime.TryParseExact(value.ToString(), "MMddyyyy"new CultureInfo("en-US"), DateTimeStyles.AssumeLocal, out date);  
    78.                     if (isValid)  
    79.                     {  
    80.                         prop.SetValue(entity, date, null);  
    81.                     }  
    82.                 }  
    83.             }  
    84.             else if (prop.PropertyType.IsEnum)  
    85.             {  
    86.                 var type = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;  
    87.                 var enumValue = Enum.Parse(type, value.ToString(), true);  
    88.                 prop.SetValue(entity, enumValue, null);  
    89.             }  
    90.             else if (prop.PropertyType == typeof(Guid))  
    91.             {  
    92.                 Guid guid;  
    93.                 bool isValid = Guid.TryParse(value.ToString(), out guid);  
    94.                 if (isValid)  
    95.                 {  
    96.                     prop.SetValue(entity, guid, null);  
    97.                 }  
    98.                 else  
    99.                 {  
    100.                     isValid = Guid.TryParseExact(value.ToString(), "B"out guid);  
    101.                     if (isValid)  
    102.                     {  
    103.                         prop.SetValue(entity, guid, null);  
    104.                     }  
    105.                 }  
    106.             }  
    107.             else if(prop.PropertyType == typeof(PaymentOptions))  
    108.             {  
    109.                   
    110.             }  
    111.         }  
    112.   
    113.         public static bool ParseBoolean(object value)  
    114.         {  
    115.             if (value == null || value == DBNull.Value) return false;  
    116.   
    117.             switch (value.ToString().ToLowerInvariant())  
    118.             {  
    119.                 case "1":  
    120.                 case "y":  
    121.                 case "yes":  
    122.                 case "true":  
    123.                     return true;  
    124.   
    125.                 case "0":  
    126.                 case "n":  
    127.                 case "no":  
    128.                 case "false":  
    129.                 default:  
    130.                     return false;  
    131.             }  
    132.         }  
    133.     }  

Brought to you by: JavaScript SDK for Bold BI dashboard and analytics embedding. Free trial.