/// <summary> /// Function the creates a new audit record. /// </summary> /// <param name="AuditRecord">Audit Domain object populated that needs /// saving away.</param> public static int Add(AuditObject AuditRecord) { using (var ctx = new AuditEntities()) { Entity newRecord = Convert(AuditRecord, ctx); // Add the new AuditRecord record ctx.Entity.AddObject(newRecord); ctx.SaveChanges(); return newRecord.Id; } }
/// <summary> /// function to find an existing Tag in the database with the same name. /// </summary> /// <param name="Name">String description of the tag to be located.</param> /// <param name="Ctx">Context being used to search for the tag.</param> /// <returns>Returns a Tag object if one is located. If no tag is /// located by name the function will return null.</returns> private static Tag GetTag(string Name, AuditEntities Ctx) { var results = from t in Ctx.Tag where t.TagName == Name select t; return results.FirstOrDefault(); }
/// <summary> /// function to find an existing Organisation in the database with the same Id. /// </summary> /// <param name="Id">Id of the Organisation you are trying to locate.</param> /// <param name="Ctx">Context being used to search for the organisation.</param> /// <returns>Returns an Organisation if one is located. If no Organisation is /// located by name the function will return null.</returns> private static Organisation GetOrganisation(int Id, AuditEntities Ctx) { var results = from o in Ctx.Organisation where o.Id == Id select o; return results.FirstOrDefault(); }
/// <summary> /// function to find an existing Tag in the database with the same Id. /// </summary> /// <param name="Id">Id of the audit Level you are trying to locate.</param> /// <param name="Ctx">Context being used to search for the tag.</param> /// <returns>Returns an AuditLevel if one is located. If no AuditLevel is /// located by name the function will return null.</returns> private static AuditLevel GetLevel(int Id, AuditEntities Ctx) { var results = from l in Ctx.AuditLevel where l.Id == Id select l; return results.FirstOrDefault(); }
private static Audit Convert(AuditObject AuditRecord, AuditEntities Ctx) { var audit = new Audit { DateCreated = AuditRecord.DateCreated, Application = string.IsNullOrEmpty(AuditRecord.Application) ? "UNKNOWN" : AuditRecord.Application }; var level = GetLevel(AuditRecord.Level, Ctx); if (level != null) { audit.AuditLevel = level; } else { throw new InvalidLevelException(string.Format("Audit Level {0} does not exist", AuditRecord.Level)); } var organisation = GetOrganisation(AuditRecord.Organisation.Id, Ctx); if (organisation != null) { audit.Organisation = organisation; } else { throw new InvalidOrganisationException(string.Format("Organisation {0} does not exist", AuditRecord.Organisation.Id)); } audit = ConvertFileAttributes(AuditRecord, audit); audit = ConvertStringAttributes(AuditRecord, audit); audit = ConvertBinaryAttributes(AuditRecord, audit); audit = ConvertTagAttributes(AuditRecord, audit, Ctx); return audit; }
private static List<Audit> Convert(IEnumerable<AuditObject> AuditObjects, AuditEntities Ctx) { return AuditObjects.Select(C => Convert(C, Ctx)).ToList(); }
/// <summary> /// Search function to retrieve Audit records based on Tags, String /// Attributes and Date range. All parameters are optional. /// </summary> /// <param name="Tags">Tag list where list is separated by '|'</param> /// <param name="StrAttribs">string key value list. Each pair is /// separated by '|'. The key value pair is separated by '^'</param> /// <param name="DateFrom">Date From that the search should be associated to.</param> /// <param name="DateTo">Date To that the search should be associated to.</param> /// <param name="OrganisationId">Id of the organisation that the records are associated to.</param> /// <param name="Offset">The amount of records already been viewed by the client</param> /// <param name="ShowDeleted">Boolean to define if deleted records get returned.</param> /// <param name="PageSize">The amount of records to record</param> /// <returns>A list of Audit Domain objects that the search matches.</returns> public static List<AuditObject> Get(IEnumerable<string> Tags, Dictionary<string, string> StrAttribs, DateTime? DateFrom, DateTime? DateTo, int OrganisationId, int PageSize, int Offset, bool ShowDeleted) { using (var ctx = new AuditEntities()) { var records = from a in ctx.Entity.OfType<Audit>() .Include("Attribute") select a; if (ShowDeleted == false) { records = records.Where(R => R.DateDeleted == null); } if (DateFrom.HasValue) { records = records.Where(R => R.DateCreated >= DateFrom); } if (DateTo.HasValue) { records = records.Where(R => R.DateCreated <= DateTo); } records = records.Where(O => O.Organisation.Id == OrganisationId); records = records.OrderByDescending(O => O.DateCreated); records = Tags.Aggregate(records, (Current, Tag) => Current.Where(T => T.Tag.Any(Tn => Tn.TagName == Tag))); records = StrAttribs.Aggregate(records, (Current, StrAttrib) => Current.Where( S => S.Attribute.OfType<StringAttribute>().Any( Sv => Sv.Value == StrAttrib.Value && Sv.Key == StrAttrib.Key))); // Sort the paging if (PageSize > 0 || Offset > 0) { records = records.Skip(Offset); records = records.Take(PageSize); } return Convert(records.ToList()); } }
/// <summary> /// Suspect this function is NOT USED !!!!!! /// </summary> /// <param name="SearchCriteria"></param> /// <returns></returns> public static List<AuditObject> Get(AuditObject SearchCriteria) { using (var ctx = new AuditEntities()) { var records = from a in ctx.Entity.OfType<Audit>() select a; records = SearchCriteria.Tags.Aggregate(records, (Current, Record) => Current.Where(T => T.Tag.Any(R => R.TagName == Record))); return Convert(records.ToList()); } }
/// <summary> /// Get an Audit record based on the unique Id of the record. /// </summary> /// <param name="Id">Id of the Audit record to be retrieved.</param> /// <returns>The Audit domain object based on the Id passed in. if no /// record is found the function will return null.</returns> public static AuditObject Get(int Id) { using (var ctx = new AuditEntities()) { var results = from a in ctx.Entity.OfType<Audit>() where a.Id == Id select a; return results.FirstOrDefault() != null ? Convert(results.FirstOrDefault()) : null; } }
public static void Delete(int Id) { using (var ctx = new AuditEntities()) { var results = from a in ctx.Entity.OfType<Audit>() where a.Id == Id select a; var recordToDelete = results.FirstOrDefault(); if (recordToDelete != null) { recordToDelete.DateDeleted = DateTime.Now; ctx.SaveChanges(); } } }
/// <summary> /// Convert the Tags from domain object to data object. If the tag /// already exists in the database the tag will be attached to the /// existing record stopping duplicates being created. /// </summary> /// <param name="AuditDomainObject">Domain object with any tags associated.</param> /// <param name="AuditRecord">Audit data record where the tags need to /// be attached if they already exist and added if they do not.</param> /// <param name="Ctx">Context to manage the tags being attached or added.</param> /// <returns></returns> public static Audit ConvertTagAttributes(AuditObject AuditDomainObject, Audit AuditRecord, AuditEntities Ctx) { foreach (var tagObject in AuditDomainObject.Tags) { Tag tag = GetTag(tagObject, Ctx); if (tag != null) AuditRecord.Tag.Add(tag); else { var tagAttribute = new Tag { TagName = tagObject, DateCreated = DateTime.Now }; AuditRecord.Tag.Add(tagAttribute); } } return AuditRecord; }