public void ImportAd(Ad ad)
        {
            long adUsid = GetAdIdentity(ad);

            DataRow row = _adDataTable.NewRow();

            row[adUsidFieldName]                   = CheckNull(adUsid);
            row[ads_Name_FieldName]                = CheckNull(ad.Name);
            row[ads_OriginalID_FieldName]          = CheckNull(ad.OriginalID);
            row[ads_DestinationUrl_FieldName]      = CheckNull(ad.DestinationUrl);
            row[ads_Campaign_Account_FieldName]    = CheckNull(ad.Campaign.Account.ID);
            row[ads_Campaign_Channel_FieldName]    = CheckNull(ad.Campaign.Channel.ID);
            row[ads_Campaign_Name_FieldName]       = CheckNull(ad.Campaign.Name);
            row[ads_Campaign_OriginalID_FieldName] = CheckNull(ad.Campaign.OriginalID);
            row[ads_Campaign_Status_FieldName]     = CheckNull(((int)ad.Campaign.Status).ToString());



            _adDataTable.Rows.Add(row);
            if (_adDataTable.Rows.Count == _bufferSize)
            {
                _bulkAd.WriteToServer(_adDataTable);
                _adDataTable.Clear();
            }
            //Targets
            foreach (Target target in ad.Targets)
            {
                row = _adTargetDataTable.NewRow();
                row[adUsidFieldName]              = adUsid;
                row[ads_OriginalID_FieldName]     = CheckNull(target.OriginalID);
                row[ads_DestinationUrl_FieldName] = CheckNull(target.DestinationUrl);
                int targetType = GetTargetType(target.GetType());
                row[ads_TargetType_FieldName] = CheckNull(targetType);
                foreach (FieldInfo field in target.GetType().GetFields())                //TODO: GET FILEDS ONLY ONE TIME
                {
                    if (Attribute.IsDefined(field, typeof(TargetFieldIndexAttribute)))
                    {
                        TargetFieldIndexAttribute TargetColumn = (TargetFieldIndexAttribute)Attribute.GetCustomAttribute(field, typeof(TargetFieldIndexAttribute));
                        row[string.Format(FieldX_FiledName, TargetColumn.TargetColumnIndex)] = CheckNull(field.GetValue(target));
                    }
                }
                foreach (KeyValuePair <TargetCustomField, object> customField in target.CustomFields)
                {
                    row[string.Format(Target_CustomField_Name, customField.Key.FieldIndex)] = CheckNull(customField.Value);
                }
                _adTargetDataTable.Rows.Add(row);
                if (_metricsTargetMatchDataTable.Rows.Count == _bufferSize)
                {
                    _bulkAdTarget.WriteToServer(_adTargetDataTable);
                    _adTargetDataTable.Clear();
                }
            }

            //Creatives
            foreach (Creative creative in ad.Creatives)
            {
                row = _adCreativesDataTable.NewRow();
                row[adUsidFieldName]          = CheckNull(adUsid);
                row[ads_OriginalID_FieldName] = CheckNull(creative.OriginalID);
                row[ads_Name_FieldName]       = CheckNull(creative.Name);
                int creativeType = GetCreativeType(creative.GetType());
                row[ads_CreativeType_FieldName] = CheckNull(creativeType);
                foreach (FieldInfo field in creative.GetType().GetFields())
                {
                    if (Attribute.IsDefined(field, typeof(CreativeFieldIndexAttribute)))
                    {
                        CreativeFieldIndexAttribute creativeColumn = (CreativeFieldIndexAttribute)Attribute.GetCustomAttribute(field, typeof(CreativeFieldIndexAttribute));
                        row[string.Format(FieldX_FiledName, creativeColumn.CreativeFieldIndex)] = CheckNull(field.GetValue(creative));
                    }
                }
                _adCreativesDataTable.Rows.Add(row);
                if (_adCreativesDataTable.Rows.Count == _bufferSize)
                {
                    _bulkAdCreatives.WriteToServer(_adCreativesDataTable);
                    _adCreativesDataTable.Clear();
                }
            }
            //segments


            foreach (KeyValuePair <Segment, SegmentValue> Segment in ad.Segments)
            {
                row = _segmetsDataTable.NewRow();
                row[adUsidFieldName] = adUsid;
                row[Segments_SegmentID_FieldName]       = Segment.Key.ID;
                row[Segments_Value_FieldName]           = Segment.Value.Value;
                row[Segments_ValueOriginalID_FieldName] = Segment.Value.OriginalID;
                _segmetsDataTable.Rows.Add(row);
                if (_segmetsDataTable.Rows.Count == _bufferSize)
                {
                    _bulkSegments.WriteToServer(_segmetsDataTable);
                    _segmetsDataTable.Clear();
                }
            }
        }
        public void ImportMetrics(AdMetricsUnit metrics)
        {
            long adUsid = -1;

            metrics.Guid = Guid.NewGuid();
            DataRow row;

            foreach (KeyValuePair <Measure, double> measure in metrics.Measures)
            {
                row = _metricsDataTable.NewRow();

                row[MetricsUnit_Guid_FieldName] = metrics.Guid.ToString("N");
                if (metrics.Ad != null)
                {
                    adUsid = GetAdIdentity(metrics.Ad);
                }
                row[adUsidFieldName] = CheckNull(adUsid);
                row[Metrics_TargetPeriodStart_FieldName] = metrics.PeriodStart;
                row[Metrics_TargetPeriodEnd_FieldName]   = metrics.PeriodEnd;
                row[Metrics_Currency_FieldName]          = CheckNull(metrics.Currency.Code);


                //Measures
                row[Metrics_MeasureID_FieldName]    = measure.Key.ID;
                row[Metrics_MeasureValue_FieldName] = measure.Value;



                _metricsDataTable.Rows.Add(row);
                if (_metricsDataTable.Rows.Count == _bufferSize)
                {
                    _bulkMetrics.WriteToServer(_metricsDataTable);
                    _metricsDataTable.Rows.Clear();
                }
            }

            //tagetmatches
            foreach (Target target in metrics.TargetMatches)
            {
                row = _metricsTargetMatchDataTable.NewRow();
                row[adUsidFieldName]              = CheckNull(adUsid);
                row[ads_OriginalID_FieldName]     = CheckNull(target.OriginalID);
                row[ads_DestinationUrl_FieldName] = CheckNull(target.DestinationUrl);
                int targetType = GetTargetType(target.GetType());
                row[ads_TargetType_FieldName] = CheckNull(targetType);
                foreach (FieldInfo field in target.GetType().GetFields())
                {
                    if (Attribute.IsDefined(field, typeof(TargetFieldIndexAttribute)))
                    {
                        TargetFieldIndexAttribute TargetColumn = (TargetFieldIndexAttribute)Attribute.GetCustomAttribute(field, typeof(TargetFieldIndexAttribute));
                        row[string.Format(FieldX_FiledName, TargetColumn.TargetColumnIndex)] = CheckNull(field.GetValue(target));
                    }
                }
                foreach (KeyValuePair <TargetCustomField, object> customField in target.CustomFields)
                {
                    row[string.Format(Target_CustomField_Name, customField.Key.FieldIndex)] = CheckNull(customField.Value);
                }
                _metricsTargetMatchDataTable.Rows.Add(row);
                if (_metricsTargetMatchDataTable.Rows.Count == _bufferSize)
                {
                    _bulkMetricsTargetMatch.WriteToServer(_metricsTargetMatchDataTable);
                    _metricsTargetMatchDataTable.Clear();
                }
            }
        }