Beispiel #1
0
        private bool IsEnabled()
        {
            if (localAttr == null)
            {
                return(false);
            }

            if (localRowInstance != null)
            {
                return(true);
            }

            localRowInstance = (ILocalizationRow)Activator.CreateInstance(localAttr.LocalizationRow);
            rowPrefixLength  = PrefixHelper.DeterminePrefixLength(BasedOnRow.EnumerateTableFields(),
                                                                  x => x.Name);
            localRowPrefixLength = PrefixHelper.DeterminePrefixLength(localRowInstance.EnumerateTableFields(),
                                                                      x => x.Name);
            mappedIdField = localRowInstance.FindField(localAttr.MappedIdField ?? BasedOnRow.IdField.Name);
            if (mappedIdField is null)
            {
                throw new InvalidOperationException(string.Format("Can't locate localization table mapped ID field for {0}!",
                                                                  localRowInstance.Table));
            }

            return(true);
        }
Beispiel #2
0
        static StaticInfo EnsureInfo()
        {
            var newInfo = info;

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

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

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

            schemaName = RowRegistry.GetSchemaName(typeof(TRow));
            var instance = RowRegistry.GetSchemaRow(schemaName, logTableAttr.LogTable);

            if (instance == null)
            {
                throw new InvalidOperationException(String.Format("Can't locate {0} capture log table in schema {1} for {2} row type!",
                                                                  logTableAttr.LogTable, schemaName, typeof(TRow).Name));
            }

            var captureLogRow = instance as ICaptureLogRow;

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

            if (!(captureLogRow is IIsActiveRow))
            {
                throw new InvalidOperationException(String.Format("Capture log table {0} doesn't implement IIsActiveRow interface!",
                                                                  logTableAttr.LogTable, schemaName, typeof(TRow).Name));
            }

            newInfo = new StaticInfo();
            newInfo.logRowInstance       = instance;
            newInfo.captureLogInstance   = captureLogRow;
            newInfo.rowInstance          = new TRow();
            newInfo.rowFieldPrefixLength = PrefixHelper.DeterminePrefixLength(newInfo.rowInstance.EnumerateTableFields(), x => x.Name);
            newInfo.logFieldPrefixLength = PrefixHelper.DeterminePrefixLength(instance.EnumerateTableFields(), x => x.Name);
            newInfo.mappedIdField        = ((Row)captureLogRow).FindField(logTableAttr.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);
        }
Beispiel #3
0
        static StaticInfo EnsureInfo()
        {
            var newInfo = info;

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

            var localAttr = typeof(TRow).GetCustomAttribute <LocalizationRowAttribute>(false);

            if (localAttr == null || localAttr.LocalizationTable.IsTrimmedEmpty())
            {
                throw new InvalidOperationException(String.Format("{0} row type has no localization attribute defined!", typeof(TRow).Name));
            }

            schemaName = RowRegistry.GetConnectionKey(typeof(TRow));
            var localInstance = RowRegistry.GetConnectionRow(schemaName, localAttr.LocalizationTable);

            if (localInstance == null)
            {
                throw new InvalidOperationException(String.Format("Can't locate {0} localization table in schema {1} for {2} row type!",
                                                                  localAttr.LocalizationTable, schemaName, typeof(TRow).Name));
            }

            var localRow = localInstance as ILocalizationRow;

            if (localRow == null)
            {
                throw new InvalidOperationException(String.Format("Localization table {0} doesn't implement ILocalizationRow interface!",
                                                                  localAttr.LocalizationTable, schemaName, typeof(TRow).Name));
            }


            newInfo = new StaticInfo();
            newInfo.localRowInterface         = localRow;
            newInfo.localRowInstance          = localInstance;
            newInfo.rowInstance               = new TRow();
            newInfo.rowFieldPrefixLength      = PrefixHelper.DeterminePrefixLength(newInfo.rowInstance.EnumerateTableFields(), x => x.Name);
            newInfo.localRowFieldPrefixLength = PrefixHelper.DeterminePrefixLength(localInstance.EnumerateTableFields(), x => x.Name);
            newInfo.mappedIdField             = (IIdField)((Row)localInstance).FindField(localAttr.MappedIdField);
            if (newInfo.mappedIdField == null)
            {
                throw new InvalidOperationException(String.Format("Can't locate localization table mapped ID field for {0}!",
                                                                  localInstance.Table));
            }

            info = newInfo;
            return(newInfo);
        }
Beispiel #4
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));
            }

            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, typeof(TRow).Name));
            }

            newInfo = new StaticInfo();
            newInfo.logRowInstance     = logRowInstance;
            newInfo.captureLogInstance = captureLogRow;
            newInfo.rowInstance        = new TRow();
            logConnectionKey           = newInfo.rowInstance.GetFields().ConnectionKey;

            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 Field;
            if (ReferenceEquals(null, newInfo.mappedIdField))
            {
                throw new InvalidOperationException(String.Format("Can't locate capture log table mapped ID field for {0}!",
                                                                  ((Row)captureLogRow).Table));
            }

            info = newInfo;
            return(newInfo);
        }
        static StaticInfo EnsureInfo()
        {
            var newInfo = info;

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

            var localAttr = typeof(TRow).GetCustomAttribute <LocalizationRowAttribute>(false);

            if (localAttr == null || localAttr.LocalizationRow == null)
            {
                throw new InvalidOperationException(String.Format("{0} row type has no localization row type defined!", typeof(TRow).Name));
            }

            var localInstance = (Row)Activator.CreateInstance(localAttr.LocalizationRow);

            var localRow = localInstance as ILocalizationRow;

            if (localRow == null)
            {
                throw new InvalidOperationException(String.Format("Localization table {0} doesn't implement ILocalizationRow interface!",
                                                                  localAttr.LocalizationRow.GetType().FullName, schemaName, typeof(TRow).Name));
            }

            schemaName = localInstance.GetFields().ConnectionKey;

            newInfo = new StaticInfo();
            newInfo.localRowInterface         = localRow;
            newInfo.localRowInstance          = localInstance;
            newInfo.rowInstance               = new TRow();
            newInfo.rowFieldPrefixLength      = PrefixHelper.DeterminePrefixLength(newInfo.rowInstance.EnumerateTableFields(), x => x.Name);
            newInfo.localRowFieldPrefixLength = PrefixHelper.DeterminePrefixLength(localInstance.EnumerateTableFields(), x => x.Name);
            newInfo.mappedIdField             = (IIdField)((Row)localInstance).FindField(localAttr.MappedIdField ?? ((Field) new TRow().IdField).Name);
            if (newInfo.mappedIdField == null)
            {
                throw new InvalidOperationException(String.Format("Can't locate localization table mapped ID field for {0}!",
                                                                  localInstance.Table));
            }

            info = newInfo;
            return(newInfo);
        }
Beispiel #6
0
        public void Log(IUnitOfWork uow, IRow old, IRow row, object userId)
        {
            if (old == null && row == null)
            {
                throw new ArgumentNullException("old");
            }

            var now         = DateTime.Now;
            var rowInstance = row ?? old;
            var rowType     = rowInstance.GetType();
            var logRow      = (Activator.CreateInstance(captureLogAttr.LogRow) as ICaptureLogRow) ??
                              throw new InvalidOperationException($"Capture log table {captureLogAttr.LogRow.FullName} " +
                                                                  $"for {rowType.FullName} doesn't implement ICaptureLogRow interface!");

            var logConnectionKey = logRow.Fields.ConnectionKey;

            var rowFieldPrefixLength = PrefixHelper.DeterminePrefixLength(rowInstance.EnumerateTableFields(), x => x.Name);
            var logFieldPrefixLength = PrefixHelper.DeterminePrefixLength(logRow.EnumerateTableFields(), x => x.Name);
            var mappedIdFieldName    = captureLogAttr.MappedIdField ?? rowInstance.IdField.Name;
            var mappedIdField        = logRow.FindField(mappedIdFieldName);

            if (mappedIdField is null)
            {
                throw new InvalidOperationException($"Can't locate capture log table " +
                                                    $"mapped ID field for {logRow.Table}!");
            }

            logRow.TrackAssignments = true;
            logRow.ChangingUserIdField.AsObject(logRow, userId == null ? null :
                                                logRow.ChangingUserIdField.ConvertValue(userId, CultureInfo.InvariantCulture));

            var operationType = old == null ? CaptureOperationType.Insert :
                                (row == null ? CaptureOperationType.Delete : CaptureOperationType.Before);

            logRow.OperationTypeField[logRow] = operationType;
            logRow.ValidFromField[logRow]     = now;

            IEnumerable <Tuple <Field, Field> > enumerateCapturedFields()
            {
                foreach (var logField in logRow.Fields)
                {
                    if (!logField.IsTableField())
                    {
                        continue;
                    }

                    if (ReferenceEquals(logRow.ChangingUserIdField, logField) ||
                        ReferenceEquals(logRow.ValidFromField, logField) ||
                        ReferenceEquals(logRow.ValidUntilField, logField) ||
                        ReferenceEquals(logRow.OperationTypeField, logField) ||
                        ReferenceEquals(logRow.IdField, logField))
                    {
                        continue;
                    }

                    if (ReferenceEquals(logField, mappedIdField))
                    {
                        yield return(new Tuple <Field, Field>(logField, rowInstance.IdField));
                    }
                    else
                    {
                        var name = logField.Name[logFieldPrefixLength..];
        public bool ActivateFor(Row row)
        {
            attr = row.GetType().GetCustomAttribute <LocalizationRowAttribute>();
            if (attr == null)
            {
                return(false);
            }

            localRowType = attr.LocalizationRow;
            if (!typeof(ILocalizationRow).IsAssignableFrom(localRowType))
            {
                throw new ArgumentException(String.Format(
                                                "Row type '{0}' has a LocalizationRowAttribute, " +
                                                "but its localization row type ('{1}') doesn't implement ILocalizationRow interface!",
                                                row.GetType().FullName, localRowType.FullName));
            }

            if (!typeof(IIdRow).IsAssignableFrom(localRowType))
            {
                throw new ArgumentException(String.Format(
                                                "Row type '{0}' has a LocalizationRowAttribute, " +
                                                "but its localization row type ('{1}') doesn't implement IIdRow interface!",
                                                row.GetType().FullName, localRowType.FullName));
            }

            if (!(row is IIdRow))
            {
                throw new ArgumentException(String.Format(
                                                "Row type '{0}' has a LocalizationRowAttribute, " +
                                                "but row type itself doesn't implement IIdRow interface!",
                                                row.GetType().FullName));
            }

            rowFactory      = FastReflection.DelegateForConstructor <Row>(row.GetType());
            localRowFactory = FastReflection.DelegateForConstructor <Row>(localRowType);

            var localRow = localRowFactory();

            localRowInstance = localRow;

            rowPrefixLength      = PrefixHelper.DeterminePrefixLength(row.EnumerateTableFields(), x => x.Name);
            localRowPrefixLength = PrefixHelper.DeterminePrefixLength(localRow.EnumerateTableFields(), x => x.Name);
            localRowIdField      = (Field)(((IIdRow)localRow).IdField);
            cultureIdField       = ((ILocalizationRow)localRow).CultureIdField;

            var foreignKeyFieldName = attr.MappedIdField ?? ((Field)((IIdRow)row).IdField).PropertyName;

            foreignKeyField = localRow.FindFieldByPropertyName(foreignKeyFieldName) ??
                              localRow.FindField(foreignKeyFieldName);

            if (ReferenceEquals(null, foreignKeyField))
            {
                throw new ArgumentException(String.Format(
                                                "Row type '{0}' has a LocalizationRowAttribute, " +
                                                "but its localization row type ('{1}') doesn't have a field with name '{2}'!",
                                                row.GetType().FullName, localRowType.FullName, foreignKeyFieldName));
            }

            dictionaryFactory = FastReflection.DelegateForConstructor <IDictionary>(
                typeof(Dictionary <,>).MakeGenericType(typeof(string), row.GetType()));

            this.foreignKeyCriteria = new Criteria(foreignKeyField.PropertyName ?? foreignKeyField.Name);
            return(true);
        }
        public bool ActivateFor(IRow row)
        {
            attr = row.GetType().GetCustomAttribute <LocalizationRowAttribute>();
            if (attr == null)
            {
                return(false);
            }

            localRowType = attr.LocalizationRow;
            if (!typeof(ILocalizationRow).IsAssignableFrom(localRowType))
            {
                throw new ArgumentException(string.Format(
                                                "Row type '{0}' has a LocalizationRowAttribute, " +
                                                "but its localization row type ('{1}') doesn't implement ILocalizationRow interface!",
                                                row.GetType().FullName, localRowType.FullName));
            }

            if (!typeof(IIdRow).IsAssignableFrom(localRowType))
            {
                throw new ArgumentException(string.Format(
                                                "Row type '{0}' has a LocalizationRowAttribute, " +
                                                "but its localization row type ('{1}') doesn't implement IIdRow interface!",
                                                row.GetType().FullName, localRowType.FullName));
            }

            if (!(row is IIdRow))
            {
                throw new ArgumentException(string.Format(
                                                "Row type '{0}' has a LocalizationRowAttribute, " +
                                                "but row type itself doesn't implement IIdRow interface!",
                                                row.GetType().FullName));
            }

            var rowType = row.GetType();

            rowFactory      = () => (IIdRow)Activator.CreateInstance(rowType);
            localRowFactory = () => (ILocalizationRow)Activator.CreateInstance(localRowType);

            var localRow = localRowFactory();

            localRowInstance = localRow;

            rowPrefixLength = PrefixHelper.DeterminePrefixLength(row.EnumerateTableFields(),
                                                                 x => x.Name);
            localRowPrefixLength = PrefixHelper.DeterminePrefixLength(localRow.EnumerateTableFields(),
                                                                      x => x.Name);
            localRowIdField = localRow.IdField;
            cultureIdField  = localRow.CultureIdField;

            var foreignKeyFieldName = attr.MappedIdField ?? row.IdField.PropertyName;

            foreignKeyField = localRow.FindFieldByPropertyName(foreignKeyFieldName) ??
                              localRow.FindField(foreignKeyFieldName);

            if (foreignKeyField is null)
            {
                throw new ArgumentException(string.Format(
                                                "Row type '{0}' has a LocalizationRowAttribute, " +
                                                "but its localization row type ('{1}') doesn't have a field with name '{2}'!",
                                                row.GetType().FullName, localRowType.FullName, foreignKeyFieldName));
            }

            var dictionaryType = typeof(Dictionary <,>).MakeGenericType(typeof(string), row.GetType());

            dictionaryFactory = () => (IDictionary)Activator.CreateInstance(dictionaryType);

            foreignKeyCriteria = new Criteria(foreignKeyField.PropertyName ?? foreignKeyField.Name);
            return(true);
        }