public ExelConvertionRule CopyFrom(ExelConvertionRule source) { SheetHeadersSearchTags = source.SheetHeadersSearchTags; MainHeaderSearchTags = source.MainHeaderSearchTags; MapParsingData.Clear(); foreach (var item in source.MapParsingData) { MapParsingData.Add((new ImageParsingData()).CopyFrom(item)); } PhotoParsingData.Clear(); foreach (var item in source.PhotoParsingData) { PhotoParsingData.Add((new ImageParsingData()).CopyFrom(item)); } ConvertionData.Clear(); foreach (var item in source.ConvertionData) { var cd = (new FieldConvertionData()).CopyFrom(item); cd.Owner = this; ConvertionData.Add(cd); } //FindSheetHeadersByTags = source.FindSheetHeadersByTags; //FindMainHeaderByTags = source.FindMainHeaderByTags; Name = source.Name; FkOperatorId = source.FkOperatorId; UpdateMappingTablesValues(); return(this); }
public void ReadDataFromFile() { string filePath = Application.CommonAppDataPath + @"\currencies.xml"; if (!File.Exists(filePath)) { return; } using (FileStream fs = File.Open(filePath, FileMode.Open)) { using (XmlReader xmlr = XmlReader.Create(fs)) { while (xmlr.Read()) { if (xmlr.NodeType == XmlNodeType.Element && xmlr.Name == "Conversion") { string from = xmlr.GetAttribute("From"); string to = xmlr.GetAttribute("To"); double value = double.Parse(xmlr.GetAttribute("Value")); int fetchServerInt = int.Parse(xmlr.GetAttribute("FetchServer")); Hosts fetchServer = (Hosts)fetchServerInt; DateTime fetchTime = DateTime.ParseExact(xmlr.GetAttribute("FetchTime"), fileDateFormat, CultureInfo.InvariantCulture); DateTime?serverUpdateTime = xmlr.AttributeCount > 5 ? (DateTime?)DateTime.ParseExact(xmlr.GetAttribute("ServerUpdateTime"), fileDateFormat, CultureInfo.InvariantCulture) : null; if (!data.ContainsKey(from + '-' + to)) { ConvertionData entry = new ConvertionData(from, to, value, fetchTime, serverUpdateTime, fetchServer); data.Add(from + '-' + to, entry); } } } } } }
ConvertionData TryGetFromData(string from, string to) { if (data.ContainsKey(from + '-' + to)) { return(data[from + '-' + to]); //data[from + '-' + to].value; } else if (data.ContainsKey(to + '-' + from)) { ConvertionData d = new ConvertionData(data[from + '-' + to]); d.value = 1.0 / d.value; return(d); } else { //wyszukiwanie połączeń HashSet <string> fromValues = new HashSet <string>(); HashSet <string> toValues = new HashSet <string>(); foreach (var a in data) { string aFrom = a.Key.Remove(a.Key.IndexOf('-')); string aTo = a.Key.Substring(a.Key.IndexOf('-') + 1); fromValues.Add(aFrom); toValues.Add(aTo); } foreach (var a in data) { } return(null); } //else return null; }
public void initFileCoverter() { FileConverter tempFC = new FileConverter(); ConvertionData tempCD = new ConvertionData(); fileConverter = tempFC; fileConverter.ExcelExists = CheckForExcel(); convertionData = tempCD; }
public ConvertionData(ConvertionData other) { from = other.from; to = other.to; value = other.value; fetchTime = other.fetchTime; serverUpdateTime = other.serverUpdateTime; fetchHost = other.fetchHost; }
void AddEntry(ConvertionData convertion) { if (data.ContainsKey(convertion.from + '-' + convertion.to)) { data.Remove(convertion.from + '-' + convertion.to); } if (data.ContainsKey(convertion.to + '-' + convertion.from)) { data.Remove(convertion.to + '-' + convertion.from); } data.Add(convertion.from + '-' + convertion.to, convertion); }
public List <OutputRow> Convert(ExelSheet sheet, string[] conversionDataLimiter = null, Action <int> progressAction = null, Action <Exception, int> additionalErrorAction = null, Func <bool> isCanceled = null) { Guid logSession = Helpers.Old.Log.SessionStart("ExelConvertionRule.Convert()"); var result = new List <OutputRow>(); try { if (sheet.MainHeader != null) { var initialRow = 0; var headers = sheet.SheetHeaders.Headers; var subheaders = sheet.SheetHeaders.Subheaders; initialRow = sheet.Rows.IndexOf(sheet.MainHeader) + sheet.MainHeaderRowCount; int excludeRow = 0; bool hasError = false; int logPart = 0; var exceptions = new List <Exception>(); for (var i = initialRow; i < sheet.Rows.Count; i++) { if (isCanceled != null && isCanceled()) { break; } var outputRow = new OutputRow(); try { exceptions.Clear(); hasError = false; if (progressAction != null) { progressAction((int)(((decimal)i / (decimal)sheet.Rows.Count) * 100m)); } logPart = 1; if (headers.Any(h => h.RowNumber == i) || subheaders.Any(h => h.RowNumber == i)) { continue; } outputRow.OriginalIndex = sheet.Rows[i].Index; outputRow.OriginalSheet = sheet.Name; logPart++; foreach (var convertionData in (conversionDataLimiter == null ? ConvertionData : ConvertionData.Where(i2 => conversionDataLimiter.Contains(i2.PropertyId)))) { var cellResultContent = string.Empty; int subLogPart = 1; try { var propertyOld = typeof(OutputRow).GetProperty("Original" + convertionData.PropertyId); if (propertyOld != null) { string oldValue = null; subLogPart++; var blocks = convertionData.Blocks.Blocks.FirstOrDefault(); if (blocks != null) { subLogPart++; var startFunc = blocks.UsedFunctions.FirstOrDefault(); if (startFunc != null) { subLogPart++; if (startFunc.Function.SelectedParameter == FunctionParameters.CellName) { subLogPart++; var header = sheet.Rows[sheet.Rows.IndexOf(sheet.MainHeader)].HeaderCells.Select(c => c.Value).ToList(); var columnNumber = header.IndexOf(header.Where(s => s.Trim().ToLower() == startFunc.Function.ColumnName.Trim().ToLower()).FirstOrDefault()); if (columnNumber >= 0 && sheet.Rows.ElementAt(i).Cells.Count > columnNumber) { oldValue = sheet.Rows.ElementAt(i).Cells.ElementAt(columnNumber).Value; } } else if (startFunc.Function.SelectedParameter == FunctionParameters.CellNumber) { subLogPart++; if (sheet.Rows.ElementAt(i).Cells.Count > startFunc.Function.ColumnNumber) { oldValue = sheet.Rows.ElementAt(i).Cells.ElementAt(startFunc.Function.ColumnNumber).Value; } } } } if (oldValue != null) { propertyOld.SetValue(outputRow, oldValue.Trim(), null); } } try { cellResultContent = convertionData.Blocks.Run(sheet, i, convertionData); //if (cellResultContent != null) //{ // cellResultContent = cellResultContent.Trim(); //} } catch (Exception ex) { throw new Exception(string.Format("Сетка '{0}'. Ошибка для правил столбца '{1}' ('{2}')", sheet.Name, convertionData.FieldName, convertionData.PropertyId), ex); } var property = typeof(OutputRow).GetProperty(convertionData.PropertyId); if (property != null) { property.SetValue(outputRow, cellResultContent, null); } } catch (Exception ex) { hasError = true; if (additionalErrorAction != null) { Helpers.Old.Log.Add(logSession, ex); exceptions.Add(ex); } else { throw new Exception(string.Format("exception on update field '{0}' at sub step '{1}';", convertionData.PropertyId, subLogPart), ex); } } } logPart++; if (!string.IsNullOrWhiteSpace(outputRow.Region) && outputRow.Region.Contains('(') && outputRow.Region.Contains(')')) { //outputRow.Region = outputRow.Region.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault(); outputRow.Region = outputRow.Region.Substring(0, outputRow.Region.LastIndexOf("(") - 1).Trim(); } logPart++; if (!string.IsNullOrWhiteSpace(outputRow.Size) && outputRow.Size.Contains('(') && outputRow.Size.Contains(')')) { //outputRow.Size = outputRow.Size.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault(); outputRow.Size = outputRow.Size.Substring(0, outputRow.Size.LastIndexOf("(") - 1).Trim(); } logPart++; if (!string.IsNullOrWhiteSpace(outputRow?.Code?.Trim()) || (hasError && additionalErrorAction != null)) { if (additionalErrorAction != null) { foreach (var ex in exceptions) { additionalErrorAction(ex, i - initialRow - excludeRow); } exceptions.Clear(); } result.Add(outputRow); } else { excludeRow++; } } catch (Exception ex) { if (additionalErrorAction != null) { Helpers.Old.Log.Add(logSession, ex); if (!result.Contains(outputRow)) { result.Add(outputRow); } additionalErrorAction(ex, i - initialRow - excludeRow); } else { throw new Exception(string.Format("exception at row line: '{0}', log part: '{1}';", i, logPart), ex); } } } } else { Log.Add(string.Format("sheet '{0}' has not header!", sheet.Name)); } } catch (Exception ex) { Helpers.Old.Log.Add(logSession, ex); } finally { RemoveRepeatingId(result); Helpers.Old.Log.SessionEnd(logSession); } return(result); }
public async Task <double> Convert(string from, string to, double amount) { if (from == to) { return(amount); } if (!currencies.Contains(from)) { throw new CurrencyConvertException(CurrencyConvertException.ErrorCase.UnaviableCurrency, "No currency: " + from); } if (!currencies.Contains(to)) { throw new CurrencyConvertException(CurrencyConvertException.ErrorCase.UnaviableCurrency, "No currency: " + to); } ConvertionData value = TryGetFromData(from, to); bool tryToUpdate = false; if (value != null) { tryToUpdate = (value.serverUpdateTime != null) ? value.serverUpdateTime + updateTime >= DateTime.Now : value.fetchTime + updateTime >= DateTime.Now; } if (value != null && !tryToUpdate) { return(value.value * amount); } if (value == null || tryToUpdate) { if (host_FreeCurrencyConverterApi.aviableCurrencies.Contains(from) && host_FreeCurrencyConverterApi.aviableCurrencies.Contains(to)) { try { ConvertionData convertion = await FreeCurrencyConverterApi_GetFromTo(from, to); AddEntry(convertion); SaveDataToFile(); value = convertion; return(value.value * amount); } catch (CurrencyConverterInnerException ex) { if (ex.HResult != (int)CurrencyConverterInnerException.ErrorCase.ServerFetchFailature) { if (!tryToUpdate) { throw new CurrencyConvertException(CurrencyConvertException.ErrorCase.Unknown); } } } } if (host_EuropeanCentralBank.aviableCurrencies.Contains(from) && host_EuropeanCentralBank.aviableCurrencies.Contains(to)) { try { Dictionary <string, ConvertionData> newData = await EuropeanCentralBank_GetAll(); foreach (var c in newData.Values) { AddEntry(c); } SaveDataToFile(); value = TryGetFromData(from, to); return(value.value * amount); } catch (CurrencyConverterInnerException ex) { if (ex.HResult != (int)CurrencyConverterInnerException.ErrorCase.ServerFetchFailature) { if (!tryToUpdate) { throw new CurrencyConvertException(CurrencyConvertException.ErrorCase.Unknown); } } } } if (!tryToUpdate) { throw new CurrencyConvertException(CurrencyConvertException.ErrorCase.NoServerAviable); } } if (value != null) { return(value.value * amount); } else { throw new CurrencyConvertException(CurrencyConvertException.ErrorCase.NoServerAviable); } }