public static void Import( [CanBeNull] string importWhereClause, [NotNull] IList <IObjectClass> targetExceptionClasses, [NotNull] IList <IObjectClass> importExceptionClasses, [NotNull] string importOriginValue, DateTime importDate) { IIssueTableFields importFields = GetImportFields(importExceptionClasses); IIssueTableFields targetFields = GetTargetFields(targetExceptionClasses); IDictionary <Guid, QualityConditionExceptions> targetExceptionsByConditionVersion; using (_msg.IncrementIndentation( "Reading existing exceptions in target workspace")) { targetExceptionsByConditionVersion = ReadTargetExceptions(targetExceptionClasses, targetFields); } var replacedExceptionObjects = new Dictionary <esriGeometryType, HashSet <int> >(); using (_msg.IncrementIndentation("Importing new exceptions from issue datasets...") ) { foreach (ITable importTable in importExceptionClasses.Cast <ITable>()) { using (_msg.IncrementIndentation("from {0}...", DatasetUtils.GetName(importTable))) { ITable targetTable = GetTargetTable(importTable, targetExceptionClasses); if (targetTable == null) { _msg.Warn( "No matching table in target workspace, ignoring import table"); continue; } var factory = new ExceptionObjectFactory( importTable, importFields, defaultStatus: ExceptionObjectStatus.Inactive); var newCount = 0; var updateCount = 0; var ignoredCount = 0; using (var writer = new ExceptionWriter(importTable, importFields, targetTable, targetFields)) { foreach (IRow row in GdbQueryUtils.GetRows(importTable, GetQueryFilter( importWhereClause), recycle: true)) { int matchCount; bool added = ImportException(row, importOriginValue, importDate, factory, targetExceptionsByConditionVersion, replacedExceptionObjects, writer, out matchCount); if (!added) { ignoredCount++; } else if (matchCount == 0) { newCount++; } else { updateCount++; } } } _msg.InfoFormat("{0:N0} exception(s) imported as new", newCount); _msg.InfoFormat("{0:N0} exception(s) imported as updates", updateCount); if (ignoredCount > 0) { _msg.InfoFormat("{0:N0} exception(s) ignored", ignoredCount); } } } } using (_msg.IncrementIndentation("Processing replaced exceptions...")) { foreach (ITable targetTable in targetExceptionClasses.Cast <ITable>()) { using (_msg.IncrementIndentation("Target table {0}...", DatasetUtils.GetName(targetTable))) { int fixedStatusCount; int updateCount = ProcessReplacedExceptions(targetTable, targetFields, replacedExceptionObjects, importDate, out fixedStatusCount); _msg.InfoFormat("{0:N0} replaced exception(s) updated", updateCount); if (fixedStatusCount > 0) { _msg.InfoFormat("Status value of {0:N0} old exception version(s) fixed", fixedStatusCount); } } } } }
private static bool ImportException( [NotNull] IRow row, [NotNull] string importOriginValue, DateTime importDate, [NotNull] ExceptionObjectFactory factory, [NotNull] IDictionary <Guid, QualityConditionExceptions> targetExceptionsByConditionVersion, [NotNull] IDictionary <esriGeometryType, HashSet <int> > replacedExceptionObjects, [NotNull] ExceptionWriter writer, out int matchCount) { ExceptionObject exceptionObject = factory.CreateExceptionObject(row); matchCount = 0; // import only active exceptions if (exceptionObject.Status != ExceptionObjectStatus.Active) { return(false); } var matchingLineageUuids = new DistinctValues <Guid>(); var origins = new HashSet <string>(StringComparer.OrdinalIgnoreCase) { importOriginValue }; foreach (ExceptionObject matchingExceptionObject in GetMatchingExceptionObjects( targetExceptionsByConditionVersion, exceptionObject)) { // add to Uuids of matching exceptions (usually, should be unique) if (matchingExceptionObject.ManagedLineageUuid != null) { // also if matching exception is inactive // --> lineage of inactive exceptions may be continued (resurrection of inactive exceptions) matchingLineageUuids.Add(matchingExceptionObject.ManagedLineageUuid.Value); // NOTE: consider *preferring* active exceptions, only if none: resurrect inactive exception lineage } // include the origin values of active replaced exceptions // Note: not for resurrected exceptions (inactive/with end date -> active) if (matchingExceptionObject.ManagedVersionEndDate == null) { foreach (string replacedOrigin in ExceptionObjectUtils.ParseOrigins(matchingExceptionObject.ManagedOrigin)) { origins.Add(replacedOrigin); } } matchCount++; AddToReplacedExceptionObjects(matchingExceptionObject, replacedExceptionObjects); } writer.Write(row, importDate, ExceptionObjectUtils.FormatOrigins(origins), GetLineageUuid(matchingLineageUuids), versionOriginValue: importOriginValue, statusValue: "Active"); return(true); }
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); } } } } } }