Example #1
0
        private ExelConvertionRule GetRuleFromRow(convertion_rules rl)
        {
            ExelConvertionRule rule = null;

            if (rule == null && !string.IsNullOrWhiteSpace(rl.convertion_rule))
            {
                rule = ExelConvertionRule.DeserializeFromB64String(rl.convertion_rule);
            }

            if (rule == null && rl.convertion_rule_image != null && rl.convertion_rule_image.Length > 0)
            {
                rule = ExelConvertionRule.DeserializeFromBytes(rl.convertion_rule_image);
            }

            if (rule == null && rl.convertion_rule_image_cprs != null && rl.convertion_rule_image_cprs.Length > 0)
            {
                rule = ExelConvertionRule.DeserializeFromCompressedBytes(rl.convertion_rule_image_cprs);
            }

            if (rule == null)
            {
                rule = new ExelConvertionRule()
                {
                    Name = ExelConvertionRule.DefaultName
                }
            }
            ;

            rule.Id           = rl.id;
            rule.FkOperatorId = rl.fk_operator_id;

            return(rule);
        }
        private void AddRule()
        {
            var rule = new ExelConvertionRule();

            rule.FkOperatorId = (int)Operator.Id;
            Operator.MappingRules.Add(rule);
        }
Example #3
0
        private convertion_rules SetRuleToRow(convertion_rules rl, ExelConvertionRule rule)
        {
            if (rl == null)
            {
                rl = new convertion_rules();
            }

            rl.convertion_rule            = string.Empty; // do not save old rule // serializedRule;
            rl.convertion_rule_image      = null;         // do not save old rule //rule.SerializeToBytes();
            rl.convertion_rule_image_cprs = rule.SerializeToCompressedBytes();
            rl.fk_operator_id             = rule.FkOperatorId;

            return(rl);
        }
Example #4
0
        public void AddOperatorRules(ExelConvertionRule[] rules)
        {
            if (rules != null && rules.FirstOrDefault() != null)
                try
                {
                    using (var dc = exelconverterEntities2.New())
                    {
                        convertion_rules[] convRules =
                            rules
                            .Select(r => SetRuleToRow(null, r))
                            .ToArray();

                        foreach (var rule in convRules)
                            dc.convertion_rules.Add(rule);

                        dc.SaveChanges();

                        for (int i = 0; i < convRules.Length; i++)
                            rules[i].Id = convRules[i].id;
                    }
                }
                catch(DbEntityValidationException ex)
                {
                    StringBuilder sb = new StringBuilder();

                    foreach (var failure in ex.EntityValidationErrors)
                    {
                        sb.AppendFormat("{0} failed validation\n", failure.Entry.Entity.GetType());
                        foreach (var error in failure.ValidationErrors)
                        {
                            sb.AppendFormat("- {0} : {1}", error.PropertyName, error.ErrorMessage);
                            sb.AppendLine();
                        }
                    }

                    throw new DbEntityValidationException(
                        "Entity Validation Failed - errors follow:\n" +
                        sb.ToString(), ex
                    ); // Add the original exception as the innerException
                }
        }
Example #5
0
        private convertion_rules SetRuleToRow(convertion_rules rl, ExelConvertionRule rule)
        {
            if (rl == null)
                rl = new convertion_rules();

            rl.convertion_rule = string.Empty;// do not save old rule // serializedRule;
            rl.convertion_rule_image = null;// do not save old rule //rule.SerializeToBytes();
            rl.convertion_rule_image_cprs = rule.SerializeToCompressedBytes();
            rl.fk_operator_id = rule.FkOperatorId;

            return rl;
        }
Example #6
0
        private ExelConvertionRule GetRuleFromRow(convertion_rules rl)
        {
            ExelConvertionRule rule = null;

            if (rule == null && !string.IsNullOrWhiteSpace(rl.convertion_rule))
                rule = ExelConvertionRule.DeserializeFromB64String(rl.convertion_rule);

            if (rule == null && rl.convertion_rule_image != null && rl.convertion_rule_image.Length > 0)
                rule = ExelConvertionRule.DeserializeFromBytes(rl.convertion_rule_image);

            if (rule == null && rl.convertion_rule_image_cprs != null && rl.convertion_rule_image_cprs.Length > 0)
                rule = ExelConvertionRule.DeserializeFromCompressedBytes(rl.convertion_rule_image_cprs);

            if (rule == null)
                rule = new ExelConvertionRule() { Name = ExelConvertionRule.DefaultName };

            rule.Id = rl.id;
            rule.FkOperatorId = rl.fk_operator_id;

            return rule;
        }
Example #7
0
 public void RemoveOpertaorRule(ExelConvertionRule[] rules)
 {
     if (rules != null && rules.Length > 0)
         RemoveOpertaorRule(rules.Select(r => r.Id).ToArray());
 }
Example #8
0
 public void RemoveOperatorRule(ExelConvertionRule rule)
 {
     RemoveOpertaorRule(new ExelConvertionRule[] { rule });
 }
Example #9
0
 public void AddOperatorRule(ExelConvertionRule rule)
 {
     AddOperatorRules(new ExelConvertionRule[] { rule });
 }
 private void AddRule()
 {
     var rule = new ExelConvertionRule();
     rule.FkOperatorId = (int)Operator.Id;
     Operator.MappingRules.Add(rule);
 }
Example #11
0
 public void RemoveOperatorRule(ExelConvertionRule rule)
 {
     RemoveOpertaorRule(new ExelConvertionRule[] { rule });
 }
Example #12
0
 public void AddOperatorRule(ExelConvertionRule rule)
 {
     AddOperatorRules(new ExelConvertionRule[] { rule });
 }
Example #13
0
        public void Initialize()
        {
            InitializeError = string.Empty;
            if (Export2CsvCommand == null)
            {
                Export2CsvCommand = new RelayCommand(() => Export2Csv());
            }
            if (Export2DbCommand == null)
            {
                Export2DbCommand = new RelayCommand(Export2Db);
            }
            if (UpdateErrorsCommand == null)
            {
                UpdateErrorsCommand = new RelayCommand(UpdateErrors);
            }
            if (UpdateSelectedErrorCommand == null)
            {
                UpdateSelectedErrorCommand = new RelayCommand(UpdateSelectedError);
            }
            if (UpdateSelectedWarningCommand == null)
            {
                UpdateSelectedWarningCommand = new RelayCommand(UpdateSelectedWarning);
            }

            loadWorker = new BackgroundWorker()
            {
                WorkerReportsProgress = true, WorkerSupportsCancellation = true
            };
            loadWorker.DoWork += (s, prm) =>
            {
                var Disp = prm.Argument as System.Windows.Threading.Dispatcher;

                ObservableCollection <OutputRow> rowsToExport = new ObservableCollection <OutputRow>();
                Guid logSession = Helpers.Old.Log.SessionStart("ExportViewModel.Initialize()");
                try
                {
                    Log.Add(string.Format("total sheets count: '{0}'", App.Locator.Import.Document.DocumentSheets.Count));
                    var addErr  = new List <Error>();
                    var addGErr = new List <GlobalError>();

                    var progress = new PercentageProgress();

                    foreach (var item in App.Locator.Import
                             .ExportRules
                             .Where(r => r.Rule != App.Locator.Import.NullRule)
                             .Select(r => new { Rule = r, Progress = progress.GetChild() })
                             .ToArray()
                             )
                    {
                        if (((BackgroundWorker)s).CancellationPending || s != loadWorker)
                        {
                            break;
                        }

                        var mappingRule = item.Rule.Rule;
                        var ds          = item.Rule.Sheet;

                        if (mappingRule == null || ds == null)
                        {
                            if (!string.IsNullOrWhiteSpace(item.Rule.Status))
                            {
                                addGErr.Add(new GlobalError()
                                {
                                    Description = item.Rule.Status
                                });
                            }
                            item.Progress.Value = 100;
                            continue;
                        }
                        else
                        {
                            if (ds.MainHeader == null)
                            {
                                Log.Add(string.Format("should update main header row..."));

                                ds.UpdateMainHeaderRow(mappingRule.MainHeaderSearchTags
                                                       .Select(h => h.Tag)
                                                       .Union(SettingsProvider.CurrentSettings.HeaderSearchTags.Split(new char[] { ',' }))
                                                       .Select(i => i.Trim())
                                                       .Where(i => !string.IsNullOrEmpty(i))
                                                       .Distinct()
                                                       .ToArray());

                                ds.UpdateHeaders(mappingRule.SheetHeadersSearchTags
                                                 .Select(h => h.Tag.Trim())
                                                 .Where(i => !string.IsNullOrEmpty(i))
                                                 .Distinct()
                                                 .ToArray());
                            }

                            var oc = new ObservableCollection <OutputRow>(mappingRule.Convert(ds,
                                                                                              progressAction: (i) =>
                            {
                                item.Progress.Value = i;
                                ((BackgroundWorker)s).ReportProgress((int)progress.Value);
                            },
                                                                                              isCanceled: () => { return(((BackgroundWorker)s).CancellationPending); },
                                                                                              additionalErrorAction: (e, r) =>
                            {
                                addErr.Add(new Error()
                                {
                                    Description = e.GetExceptionText(includeStackTrace: false, clearText: true).Trim(), RowNumber = r
                                });
                            }));
                            Log.Add(string.Format("row count on sheet '{0}' : '{1}'", ds.Name, oc.Count));
                            rowsToExport = new ObservableCollection <OutputRow>(rowsToExport.Union(oc));
                            Log.Add(string.Format("subtotal row count on sheets: '{0}'", rowsToExport.Count));
                        }
                    }

                    ExelConvertionRule.RemoveRepeatingId(rowsToExport.ToList());

                    var UrlsPhoto  = new UrlCollection();
                    var UrlsSchema = new UrlCollection();
                    var UrlsAll    = new UrlCollection();

                    var photos  = rowsToExport.Select(r => r.Photo_img).Where(r => Helper.IsWellFormedUriString(r, UriKind.Absolute)).Distinct();
                    var schemas = rowsToExport.Select(r => r.Location_img).Where(r => Helper.IsWellFormedUriString(r, UriKind.Absolute)).Distinct();
                    var all     = photos.Union(schemas).Distinct();

                    foreach (var p in photos)
                    {
                        UrlsPhoto.Add(new StringUrlWithResultWrapper(p));
                    }
                    foreach (var p in schemas)
                    {
                        UrlsSchema.Add(new StringUrlWithResultWrapper(p));
                    }
                    foreach (var p in all)
                    {
                        UrlsAll.Add(new StringUrlWithResultWrapper(p));
                    }

                    if (!((BackgroundWorker)s).CancellationPending && s == loadWorker)
                    {
                        Disp.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(() =>
                        {
                            UrlCollection.Clear();
                            if (UrlsPhoto.Count > 0)
                            {
                                UrlCollection.Add(new UrlCollectionAdditional()
                                {
                                    Name = DBParsers.Labels.ElementAt(0), Collection = UrlsPhoto
                                });
                            }
                            if (UrlsSchema.Count > 0)
                            {
                                UrlCollection.Add(new UrlCollectionAdditional()
                                {
                                    Name = DBParsers.Labels.ElementAt(1), Collection = UrlsSchema
                                });
                            }
                            if (UrlsPhoto.Count > 0 && UrlsSchema.Count > 0 && UrlsAll.Count > 0)
                            {
                                UrlCollection.Add(new UrlCollectionAdditional()
                                {
                                    Name = "Все", Collection = UrlsAll
                                });
                            }
                            UrlCollectionSelectedIndex = UrlCollection.Count - 1;
                            RowsToExport = rowsToExport;
                            UpdateErrors(addErr, addGErr);
                        }));
                    }
                    else
                    {
                        prm.Cancel = true;
                    }
                }
                catch (Exception ex)
                {
                    Helpers.Old.Log.Add(logSession, ex.GetExceptionText());
                    throw ex;
                }
                finally
                {
                    Log.Add(string.Format("total row count to export: '{0}'", rowsToExport.Count));
                    Helpers.Old.Log.SessionEnd(logSession);
                }
            };

            loadWorker.ProgressChanged += (s, e) =>
            {
                if (s == loadWorker)
                {
                    LoadProgress = e.ProgressPercentage;
                }
            };

            loadWorker.RunWorkerCompleted += (s, e) =>
            {
                if (s == loadWorker)
                {
                    try
                    {
                        if (e.Cancelled)
                        {
                            throw new Exception("Загрузка отменена пользователем");
                        }
                        if (e.Error != null)
                        {
                            throw e.Error;
                        }
                    }
                    catch (Exception ex)
                    {
                        InitializeError = ex.GetExceptionText();
                    }
                }
                finally
                {
                    if (s == loadWorker)
                    {
                        IsLoading  = false;
                        loadWorker = null;
                    }
                }
                ((BackgroundWorker)s).Dispose();
            };