Exemplo n.º 1
0
        public static AuditLogListResponse List(string schema, AuditLogListRequest request)
        {
            var fld = IoC.Resolve <IAuditLogRow>(schema);

            SecurityHelper.EnsureLoggedIn(RightErrorHandling.ThrowException);

            var response = new AuditLogListResponse();

            using (var connection = SqlConnections.New())
            {
                response.Entities = new List <Row>();

                var row = ((Row)fld).CreateNew();

                if (request.Sort == null ||
                    request.Sort.Length == 0)
                {
                    request.Sort = new SortBy[] { new SortBy(fld.DateField.Name, true) }
                }
                ;

                var query = new SqlQuery().From(row)
                            .Select(
                    (Field)fld.IdField,
                    fld.EntityTypeIdField,
                    fld.EntityIdField,
                    fld.ParentTypeIdField,
                    fld.OldParentIdField,
                    fld.NewParentIdField,
                    fld.DateField,
                    (Field)fld.UserIdField,
                    fld.AuditTypeIdField,
                    fld.OldAuditDataField,
                    fld.NewAuditDataField)
                            .OrderBy(
                    (Field)fld.IdField)
                            .ApplySkipTakeAndCount(request.Skip, request.Take, request.ExcludeTotalCount)
                            .ApplySort(request.Sort);

                if (request.EntityTypeId != null &&
                    request.EntityId != null)
                {
                    var pEntityId     = query.AddParam(request.EntityId);
                    var pEntityTypeId = query.AddParam(request.EntityTypeId);

                    query.Where(~(
                                    ~(
                                        new Criteria(0, fld.EntityTypeIdField) == pEntityTypeId &
                                        new Criteria(0, fld.EntityIdField) == pEntityId) |
                                    ~(
                                        new Criteria(0, fld.ParentTypeIdField) == pEntityTypeId &
                                        ~(
                                            new Criteria(0, fld.OldParentIdField) == pEntityId |
                                            new Criteria(0, fld.NewParentIdField) == pEntityId))));
                }
                else
                {
                    if (request.EntityTypeId != null)
                    {
                        query.WhereEqual(fld.EntityTypeIdField, request.EntityTypeId);//Convert.ToInt32(request.EntityTypeId.Value));
                    }
                    if (request.EntityId != null)
                    {
                        query.WhereEqual(fld.EntityIdField, request.EntityId.Value);
                    }
                }

                response.TotalCount = query.ForEach(connection, delegate()
                {
                    response.Entities.Add(row.Clone());
                });

                response.SetSkipTakeTotal(query);

                response.IdNameLookups      = new Dictionary <EntityType, Dictionary <long, string> >();
                response.FieldTitles        = new Dictionary <EntityType, Dictionary <string, string> >();
                response.ForeignEntityTypes = new Dictionary <EntityType, Dictionary <string, string> >();
                response.EntityTitles       = new Dictionary <EntityType, string>();

                var lookups  = response.IdNameLookups;
                var titles   = response.FieldTitles;
                var foreigns = response.ForeignEntityTypes;
                var entities = response.EntityTitles;

                Action <EntityType, Int64> addLookup = (entityType, id) => {
                    Dictionary <long, string> lookup;
                    if (!lookups.TryGetValue(entityType, out lookup))
                    {
                        lookup = new Dictionary <long, string>();
                        lookups[entityType] = lookup;
                    }

                    if (!lookup.ContainsKey(id))
                    {
                        lookup[id] = null;
                    }
                };

                Action <EntityType, string> addTitle = (entityType, field) =>
                {
                    Dictionary <string, string> lookup;
                    if (!titles.TryGetValue(entityType, out lookup))
                    {
                        lookup             = new Dictionary <string, string>();
                        titles[entityType] = lookup;
                    }

                    if (!lookup.ContainsKey(field))
                    {
                        lookup[field] = null;
                    }
                };

                Action <EntityType> addEntity = (entityType) =>
                {
                    if (!entities.ContainsKey(entityType))
                    {
                        //Row r;
                        String s = null;
                        // TODO: FIX!
                        //if (schema.TypeToTable.TryGetValue(entityType, out r))
                        //    s = LocalText.TryGet(1055, "Db." + r.Table + ".EntitySingular", false);
                        s = s ?? Enum.GetName(typeof(EntityType), entityType);
                        entities[entityType] = s;
                    }
                };

                Action <EntityType, string, EntityType> addForeign = (entityType, field, foreignType) =>
                {
                    Dictionary <string, string> foreign;
                    if (!foreigns.TryGetValue(entityType, out foreign))
                    {
                        foreign = new Dictionary <string, string>();
                        foreigns[entityType] = foreign;
                    }

                    if (!foreign.ContainsKey(field))
                    {
                        foreign[field] = Enum.GetName(typeof(EntityType), foreignType);
                    }
                };

                foreach (var entity in response.Entities)
                {
                    addEntity(fld.EntityTypeIdField[entity]);
                    addLookup(fld.EntityTypeIdField[entity], fld.EntityIdField[entity].Value);
                    if (fld.ParentTypeIdField[entity] != null)
                    {
                        addEntity(fld.ParentTypeIdField[entity]);
                    }

                    //if (entity.UserId != null)
                    //    addLookup(UserRow.TableName, entity.UserId.Value);

                    Row theRow;
                    if (((AuditType?)fld.AuditTypeIdField[entity] == AuditType.Insert ||
                         (AuditType?)fld.AuditTypeIdField[entity] == AuditType.Update) &&
                        (fld.OldAuditDataField[entity] != null || fld.NewAuditDataField[entity] != null))
                    {
                        theRow = RowRegistry.GetSchemaRow(RowRegistry.DefaultSchema, fld.EntityTypeIdField[entity]);
                        if (theRow == null)
                        {
                            continue;
                        }

                        UpdateAuditDataDictionary ud = new UpdateAuditDataDictionary();
                        if (fld.OldAuditDataField[entity] != null)
                        {
                            ud.Old = JsonConvert.DeserializeObject <Dictionary <string, object> >(fld.OldAuditDataField[entity].TrimToNull() ?? "{}", JsonSettings.Tolerant);
                        }

                        if (fld.NewAuditDataField[entity] != null)
                        {
                            ud.New = JsonConvert.DeserializeObject <Dictionary <string, object> >(fld.OldAuditDataField[entity].TrimToNull() ?? "{}", JsonSettings.Tolerant);
                        }

                        for (var i = 0; i < 2; i++)
                        {
                            var d = (i == 0) ? ud.Old : ud.New;
                            if (d != null)
                            {
                                foreach (var p in d)
                                {
                                    addTitle(fld.EntityTypeIdField[entity], p.Key);

                                    if (p.Value != null &&
                                        p.Value is Int16 ||
                                        p.Value is Int32 ||
                                        p.Value is Int64)
                                    {
                                        var f = theRow.FindField(p.Key);
                                        if (f != null &&
                                            f.ForeignTable != null)
                                        {
                                            //EntityType foreignType;
                                            //if (schema.TableToType.TryGetValue(f.ForeignTable, out foreignType))
                                            {
                                                addForeign(fld.EntityTypeIdField[entity], p.Key, f.ForeignTable);
                                                addLookup(f.ForeignTable, Convert.ToInt64(p.Value));
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                foreach (var pair in response.IdNameLookups)
                {
                    Row entity = RowRegistry.GetSchemaRow(RowRegistry.DefaultSchema, pair.Key);
                    if (entity != null)
                    {
                        var idRow   = entity as IIdRow;
                        var nameRow = entity as INameRow;
                        if (idRow != null &&
                            nameRow != null)
                        {
                            var lookup = pair.Value;
                            var idName = GetIdNameDictionary(connection, (IIdRow)entity, ((INameRow)entity).NameField, lookup.Keys);
                            foreach (var p in idName)
                            {
                                lookup[p.Key] = p.Value;
                            }
                        }
                    }
                }

                foreach (var pair in response.FieldTitles)
                {
                    Row entity = RowRegistry.GetSchemaRow(RowRegistry.DefaultSchema, pair.Key);
                    if (entity != null)
                    {
                        var lookup = pair.Value;
                        var keys   = new string[lookup.Keys.Count];
                        lookup.Keys.CopyTo(keys, 0);
                        foreach (var key in keys)
                        {
                            Field f;
                            if (key.EndsWith("Id"))
                            {
                                var s = key.Substring(0, key.Length - 2);
                                f = entity.FindField(s);
                                if (f != null)
                                {
                                    lookup[key] = f.Title;
                                    continue;
                                }
                            }

                            f = entity.FindField(key);
                            if (f != null)
                            {
                                lookup[key] = f.Title;
                            }
                        }
                    }
                }


                return(response);
            }
        }
Exemplo n.º 2
0
        public static AuditLogListResponse List(string schema, AuditLogListRequest request)
        {
#if COREFX
            var fld = Dependency.Resolve<IAuditLogRow>();
#else
            var fld = Dependency.Resolve<IAuditLogRow>(schema);
#endif

            Authorization.ValidateLoggedIn();

            var response = new AuditLogListResponse();

            using (var connection = SqlConnections.NewByKey("Default"))
            {
                response.Entities = new List<Row>();

                var row = ((Row)fld).CreateNew();

                if (request.Sort == null ||
                    request.Sort.Length == 0)
                    request.Sort = new SortBy[] { new SortBy(fld.DateField.Name, true) };

                var query = new SqlQuery().From(row)
                    .Select(
                        (Field)fld.IdField,
                        fld.EntityTypeIdField,
                        fld.EntityIdField,
                        fld.ParentTypeIdField,
                        fld.OldParentIdField,
                        fld.NewParentIdField,
                        fld.DateField,
                        (Field)fld.UserIdField,
                        fld.AuditTypeIdField,
                        fld.OldAuditDataField,
                        fld.NewAuditDataField)
                    .OrderBy(
                        (Field)fld.IdField)
                    .ApplySkipTakeAndCount(request.Skip, request.Take, request.ExcludeTotalCount)
                    .ApplySort(request.Sort);

                if (request.EntityTypeId != null &&
                    request.EntityId != null)
                {
                    var pEntityId = query.AddParam(request.EntityId);
                    var pEntityTypeId = query.AddParam(request.EntityTypeId);

                    query.Where(~(
                        ~(
                            new Criteria(0, fld.EntityTypeIdField) == pEntityTypeId &
                            new Criteria(0, fld.EntityIdField) == pEntityId) |
                        ~(
                            new Criteria(0, fld.ParentTypeIdField) == pEntityTypeId &
                            ~(
                                new Criteria(0, fld.OldParentIdField) == pEntityId |
                                new Criteria(0, fld.NewParentIdField) == pEntityId))));
                }
                else
                {
                    if (request.EntityTypeId != null)
                        query.WhereEqual(fld.EntityTypeIdField, request.EntityTypeId);//Convert.ToInt32(request.EntityTypeId.Value));

                    if (request.EntityId != null)
                        query.WhereEqual(fld.EntityIdField, request.EntityId.Value);
                }

                response.TotalCount = query.ForEach(connection, delegate()
                {
                    response.Entities.Add(row.Clone());
                });

                response.SetSkipTakeTotal(query);

                response.IdNameLookups = new Dictionary<EntityType, Dictionary<long, string>>();
                response.FieldTitles = new Dictionary<EntityType, Dictionary<string, string>>();
                response.ForeignEntityTypes = new Dictionary<EntityType, Dictionary<string, string>>();
                response.EntityTitles = new Dictionary<EntityType, string>();

                var lookups = response.IdNameLookups;
                var titles = response.FieldTitles;
                var foreigns = response.ForeignEntityTypes;
                var entities = response.EntityTitles;

                Action<EntityType, Int64> addLookup = (entityType, id) => {

                    Dictionary<long, string> lookup;
                    if (!lookups.TryGetValue(entityType, out lookup))
                    {
                        lookup = new Dictionary<long, string>();
                        lookups[entityType] = lookup;
                    }

                    if (!lookup.ContainsKey(id))
                        lookup[id] = null;
                };

                Action<EntityType, string> addTitle = (entityType, field) =>
                {

                    Dictionary<string, string> lookup;
                    if (!titles.TryGetValue(entityType, out lookup))
                    {
                        lookup = new Dictionary<string, string>();
                        titles[entityType] = lookup;
                    }

                    if (!lookup.ContainsKey(field))
                        lookup[field] = null;
                };

                Action<EntityType> addEntity = (entityType) =>
                {
                    if (!entities.ContainsKey(entityType))
                    {
                        //Row r;
                        String s = null;
                        // TODO: FIX!
                        //if (schema.TypeToTable.TryGetValue(entityType, out r))
                        //    s = LocalText.TryGet(1055, "Db." + r.Table + ".EntitySingular", false);
                         s = s ?? Enum.GetName(typeof(EntityType), entityType);
                        entities[entityType] = s;
                    }
                };

                Action<EntityType, string, EntityType> addForeign = (entityType, field, foreignType) =>
                {
                    Dictionary<string, string> foreign;
                    if (!foreigns.TryGetValue(entityType, out foreign))
                    {
                        foreign = new Dictionary<string, string>();
                        foreigns[entityType] = foreign;
                    }

                    if (!foreign.ContainsKey(field))
                        foreign[field] = Enum.GetName(typeof(EntityType), foreignType);
                };

                foreach (var entity in response.Entities)
                {
                    addEntity(fld.EntityTypeIdField[entity]);
                    addLookup(fld.EntityTypeIdField[entity], fld.EntityIdField[entity].Value);
                    if (fld.ParentTypeIdField[entity] != null)
                        addEntity(fld.ParentTypeIdField[entity]);

                    //if (entity.UserId != null)
                    //    addLookup(UserRow.TableName, entity.UserId.Value);

                    Row theRow;
                    if (((AuditType?)fld.AuditTypeIdField[entity] == AuditType.Insert ||
                         (AuditType?)fld.AuditTypeIdField[entity] == AuditType.Update) &&
                        (fld.OldAuditDataField[entity] != null || fld.NewAuditDataField[entity] != null))
                    {
                        theRow = RowRegistry.GetConnectionRow(RowRegistry.DefaultConnectionKey, fld.EntityTypeIdField[entity]);
                        if (theRow == null)
                            continue;

                        UpdateAuditDataDictionary ud = new UpdateAuditDataDictionary();
                        if (fld.OldAuditDataField[entity] != null)
                            ud.Old = JsonConvert.DeserializeObject<Dictionary<string, object>>(fld.OldAuditDataField[entity].TrimToNull() ?? "{}", JsonSettings.Tolerant);

                        if (fld.NewAuditDataField[entity] != null)
                            ud.New = JsonConvert.DeserializeObject<Dictionary<string, object>>(fld.OldAuditDataField[entity].TrimToNull() ?? "{}", JsonSettings.Tolerant);

                        for (var i = 0; i < 2; i++)
                        {
                            var d = (i == 0) ? ud.Old : ud.New;
                            if (d != null)
                                foreach (var p in d)
                                {
                                    addTitle(fld.EntityTypeIdField[entity], p.Key);

                                    if (p.Value != null &&
                                        p.Value is Int16 ||
                                        p.Value is Int32 ||
                                        p.Value is Int64)
                                    {
                                        var f = theRow.FindField(p.Key);
                                        if (!ReferenceEquals(null, f) &&
                                            f.ForeignTable != null)
                                        {
                                            //EntityType foreignType;
                                            //if (schema.TableToType.TryGetValue(f.ForeignTable, out foreignType))
                                            {
                                                addForeign(fld.EntityTypeIdField[entity], p.Key, f.ForeignTable);
                                                addLookup(f.ForeignTable, Convert.ToInt64(p.Value));
                                            }
                                        }
                                    }
                                }
                        }
                    }
                }

                foreach (var pair in response.IdNameLookups)
                {
                    Row entity = RowRegistry.GetConnectionRow(RowRegistry.DefaultConnectionKey, pair.Key);
                    if (entity != null)
                    {
                        var idRow = entity as IIdRow;
                        var nameRow = entity as INameRow;
                        if (idRow != null &&
                            nameRow != null)
                        {
                            var lookup = pair.Value;
                            var idName = GetIdNameDictionary(connection, (IIdRow)entity, ((INameRow)entity).NameField, lookup.Keys);
                            foreach (var p in idName)
                                lookup[p.Key] = p.Value;
                        }
                    }
                }

                foreach (var pair in response.FieldTitles)
                {
                    Row entity = RowRegistry.GetConnectionRow(RowRegistry.DefaultConnectionKey, pair.Key);
                    if (entity != null)
                    {
                        var lookup = pair.Value;
                        var keys = new string[lookup.Keys.Count];
                        lookup.Keys.CopyTo(keys, 0);
                        foreach (var key in keys)
                        {
                            Field f;
                            if (key.EndsWith("Id"))
                            {
                                var s = key.Substring(0, key.Length - 2);
                                f = entity.FindField(s);
                                if (!ReferenceEquals(null, f))
                                {
                                    lookup[key] = f.Title;
                                    continue;
                                }
                            }

                            f = entity.FindField(key);
                            if (!ReferenceEquals(null, f))
                                lookup[key] = f.Title;
                        }
                    }
                }


                return response;
            }
        }