Esempio n. 1
0
        public static void EditRecords(IEnumerable<Record> records, BatchCriteriaSettings settings)
        {
            if (settings != null && records != null)
            {
                var q = settings.Items.GroupBy(
                    x => {
                        if (x is BatchSubrecord)
                        {
                            return ((BatchSubrecord)x).Record;
                        }

                        if (x is BatchElement)
                        {
                            return ((BatchElement)x).Parent.Record;
                        }

                        return null;
                    });
                foreach (var rec in records)
                {
                    foreach (var kvp in q)
                    {
                        // delete or create any subrecords
                        foreach (BatchCriteria criteria in kvp)
                        {
                            var sr1 = criteria as BatchSubrecord;
                            if (sr1 != null)
                            {
                                sr1.Evaluate(rec);
                            }
                        }

                        var sr = rec.SubRecords.FirstOrDefault(x => x.Name == kvp.Key.name);
                        if (sr != null)
                        {
                            try
                            {
                                var elements = sr.EnumerateElements(rawData: true).ToList();
                                foreach (var sre in kvp.OfType<BatchElement>())
                                {
                                    BatchElement sre1 = sre;
                                    foreach (var elem in elements.Where(x => x.Structure.Equals(sre1.Record)))
                                    {
                                        elem.AssignValue(GetAssignmentType(sre1.Type), sre.Value);
                                    }
                                }

                                if (elements.Any(x => x.Changed))
                                {
                                    // need to repack the structure
                                    using (var ms = new MemoryStream(sr.GetReadonlyData().Length))
                                    {
                                        foreach (var seg in elements.Select(elem => elem.Data))
                                        {
                                            ms.Write(seg.Array, seg.Offset, seg.Count);
                                        }

                                        sr.SetData(ms.ToArray());
                                    }
                                }
                                else if (elements.Any(x => x.Modified))
                                {
                                    // all changes inline
                                }
                            }
                            catch
                            {
                            }
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        private bool ApplySettings()
        {
            var recStruct = this.rec;
            if (recStruct == null)
            {
                DialogResult = DialogResult.None;
                MessageBox.Show(this, "No record was selected.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
            }
            else
            {
                var checkeditems = this.filterTree.CheckedObjectsEnumerable;
                var items = checkeditems.OfType<BatchCriteria>().ToArray();
                if (items.Length == 0)
                {
                    DialogResult = DialogResult.None;
                    MessageBox.Show(this, "No Batch criteria was selected.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
                }
                else
                {
                    this.Criteria = new BatchCriteriaSettings();
                    this.Criteria.Type = this.rec.name;
                    this.Criteria.Items = items;
                    return true;
                }
            }

            return false;
        }