/// <summary>
        /// Sets the input and output ports based on the given configuration. If IPID and/or CABLEID are null, it also
        /// takes care of them
        /// </summary>
        /// <param name="feature">The device feature to configure</param>
        /// <param name="inputPorts">Number of input ports</param>
        /// <param name="outputPorts">Number of output ports</param>
        /// <param name="isExistingOperation">Flag to control whether this method is being called from within an existing
        /// edit operation -- the default behavior is "false" in which case it adds an operation of its own</param>
        /// <returns>True if the configuration is complete</returns>
        public bool ConfigureDevice(ESRI.ArcGIS.Geodatabase.IFeature feature, int inputPorts, int outputPorts, bool isExistingOperation)
        {
            bool   isComplete         = false;
            bool   isOurOperationOpen = false;
            string deviceIpid         = string.Empty;

            #region Validation

            string missingFieldFormat = "Field {0} is missing.";

            // The following will be set during Validation
            ESRI.ArcGIS.Geodatabase.IObjectClass ftClass = null;
            ESRI.ArcGIS.Geodatabase.IFields      fields  = null;

            ftClass = feature.Class;
            fields  = ftClass.Fields;

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

            if (_editor.EditState == ESRI.ArcGIS.Editor.esriEditState.esriStateNotEditing)
            {
                throw new InvalidOperationException("You must be editing the workspace to perform this operation.");
            }

            if (0 > inputPorts)
            {
                throw new ArgumentOutOfRangeException("inputPorts");
            }

            if (0 > outputPorts)
            {
                throw new ArgumentOutOfRangeException("outputPorts");
            }

            int ipidIdx = fields.FindField(ConfigUtil.IpidFieldName);
            if (-1 == ipidIdx)
            {
                throw new InvalidOperationException(string.Format(missingFieldFormat, ConfigUtil.IpidFieldName));
            }

            int inPortsIdx = fields.FindField(ConfigUtil.InputPortsFieldName);
            if (-1 == inPortsIdx)
            {
                throw new InvalidOperationException(string.Format(missingFieldFormat, ConfigUtil.InputPortsFieldName));
            }

            int outPortsIdx = fields.FindField(ConfigUtil.OutputPortsFieldName);
            if (-1 == outPortsIdx)
            {
                throw new InvalidOperationException(string.Format(missingFieldFormat, ConfigUtil.OutputPortsFieldName));
            }

            #endregion

            // Are we RE-configuring?
            //            int? oldInputPorts = GdbUtils.GetDomainedIntName(feature, ConfigUtil.InputPortsFieldName);
            //            int? oldOutputPorts = GdbUtils.GetDomainedIntName(feature, ConfigUtil.OutputPortsFieldName);

            //            int inputPortDifference = oldInputPorts.HasValue ? Math.Abs(inputPorts - oldInputPorts.Value) : inputPorts;
            //            int outputPortDifference = oldOutputPorts.HasValue ? Math.Abs(outputPorts - oldOutputPorts.Value) : outputPorts;

            ESRI.ArcGIS.esriSystem.ITrackCancel    trackCancel    = new ESRI.ArcGIS.Display.CancelTrackerClass();
            ESRI.ArcGIS.Framework.IProgressDialog2 progressDialog = _hookHelper.CreateProgressDialog(trackCancel, "Configuring device...", 1, inputPorts + outputPorts, 1, "Starting edit operation...", "Device Configuration");
            //            ESRI.ArcGIS.Framework.IProgressDialog2 progressDialog = CreateProgressDialog(trackCancel, "Configuring device...", 1, inputPortDifference + outputPortDifference + 2, 1, "Starting edit operation...", "Device Configuration");
            ESRI.ArcGIS.esriSystem.IStepProgressor stepProgressor = (ESRI.ArcGIS.esriSystem.IStepProgressor)progressDialog;

            progressDialog.ShowDialog();
            stepProgressor.Step();

            if (!isExistingOperation)
            {
                try
                {
                    _editor.StartOperation();
                    isOurOperationOpen = true;
                }
                catch (Exception ex)
                {
                    throw new Exception("Failed to start edit operation.", ex);
                }
            }

            try
            {
                feature.set_Value(inPortsIdx, inputPorts);
                feature.set_Value(outPortsIdx, outputPorts);

                if (DBNull.Value == feature.get_Value(ipidIdx))
                {
                    Guid g = Guid.NewGuid();
                    deviceIpid = g.ToString("B").ToUpper();
                    feature.set_Value(ipidIdx, deviceIpid);
                }
                else
                {
                    deviceIpid = feature.get_Value(ipidIdx).ToString();
                }

                //                if (!oldOutputPorts.HasValue && !oldInputPorts.HasValue)
                //                {
                isComplete = GeneratePorts(feature, 1, inputPorts, 1, outputPorts, progressDialog, trackCancel);
                //                }
                //                else
                //                {
                //                    bool additionsComplete = false;
                //                    bool deletionsComplete = false;
                //
                //                    additionsComplete = GeneratePorts(feature, oldInputPorts.Value + 1, oldInputPorts.Value + inputPortDifference, oldOutputPorts.Value + 1, oldOutputPorts.Value + outputPortDifference, progressDialog, trackCancel);
                //                    deletionsComplete = DeletePorts(feature, inputPorts, outputPorts, progressDialog, trackCancel);
                //
                //                    isComplete = additionsComplete && deletionsComplete;
                //                }

                if (isComplete)
                {
                    stepProgressor.Message = "Finishing configuration...";
                    stepProgressor.Step();

                    if (isOurOperationOpen)
                    {
                        feature.Store();
                        _editor.StopOperation("Configure Device");
                    }
                }
                else
                {
                    stepProgressor.Message = "Cancelling configuration...";
                    stepProgressor.Step();

                    if (isOurOperationOpen)
                    {
                        _editor.AbortOperation();
                    }
                }
            }
            catch
            {
                if (isOurOperationOpen)
                {
                    _editor.AbortOperation();
                }
            }

            if (null != progressDialog)
            {
                progressDialog.HideDialog();
            }

            return(isComplete);
        }
        ///// <summary>
        ///// The active view has refreshed. Redraw our results, if we have any
        ///// </summary>
        ///// <param name="Display">Display to draw on</param>
        ///// <param name="phase"></param>
        //private void _arcMapWrapper_ActiveViewAfterDraw(ESRI.ArcGIS.Display.IDisplay Display, ESRI.ArcGIS.Carto.esriViewDrawPhase phase)
        //{
        //    if (phase == ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGeoSelection)
        //    {
        //        // Draw after the selection
        //        if (null != _currentResults)
        //        {
        //            ESRI.ArcGIS.Display.ILineSymbol lineSymbol = new ESRI.ArcGIS.Display.SimpleLineSymbol();
        //            ESRI.ArcGIS.Display.IRgbColor color = new ESRI.ArcGIS.Display.RgbColorClass();
        //            color.Red = 255;
        //            color.Green = 0;
        //            color.Blue = 0;

        //            lineSymbol.Color = color;
        //            lineSymbol.Width = 4;

        //            ESRI.ArcGIS.Display.ISimpleMarkerSymbol markerSymbol = new ESRI.ArcGIS.Display.SimpleMarkerSymbolClass();
        //            markerSymbol.Color = color;
        //            markerSymbol.Style = ESRI.ArcGIS.Display.esriSimpleMarkerStyle.esriSMSCircle;
        //            markerSymbol.Size = 6;

        //            for (int i = 0; i < _currentResults.Count; i++)
        //            {
        //                ESRI.ArcGIS.Geometry.IGeometry geometry = _currentResults[i];
        //                if (geometry is ESRI.ArcGIS.Geometry.IPolyline)
        //                {
        //                    Display.SetSymbol((ESRI.ArcGIS.Display.ISymbol)lineSymbol);
        //                    Display.DrawPolyline((ESRI.ArcGIS.Geometry.IPolyline)geometry);
        //                }
        //                else if (geometry is ESRI.ArcGIS.Geometry.IPoint)
        //                {
        //                    Display.SetSymbol((ESRI.ArcGIS.Display.ISymbol)markerSymbol);
        //                    Display.DrawPoint((ESRI.ArcGIS.Geometry.IPoint)geometry);
        //                }
        //            }
        //        }
        //    }
        //}

        private List <ESRI.ArcGIS.Geodatabase.IRow> TracePath(ESRI.ArcGIS.Geodatabase.IFeature cableFeature, int fiberNumber, bool isStartingAtFromEnd)
        {
            List <ESRI.ArcGIS.Geodatabase.IRow> result = new List <ESRI.ArcGIS.Geodatabase.IRow>();

            string ipid = cableFeature.get_Value(cableFeature.Fields.FindField(ConfigUtil.IpidFieldName)).ToString();

            ESRI.ArcGIS.Geodatabase.IFeatureClass cableFtClass = (ESRI.ArcGIS.Geodatabase.IFeatureClass)cableFeature.Class;

            ESRI.ArcGIS.Geodatabase.IFeatureClass spliceFtClass    = _wkspHelper.FindFeatureClass(ConfigUtil.SpliceClosureFtClassName);
            ESRI.ArcGIS.Geodatabase.ITable        fiberSpliceTable = _wkspHelper.FindTable(ConfigUtil.FiberSpliceTableName);

            ESRI.ArcGIS.Geodatabase.IFields spliceFields = fiberSpliceTable.Fields;

            string fiberClassName = ConfigUtil.FiberTableName;

            ESRI.ArcGIS.Geodatabase.IRelationshipClass fiberRelationship = GdbUtils.GetRelationshipClass(cableFtClass, ConfigUtil.FiberCableToFiberRelClassName);
            if (null != fiberRelationship && null != fiberRelationship.DestinationClass)
            {
                fiberClassName = GdbUtils.ParseTableName(fiberRelationship.DestinationClass as ESRI.ArcGIS.Geodatabase.IDataset);
            }

            ESRI.ArcGIS.Geodatabase.ITable fiberTable = _wkspHelper.FindTable(fiberClassName);

            _aCableIdx            = spliceFields.FindField(ConfigUtil.ACableIdFieldName);
            _bCableIdx            = spliceFields.FindField(ConfigUtil.BCableIdFieldName);
            _aFiberNumIdx         = spliceFields.FindField(ConfigUtil.AFiberNumberFieldName);
            _bFiberNumIdx         = spliceFields.FindField(ConfigUtil.BFiberNumberFieldName);
            _isAFromIdx           = spliceFields.FindField(ConfigUtil.IsAFromEndFieldName);
            _isBFromIdx           = spliceFields.FindField(ConfigUtil.IsBFromEndFieldName);
            _spliceClosureIpidIdx = spliceFields.FindField(ConfigUtil.SpliceClosureIpidFieldName);

            ESRI.ArcGIS.Geodatabase.IQueryFilter spliceFilter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
            spliceFilter.WhereClause = string.Format("({0}='{1}' AND {2}={3})"
                                                     + " OR ({4}='{1}' AND {5}={3})",
                                                     ConfigUtil.ACableIdFieldName,
                                                     ipid,
                                                     ConfigUtil.AFiberNumberFieldName,
                                                     fiberNumber,
                                                     ConfigUtil.BCableIdFieldName,
                                                     ConfigUtil.BFiberNumberFieldName);

            int connections = fiberSpliceTable.RowCount(spliceFilter);

            if (2 < connections)
            {
                // TODO: warning?
                System.Windows.Forms.MessageBox.Show("Less than 2 connections were detected: " + fiberNumber, "Telecom Trace", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
            }

            string spliceClosureIpid = string.Empty;
            string nextCableId       = string.Empty;
            int    nextFiberNumber   = -1;
            bool   isNextFromEnd     = false;

            // {{0}} causes the string.format to
            string cableWhereFormat  = string.Format("{0}='{{0}}'", ConfigUtil.IpidFieldName);
            string spliceWhereFormat = string.Format("{0}='{{0}}'", ConfigUtil.IpidFieldName);
            string fiberWhereFormat  = string.Format("{0}='{{0}}' AND {1}={{1}}", fiberRelationship.OriginForeignKey, ConfigUtil.Fiber_NumberFieldName);

            using (ESRI.ArcGIS.ADF.ComReleaser releaser = new ESRI.ArcGIS.ADF.ComReleaser())
            {
                ESRI.ArcGIS.Geodatabase.IQueryFilter filter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
                releaser.ManageLifetime(filter);

                // Ripple down the start cable's to end
                ESRI.ArcGIS.Geodatabase.IRow spliceRow = GetNextSplice(fiberSpliceTable, ipid, fiberNumber, isStartingAtFromEnd, out nextCableId, out nextFiberNumber, out spliceClosureIpid, out isNextFromEnd);
                while (null != spliceRow)
                {
                    ESRI.ArcGIS.Geodatabase.IFeature spliceClosure = null;

                    if (spliceClosureIpid.Equals(""))
                    {
                        System.Windows.Forms.MessageBox.Show("Found Splice with no SpliceClosure (ID/#) " + nextCableId + "/" + nextFiberNumber, "Telecom Trace", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
                    }
                    else
                    {
                        filter.WhereClause = string.Format(spliceWhereFormat, spliceClosureIpid);
                        ESRI.ArcGIS.Geodatabase.IFeatureCursor spliceCursor = spliceFtClass.Search(filter, false);
                        releaser.ManageLifetime(spliceCursor);
                        spliceClosure = spliceCursor.NextFeature();
                        if (spliceClosure == null)
                        {
                            System.Windows.Forms.MessageBox.Show("Invalid SpliceClosure referenced: (IPID)" + spliceClosureIpid, "Telecom Trace", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
                        }
                    }

                    filter.WhereClause = string.Format(cableWhereFormat, nextCableId);
                    ESRI.ArcGIS.Geodatabase.IFeatureCursor cableCursor = cableFtClass.Search(filter, false);
                    releaser.ManageLifetime(cableCursor);
                    ESRI.ArcGIS.Geodatabase.IFeature cable = cableCursor.NextFeature();
                    if (cable == null)
                    {
                        System.Windows.Forms.MessageBox.Show("Invalid cable ID referenced: (ID)" + nextCableId, "Telecom Trace", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
                    }

                    filter.WhereClause = string.Format(fiberWhereFormat, nextCableId, nextFiberNumber);
                    ESRI.ArcGIS.Geodatabase.ICursor fiberCursor = fiberTable.Search(filter, false);
                    releaser.ManageLifetime(fiberCursor);
                    ESRI.ArcGIS.Geodatabase.IRow fiber = fiberCursor.NextRow();
                    if (fiber == null)
                    {
                        System.Windows.Forms.MessageBox.Show("Invalid Fiber Cable or # referenced: (ID/#) " + nextCableId + "/" + nextFiberNumber, "Telecom Trace", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
                    }

                    if (isStartingAtFromEnd)
                    {
                        if (spliceRow != null)
                        {
                            result.Add(spliceRow);
                        }
                        if (spliceClosure != null)
                        {
                            result.Add(spliceClosure);
                        }
                        if (fiber != null)
                        {
                            result.Add(fiber);
                        }
                        if (cable != null)
                        {
                            result.Add(cable);
                        }
                    }
                    else
                    {
                        if (spliceClosure != null)
                        {
                            result.Add(spliceClosure);
                        }
                        if (spliceRow != null)
                        {
                            result.Add(spliceRow);
                        }
                        if (cable != null)
                        {
                            result.Add(cable);
                        }
                        if (fiber != null)
                        {
                            result.Add(fiber);
                        }
                    }

                    spliceRow = GetNextSplice(fiberSpliceTable, nextCableId, nextFiberNumber, !isNextFromEnd, out nextCableId, out nextFiberNumber, out spliceClosureIpid, out isNextFromEnd);
                }

                // See if there is a port for this one
                ESRI.ArcGIS.Geodatabase.IRow     portRow  = null;
                ESRI.ArcGIS.Geodatabase.IFeature deviceFt = null;
                if (GetConnectedPort(cableFtClass, nextCableId, nextFiberNumber, isNextFromEnd, out portRow, out deviceFt))
                {
                    if (isStartingAtFromEnd)
                    {
                        result.Add(portRow);
                        result.Add(deviceFt);
                    }
                    else
                    {
                        result.Add(deviceFt);
                        result.Add(portRow);
                    }
                }

                return(result);
            }
        }