Пример #1
0
        private static ExceptionUpdateDetector GetUpdateDetector(
            [NotNull] ITable targetTable,
            [NotNull] ManagedExceptionVersionFactory targetExceptionVersionFactory,
            [NotNull] IEnumerable <IssueAttribute> editableAttributes)
        {
            var result = new ExceptionUpdateDetector(editableAttributes);

            foreach (IRow targetRow in GdbQueryUtils.GetRows(targetTable, recycle: true))
            {
                result.AddExistingException(
                    targetExceptionVersionFactory.CreateExceptionVersion(targetRow));
            }

            return(result);
        }
Пример #2
0
        public static void Update(
            [CanBeNull] string whereClause,
            [NotNull] IList <IObjectClass> targetExceptionClasses,
            [NotNull] IList <IObjectClass> updateExceptionClasses,
            [NotNull] string updateOriginValue,
            DateTime updateDate,
            bool requireOriginalVersionExists = true)
        {
            IIssueTableFields updateFields = GetUpdateFields(updateExceptionClasses);
            IIssueTableFields targetFields = GetTargetFields(targetExceptionClasses);

            var editableAttributes = new[]
            {
                IssueAttribute.ExceptionStatus,
                IssueAttribute.ExceptionCategory,
                IssueAttribute.ExceptionNotes,
                IssueAttribute.ExceptionOrigin,
                IssueAttribute.ExceptionDefinitionDate,
                IssueAttribute.ExceptionLastRevisionDate,
                IssueAttribute.ExceptionRetirementDate,
                IssueAttribute.IssueAssignment
            };

            using (_msg.IncrementIndentation(
                       "Updating exceptions based on exported exception datasets..."))
            {
                foreach (ITable updateTable in updateExceptionClasses.Cast <ITable>())
                {
                    using (_msg.IncrementIndentation("from {0}...",
                                                     DatasetUtils.GetName(updateTable)))
                    {
                        ITable targetTable = GetTargetTable(updateTable, targetExceptionClasses);
                        if (targetTable == null)
                        {
                            _msg.Warn(
                                "No matching table in target workspace, ignoring import table");
                            continue;
                        }

                        var targetExceptionFactory = new ManagedExceptionVersionFactory(
                            targetTable, targetFields, editableAttributes);
                        var updateExceptionFactory = new ManagedExceptionVersionFactory(
                            updateTable, updateFields, editableAttributes);

                        ExceptionUpdateDetector updateDetector = GetUpdateDetector(
                            targetTable,
                            targetExceptionFactory,
                            editableAttributes);

                        var replacedOids = new HashSet <int>();

                        var updatedRowsCount       = 0;
                        var rowsWithConflictsCount = 0;

                        using (var exceptionWriter = new ExceptionWriter(updateTable, updateFields,
                                                                         targetTable, targetFields))
                        {
                            foreach (IRow updateRow in GdbQueryUtils.GetRows(
                                         updateTable, GetQueryFilter(whereClause), recycle: true))
                            {
                                ManagedExceptionVersion updateExceptionVersion =
                                    updateExceptionFactory.CreateExceptionVersion(updateRow);

                                ManagedExceptionVersion            mergedException;
                                ManagedExceptionVersion            replacedExceptionVersion;
                                IList <ExceptionAttributeConflict> conflicts;

                                if (updateDetector.HasChange(updateExceptionVersion,
                                                             out mergedException,
                                                             out replacedExceptionVersion,
                                                             out conflicts))
                                {
                                    if (replacedExceptionVersion == null)
                                    {
                                        string message = string.Format(
                                            "Exception version {0} not found in lineage {1} (target table: {2})",
                                            ExceptionObjectUtils.FormatGuid(
                                                updateExceptionVersion.VersionUuid),
                                            ExceptionObjectUtils.FormatGuid(
                                                updateExceptionVersion.LineageUuid),
                                            DatasetUtils.GetName(targetTable));

                                        if (requireOriginalVersionExists)
                                        {
                                            throw new InvalidDataException(message);
                                        }

                                        _msg.WarnFormat(
                                            "{0}. Creating new version with attributes from update row.",
                                            message);
                                    }

                                    updatedRowsCount++;

                                    string versionImportStatus;
                                    if (conflicts.Count == 0)
                                    {
                                        versionImportStatus = null;
                                    }
                                    else
                                    {
                                        versionImportStatus =
                                            FormatConflicts(conflicts, targetFields);

                                        rowsWithConflictsCount++;

                                        LogConflicts(conflicts, targetFields);
                                    }

                                    exceptionWriter.Write(updateRow, updateDate, mergedException,
                                                          FormatOriginValue(updateOriginValue,
                                                                            replacedExceptionVersion),
                                                          updateOriginValue, versionImportStatus);

                                    if (replacedExceptionVersion != null)
                                    {
                                        replacedOids.Add(replacedExceptionVersion.ObjectID);
                                    }
                                }
                            }
                        }

                        _msg.InfoFormat("{0:N0} exception(s) updated", updatedRowsCount);
                        if (rowsWithConflictsCount > 0)
                        {
                            _msg.WarnFormat("{0:N0} exception(s) with conflicts",
                                            rowsWithConflictsCount);
                        }

                        if (replacedOids.Count > 0)
                        {
                            int fixedStatusCount;
                            int updateCount = ProcessReplacedExceptions(targetTable, targetFields,
                                                                        updateDate, replacedOids,
                                                                        out fixedStatusCount);

                            _msg.DebugFormat("{0:N0} replaced exception version(s) updated",
                                             updateCount);
                            if (fixedStatusCount > 0)
                            {
                                _msg.WarnFormat(
                                    "Status value of {0:N0} old exception version(s) fixed",
                                    fixedStatusCount);
                            }
                        }
                    }
                }
            }
        }