protected virtual void DoGenericAudit()
        {
            var auditFields = new HashSet <Field>();

            GetEditableFields(auditFields);

            var auditRequest = GetAuditRequest(auditFields);

            if (IsUpdate)
            {
                Row audit = null;
                if (auditRequest != null)
                {
                    audit = AuditLogService.PrepareAuditUpdate(RowRegistry.GetConnectionKey(Row), auditRequest);
                }

                if (audit != null)
                {
                    Connection.Insert(audit);
                }
            }
            else if (IsCreate)
            {
                if (auditRequest != null)
                {
                    AuditLogService.AuditInsert(Connection, RowRegistry.GetConnectionKey(Row), auditRequest);
                }
            }
        }
Exemplo n.º 2
0
        protected virtual void DoGenericAudit()
        {
            var auditRequest = GetAuditRequest();

            if (auditRequest != null)
            {
                AuditLogService.AuditUndelete(Connection, RowRegistry.GetSchemaName(Row), auditRequest);
            }
        }
Exemplo n.º 3
0
        public override void OnAudit(IDeleteRequestHandler handler)
        {
            var auditRequest = GetAuditDeleteRequest(handler);

            if (auditRequest != null)
            {
                AuditLogService.AuditDelete(handler.UnitOfWork.Connection, RowRegistry.GetConnectionKey(handler.Row), auditRequest);
            }
        }
Exemplo n.º 4
0
        private void OnStartButtonClicked()
        {
            var players = RowRegistry.GetValidatedPlayers();

            if (players == null || players.Count < 2)
            {
                ModalDialog.Show("<color=red>Error!</color>\nNot all values are filled.");
                return;
            }
            Info.Players = players;
            SceneLoader.LoadScene("GameBoard", LoadSceneMode.Single, container =>
            {
                container.BindInstance(Info).WhenInjectedInto <GameBoardInstaller>();
            });
        }
Exemplo n.º 5
0
        public static void Initialize(string languageID = LocalText.InvariantLanguageID, ILocalTextRegistry registry = null)
#endif
        {
            var provider = registry ?? Dependency.Resolve <ILocalTextRegistry>();

            foreach (var row in RowRegistry.EnumerateRows())
            {
                var fields = row.GetFields();
                var prefix = fields.LocalTextPrefix;

                foreach (var field in row.GetFields())
                {
                    LocalText lt = field.Caption;
                    if (lt != null &&
                        !lt.Key.IsEmptyOrNull())
                    {
                        var initialized = lt as InitializedLocalText;
                        if (initialized != null)
                        {
                            provider.Add(languageID, initialized.Key, initialized.InitialText);
                        }
                        else
                        {
                            if (!lt.Key.StartsWith("Db."))
                            {
                                var key = "Db." + prefix + "." + (field.PropertyName ?? field.Name);
                                provider.Add(languageID, key, lt.Key);
                                field.Caption = new InitializedLocalText(key, lt.Key);
                            }
                        }
                    }
                }

                var displayName = row.GetType().GetCustomAttribute <DisplayNameAttribute>();
                if (displayName != null)
                {
                    provider.Add(languageID, "Db." + prefix + ".EntityPlural", displayName.DisplayName);
                }

                var instanceName = row.GetType().GetCustomAttribute <InstanceNameAttribute>();
                if (instanceName != null)
                {
                    provider.Add(languageID, "Db." + prefix + ".EntitySingular", instanceName.InstanceName);
                }
            }
        }
Exemplo n.º 6
0
        static StaticInfo EnsureInfo()
        {
            var newInfo = info;

            if (newInfo != null)
            {
                return(newInfo);
            }

            var captureLogAttr = typeof(TRow).GetCustomAttribute <CaptureLogAttribute>(false);

            if (captureLogAttr == null || captureLogAttr.LogRow == null)
            {
                throw new InvalidOperationException(String.Format("{0} row type has no capture log attribute defined!", typeof(TRow).Name));
            }

            logConnectionKey = RowRegistry.GetConnectionKey(captureLogAttr.LogRow);
            var logRowInstance = (Row)Activator.CreateInstance(captureLogAttr.LogRow);

            var captureLogRow = logRowInstance as ICaptureLogRow;

            if (captureLogRow == null)
            {
                throw new InvalidOperationException(String.Format("Capture log table {0} doesn't implement ICaptureLogRow interface!",
                                                                  captureLogAttr.LogRow.FullName, logConnectionKey, typeof(TRow).Name));
            }

            newInfo = new StaticInfo();
            newInfo.logRowInstance       = logRowInstance;
            newInfo.captureLogInstance   = captureLogRow;
            newInfo.rowInstance          = new TRow();
            newInfo.rowFieldPrefixLength = PrefixHelper.DeterminePrefixLength(newInfo.rowInstance.EnumerateTableFields(), x => x.Name);
            newInfo.logFieldPrefixLength = PrefixHelper.DeterminePrefixLength(logRowInstance.EnumerateTableFields(), x => x.Name);
            var mappedIdField = captureLogAttr.MappedIdField ?? ((Field)newInfo.rowInstance.IdField).Name;

            newInfo.mappedIdField = ((Row)captureLogRow).FindField(mappedIdField) as IIdField;
            if (newInfo.mappedIdField == null)
            {
                throw new InvalidOperationException(String.Format("Can't locate capture log table mapped ID field for {0}!",
                                                                  ((Row)captureLogRow).Table));
            }

            info = newInfo;
            return(newInfo);
        }
Exemplo n.º 7
0
        protected virtual List <TRow> GetItems()
        {
            var list   = new List <TRow>();
            var loader = new TRow();

            var query = new SqlQuery()
                        .From(loader);

            PrepareQuery(query);
            ApplyOrder(query);

            using (var connection = SqlConnections.NewByKey(RowRegistry.GetConnectionKey(loader)))
            {
                query.ForEach(connection, delegate()
                {
                    list.Add(loader.Clone());
                });
            }

            return(list);
        }
Exemplo n.º 8
0
        public override void OnAudit(ISaveRequestHandler handler)
        {
            if (handler.Row == null)
            {
                return;
            }

            var auditRequest = GetAuditSaveRequest(handler);

            if (connectionKey == null)
            {
                connectionKey = RowRegistry.GetConnectionKey(handler.Row);
            }

            if (handler.IsCreate)
            {
                if (auditRequest != null)
                {
                    AuditLogService.AuditInsert(handler.UnitOfWork.Connection, connectionKey, auditRequest);
                }

                return;
            }

            Row audit = null;

            if (auditRequest != null)
            {
                audit = AuditLogService.PrepareAuditUpdate(connectionKey, auditRequest);
            }

            if (audit != null)
            {
                handler.UnitOfWork.Connection.Insert(audit);
            }
        }
Exemplo n.º 9
0
        public override void OnValidateRequest(ISaveRequestHandler handler)
        {
            base.OnValidateRequest(handler);

            var row      = handler.Row;
            var old      = handler.Old;
            var isUpdate = old == null;

            var parentIdRow = row as IParentIdRow;

            if (parentIdRow == null)
            {
                return;
            }

            var parentId = parentIdRow.ParentIdField[row];

            if (parentId == null)
            {
                return;
            }

            if (isUpdate && parentId == parentIdRow.ParentIdField[old])
            {
                return;
            }

            var parentIdField = (Field)parentIdRow.ParentIdField;

            if (parentIdField.ForeignTable.IsNullOrEmpty())
            {
                return;
            }

            var foreignRow = RowRegistry.GetConnectionRow(RowRegistry.GetConnectionKey(row),
                                                          parentIdField.ForeignTable);

            if (foreignRow == null)
            {
                return;
            }

            var idForeign = (IIdRow)foreignRow;

            if (idForeign == null)
            {
                return;
            }

            var isActiveForeign = (IIsActiveRow)foreignRow;

            if (isActiveForeign == null)
            {
                return;
            }

            ServiceHelper.CheckParentNotDeleted(handler.UnitOfWork.Connection, foreignRow.Table,
                                                query => query.Where(
                                                    new Criteria((Field)idForeign.IdField) == parentId.Value &
                                                    new Criteria(isActiveForeign.IsActiveField) < 0));
        }
Exemplo n.º 10
0
        public DbLookupScript(string name, string schema = null, bool authorize = false, string right = null, bool nonCached = false,
                              Func <IDbConnection, IEnumerable <TItem> > getItems = null)
        {
            _lookupParams = new Dictionary <string, object>();

            Row row = null;

            if (typeof(TItem).IsSubclassOf(typeof(Row)))
            {
                row = (Row)(object)(new TItem());
            }

            if (name == null)
            {
                if (row == null)
                {
                    throw new ArgumentNullException("name");
                }

                name = row.Table;
            }

            if (row != null)
            {
                Field field;

                var idRow = row as IIdRow;
                if (idRow != null)
                {
                    field   = ((Field)idRow.IdField);
                    IdField = field.PropertyName ?? field.Name;
                }

                var nameRow = row as INameRow;
                if (nameRow != null)
                {
                    field     = ((Field)nameRow.NameField);
                    TextField = field.PropertyName ?? field.Name;
                }

                var treeRow = row as IParentIdRow;
                if (treeRow != null)
                {
                    field         = ((Field)treeRow.ParentIdField);
                    ParentIdField = field.PropertyName ?? field.Name;
                }
            }

            _scriptName = "Lookup." + name;

            if (schema == null)
            {
                if (row != null)
                {
                    schema = RowRegistry.GetSchemaName(row);
                }
                else
                {
                    schema = RowRegistry.DefaultSchema;
                }
            }

            _schema = schema;

            _lookup    = name;
            _getItems  = getItems;
            _authorize = authorize;
            _right     = right;
            _nonCached = nonCached;

            if (row != null)
            {
                GlobalGenerationKey = row.GetFields().GenerationKey;
            }

            LocalExpiration  = CacheExpiration.Never;
            RemoteExpiration = CacheExpiration.OneDay;

            DynamicScriptManager.Register(this);
        }
Exemplo n.º 11
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);
            }
        }