ArcFM Engine Developer Guide
LogToReconcileHistory.cs

Resource Center Home

Action Handler Developer Sample (C#)

LogToReconcileHistory.cs

 

Copy Code
using System;
using System.Collections.Generic;
using ESRI.ArcGIS.Geodatabase;
using Miner.Geodatabase.GeodatabaseManager;
using Miner.Geodatabase.GeodatabaseManager.ActionHandlers;
using Miner.Geodatabase.GeodatabaseManager.Serialization;
using Miner.Interop;
namespace Miner.DeveloperSamples.GeodatabaseManager
{
    /// <summary>
    /// Sample action handler for writing information about the reconcile process.
    /// </summary>
    public class LogToReconcileHistory : ActionHandler
    {
        #region constants
        /// <summary>
        /// Constants specific to the Reconcile History table.
        /// </summary>
        private const string TableReconcileHistory = "GDBM_RECONCILE_HISTORY";
        private const string ReconcileHistoryId = "ReconcileHistoryID";
        private const string BeforeReconcileAction = "Before Reconcile";
        private const string AfterReconcileAction = "After Reconcile";
        private const string VersionNameField = "VERSION_NAME";
        private const string StartDateField = "RECONCILE_START_DT";
        private const string ServiceNameField = "SERVICE_NAME";
        private const string EndDateField = "RECONCILE_END_DT";
        private const string ResultField = "RECONCILE_RESULT";
        private const string ReconciledResult = "Reconciled";
        private const string ConflictsResult = "Conflicts";
        #endregion
        #region constructor
        /// <summary>
        /// Constructor for the action handler.  Sets the name and description of the action handler.
        /// </summary>
        public LogToReconcileHistory()
        {
            this.Name = "SAMPLE: Log To Reconcile History";
            this.Description = "Inserts or updates a row in the Reconcile History table.";
        }
        #endregion
        #region ActionHandler overrides
        /// <summary>
        /// Determines when this action handler is enabled.  This method is used to determine when the action
        /// handler appears in the list of available action handlers.
        /// </summary>
        /// <param name="actionType">Indicates context, whether in the reconcile or reconcile/post sequence.</param>
        /// <param name="gdbmAction">The particular event to which the action handler might be assigned.</param>
        /// <param name="versionType">If the context is reconcile, the versionType will be Any; If in
        /// the posting sequence, versionType distinguishes between Plain Versions, Sessions and Designs.</param>
        /// <returns>True if enabled, false otherwise.</returns>
        /// <remarks>For best results, this action handler should be assigned to both BeforeReconcile and AfterReconcile
        /// events.  This method helps to enforce that.</remarks>
        public override bool Enabled(ActionType actionType, Actions gdbmAction, PostVersionType versionType)
        {
            bool enabled = false;
            if (gdbmAction == Actions.BeforeReconcile || gdbmAction == Actions.AfterReconcile)
            {
                enabled = true;
            }
            return enabled;
        }
        /// <summary>
        /// Writes values to the Reconcile History table when the BeforeReconcile and AfterReconcile events
        /// are fired.
        /// </summary>
        /// <param name="actionData">A dictionary object containing information about the version being reconciled
        /// and how it is to be done (eg. child wins).</param>
        /// <param name="parameters">Some action handlers require parameters such as the e-mailing action handlers
        /// and the <see cref="Miner.DeveloperSamples.GeodatabaseManager.PxDeleteByWorkFunction">PxDeleteByWorkFunction</see>
        /// sample.  This one doesn't use any parameters.</param>
        /// <returns>True if the action handler was executed successfully, false otherwise.</returns>
        protected override bool SubExecute(ActionData actionData, GdbmParameter[] parameters)
        {
            bool success = false;
            IWorkspace workspace = null;
            try
            {
                // Find the ReconcileHistory table name from configuration.
                string tableName = TableReconcileHistory;
                workspace = (IWorkspace)actionData.Version;
                // Determine if there is a row already in the table for this version processing.
                int recId = -1;
                if (actionData.Contains(ReconcileHistoryId))  // ID value is present
                {
                    recId = Convert.ToInt32(actionData[ReconcileHistoryId]);
                }
                // Populate dictionary with the field names and values to be inserted or updated.
                Dictionary<string, object> fieldsAndValues = null;
                if (string.Equals(ActionName, BeforeReconcileAction, StringComparison.OrdinalIgnoreCase))
                {
                    fieldsAndValues = new Dictionary<string, object>(3);
                    fieldsAndValues.Add(VersionNameField, actionData.VersionName);
                    fieldsAndValues.Add(StartDateField, DateTime.Now);
                    fieldsAndValues.Add(ServiceNameField, ServiceConfiguration.Name);
                }
                else if (string.Equals(ActionName, AfterReconcileAction, StringComparison.OrdinalIgnoreCase))
                {
                    // Write all known info on after event
                    string result = ReconciledResult;
                    if (actionData.HasConflicts) result = ConflictsResult;
                    fieldsAndValues = new Dictionary<string, object>(4);
                    fieldsAndValues.Add(VersionNameField, actionData.VersionName);
                    fieldsAndValues.Add(EndDateField, DateTime.Now);
                    fieldsAndValues.Add(ResultField, result);
                    fieldsAndValues.Add(ServiceNameField, ServiceConfiguration.Name);
                }
                // Insert into or update the Reconcile History table.
                recId = UpdateTable(workspace, tableName, recId, fieldsAndValues, ServiceConfiguration.Name);
                if (string.Equals(ActionName, BeforeReconcileAction, StringComparison.OrdinalIgnoreCase))
                {
                    actionData.Add(ReconcileHistoryId, recId);  // Store the ID so the AfterReconcile event can use it.
                }
                success = true;
            }
            catch (Exception e)
            {
                // Log exception that a problem executing the action handler has occurred
                // Note that the Log object has been extended to take a service name as the first parameter.
                Log.Error(ServiceConfiguration.Name, string.Format("Error Executing GDBM ActionHandler {0}: {1}.", Name, e.Message), e);
            }
            return success;
        }
        #endregion
        #region private table manipulation methods
        /// <summary>
        /// Looks for a row with a matching objectId and if it finds one, values are updated for the existing row.
        /// If no matching objectId is found, a new row is created with the values specified.
        /// </summary>
        /// <param name="workspace">The workspace that contains the table to update.</param>
        /// <param name="tableName">The short name (i.e., not fully qualified) of the table containing the row to update.</param>
        /// <param name="objectId">The objectId for the row being updated.</param>
        /// <param name="fieldsAndValues">Fields and corresponding values to be changed.</param>
        /// <param name="serviceName">The name of the service to which the update applies.</param>
        /// <returns>The objectId of the row updated.</returns>
        private int UpdateTable(IWorkspace workspace, string tableName, int objectId, Dictionary<string, object> fieldsAndValues, string serviceName)
        {
            int oid = -1;
            try
            {
                ITable table = GetSystemTable(workspace, tableName);
                if (table != null && fieldsAndValues != null && fieldsAndValues.Count > 0)
                {
                    IRow row = null;
                    if (objectId != -1)
                    {
                        row = table.GetRow(objectId);
                    }
                    if (row == null)
                    {
                        row = table.CreateRow();
                    }
                    foreach (KeyValuePair<string, object> kvp in fieldsAndValues)
                    {
                        int fieldIndex = table.FindField(kvp.Key);
                        if (fieldIndex > -1)
                        {
                            row.set_Value(fieldIndex, kvp.Value);
                        }
                    }
                    row.Store();
                    oid = row.OID;
                }
            }
            catch (Exception e)
            {
                // Log exception that a problem occurred while attempting to update the table
                // Note that the Log object has been extended to take a service name as the first parameter.
                Log.Error(ServiceConfiguration.Name, string.Format("Error updating table {0}: {1}.", tableName, e.Message), e);
            }
            return oid;
        }
        /// <summary>
        /// Gets a table which must be owned by the same schema owner as the default version.
        /// </summary>
        /// <param name="workspace">Workspace containing the table.</param>
        /// <param name="tableName">Short name (i.e. not fully qualified) of the table sought.</param>
        /// <returns>A table object if one is found, null otherwise.</returns>
        private ITable GetSystemTable(IWorkspace workspace, string tableName)
        {
            ITable table = null;
            
            try
            {
                if (workspace != null && tableName.Length > 0)
                {
                    IMMTableUtils tableUtils = new MMTableUtilsClass();
                    table = tableUtils.OpenSystemTable(workspace, tableName);
                }
            }
            catch (Exception e)
            {
                // Log exception that the system table was not found
                // Note that the Log object has been extended to take a service name as the first parameter.
                Log.Error(ServiceConfiguration.Name, string.Format("Error occurred getting system table {0}: {1}.", tableName, e.Message), e);
            }
            return table;
        }
        #endregion
    }
}

 

 

 

 

 


Send Comment to ArcFMdocumentation@schneider-electric.com