protected virtual void WriteEntity(EdiBaseEntity ent, ref StringBuilder sb, IValidatedEntity validationScope = null)
        {
            if (ent is EdiLoop)
            {
                foreach (var child in ((EdiLoop)ent).Content)
                {
                    WriteEntity(child, ref sb);
                }
            }
            else if (ent is EdiSegment)
            {
                var seg = (EdiSegment)ent;

                _currentTranSegCount++;

                sb.Append(ent.Name);
                foreach (var el in ((EdiSegment)ent).Content)
                {
                    sb.Append($"{CurrentElementSeparator}{el.Val}");
                }
                sb.Append(CurrentSegmentSeparator);

                if (validationScope != null)
                {
                    SegmentValidator.ValidateSegment(seg, _currentTranSegCount, validationScope);
                }
            }
        }
 public static void CheckArgumentException(this IValidatedEntity entity)
 {
     if (entity == null ||
         !entity.Validate())
     {
         throw new ArgumentException();
     }
 }
Esempio n. 3
0
        private void AddValidationError(IValidatedEntity obj, string message)
        {
            ValidationError err = new ValidationError()
            {
                Message = message
            };

            obj?.ValidationErrors.Add(err);
        }
Esempio n. 4
0
        public static void ValidateSegment(EdiSegment seg, int rowPos, IValidatedEntity validationScope)
        {
            int i = 0;

            foreach (var dataElement in seg.Content)
            {
                if (dataElement is EdiSimpleDataElement)
                {
                    EdiSimpleDataElement el = (EdiSimpleDataElement)dataElement;
                    ValidateSimpleDataElement(el, seg.Name, rowPos, i + 1, validationScope);
                }
                else if (dataElement is EdiCompositeDataElement)
                {
                    EdiCompositeDataElement c = (EdiCompositeDataElement)dataElement;
                    foreach (EdiSimpleDataElement el in c.Content)
                    {
                        ValidateSimpleDataElement(el, seg.Name, rowPos, i + 1, validationScope);
                    }
                }
                i++;
            }

            var syntaxNotes = ((MapSegment)seg.Definition).SyntaxNotes;

            if (syntaxNotes != null && syntaxNotes.Count > 0)
            {
                foreach (var sn in syntaxNotes)
                {
                    var syntaxNote = SyntaxNoteFactory.GetSyntaxNote(sn);
                    if (!syntaxNote.IsValid(seg.Content.Select(e => e.Val).ToArray()))
                    {
                        AddError(seg.Name, rowPos, null, $"Syntax note violation '{syntaxNote}'", validationScope);
                    }
                }
            }
        }
Esempio n. 5
0
        public static EdiSegment ProcessSegment(MapBaseEntity definition, string[] content, int rowPos, string compositeSeparator, IValidatedEntity validationScope)
        {
            MapSegment segDef = (MapSegment)definition;
            EdiSegment seg    = new EdiSegment(segDef);

            int i = 0;

            foreach (string val in content.Skip(1))
            {
                MapSimpleDataElement    elDef = null;
                MapCompositeDataElement cDef  = null;
                if (i < segDef.Content.Count)
                {
                    if (segDef.Content[i] is MapSimpleDataElement)
                    {
                        elDef = (MapSimpleDataElement)segDef.Content[i];
                    }
                    else if (segDef.Content[i] is MapCompositeDataElement)
                    {
                        cDef = (MapCompositeDataElement)segDef.Content[i];
                    }
                }

                //if cDef is null - create simple element. Even if elDef is null
                // validation will add error of unknown element later on
                if (cDef == null)
                {
                    EdiSimpleDataElement el = new EdiSimpleDataElement(elDef, val);
                    seg.Content.Add(el);
                }
                else
                {
                    EdiCompositeDataElement composite = new EdiCompositeDataElement(cDef);
                    string[] compositeContent         = val.Split(new[] { compositeSeparator }, StringSplitOptions.None);
                    ProcessComposite(composite, compositeContent);
                    seg.Content.Add(composite);
                }

                i++;
            }

            SegmentValidator.ValidateSegment(seg, rowPos, validationScope);
            return(seg);
        }
Esempio n. 6
0
        public override int SaveChanges()
        {
            List <RuleViolation> errors = new List <RuleViolation>();

            KcsarContext comparisonContext = new KcsarContext();

            List <AuditLog> changes = new List <AuditLog>();

            // Validate the state of each entity in the context
            // before SaveChanges can succeed.
            Random rand = new Random();

            ObjectContext oc  = ((IObjectContextAdapter)this).ObjectContext;
            var           osm = oc.ObjectStateManager;

            oc.DetectChanges();

            // Added and modified objects - we can describe the state of the object with
            // the information already present.
            foreach (ObjectStateEntry entry in
                     osm.GetObjectStateEntries(
                         EntityState.Added | EntityState.Modified))
            {
                // Do Validation
                if (entry.Entity is IValidatedEntity)
                {
                    IValidatedEntity validate = (IValidatedEntity)entry.Entity;
                    if (!validate.Validate())
                    {
                        errors.AddRange(validate.Errors);
                    }
                    else
                    {
                        Document d = entry.Entity as Document;
                        if (d != null)
                        {
                            if (string.IsNullOrWhiteSpace(d.StorePath))
                            {
                                string path = string.Empty;
                                for (int i = 0; i < Document.StorageTreeDepth; i++)
                                {
                                    path += ((i > 0) ? "\\" : "") + rand.Next(Document.StorageTreeSpan).ToString();
                                }
                                if (!System.IO.Directory.Exists(Document.StorageRoot + path))
                                {
                                    System.IO.Directory.CreateDirectory(Document.StorageRoot + path);
                                }
                                path       += "\\" + d.Id.ToString();
                                d.StorePath = path;
                            }
                            System.IO.File.WriteAllBytes(Document.StorageRoot + d.StorePath, d.Contents);
                        }
                        // New values are valid

                        if (entry.Entity is IModelObject)
                        {
                            IModelObject obj = (IModelObject)entry.Entity;

                            // Keep track of the change for reporting.
                            obj.LastChanged = DateTime.Now;
                            obj.ChangedBy   = Thread.CurrentPrincipal.Identity.Name;

                            IModelObject original = (entry.State == EntityState.Added) ? null : GetOriginalVersion(comparisonContext, entry);


                            if (original == null)
                            {
                                changes.Add(new AuditLog
                                {
                                    Action     = entry.State.ToString(),
                                    Comment    = obj.GetReportHtml(),
                                    Collection = entry.EntitySet.Name,
                                    Changed    = DateTime.Now,
                                    ObjectId   = obj.Id,
                                    User       = Thread.CurrentPrincipal.Identity.Name
                                });
                            }
                            else
                            {
                                string report = string.Format("<b>{0}</b><br/>", obj);

                                foreach (PropertyInfo pi in GetReportableProperties(obj.GetType()))
                                {
                                    object left  = pi.GetValue(original, null);
                                    object right = pi.GetValue(obj, null);
                                    if ((left == null && right == null) || (left != null && left.Equals(right)))
                                    {
                                        //   report += string.Format("{0}: unchanged<br/>", pi.Name);
                                    }
                                    else
                                    {
                                        report += string.Format("{0}: {1} => {2}<br/>", pi.Name, left, right);
                                    }
                                }
                                changes.Add(new AuditLog
                                {
                                    Action     = entry.State.ToString(),
                                    Comment    = report,
                                    Collection = entry.EntitySet.Name,
                                    Changed    = DateTime.Now,
                                    ObjectId   = obj.Id,
                                    User       = Thread.CurrentPrincipal.Identity.Name
                                });
                            }
                        }
                    }
                }
            }

            // Added and modified objects - we need to fetch more data before we can report what the change was in readable form.
            foreach (ObjectStateEntry entry in osm.GetObjectStateEntries(EntityState.Deleted))
            {
                IModelObject modelObject = GetOriginalVersion(comparisonContext, entry);
                if (modelObject != null)
                {
                    Document d = modelObject as Document;
                    if (d != null && !string.IsNullOrWhiteSpace(d.StorePath))
                    {
                        string path = Document.StorageRoot + d.StorePath;
                        System.IO.File.Delete(path);
                        for (int i = 0; i < Document.StorageTreeDepth; i++)
                        {
                            path = System.IO.Path.GetDirectoryName(path);
                            if (System.IO.Directory.GetDirectories(path).Length + System.IO.Directory.GetFiles(path).Length == 0)
                            {
                                System.IO.Directory.Delete(path);
                            }
                        }
                    }
                    changes.Add(new AuditLog
                    {
                        Action     = entry.State.ToString(),
                        Comment    = modelObject.GetReportHtml(),
                        Collection = entry.EntitySet.Name,
                        Changed    = DateTime.Now,
                        ObjectId   = modelObject.Id,
                        User       = Thread.CurrentPrincipal.Identity.Name
                    });
                }
            }

            if (errors.Count > 0)
            {
                throw new RuleViolationsException(errors);
            }

            changes.ForEach(f => this.AuditLog.Add(f));

            return(base.SaveChanges());
            //if (SavedChanges != null)
            //{
            //    SavedChanges(this, new SavingChangesArgs { Changes = changes.Select(f => f.Comment).ToList() });
            //}
        }
Esempio n. 7
0
        public static EdiSegment ProcessSegment(MapBaseEntity definition, string[] content, int rowPos, IValidatedEntity validationScope)
        {
            MapSegment segDef = (MapSegment)definition;
            EdiSegment seg    = new EdiSegment(segDef);

            int i = 0;

            foreach (string val in content.Skip(1))
            {
                MapDataElement elDef = null;
                if (i < segDef.Content.Count)
                {
                    elDef = segDef.Content[i];
                }

                if (elDef == null)
                {
                    ValidationError err = new ValidationError()
                    {
                        SegmentPos  = rowPos,
                        SegmentName = content[0],
                        ElementPos  = i + 1,
                        Message     = $"Unexpected element '{val}'"
                    };
                    validationScope.ValidationErrors.Add(err);
                }

                EdiDataElement el = new EdiDataElement(elDef, val);
                if (elDef != null && !el.IsValid(elDef))
                {
                    ValidationError err = new ValidationError()
                    {
                        SegmentPos  = rowPos,
                        SegmentName = content[0],
                        ElementPos  = i + 1,
                        Message     = $"Invalid value '{val}'"
                    };
                    validationScope.ValidationErrors.Add(err);
                }

                i++;
                seg.Content.Add(el);
            }
            return(seg);
        }
Esempio n. 8
0
        private static void AddError(string segName, int?segPos, int?elPos, string message, IValidatedEntity validationScope)
        {
            ValidationError err = new ValidationError()
            {
                SegmentPos  = segPos,
                SegmentName = segName,
                ElementPos  = elPos,
                Message     = message
            };

            validationScope.ValidationErrors.Add(err);
        }
Esempio n. 9
0
 private static void ValidateSimpleDataElement(EdiSimpleDataElement el, string segName, int segPos, int elPos, IValidatedEntity validationScope)
 {
     if (el.Definition == null)
     {
         AddError(segName, segPos, elPos, $"Unexpected element '{el.Val}'", validationScope);
     }
     else if (el.Definition != null && !el.IsValid(el.Definition))
     {
         AddError(segName, segPos, elPos, $"Invalid value '{el.Val}'", validationScope);
     }
 }