static void export(string pWhereFilter, ICdrExportInfo pCdrExportInfo, string pFilePath, BackgroundWorker pBackgroundWorker) { pBackgroundWorker.ReportStatus("CDR Export started..."); pBackgroundWorker.ReportProgress(0); DateTime _processDate = pCdrExportInfo.DateStart; if (File.Exists(pFilePath)) { File.Delete(pFilePath); } string _tempFilePath = pFilePath + ".temp"; if (File.Exists(_tempFilePath)) { File.Delete(_tempFilePath); } try { int _totalRecords = 0; while (_processDate <= pCdrExportInfo.DateEnd) { if (pBackgroundWorker.CancellationPending) { throw new Exception("CDR Export canceled"); } var _records = new List <string>(); for (int _hour = 0; _hour <= 23; _hour++) { int _timokDate = TimokDate.Parse(_processDate, _hour); // YYYYJJJ00 _records.AddRange(getCDRs(pWhereFilter, pCdrExportInfo, _timokDate, pBackgroundWorker)); _totalRecords += _records.Count; } using (var _sw = new StreamWriter(_tempFilePath, true)) { foreach (string _record in _records) { _sw.WriteLine(_record); } } _processDate = _processDate.AddDays(1); } FileHelper.Rename(_tempFilePath, pFilePath, TimokLogger.Instance.LogRbr); pBackgroundWorker.ReportStatus(string.Format("Exported total of {0} CDRs", _totalRecords)); pBackgroundWorker.ReportProgress(0); } catch { if (File.Exists(_tempFilePath)) { try { File.Delete(_tempFilePath); } catch {} } throw; } }
///NOTE: depricated //static List<string> getCDRs(string pWhereFilter, CdrExportMap pCdrExportMap, int pStartTimokDate, int pEndTimokDate, string pDecimalFormatString, BackgroundWorker pBackgroundWorker) { // pBackgroundWorker.ReportStatus("Retrieving CDRs..."); // List<string> _records = new List<string>(); // if (Cdr_Db.Exists(TimokDate.ToDateTime(pStartTimokDate))) { // using (Cdr_Db _db = new Cdr_Db(TimokDate.ToDateTime(pStartTimokDate))) { // IDbCommand _cmd = _db.Connection.CreateCommand(); // _cmd.CommandText = getSQLForCDRViewExport(_db.Connection.Database, pCdrExportMap.CdrExportMapDetails, pStartTimokDate, pEndTimokDate, pWhereFilter); // IDataReader _reader = _cmd.ExecuteReader(); // while (_reader.Read()) { // if (pBackgroundWorker.CancellationPending) { // throw new Exception("CDR Export canceled"); // } // StringBuilder _record = new StringBuilder(); // foreach (CdrExportMapDetail _field in pCdrExportMap.CdrExportMapDetails) { // object _value = _reader.GetValue(_field.Sequence - 1); // _record.Append(_value); // //NOTE: if need to format prices, here is the place to do that // //if (_value is Decimal) { // // _record.Append(((decimal) _value).ToString(pDecimalFormatString)); // //} // //else { // // _record.Append(_value); // //} // _record.Append((char) pCdrExportMap.CdrExportDelimeter); // } // _records.Add(_record.ToString().TrimEnd((char) pCdrExportMap.CdrExportDelimeter)); // } // } // } // pBackgroundWorker.ReportStatus(string.Format("Retrieved {0} CDRs", _records.Count)); // return _records; //} static List <string> getCDRs(string pWhereFilter, ICdrExportInfo pCdrExportInfo, int pTimokDate, IBackgroundWorker pBackgroundWorker) { var _records = new List <string>(); if (Cdr_Db.Exists(TimokDate.ToDateTime(pTimokDate))) { CDRViewRow[] _cdrViewRows; try { using (var _db = new Cdr_Db(TimokDate.ToDateTime(pTimokDate))) { _cdrViewRows = _db.CDRViewCollection.Get(pTimokDate, pWhereFilter); } } catch (Exception _ex) { TimokLogger.Instance.LogRbr(LogSeverity.Critical, GetcdrsLabel, string.Format("Exception:\r\n{0}", _ex)); pBackgroundWorker.ReportStatus(string.Format("Exception! Exported {0} CDRs", _records.Count)); return(_records); } if (_cdrViewRows == null || _cdrViewRows.Length == 0) { TimokLogger.Instance.LogRbr(LogSeverity.Error, GetcdrsLabel, "No CDR Records found"); pBackgroundWorker.ReportStatus("No CDR Records found"); return(_records); } pBackgroundWorker.ReportStatus(string.Format("Retrieved {0} CDRs for: {1}", _cdrViewRows.Length, TimokDate.ToDateTime(pTimokDate).ToString("yyyy-MM-dd HH:mm"))); var _recordCount = 0; foreach (var _cdrViewRow in _cdrViewRows) { if (pBackgroundWorker.CancellationPending) { throw new Exception("CDR Export canceled"); } if (pCdrExportInfo.WithRerating && _cdrViewRow.Duration > 0) { if (!rerateCDR(_cdrViewRow, pBackgroundWorker)) { continue; } } _records.Add(mapToExportedRecord(_cdrViewRow, pCdrExportInfo.CdrExportMap)); pBackgroundWorker.ReportProgress(++_recordCount * 100 / _cdrViewRows.Length); } if (_records.Count != _cdrViewRows.Length) { pBackgroundWorker.ReportStatus(string.Format("ERROR: Exported {0} out of {1} retreived CDRs", _records.Count, _cdrViewRows.Length)); } else { pBackgroundWorker.ReportStatus(string.Format("Exported {0} CDRs for: {1}", _records.Count, TimokDate.ToDateTime(pTimokDate).ToString("yyyy-MM-dd HH:mm"))); } } return(_records); }