/// <summary>
        /// Gets a list of all strand numbers from a cable that are spliced on the given end
        /// </summary>
        /// <param name="cable">Cable to check</param>
        /// <param name="isFromEnd">True to check from end, False to check to end</param>
        /// <returns>List of int</returns>
        private static List <int> GetSplicedStrands(FiberCableWrapper cable, bool isFromEnd)
        {
            if (null == cable)
            {
                throw new ArgumentNullException("cable");
            }

            List <int> result = new List <int>();

            using (ESRI.ArcGIS.ADF.ComReleaser releaser = new ESRI.ArcGIS.ADF.ComReleaser())
            {
                ESRI.ArcGIS.Geodatabase.ITable fiberSpliceTable = TelecomWorkspaceHelper.Instance().FindTable(ConfigUtil.FiberSpliceTableName);
//                ESRI.ArcGIS.Geodatabase.ITable fiberSpliceTable = GdbUtils.GetTable(cable.Feature.Class, ConfigUtil.FiberSpliceTableName);
                ESRI.ArcGIS.Geodatabase.IQueryFilter filter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
                releaser.ManageLifetime(filter);
                filter.WhereClause = string.Format("{0}='{1}' AND {2}='{3}'",
                                                   ConfigUtil.ACableIdFieldName,
                                                   cable.IPID,
                                                   ConfigUtil.IsAFromEndFieldName,
                                                   (isFromEnd ? "T" : "F"));

                ESRI.ArcGIS.Geodatabase.ICursor spliceCursor = fiberSpliceTable.Search(filter, true);
                ESRI.ArcGIS.Geodatabase.IRow    spliceRow    = spliceCursor.NextRow();
                int fiberIdIdx = fiberSpliceTable.FindField(ConfigUtil.AFiberNumberFieldName);

                while (null != spliceRow)
                {
                    result.Add((int)spliceRow.get_Value(fiberIdIdx));

                    ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(spliceRow);
                    spliceRow = spliceCursor.NextRow();
                }

                ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(spliceCursor);

                filter.WhereClause = string.Format("{0}='{1}' AND {2}='{3}'",
                                                   ConfigUtil.BCableIdFieldName,
                                                   cable.IPID,
                                                   ConfigUtil.IsBFromEndFieldName,
                                                   (isFromEnd ? "T" : "F")); spliceCursor = fiberSpliceTable.Search(filter, true);

                spliceRow  = spliceCursor.NextRow();
                fiberIdIdx = fiberSpliceTable.FindField(ConfigUtil.BFiberNumberFieldName);

                while (null != spliceRow)
                {
                    result.Add((int)spliceRow.get_Value(fiberIdIdx));

                    ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(spliceRow);
                    spliceRow = spliceCursor.NextRow();
                }

                ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(spliceCursor);
            }

            return(result);
        }
        /// <summary>
        /// Finds the connected device/port
        /// </summary>
        /// <param name="siblingFtClass">Any feature class from the workspace</param>
        /// <param name="cableId">Cable ID to check connx for</param>
        /// <param name="fiberNumber">Fiber Number to check connx for</param>
        /// <param name="isFromEnd">Whether to check the cable's from or to end</param>
        /// <param name="portRow">(out) result port</param>
        /// <param name="deviceFt">(out) result device</param>
        /// <returns>True if a connx was found</returns>
        private bool GetConnectedPort(ESRI.ArcGIS.Geodatabase.IFeatureClass siblingFtClass, string cableId, int fiberNumber, bool isFromEnd, out ESRI.ArcGIS.Geodatabase.IRow portRow, out ESRI.ArcGIS.Geodatabase.IFeature deviceFt)
        {
            portRow  = null;
            deviceFt = null;

            bool result = false;

            string[] portTableNames = ConfigUtil.PortTableNames;
            using (ESRI.ArcGIS.ADF.ComReleaser releaser = new ESRI.ArcGIS.ADF.ComReleaser())
            {
                ESRI.ArcGIS.Geodatabase.IQueryFilter filter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
                filter.WhereClause = string.Format("{0}='{1}' AND {2}={3} AND {4}='{5}'",
                                                   ConfigUtil.ConnectedCableFieldName,
                                                   cableId,
                                                   ConfigUtil.ConnectedFiberFieldName,
                                                   fiberNumber,
                                                   ConfigUtil.ConnectedEndFieldName,
                                                   isFromEnd ? "T" : "F");
                releaser.ManageLifetime(filter);

                for (int i = 0; i < portTableNames.Length; i++)
                {
                    string portTableName = portTableNames[i];
                    ESRI.ArcGIS.Geodatabase.ITable  portTable = _wkspHelper.FindTable(portTableName);
                    ESRI.ArcGIS.Geodatabase.ICursor cursor    = portTable.Search(filter, false);
                    releaser.ManageLifetime(cursor);

                    portRow = cursor.NextRow();
                    if (null != portRow)
                    {
                        ESRI.ArcGIS.Geodatabase.IRelationshipClass deviceHasPorts = ConfigUtil.GetDeviceRelationship(portTable);
                        if (null == deviceHasPorts)
                        {
                            throw new Exception("Device to port relationship is missing or cannot be opened.");
                        }

                        ESRI.ArcGIS.Geodatabase.IFeatureClass deviceClass = deviceHasPorts.OriginClass as ESRI.ArcGIS.Geodatabase.IFeatureClass;
                        if (null == deviceClass)
                        {
                            throw new Exception("Device feature class is missing or cannot be opened.");
                        }

                        filter.WhereClause = string.Format("{0}='{1}'",
                                                           deviceHasPorts.OriginPrimaryKey,
                                                           portRow.get_Value(portTable.FindField(deviceHasPorts.OriginForeignKey)));
                        ESRI.ArcGIS.Geodatabase.IFeatureCursor deviceCursor = deviceClass.Search(filter, false);
                        deviceFt = deviceCursor.NextFeature();

                        result = true;
                        break;
                    }
                }
            }

            return(result);
        }
        /// <summary>
        /// Delete the ports for a given device
        /// </summary>
        /// <param name="device">The device feature</param>
        /// <returns>True if completed</returns>
        private bool DeletePorts(ESRI.ArcGIS.Geodatabase.IFeature device, int highInputPort, int highOutputPort, ESRI.ArcGIS.Framework.IProgressDialog2 progressDialog, ESRI.ArcGIS.esriSystem.ITrackCancel trackCancel)
        {
            bool isCancelled = false;

            ESRI.ArcGIS.esriSystem.IStepProgressor     stepProgressor = (ESRI.ArcGIS.esriSystem.IStepProgressor)progressDialog;
            ESRI.ArcGIS.Geodatabase.IRelationshipClass deviceHasPorts = ConfigUtil.GetPortRelationship((ESRI.ArcGIS.Geodatabase.IFeatureClass)device.Class);

            if (null != deviceHasPorts)
            {
                using (ESRI.ArcGIS.ADF.ComReleaser releaser = new ESRI.ArcGIS.ADF.ComReleaser())
                {
                    ESRI.ArcGIS.Geodatabase.ITable portTable = (ESRI.ArcGIS.Geodatabase.ITable)deviceHasPorts.DestinationClass;
                    releaser.ManageLifetime(portTable);

                    ESRI.ArcGIS.Geodatabase.IQueryFilter filter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
                    releaser.ManageLifetime(filter);

                    filter.WhereClause = string.Format("{0}='{1}' AND {2} > {3} AND {4}='{5}'",
                                                       deviceHasPorts.OriginForeignKey,
                                                       device.get_Value(device.Fields.FindField(deviceHasPorts.OriginPrimaryKey)),
                                                       ConfigUtil.PortIdFieldName,
                                                       highInputPort,
                                                       ConfigUtil.PortTypeFieldName,
                                                       1);

                    stepProgressor.Message = "Deleting higher input ports...";
                    int deletedPorts = portTable.RowCount(filter);

                    portTable.DeleteSearchedRows(filter);
                    for (int i = 0; i < deletedPorts; i++)
                    {
                        stepProgressor.Step();
                    }

                    filter.WhereClause = string.Format("{0}='{1}' AND {2} > {3} AND {4}='{5}'",
                                                       deviceHasPorts.OriginForeignKey,
                                                       device.get_Value(device.Fields.FindField(deviceHasPorts.OriginPrimaryKey)),
                                                       ConfigUtil.PortIdFieldName,
                                                       highOutputPort,
                                                       ConfigUtil.PortTypeFieldName,
                                                       2);

                    stepProgressor.Message = "Deleting higher output ports...";
                    deletedPorts           = portTable.RowCount(filter);

                    portTable.DeleteSearchedRows(filter);
                    for (int i = 0; i < deletedPorts; i++)
                    {
                        stepProgressor.Step();
                    }
                }
            }

            return(!isCancelled);
        }
        /// <summary>
        /// Gets a list of all port numbers from a device that are not connected on the given end
        /// </summary>
        /// <param name="device">Device to check</param>
        /// <param name="portType">Check input or output ports</param>
        /// <returns>List of int</returns>
        private static List <int> GetOpenPorts(DeviceWrapper device, PortType portType)
        {
            #region Validation
            if (null == device)
            {
                throw new ArgumentNullException("device");
            }

            if (null == device.Feature)
            {
                throw new ArgumentException("device.Class cannot be null");
            }
            #endregion

            List <int> result = new List <int>();

            using (ESRI.ArcGIS.ADF.ComReleaser releaser = new ESRI.ArcGIS.ADF.ComReleaser())
            {
                ESRI.ArcGIS.Geodatabase.IFeatureClass      deviceFtClass  = device.Feature.Class as ESRI.ArcGIS.Geodatabase.IFeatureClass;
                ESRI.ArcGIS.Geodatabase.IRelationshipClass deviceHasPorts = ConfigUtil.GetPortRelationship(deviceFtClass);
                if (null != deviceHasPorts)
                {
                    ESRI.ArcGIS.Geodatabase.ITable portTable = deviceHasPorts.DestinationClass as ESRI.ArcGIS.Geodatabase.ITable;
                    if (null != portTable)
                    {
                        ESRI.ArcGIS.Geodatabase.IQueryFilter filter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
                        releaser.ManageLifetime(filter);
                        filter.WhereClause = string.Format("{0}='{1}' AND {2} IS NULL AND {3}='{4}'",
                                                           deviceHasPorts.OriginForeignKey,
                                                           device.Feature.get_Value(deviceFtClass.FindField(deviceHasPorts.OriginPrimaryKey)),
                                                           ConfigUtil.ConnectedCableFieldName,
                                                           ConfigUtil.PortTypeFieldName,
                                                           (PortType.Input == portType ? "1" : "2"));

                        ESRI.ArcGIS.Geodatabase.ICursor portCursor = portTable.Search(filter, true);
                        ESRI.ArcGIS.Geodatabase.IRow    portRow    = portCursor.NextRow();
                        int portIdIdx = portTable.FindField(ConfigUtil.PortIdFieldName);

                        while (null != portRow)
                        {
                            result.Add((int)portRow.get_Value(portIdIdx));

                            ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(portRow);
                            portRow = portCursor.NextRow();
                        }

                        ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(portCursor);
                    }
                }
            }

            return(result);
        }
Example #5
0
        /// <summary>
        /// Finds the Device to Port relationship class for a given port table
        /// </summary>
        /// <param name="deviceClass">port table</param>
        /// <returns>ESRI.ArcGIS.Geodatabase.IRelationshipClass</returns>
        public static ESRI.ArcGIS.Geodatabase.IRelationshipClass GetDeviceRelationship(ESRI.ArcGIS.Geodatabase.ITable portTable)
        {
            // TODO: Currently returns the first relationship class found whose name ends with "Ports". Should probably make the relationship classes part of the configuration

            ESRI.ArcGIS.Geodatabase.IRelationshipClass result = null;

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

            using (ESRI.ArcGIS.ADF.ComReleaser releaser = new ESRI.ArcGIS.ADF.ComReleaser())
            {
                ESRI.ArcGIS.Geodatabase.IEnumRelationshipClass relClasses = ((ESRI.ArcGIS.Geodatabase.IObjectClass)portTable).get_RelationshipClasses(ESRI.ArcGIS.Geodatabase.esriRelRole.esriRelRoleDestination);
                releaser.ManageLifetime(relClasses);

                relClasses.Reset();
                result = relClasses.Next();

                while (null != result)
                {
                    ESRI.ArcGIS.Geodatabase.IDataset relationshipDataset = result as ESRI.ArcGIS.Geodatabase.IDataset;

                    if (relationshipDataset.Name.EndsWith("Ports", StringComparison.CurrentCultureIgnoreCase))
                    {
                        break;
                    }

                    result = relClasses.Next();
                }
            }

            if (null == result)
            {
                throw new Exception("Relationship class from device to port was not found.");
            }

            return(result);
        }
        /// <summary>
        /// Generate the ports for a given device
        /// </summary>
        /// <param name="device">The device feature</param>
        /// <returns>True if completed</returns>
        private bool GeneratePorts(ESRI.ArcGIS.Geodatabase.IFeature device, int lowInputPort, int inputPortCount, int lowOutputPort, int outputPortCount, ESRI.ArcGIS.Framework.IProgressDialog2 progressDialog, ESRI.ArcGIS.esriSystem.ITrackCancel trackCancel)
        {
            bool isCancelled = false;

            ESRI.ArcGIS.esriSystem.IStepProgressor stepProgressor = (ESRI.ArcGIS.esriSystem.IStepProgressor)progressDialog;
            ESRI.ArcGIS.Geodatabase.IRelationshipClass deviceHasPorts = ConfigUtil.GetPortRelationship((ESRI.ArcGIS.Geodatabase.IFeatureClass)device.Class);
            Guid g;

            if (null != deviceHasPorts)
            {
                using (ESRI.ArcGIS.ADF.ComReleaser releaser = new ESRI.ArcGIS.ADF.ComReleaser())
                {
                    ESRI.ArcGIS.Geodatabase.ITable portTable = (ESRI.ArcGIS.Geodatabase.ITable)deviceHasPorts.DestinationClass;
                    releaser.ManageLifetime(portTable);

                    // Fields to populate on port
                    int portIpidIdx = portTable.Fields.FindField(ConfigUtil.IpidFieldName);
                    int portNumberIdx = portTable.Fields.FindField(ConfigUtil.PortIdFieldName);
                    int portTypeIdx = portTable.Fields.FindField(ConfigUtil.PortTypeFieldName);
                    int fkeyIdx = portTable.Fields.FindField(deviceHasPorts.OriginForeignKey);

                    object originPrimaryKey = device.get_Value(device.Fields.FindField(deviceHasPorts.OriginPrimaryKey));

                    for (int portIdx = lowInputPort; portIdx <= inputPortCount; portIdx++)
                    {
                        stepProgressor.Message = string.Format("Creating input port {0} of {1}", portIdx, inputPortCount);
                        stepProgressor.Step();

                        g = Guid.NewGuid();
                        string portIpid = g.ToString("B").ToUpper();

                        ESRI.ArcGIS.Geodatabase.IRow portRow = portTable.CreateRow();
                        releaser.ManageLifetime(portRow);

                        portRow.set_Value(portIpidIdx, portIpid);
                        portRow.set_Value(portTypeIdx, PortType.Input);
                        portRow.set_Value(portNumberIdx, portIdx);
                        portRow.set_Value(fkeyIdx, originPrimaryKey);

                        portRow.Store();

                        if (!trackCancel.Continue())
                        {
                            isCancelled = true;
                            break;
                        }
                    }

                    if (trackCancel.Continue())
                    {
                        for (int portIdx = lowOutputPort; portIdx <= outputPortCount; portIdx++)
                        {
                            stepProgressor.Message = string.Format("Creating output port {0} of {1}", portIdx, outputPortCount);
                            stepProgressor.Step();

                            g = Guid.NewGuid();
                            string portIpid = g.ToString("B").ToUpper();

                            ESRI.ArcGIS.Geodatabase.IRow portRow = portTable.CreateRow();
                            releaser.ManageLifetime(portRow);

                            portRow.set_Value(portIpidIdx, portIpid);
                            portRow.set_Value(portTypeIdx, PortType.Output);
                            portRow.set_Value(portNumberIdx, portIdx);
                            portRow.set_Value(fkeyIdx, originPrimaryKey);

                            portRow.Store();

                            if (!trackCancel.Continue())
                            {
                                isCancelled = true;
                                break;
                            }
                        }
                    }
                }
            }

            return !isCancelled;
        }
        /// <summary>
        /// Creates given connections between cable and device
        /// </summary>
        /// <param name="cable">Cable</param>
        /// <param name="device">Device</param>
        /// <param name="units">Units to connect</param>
        /// <param name="isFromEnd">Is it the cable's from end?</param>
        /// <param name="portType">Input or Output?</param>
        /// <param name="isExistingOperation">Flag to control whether we need to wrap this in a new edit operation</param>
        /// <returns>Success</returns>
        public bool MakeConnections(FiberCableWrapper cable, DeviceWrapper device, Dictionary <int, int> units, bool isFromEnd, PortType portType, bool isExistingOperation)
        {
            bool success         = false;
            bool isOperationOpen = false;

            #region Validation
            if (null == cable)
            {
                throw new ArgumentNullException("cable");
            }

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

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

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

            if (!isExistingOperation)
            {
                _editor.StartOperation();
                isOperationOpen = true;
            }

            try
            {
                ESRI.ArcGIS.Geodatabase.IFeatureClass      deviceFtClass  = device.Feature.Class as ESRI.ArcGIS.Geodatabase.IFeatureClass;
                ESRI.ArcGIS.Geodatabase.IRelationshipClass deviceHasPorts = ConfigUtil.GetPortRelationship(deviceFtClass);
                if (null == deviceHasPorts)
                {
                    throw new Exception("Unable to get port relationship class.");
                }

                ESRI.ArcGIS.Geodatabase.ITable portTable = deviceHasPorts.DestinationClass as ESRI.ArcGIS.Geodatabase.ITable;
                if (null == portTable)
                {
                    throw new Exception("Invalid destination on port relationship class.");
                }

                int    portIdIdx      = portTable.FindField(ConfigUtil.PortIdFieldName);
                int    fiberNumberIdx = portTable.FindField(ConfigUtil.ConnectedFiberFieldName);
                int    cableIdIdx     = portTable.FindField(ConfigUtil.ConnectedCableFieldName);
                int    isFromEndIdx   = portTable.FindField(ConfigUtil.ConnectedEndFieldName);
                string isFromEndValue = isFromEnd ? "T" : "F";

                Dictionary <int, int> portsAsKeys = units;
                if (PortType.Input == portType)
                {
                    portsAsKeys = new Dictionary <int, int>();
                    foreach (KeyValuePair <int, int> pair in units)
                    {
                        portsAsKeys[pair.Value] = pair.Key;
                    }
                }

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

                    string format = "{0}='{1}' AND {2}='{3}'";
                    filter.WhereClause = string.Format(format,
                                                       deviceHasPorts.OriginForeignKey,
                                                       device.Feature.get_Value(deviceFtClass.FindField(deviceHasPorts.OriginPrimaryKey)),
                                                       ConfigUtil.PortTypeFieldName,
                                                       (PortType.Input == portType ? "1" : "2"));

                    // Non recylcing cursor since we are doing updates.
                    ESRI.ArcGIS.Geodatabase.ICursor portCursor = portTable.Update(filter, false);
                    releaser.ManageLifetime(portCursor);

                    ESRI.ArcGIS.Geodatabase.IRow portRow = portCursor.NextRow();
                    while (null != portRow)
                    {
                        object portIdObj = portRow.get_Value(portIdIdx);
                        if (DBNull.Value != portIdObj)
                        {
                            int portId = System.Convert.ToInt32(portIdObj);
                            if (portsAsKeys.ContainsKey(portId))
                            {
                                portRow.set_Value(cableIdIdx, cable.IPID);
                                portRow.set_Value(isFromEndIdx, isFromEndValue);
                                portRow.set_Value(fiberNumberIdx, portsAsKeys[portId]);
                                portRow.Store();
                            }
                        }

                        ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(portRow);
                        portRow = portCursor.NextRow();
                    }

                    if (isOperationOpen)
                    {
                        _editor.StopOperation("Create Connections");
                        isOperationOpen = false;
                    }

                    success = true;
                }
            }
            catch (Exception ex)
            {
                if (isOperationOpen)
                {
                    _editor.AbortOperation();
                }

                success = false;

                throw new Exception("Save operation failed.");
            }

            return(success);
        }
        /// <summary>
        /// Break all connections for a given cable
        /// </summary>
        /// <param name="cable">Cable to break connections</param>
        /// <param name="isExistingOperation">Should we start/stop the edit operation, or are we already in one?</param>
        /// <returns>Success</returns>
        public bool BreakAllConnections(FiberCableWrapper cable, bool isExistingOperation)
        {
            bool success         = false;
            bool isOperationOpen = false;

            #region Validation
            if (null == cable)
            {
                throw new ArgumentNullException("cable");
            }

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

            if (!isExistingOperation)
            {
                _editor.StartOperation();
                isOperationOpen = true;
            }

            try
            {
                using (ESRI.ArcGIS.ADF.ComReleaser releaser = new ESRI.ArcGIS.ADF.ComReleaser())
                {
                    ESRI.ArcGIS.Geodatabase.IFeatureClass cableFtClass = cable.Feature.Class as ESRI.ArcGIS.Geodatabase.IFeatureClass;
                    ESRI.ArcGIS.Geodatabase.IQueryFilter  filter       = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
                    releaser.ManageLifetime(filter);

                    filter.WhereClause = string.Format("{0}='{1}'", ConfigUtil.ConnectedCableFieldName, cable.IPID);
                    filter.SubFields   = string.Format("{0},{1},{2}", ConfigUtil.ConnectedCableFieldName, ConfigUtil.ConnectedFiberFieldName, ConfigUtil.ConnectedEndFieldName);

                    string[] deviceClassNames = ConfigUtil.DeviceFeatureClassNames;
                    for (int i = 0; i < deviceClassNames.Length; i++)
                    {
                        string deviceClassName = deviceClassNames[i];


                        ESRI.ArcGIS.Geodatabase.IFeatureClass deviceFtClass = _wkspHelper.FindFeatureClass(deviceClassName);
//                        ESRI.ArcGIS.Geodatabase.IFeatureClass deviceFtClass = GdbUtils.GetFeatureClass(cableFtClass, deviceClassName);
                        ESRI.ArcGIS.Geodatabase.ITable     portTable = ConfigUtil.GetPortTable(deviceFtClass);
                        ESRI.ArcGIS.Geodatabase.IRowBuffer buffer    = portTable.CreateRowBuffer();
                        // We want to set them to null, so we can just send the empty buffer
                        portTable.UpdateSearchedRows(filter, buffer);

                        ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(buffer);
                    }

                    if (isOperationOpen)
                    {
                        _editor.StopOperation("Break Connections");
                        isOperationOpen = false;
                    }

                    success = true;
                }
            }
            catch
            {
                if (isOperationOpen)
                {
                    _editor.AbortOperation();
                }

                success = false;
            }

            return(success);
        }
Example #9
0
        /// <summary>
        /// Returns all features from a given feature class that have a vertex or endpoint coincident with a given point
        /// </summary>
        /// <param name="point">IPoint to use as the spatial filter</param>
        /// <param name="searchFtClass">IFeatureClass to search in</param>
        /// <param name="linearEndpointsOnly">Flag to use only the endpoints of a line instead of all vertices</param>
        /// <param name="buffer">Search geometry buffer in map units</param>
        /// <returns>List of IFeature</returns>
        public static List <ESRI.ArcGIS.Geodatabase.IFeature> GetFeaturesWithCoincidentVertices(ESRI.ArcGIS.Geometry.IPoint point, ESRI.ArcGIS.Geodatabase.IFeatureClass searchFtClass, bool linearEndpointsOnly, double buffer)
        {
            List <ESRI.ArcGIS.Geodatabase.IFeature> result = new List <ESRI.ArcGIS.Geodatabase.IFeature>();

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

                ESRI.ArcGIS.Geometry.IEnvelope filterGeometry = point.Envelope;
                if (0 < buffer)
                {
                    filterGeometry.Expand(buffer, buffer, false);
                }

                filter.SpatialRel = ESRI.ArcGIS.Geodatabase.esriSpatialRelEnum.esriSpatialRelIntersects;
                filter.Geometry   = filterGeometry;

                ESRI.ArcGIS.Geodatabase.IFeatureCursor fts = searchFtClass.Search(filter, false);
                releaser.ManageLifetime(fts);

                ESRI.ArcGIS.Geodatabase.IFeature ft = fts.NextFeature();
                while (null != ft)
                {
                    if (searchFtClass.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint)
                    {
                        result.Add(ft);
                    }
                    else if (searchFtClass.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline && linearEndpointsOnly)
                    {
                        ESRI.ArcGIS.Geometry.IPolyline           polyline  = (ESRI.ArcGIS.Geometry.IPolyline)ft.Shape;
                        ESRI.ArcGIS.Geometry.IRelationalOperator fromPoint = polyline.FromPoint as ESRI.ArcGIS.Geometry.IRelationalOperator;
                        ESRI.ArcGIS.Geometry.IRelationalOperator toPoint   = polyline.ToPoint as ESRI.ArcGIS.Geometry.IRelationalOperator;

                        if (fromPoint.Equals(point) || toPoint.Equals(point))
                        {
                            result.Add(ft);
                        }
                    }
                    else
                    {
                        ESRI.ArcGIS.Geometry.IPointCollection pointCollection = ft.Shape as ESRI.ArcGIS.Geometry.IPointCollection;
                        if (null != pointCollection)
                        {
                            for (int i = 0; i < pointCollection.PointCount; i++)
                            {
                                ESRI.ArcGIS.Geometry.IRelationalOperator testPoint = pointCollection.get_Point(i) as ESRI.ArcGIS.Geometry.IRelationalOperator;
                                if (testPoint.Equals(point))
                                {
                                    result.Add(ft);
                                    break;
                                }
                            }
                        }
                    }

                    ft = fts.NextFeature();
                }
            }

            return(result);
        }
        /// <summary>
        /// Gets a list of all strand numbers from a cable that are connected on the given end
        /// </summary>
        /// <param name="cable">Cable to check</param>
        /// <param name="isFromEnd">True to check from end, False to check to end</param>
        /// <returns>List of int</returns>
        private static List <int> GetConnectedStrands(FiberCableWrapper cable, bool isFromEnd)
        {
            #region Validation
            if (null == cable)
            {
                throw new ArgumentNullException("cable");
            }

            #endregion

            List <int> result = new List <int>();

            using (ESRI.ArcGIS.ADF.ComReleaser releaser = new ESRI.ArcGIS.ADF.ComReleaser())
            {
                ESRI.ArcGIS.Geodatabase.IFeatureClass cableFtClass = cable.Feature.Class as ESRI.ArcGIS.Geodatabase.IFeatureClass;

                string[] deviceClassNames = ConfigUtil.DeviceFeatureClassNames;
                for (int i = 0; i < deviceClassNames.Length; i++)
                {
                    string deviceClassName = deviceClassNames[i];
                    ESRI.ArcGIS.Geodatabase.IFeatureClass deviceFtClass = TelecomWorkspaceHelper.Instance().FindFeatureClass(deviceClassName);
//                    ESRI.ArcGIS.Geodatabase.IFeatureClass deviceFtClass = GdbUtils.GetFeatureClass(cableFtClass, deviceClassName);
                    if (null != deviceFtClass)
                    {
                        ESRI.ArcGIS.Geodatabase.IRelationshipClass deviceHasPorts = ConfigUtil.GetPortRelationship(deviceFtClass);
                        if (null != deviceHasPorts)
                        {
                            ESRI.ArcGIS.Geodatabase.ITable portTable = deviceHasPorts.DestinationClass as ESRI.ArcGIS.Geodatabase.ITable;
                            if (null != portTable)
                            {
                                ESRI.ArcGIS.Geodatabase.IQueryFilter filter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
                                releaser.ManageLifetime(filter);

                                filter.WhereClause = string.Format("{0}='{1}' AND {2}='{3}' AND {4} IS NOT NULL",
                                                                   ConfigUtil.ConnectedCableFieldName,
                                                                   cable.IPID,
                                                                   ConfigUtil.ConnectedEndFieldName,
                                                                   (isFromEnd ? "T" : "F"),
                                                                   ConfigUtil.ConnectedFiberFieldName);

                                ESRI.ArcGIS.Geodatabase.ICursor portCursor = portTable.Search(filter, true);
                                ESRI.ArcGIS.Geodatabase.IRow    portRow    = portCursor.NextRow();
                                int fiberIdIdx = portTable.FindField(ConfigUtil.ConnectedFiberFieldName);

                                while (null != portRow)
                                {
                                    result.Add((int)portRow.get_Value(fiberIdIdx));

                                    ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(portRow);
                                    portRow = portCursor.NextRow();
                                }

                                ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(portCursor);
                            }
                        }
                    }
                }
            }

            return(result);
        }
        /// <summary>
        /// Deletes given connections between cable to device
        /// </summary>
        /// <param name="cable">Cable</param>
        /// <param name="device">Device</param>
        /// <param name="units">Units to connect</param>
        /// <param name="portType">Input or Output?</param>
        /// <param name="isExistingOperation">Flag to control whether we need to wrap this in an edit operation</param>
        /// <returns>Success</returns>
        public bool BreakConnections(FiberCableWrapper cable, DeviceWrapper device, Dictionary<int, int> units, PortType portType, bool isExistingOperation)
        {
            bool success = false;
            bool isOperationOpen = false;

            #region Validation
            if (null == cable)
            {
                throw new ArgumentNullException("cable");
            }

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

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

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

            if (0 < units.Count)
            {
                if (!isExistingOperation)
                {
                    _editor.StartOperation();
                    isOperationOpen = true;
                }

                try
                {
                    ESRI.ArcGIS.Geodatabase.IFeatureClass deviceFtClass = device.Feature.Class as ESRI.ArcGIS.Geodatabase.IFeatureClass;
                    ESRI.ArcGIS.Geodatabase.IRelationshipClass deviceHasPorts = ConfigUtil.GetPortRelationship(deviceFtClass);
                    if (null == deviceFtClass)
                    {
                        throw new Exception("Unable to find port relationship class.");
                    }

                    ESRI.ArcGIS.Geodatabase.ITable portTable = deviceHasPorts.DestinationClass as ESRI.ArcGIS.Geodatabase.ITable;
                    if (null == portTable)
                    {
                        throw new Exception("Invalid destination on port relationship class.");
                    }

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

                        StringBuilder inList = new StringBuilder(1024);
                        foreach (KeyValuePair<int, int> pair in units)
                        {
                            string appendFormat = "{0},";
                            if (PortType.Input == portType)
                            {
                                inList.AppendFormat(appendFormat, pair.Key);
                            }
                            else
                            {
                                inList.AppendFormat(appendFormat, pair.Value);
                            }
                        }
                        inList.Remove(inList.Length - 1, 1);

                        string format = "{0}='{1}' AND {2}='{3}' AND {4}='{5}' AND {6} IN ({7})";
                        filter.WhereClause = string.Format(format,
                            deviceHasPorts.OriginForeignKey,
                            device.Feature.get_Value(deviceFtClass.FindField(deviceHasPorts.OriginPrimaryKey)),
                            ConfigUtil.ConnectedCableFieldName,
                            cable.IPID,
                            ConfigUtil.PortTypeFieldName,
                            (PortType.Input == portType ? "1" : "2"),
                            ConfigUtil.ConnectedFiberFieldName,
                            inList.ToString());

                        filter.SubFields = string.Format("{0},{1},{2}", ConfigUtil.ConnectedEndFieldName, ConfigUtil.ConnectedFiberFieldName, ConfigUtil.ConnectedCableFieldName);

                        ESRI.ArcGIS.Geodatabase.IRowBuffer buffer = portTable.CreateRowBuffer();
                        releaser.ManageLifetime(buffer);
                        // We want to set them to null, so we can just send the empty buffer
                        portTable.UpdateSearchedRows(filter, buffer);

                        if (isOperationOpen)
                        {
                            _editor.StopOperation("Break Connections");
                            isOperationOpen = false;
                        }

                        success = true;
                    }
                }
                catch
                {
                    if (isOperationOpen)
                    {
                        _editor.AbortOperation();
                    }

                    success = false;
                }
            }

            return success;
        }
        /// <summary>
        /// Creates given connections between cable and device
        /// </summary>
        /// <param name="cable">Cable</param>
        /// <param name="device">Device</param>
        /// <param name="units">Units to connect</param>
        /// <param name="isFromEnd">Is it the cable's from end?</param>
        /// <param name="portType">Input or Output?</param>
        /// <param name="isExistingOperation">Flag to control whether we need to wrap this in a new edit operation</param>
        /// <returns>Success</returns>
        public bool MakeConnections(FiberCableWrapper cable, DeviceWrapper device, Dictionary<int, int> units, bool isFromEnd, PortType portType, bool isExistingOperation)
        {
            bool success = false;
            bool isOperationOpen = false;

            #region Validation
            if (null == cable)
            {
                throw new ArgumentNullException("cable");
            }

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

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

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

            if (!isExistingOperation)
            {
                _editor.StartOperation();
                isOperationOpen = true;
            }

            try
            {
                ESRI.ArcGIS.Geodatabase.IFeatureClass deviceFtClass = device.Feature.Class as ESRI.ArcGIS.Geodatabase.IFeatureClass;
                ESRI.ArcGIS.Geodatabase.IRelationshipClass deviceHasPorts = ConfigUtil.GetPortRelationship(deviceFtClass);
                if (null == deviceHasPorts)
                {
                    throw new Exception("Unable to get port relationship class.");
                }

                ESRI.ArcGIS.Geodatabase.ITable portTable = deviceHasPorts.DestinationClass as ESRI.ArcGIS.Geodatabase.ITable;
                if (null == portTable)
                {
                    throw new Exception("Invalid destination on port relationship class.");
                }

                int portIdIdx = portTable.FindField(ConfigUtil.PortIdFieldName);
                int fiberNumberIdx = portTable.FindField(ConfigUtil.ConnectedFiberFieldName);
                int cableIdIdx = portTable.FindField(ConfigUtil.ConnectedCableFieldName);
                int isFromEndIdx = portTable.FindField(ConfigUtil.ConnectedEndFieldName);
                string isFromEndValue = isFromEnd ? "T" : "F";

                Dictionary<int, int> portsAsKeys = units;
                if (PortType.Input == portType)
                {
                    portsAsKeys = new Dictionary<int, int>();
                    foreach (KeyValuePair<int, int> pair in units)
                    {
                        portsAsKeys[pair.Value] = pair.Key;
                    }
                }

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

                    string format = "{0}='{1}' AND {2}='{3}'";
                    filter.WhereClause = string.Format(format,
                        deviceHasPorts.OriginForeignKey,
                        device.Feature.get_Value(deviceFtClass.FindField(deviceHasPorts.OriginPrimaryKey)),
                        ConfigUtil.PortTypeFieldName,
                        (PortType.Input == portType ? "1" : "2"));

                    // Non recylcing cursor since we are doing updates.
                    ESRI.ArcGIS.Geodatabase.ICursor portCursor = portTable.Update(filter, false);
                    releaser.ManageLifetime(portCursor);

                    ESRI.ArcGIS.Geodatabase.IRow portRow = portCursor.NextRow();
                    while (null != portRow)
                    {
                        object portIdObj = portRow.get_Value(portIdIdx);
                        if (DBNull.Value != portIdObj)
                        {
                            int portId = System.Convert.ToInt32(portIdObj);
                            if (portsAsKeys.ContainsKey(portId))
                            {
                                portRow.set_Value(cableIdIdx, cable.IPID);
                                portRow.set_Value(isFromEndIdx, isFromEndValue);
                                portRow.set_Value(fiberNumberIdx, portsAsKeys[portId]);
                                portRow.Store();
                            }
                        }

                        ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(portRow);
                        portRow = portCursor.NextRow();
                    }

                    if (isOperationOpen)
                    {
                        _editor.StopOperation("Create Connections");
                        isOperationOpen = false;
                    }

                    success = true;
                }
            }
            catch(Exception ex)
            {
                if (isOperationOpen)
                {
                    _editor.AbortOperation();
                }

                success = false;

                throw new Exception("Save operation failed.");
            }

            return success;
        }
        /// <summary>
        /// Determines if all the ranges fall between 1 to port count
        /// </summary>
        /// <param name="ranges">Ranges to check</param>
        /// <param name="device">Device to check on</param>
        /// <param name="portType">Port type</param>
        /// <returns>True if valid ranges</returns>
        public static bool AreRangesWithinPortCount(List <Range> ranges, DeviceWrapper device, PortType portType)
        {
            bool result = false; // Default to false in case we can't even find the port relationship class

            #region Validation

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

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

            #endregion

            ESRI.ArcGIS.Geodatabase.IFeatureClass ftClass = device.Feature.Class as ESRI.ArcGIS.Geodatabase.IFeatureClass;
            if (null != ftClass)
            {
                using (ESRI.ArcGIS.ADF.ComReleaser releaser = new ESRI.ArcGIS.ADF.ComReleaser())
                {
                    ESRI.ArcGIS.Geodatabase.IRelationshipClass deviceHasPorts = ConfigUtil.GetPortRelationship(ftClass);
                    if (null != deviceHasPorts)
                    {
                        ESRI.ArcGIS.Geodatabase.ITable portTable = deviceHasPorts.DestinationClass as ESRI.ArcGIS.Geodatabase.ITable;
                        int portIdIdx = portTable.FindField(ConfigUtil.PortIdFieldName);

                        if (-1 < portIdIdx)
                        {
                            result = true; // Now that we have the ports, assume we're ok until we find a problem

                            ESRI.ArcGIS.Geodatabase.IQueryFilter filter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
                            releaser.ManageLifetime(filter);

                            filter.SubFields   = ConfigUtil.PortIdFieldName;
                            filter.WhereClause = string.Format("{0}='{1}' AND {2}='{3}' AND {4} IS NOT NULL",
                                                               deviceHasPorts.OriginForeignKey,
                                                               device.Feature.get_Value(ftClass.FindField(deviceHasPorts.OriginPrimaryKey)),
                                                               ConfigUtil.PortTypeFieldName,
                                                               PortType.Input == portType ? 1 : 2,
                                                               ConfigUtil.PortIdFieldName);

                            ((ESRI.ArcGIS.Geodatabase.IQueryFilterDefinition)filter).PostfixClause = string.Format("ORDER BY {0}", ConfigUtil.PortIdFieldName);
                            ESRI.ArcGIS.Geodatabase.ICursor cursor = portTable.Search(filter, true);
                            releaser.ManageLifetime(cursor);

                            int minPort = int.MinValue;
                            int maxPort = int.MaxValue;
                            ESRI.ArcGIS.Geodatabase.IRow row = cursor.NextRow();

                            if (null != row)
                            {
                                minPort = (int)row.get_Value(portIdIdx);

                                while (null != row)
                                {
                                    maxPort = (int)row.get_Value(portIdIdx);
                                    ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(row);

                                    row = cursor.NextRow();
                                }
                            }

                            foreach (Range r in ranges)
                            {
                                if (r.High > maxPort ||
                                    minPort > r.Low)
                                {
                                    result = false;
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            return(result);
        }
        /// <summary>
        /// Generate the ports for a given device
        /// </summary>
        /// <param name="device">The device feature</param>
        /// <returns>True if completed</returns>
        private bool GeneratePorts(ESRI.ArcGIS.Geodatabase.IFeature device, int lowInputPort, int inputPortCount, int lowOutputPort, int outputPortCount, ESRI.ArcGIS.Framework.IProgressDialog2 progressDialog, ESRI.ArcGIS.esriSystem.ITrackCancel trackCancel)
        {
            bool isCancelled = false;

            ESRI.ArcGIS.esriSystem.IStepProgressor     stepProgressor = (ESRI.ArcGIS.esriSystem.IStepProgressor)progressDialog;
            ESRI.ArcGIS.Geodatabase.IRelationshipClass deviceHasPorts = ConfigUtil.GetPortRelationship((ESRI.ArcGIS.Geodatabase.IFeatureClass)device.Class);
            Guid g;

            if (null != deviceHasPorts)
            {
                using (ESRI.ArcGIS.ADF.ComReleaser releaser = new ESRI.ArcGIS.ADF.ComReleaser())
                {
                    ESRI.ArcGIS.Geodatabase.ITable portTable = (ESRI.ArcGIS.Geodatabase.ITable)deviceHasPorts.DestinationClass;
                    releaser.ManageLifetime(portTable);

                    // Fields to populate on port
                    int portIpidIdx   = portTable.Fields.FindField(ConfigUtil.IpidFieldName);
                    int portNumberIdx = portTable.Fields.FindField(ConfigUtil.PortIdFieldName);
                    int portTypeIdx   = portTable.Fields.FindField(ConfigUtil.PortTypeFieldName);
                    int fkeyIdx       = portTable.Fields.FindField(deviceHasPorts.OriginForeignKey);

                    object originPrimaryKey = device.get_Value(device.Fields.FindField(deviceHasPorts.OriginPrimaryKey));

                    for (int portIdx = lowInputPort; portIdx <= inputPortCount; portIdx++)
                    {
                        stepProgressor.Message = string.Format("Creating input port {0} of {1}", portIdx, inputPortCount);
                        stepProgressor.Step();

                        g = Guid.NewGuid();
                        string portIpid = g.ToString("B").ToUpper();

                        ESRI.ArcGIS.Geodatabase.IRow portRow = portTable.CreateRow();
                        releaser.ManageLifetime(portRow);

                        portRow.set_Value(portIpidIdx, portIpid);
                        portRow.set_Value(portTypeIdx, PortType.Input);
                        portRow.set_Value(portNumberIdx, portIdx);
                        portRow.set_Value(fkeyIdx, originPrimaryKey);

                        portRow.Store();

                        if (!trackCancel.Continue())
                        {
                            isCancelled = true;
                            break;
                        }
                    }

                    if (trackCancel.Continue())
                    {
                        for (int portIdx = lowOutputPort; portIdx <= outputPortCount; portIdx++)
                        {
                            stepProgressor.Message = string.Format("Creating output port {0} of {1}", portIdx, outputPortCount);
                            stepProgressor.Step();

                            g = Guid.NewGuid();
                            string portIpid = g.ToString("B").ToUpper();

                            ESRI.ArcGIS.Geodatabase.IRow portRow = portTable.CreateRow();
                            releaser.ManageLifetime(portRow);

                            portRow.set_Value(portIpidIdx, portIpid);
                            portRow.set_Value(portTypeIdx, PortType.Output);
                            portRow.set_Value(portNumberIdx, portIdx);
                            portRow.set_Value(fkeyIdx, originPrimaryKey);

                            portRow.Store();

                            if (!trackCancel.Continue())
                            {
                                isCancelled = true;
                                break;
                            }
                        }
                    }
                }
            }

            return(!isCancelled);
        }
        /// <summary>
        /// Looks for a fiber splice record at one end of a given cable
        /// </summary>
        /// <param name="fiberSpliceTable">Fiber splice table</param>
        /// <param name="cableId">Cable ID of the cable we are checking</param>
        /// <param name="fiberNumber">Fiber Number we are checking</param>
        /// <param name="checkFromEnd">Which end of the cable are we checking?</param>
        /// <param name="nextCableId">(out) Cable ID of the cable spliced on this end, or string.Empty if none</param>
        /// <param name="nextFiberNumber">(out) Fiber Number of spliced on this end, or -1 if none</param>
        /// <param name="spliceClosureIpid">(out) IPID of Splice Closure</param>
        /// <param name="isNextFromEnd">(out) Is the result cable spliced on its from end or its to end?</param>
        /// <returns>The splice record</returns>
        private ESRI.ArcGIS.Geodatabase.IRow GetNextSplice(ESRI.ArcGIS.Geodatabase.ITable fiberSpliceTable, string cableId, int fiberNumber, bool checkFromEnd, out string nextCableId, out int nextFiberNumber, out string spliceClosureIpid, out bool isNextFromEnd)
        {
            ESRI.ArcGIS.Geodatabase.IRow spliceRow = null;

            spliceClosureIpid = string.Empty;
            nextCableId       = cableId;
            nextFiberNumber   = fiberNumber;
            isNextFromEnd     = checkFromEnd;

            using (ESRI.ArcGIS.ADF.ComReleaser releaser = new ESRI.ArcGIS.ADF.ComReleaser())
            {
                ESRI.ArcGIS.Geodatabase.IQueryFilter filter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
                filter.WhereClause = string.Format("({0}='{1}' AND {2}={3} AND {4}='{5}')"
                                                   + " OR ({6}='{1}' AND {7}={3} AND {8}='{5}')",
                                                   ConfigUtil.ACableIdFieldName,
                                                   cableId,
                                                   ConfigUtil.AFiberNumberFieldName,
                                                   fiberNumber,
                                                   ConfigUtil.IsAFromEndFieldName,
                                                   (checkFromEnd ? "T" : "F"),
                                                   ConfigUtil.BCableIdFieldName,
                                                   ConfigUtil.BFiberNumberFieldName,
                                                   ConfigUtil.IsBFromEndFieldName);

                releaser.ManageLifetime(filter);

                // TODO: should we give a warning if the rowcount is more than 1? We should technically only find one splice
                // record on this end of the fiber...

                ESRI.ArcGIS.Geodatabase.ICursor search = fiberSpliceTable.Search(filter, false);
                releaser.ManageLifetime(search);

                spliceRow = search.NextRow();
                if (null != spliceRow)
                {
                    object scIpidValue = spliceRow.get_Value(_spliceClosureIpidIdx);
                    if (DBNull.Value != scIpidValue)
                    {
                        spliceClosureIpid = scIpidValue.ToString();
                    }

                    string aCableId = spliceRow.get_Value(_aCableIdx).ToString();
                    if (0 == string.Compare(aCableId, cableId))
                    {
                        // b is the one we want to return
                        nextCableId     = spliceRow.get_Value(_bCableIdx).ToString();
                        nextFiberNumber = (int)spliceRow.get_Value(_bFiberNumIdx);
                        isNextFromEnd   = spliceRow.get_Value(_isBFromIdx).ToString() == "T" ? true : false;
                    }
                    else
                    {
                        // a is the one we want to return
                        nextCableId     = aCableId;
                        nextFiberNumber = (int)spliceRow.get_Value(_aFiberNumIdx);
                        isNextFromEnd   = spliceRow.get_Value(_isAFromIdx).ToString() == "T" ? true : false;
                    }
                }
            }

            return(spliceRow);
        }
        private FiberCableWrapper GetConnectedFiber(DeviceWrapper device, int portId, PortType portType, out int fiberNumber)
        {
            FiberCableWrapper result = null;

            fiberNumber = -1;

            ESRI.ArcGIS.Geodatabase.IFeatureClass      deviceFtClass  = (ESRI.ArcGIS.Geodatabase.IFeatureClass)device.Feature.Class;
            ESRI.ArcGIS.Geodatabase.IRelationshipClass deviceHasPorts = ConfigUtil.GetPortRelationship(deviceFtClass);
            if (null == deviceHasPorts)
            {
                throw new Exception("Device to port relationship is missing or cannot be opened.");
            }

            ESRI.ArcGIS.Geodatabase.ITable portTable = deviceHasPorts.DestinationClass as ESRI.ArcGIS.Geodatabase.ITable;
            if (null == portTable)
            {
                throw new Exception("Port table is missing or cannot be opened.");
            }


            using (ESRI.ArcGIS.ADF.ComReleaser releaser = new ESRI.ArcGIS.ADF.ComReleaser())
            {
                ESRI.ArcGIS.Geodatabase.IQueryFilter filter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
                releaser.ManageLifetime(filter);
                filter.WhereClause = string.Format("{0}='{1}' AND {2}={3} AND {4}='{5}'",
                                                   deviceHasPorts.OriginForeignKey,
                                                   device.Feature.get_Value(deviceFtClass.FindField(deviceHasPorts.OriginPrimaryKey)),
                                                   ConfigUtil.PortIdFieldName,
                                                   portId,
                                                   ConfigUtil.PortTypeFieldName,
                                                   PortType.Input == portType ? 1 : 2);

                ESRI.ArcGIS.Geodatabase.ICursor cursor = portTable.Search(filter, false);
                releaser.ManageLifetime(cursor);
                ESRI.ArcGIS.Geodatabase.IRow portRow = cursor.NextRow();

                if (null != portRow)
                {
                    //releaser.ManageLifetime(portRow);

                    object cableIdValue = portRow.get_Value(portTable.FindField(ConfigUtil.ConnectedCableFieldName));
                    if (DBNull.Value != cableIdValue)
                    {
                        ESRI.ArcGIS.Geodatabase.IFeatureClass cableFtClass = _wkspHelper.FindFeatureClass(ConfigUtil.FiberCableFtClassName);
                        filter.WhereClause = string.Format("{0}='{1}'", ConfigUtil.IpidFieldName, cableIdValue);
                        ESRI.ArcGIS.Geodatabase.IFeatureCursor cableCursor = cableFtClass.Search(filter, false);
                        releaser.ManageLifetime(cableCursor);

                        ESRI.ArcGIS.Geodatabase.IFeature cable = cableCursor.NextFeature();
                        if (null != cable)
                        {
                            result = new FiberCableWrapper(cable);
                            object fiberNumberValue = portRow.get_Value(portTable.FindField(ConfigUtil.ConnectedFiberFieldName));
                            if (DBNull.Value != fiberNumberValue)
                            {
                                int.TryParse(fiberNumberValue.ToString(), out fiberNumber);
                            }
                        }
                    }
                }
            }

            return(result);
        }
        ///// <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);
            }
        }
Example #18
0
        /// <summary>
        /// Splices all available strands from one cable to the other, at a given splice closure
        /// </summary>
        /// <param name="cableA">Cable A</param>
        /// <param name="cableB">Cable B</param>
        /// <param name="splice">Splice Closure</param>
        /// <param name="strands">Strands to splice together</param>
        /// <param name="isExistingOperation">Flag to control whether we need to wrap this in an edit operation</param>
        /// <returns>Success</returns>
        public bool CreateSplices(FiberCableWrapper cableA, SpliceableCableWrapper cableB, SpliceClosureWrapper splice, Dictionary <int, FiberSplice> strands, bool isExistingOperation)
        {
            bool success         = false;
            bool isOperationOpen = false;

            #region Validation
            if (null == cableA)
            {
                throw new ArgumentNullException("cableA");
            }

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

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

            if (!isExistingOperation)
            {
                _editor.StartOperation();
                isOperationOpen = true;
            }


            if (null == splice)
            {
                splice = GenerateSpliceClosure(cableA, cableB);
            }
            else if (0 == splice.IPID.Length)
            {
                // Populate an IPID; we will need it
                ESRI.ArcGIS.Geodatabase.IFeature spliceFt = splice.Feature;
                Guid g = Guid.NewGuid();

                spliceFt.set_Value(spliceFt.Fields.FindField(ConfigUtil.IpidFieldName), g.ToString("B").ToUpper());
                spliceFt.Store();
            }

            try
            {
                ESRI.ArcGIS.Geodatabase.ITable fiberSpliceTable = _wkspHelper.FindTable(ConfigUtil.FiberSpliceTableName);
//                ESRI.ArcGIS.Geodatabase.ITable fiberSpliceTable = GdbUtils.GetTable(cableA.Feature.Class, ConfigUtil.FiberSpliceTableName);
                int aCableIdx     = fiberSpliceTable.FindField(ConfigUtil.ACableIdFieldName);
                int bCableIdx     = fiberSpliceTable.FindField(ConfigUtil.BCableIdFieldName);
                int aFiberNumIdx  = fiberSpliceTable.FindField(ConfigUtil.AFiberNumberFieldName);
                int bFiberNumIdx  = fiberSpliceTable.FindField(ConfigUtil.BFiberNumberFieldName);
                int spliceIpidIdx = fiberSpliceTable.FindField(ConfigUtil.SpliceClosureIpidFieldName);
                int isAFromIdx    = fiberSpliceTable.FindField(ConfigUtil.IsAFromEndFieldName);
                int isBFromIdx    = fiberSpliceTable.FindField(ConfigUtil.IsBFromEndFieldName);
                int lossIdx       = fiberSpliceTable.FindField(ConfigUtil.LossFieldName);
                int typeIdx       = fiberSpliceTable.FindField(ConfigUtil.TypeFieldName);
                ESRI.ArcGIS.Geodatabase.IField typeField = fiberSpliceTable.Fields.get_Field(typeIdx);

                string aCableId   = cableA.IPID;
                string bCableId   = cableB.IPID;
                string isAFromEnd = cableB.IsOtherFromEnd ? "T" : "F";
                string isBFromEnd = cableB.IsThisFromEnd ? "T" : "F";
                string spliceIpid = splice.IPID;

                using (ESRI.ArcGIS.ADF.ComReleaser releaser = new ESRI.ArcGIS.ADF.ComReleaser())
                {
                    // TODO cant use insert cursor since edit events wont fire.
                    // We need evetns to fire for dynamic values to get populated.
                    // ESRI.ArcGIS.Geodatabase.ICursor insertCursor = fiberSpliceTable.Insert(true);
                    // releaser.ManageLifetime(insertCursor);

                    foreach (KeyValuePair <int, FiberSplice> pair in strands)
                    {
                        IRow row = fiberSpliceTable.CreateRow();

                        releaser.ManageLifetime(row);

                        FiberSplice fiberSplice = pair.Value;

                        row.set_Value(aCableIdx, aCableId);
                        row.set_Value(bCableIdx, bCableId);
                        row.set_Value(aFiberNumIdx, pair.Key);
                        row.set_Value(bFiberNumIdx, fiberSplice.BRange.Low);
                        row.set_Value(spliceIpidIdx, spliceIpid);
                        row.set_Value(isAFromIdx, isAFromEnd);
                        row.set_Value(isBFromIdx, isBFromEnd);

                        if (null == fiberSplice.Loss)
                        {
                            row.set_Value(lossIdx, DBNull.Value);
                        }
                        else
                        {
                            row.set_Value(lossIdx, fiberSplice.Loss);
                        }

                        object typeValue = DBNull.Value;
                        if (null != fiberSplice.Type)
                        {
                            try
                            {
                                typeValue = GdbUtils.CheckForCodedValue(typeField, fiberSplice.Type);
                            }
                            catch
                            {
                                // TODO: Log a warning about why we can't set the default split splice type?
                            }
                        }

                        row.set_Value(typeIdx, typeValue);
                        row.Store();
                    }
                }

                if (isOperationOpen)
                {
                    _editor.StopOperation("Edit Splices");
                }

                success = true;
            }
            catch
            {
                if (isOperationOpen)
                {
                    _editor.AbortOperation();
                }

                success = false;
            }

            return(success);
        }
Example #19
0
        /// <summary>
        /// Deletes given splices from one cable to the other, at a given splice closure
        /// </summary>
        /// <param name="cableA">Cable A</param>
        /// <param name="cableB">Cable B</param>
        /// <param name="splice">Splice Closure</param>
        /// <param name="strands">Strands to remove</param>
        /// <param name="isExistingOperation">Flag to control whether we need to wrap this in an edit operation</param>
        /// <returns>Success</returns>
        public bool BreakSplices(FiberCableWrapper cableA, SpliceableCableWrapper cableB, SpliceClosureWrapper splice, Dictionary <int, FiberSplice> strands, bool isExistingOperation)
        {
            bool success         = false;
            bool isOperationOpen = false;

            #region Validation
            if (null == cableA)
            {
                throw new ArgumentNullException("cableA");
            }

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

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

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

            if (!isExistingOperation)
            {
                _editor.StartOperation();
                isOperationOpen = true;
            }

            try
            {
                ESRI.ArcGIS.Geodatabase.ITable spliceTable = _wkspHelper.FindTable(ConfigUtil.FiberSpliceTableName);
//                ESRI.ArcGIS.Geodatabase.ITable spliceTable = GdbUtils.GetTable(splice.Feature.Class, ConfigUtil.FiberSpliceTableName);

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

                    // A and B is arbitrary, so we check the given combinations going both ways. The structure is:
                    // Where the splice closure IPID is ours and
                    // ((the A Cable/Fiber matches our A cable and B Cable/Fiber matches our B)
                    //  OR (the A Cable/Fiber matches our B cable and B Cable/Fiber matches our A))
                    string format = "{0}='{1}' AND (({2}='{3}' AND {4}={5} AND {6}='{7}' AND {8}={9})" +
                                    " OR ({2}='{7}' AND {4}={9} AND {6}='{3}' AND {8}={5}))";

                    foreach (KeyValuePair <int, FiberSplice> pair in strands)
                    {
                        filter.WhereClause = string.Format(format,
                                                           ConfigUtil.SpliceClosureIpidFieldName,
                                                           splice.IPID,
                                                           ConfigUtil.ACableIdFieldName,
                                                           cableA.IPID,
                                                           ConfigUtil.AFiberNumberFieldName,
                                                           pair.Key,
                                                           ConfigUtil.BCableIdFieldName,
                                                           cableB.IPID,
                                                           ConfigUtil.BFiberNumberFieldName,
                                                           pair.Value.BRange.Low);

                        spliceTable.DeleteSearchedRows(filter);
                    }

                    if (isOperationOpen)
                    {
                        _editor.StopOperation("Break Splices");
                        isOperationOpen = false;
                    }

                    success = true;
                }
            }
            catch
            {
                if (isOperationOpen)
                {
                    _editor.AbortOperation();
                }

                success = false;
            }

            return(success);
        }
        /// <summary>
        /// Deletes given splices from one cable to the other, at a given splice closure
        /// </summary>
        /// <param name="cableA">Cable A</param>
        /// <param name="cableB">Cable B</param>
        /// <param name="splice">Splice Closure</param>
        /// <param name="strands">Strands to remove</param>
        /// <param name="isExistingOperation">Flag to control whether we need to wrap this in an edit operation</param>
        /// <returns>Success</returns>
        public bool BreakSplices(FiberCableWrapper cableA, SpliceableCableWrapper cableB, SpliceClosureWrapper splice, Dictionary<int,FiberSplice> strands, bool isExistingOperation)
        {
            bool success = false;
            bool isOperationOpen = false;

            #region Validation
            if (null == cableA)
            {
                throw new ArgumentNullException("cableA");
            }

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

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

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

            if (!isExistingOperation)
            {
                _editor.StartOperation();
                isOperationOpen = true;
            }

            try
            {
                ESRI.ArcGIS.Geodatabase.ITable spliceTable = _wkspHelper.FindTable(ConfigUtil.FiberSpliceTableName);
            //                ESRI.ArcGIS.Geodatabase.ITable spliceTable = GdbUtils.GetTable(splice.Feature.Class, ConfigUtil.FiberSpliceTableName);

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

                    // A and B is arbitrary, so we check the given combinations going both ways. The structure is:
                    // Where the splice closure IPID is ours and
                    // ((the A Cable/Fiber matches our A cable and B Cable/Fiber matches our B)
                    //  OR (the A Cable/Fiber matches our B cable and B Cable/Fiber matches our A))
                    string format = "{0}='{1}' AND (({2}='{3}' AND {4}={5} AND {6}='{7}' AND {8}={9})" +
                        " OR ({2}='{7}' AND {4}={9} AND {6}='{3}' AND {8}={5}))";

                    foreach (KeyValuePair<int, FiberSplice> pair in strands)
                    {
                        filter.WhereClause = string.Format(format,
                            ConfigUtil.SpliceClosureIpidFieldName,
                            splice.IPID,
                            ConfigUtil.ACableIdFieldName,
                            cableA.IPID,
                            ConfigUtil.AFiberNumberFieldName,
                            pair.Key,
                            ConfigUtil.BCableIdFieldName,
                            cableB.IPID,
                            ConfigUtil.BFiberNumberFieldName,
                            pair.Value.BRange.Low);

                        spliceTable.DeleteSearchedRows(filter);
                    }

                    if (isOperationOpen)
                    {
                        _editor.StopOperation("Break Splices");
                        isOperationOpen = false;
                    }

                    success = true;
                }
            }
            catch
            {
                if (isOperationOpen)
                {
                    _editor.AbortOperation();
                }

                success = false;
            }

            return success;
        }
        /// <summary>
        /// Returns all features from a given feature class that have a vertex or endpoint coincident with a given point 
        /// </summary>
        /// <param name="point">IPoint to use as the spatial filter</param>
        /// <param name="searchFtClass">IFeatureClass to search in</param>
        /// <param name="linearEndpointsOnly">Flag to use only the endpoints of a line instead of all vertices</param>
        /// <param name="buffer">Search geometry buffer in map units</param>
        /// <returns>List of IFeature</returns>
        public static List<ESRI.ArcGIS.Geodatabase.IFeature> GetFeaturesWithCoincidentVertices(ESRI.ArcGIS.Geometry.IPoint point, ESRI.ArcGIS.Geodatabase.IFeatureClass searchFtClass, bool linearEndpointsOnly, double buffer)
        {
            List<ESRI.ArcGIS.Geodatabase.IFeature> result = new List<ESRI.ArcGIS.Geodatabase.IFeature>();

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

                ESRI.ArcGIS.Geometry.IEnvelope filterGeometry = point.Envelope;
                if (0 < buffer)
                {
                    filterGeometry.Expand(buffer, buffer, false);
                }

                filter.SpatialRel = ESRI.ArcGIS.Geodatabase.esriSpatialRelEnum.esriSpatialRelIntersects;
                filter.Geometry = filterGeometry;

                ESRI.ArcGIS.Geodatabase.IFeatureCursor fts = searchFtClass.Search(filter, false);
                releaser.ManageLifetime(fts);

                ESRI.ArcGIS.Geodatabase.IFeature ft = fts.NextFeature();
                while (null != ft)
                {
                    if (searchFtClass.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint)
                    {
                        result.Add(ft);
                    }
                    else if (searchFtClass.ShapeType == ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline && linearEndpointsOnly)
                    {
                        ESRI.ArcGIS.Geometry.IPolyline polyline = (ESRI.ArcGIS.Geometry.IPolyline)ft.Shape;
                        ESRI.ArcGIS.Geometry.IRelationalOperator fromPoint = polyline.FromPoint as ESRI.ArcGIS.Geometry.IRelationalOperator;
                        ESRI.ArcGIS.Geometry.IRelationalOperator toPoint = polyline.ToPoint as ESRI.ArcGIS.Geometry.IRelationalOperator;

                        if (fromPoint.Equals(point) || toPoint.Equals(point))
                        {
                            result.Add(ft);
                        }
                    }
                    else
                    {
                        ESRI.ArcGIS.Geometry.IPointCollection pointCollection = ft.Shape as ESRI.ArcGIS.Geometry.IPointCollection;
                        if (null != pointCollection)
                        {
                            for (int i = 0; i < pointCollection.PointCount; i++)
                            {
                                ESRI.ArcGIS.Geometry.IRelationalOperator testPoint = pointCollection.get_Point(i) as ESRI.ArcGIS.Geometry.IRelationalOperator;
                                if (testPoint.Equals(point))
                                {
                                    result.Add(ft);
                                    break;
                                }
                            }
                        }
                    }

                    ft = fts.NextFeature();
                }
            }

            return result;
        }
        /// <summary>
        /// Splices all available strands from one cable to the other, at a given splice closure
        /// </summary>
        /// <param name="cableA">Cable A</param>
        /// <param name="cableB">Cable B</param>
        /// <param name="splice">Splice Closure</param>
        /// <param name="strands">Strands to splice together</param>
        /// <param name="isExistingOperation">Flag to control whether we need to wrap this in an edit operation</param>
        /// <returns>Success</returns>
        public bool CreateSplices(FiberCableWrapper cableA, SpliceableCableWrapper cableB, SpliceClosureWrapper splice, Dictionary<int,FiberSplice> strands, bool isExistingOperation)
        {
            bool success = false;
            bool isOperationOpen = false;

            #region Validation
            if (null == cableA)
            {
                throw new ArgumentNullException("cableA");
            }

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

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

            if (!isExistingOperation)
            {
                _editor.StartOperation();
                isOperationOpen = true;
            }

            if (null == splice)
            {
                splice = GenerateSpliceClosure(cableA, cableB);
            }
            else if (0 == splice.IPID.Length)
            {
                // Populate an IPID; we will need it
                ESRI.ArcGIS.Geodatabase.IFeature spliceFt = splice.Feature;
                Guid g = Guid.NewGuid();

                spliceFt.set_Value(spliceFt.Fields.FindField(ConfigUtil.IpidFieldName), g.ToString("B").ToUpper());
                spliceFt.Store();
            }

            try
            {
                ESRI.ArcGIS.Geodatabase.ITable fiberSpliceTable = _wkspHelper.FindTable(ConfigUtil.FiberSpliceTableName);
            //                ESRI.ArcGIS.Geodatabase.ITable fiberSpliceTable = GdbUtils.GetTable(cableA.Feature.Class, ConfigUtil.FiberSpliceTableName);
                int aCableIdx = fiberSpliceTable.FindField(ConfigUtil.ACableIdFieldName);
                int bCableIdx = fiberSpliceTable.FindField(ConfigUtil.BCableIdFieldName);
                int aFiberNumIdx = fiberSpliceTable.FindField(ConfigUtil.AFiberNumberFieldName);
                int bFiberNumIdx = fiberSpliceTable.FindField(ConfigUtil.BFiberNumberFieldName);
                int spliceIpidIdx = fiberSpliceTable.FindField(ConfigUtil.SpliceClosureIpidFieldName);
                int isAFromIdx = fiberSpliceTable.FindField(ConfigUtil.IsAFromEndFieldName);
                int isBFromIdx = fiberSpliceTable.FindField(ConfigUtil.IsBFromEndFieldName);
                int lossIdx = fiberSpliceTable.FindField(ConfigUtil.LossFieldName);
                int typeIdx = fiberSpliceTable.FindField(ConfigUtil.TypeFieldName);
                ESRI.ArcGIS.Geodatabase.IField typeField = fiberSpliceTable.Fields.get_Field(typeIdx);

                string aCableId = cableA.IPID;
                string bCableId = cableB.IPID;
                string isAFromEnd = cableB.IsOtherFromEnd ? "T" : "F";
                string isBFromEnd = cableB.IsThisFromEnd ? "T" : "F";
                string spliceIpid = splice.IPID;

                using (ESRI.ArcGIS.ADF.ComReleaser releaser = new ESRI.ArcGIS.ADF.ComReleaser())
                {
                    // TODO cant use insert cursor since edit events wont fire.
                    // We need evetns to fire for dynamic values to get populated.
                    // ESRI.ArcGIS.Geodatabase.ICursor insertCursor = fiberSpliceTable.Insert(true);
                    // releaser.ManageLifetime(insertCursor);

                    foreach (KeyValuePair<int, FiberSplice> pair in strands)
                    {
                        IRow row = fiberSpliceTable.CreateRow();

                        releaser.ManageLifetime(row);

                        FiberSplice fiberSplice = pair.Value;

                        row.set_Value(aCableIdx, aCableId);
                        row.set_Value(bCableIdx, bCableId);
                        row.set_Value(aFiberNumIdx, pair.Key);
                        row.set_Value(bFiberNumIdx, fiberSplice.BRange.Low);
                        row.set_Value(spliceIpidIdx, spliceIpid);
                        row.set_Value(isAFromIdx, isAFromEnd);
                        row.set_Value(isBFromIdx, isBFromEnd);

                        if (null == fiberSplice.Loss)
                        {
                            row.set_Value(lossIdx, DBNull.Value);
                        }
                        else
                        {
                            row.set_Value(lossIdx, fiberSplice.Loss);
                        }

                        object typeValue = DBNull.Value;
                        if (null != fiberSplice.Type)
                        {
                            try
                            {
                                typeValue = GdbUtils.CheckForCodedValue(typeField, fiberSplice.Type);
                            }
                            catch
                            {
                                // TODO: Log a warning about why we can't set the default split splice type?
                            }
                        }

                        row.set_Value(typeIdx, typeValue);
                        row.Store();
                    }
                }

                if (isOperationOpen)
                {
                    _editor.StopOperation("Edit Splices");
                }

                success = true;
            }
            catch
            {
                if (isOperationOpen)
                {
                    _editor.AbortOperation();
                }

                success = false;
            }

            return success;
        }
        /// <summary>
        /// Determines if all the ranges fall between 1 to port count
        /// </summary>
        /// <param name="ranges">Ranges to check</param>
        /// <param name="device">Device to check on</param>
        /// <param name="portType">Port type</param>
        /// <returns>True if valid ranges</returns>
        public static bool AreRangesWithinPortCount(List<Range> ranges, DeviceWrapper device, PortType portType)
        {
            bool result = false; // Default to false in case we can't even find the port relationship class

            #region Validation

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

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

            #endregion

            ESRI.ArcGIS.Geodatabase.IFeatureClass ftClass = device.Feature.Class as ESRI.ArcGIS.Geodatabase.IFeatureClass;
            if (null != ftClass)
            {
                using (ESRI.ArcGIS.ADF.ComReleaser releaser = new ESRI.ArcGIS.ADF.ComReleaser())
                {
                    ESRI.ArcGIS.Geodatabase.IRelationshipClass deviceHasPorts = ConfigUtil.GetPortRelationship(ftClass);
                    if (null != deviceHasPorts)
                    {
                        ESRI.ArcGIS.Geodatabase.ITable portTable = deviceHasPorts.DestinationClass as ESRI.ArcGIS.Geodatabase.ITable;
                        int portIdIdx = portTable.FindField(ConfigUtil.PortIdFieldName);

                        if (-1 < portIdIdx)
                        {
                            result = true; // Now that we have the ports, assume we're ok until we find a problem

                            ESRI.ArcGIS.Geodatabase.IQueryFilter filter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
                            releaser.ManageLifetime(filter);

                            filter.SubFields = ConfigUtil.PortIdFieldName;
                            filter.WhereClause = string.Format("{0}='{1}' AND {2}='{3}' AND {4} IS NOT NULL",
                                deviceHasPorts.OriginForeignKey,
                                device.Feature.get_Value(ftClass.FindField(deviceHasPorts.OriginPrimaryKey)),
                                ConfigUtil.PortTypeFieldName,
                                PortType.Input == portType ? 1 : 2,
                                ConfigUtil.PortIdFieldName);

                            ((ESRI.ArcGIS.Geodatabase.IQueryFilterDefinition)filter).PostfixClause = string.Format("ORDER BY {0}", ConfigUtil.PortIdFieldName);
                            ESRI.ArcGIS.Geodatabase.ICursor cursor = portTable.Search(filter, true);
                            releaser.ManageLifetime(cursor);

                            int minPort = int.MinValue;
                            int maxPort = int.MaxValue;
                            ESRI.ArcGIS.Geodatabase.IRow row = cursor.NextRow();

                            if (null != row)
                            {
                                minPort = (int)row.get_Value(portIdIdx);

                                while (null != row)
                                {
                                    maxPort = (int)row.get_Value(portIdIdx);
                                    ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(row);

                                    row = cursor.NextRow();
                                }
                            }

                            foreach (Range r in ranges)
                            {
                                if (r.High > maxPort
                                    || minPort > r.Low)
                                {
                                    result = false;
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            return result;
        }
        /// <summary>
        /// Returns a list of connections between the cable and the device, at the cable's given end, to the device's given port type
        /// </summary>
        /// <param name="cable">Cable to check</param>
        /// <param name="device">Device to check</param>
        /// <param name="isFromEnd">Digitized end of cable</param>
        /// <param name="portType">Input or output</param>
        /// <returns>List of Connection</returns>
        public List<Connection> GetConnections(FiberCableWrapper cable, DeviceWrapper device, bool isFromEnd, PortType portType)
        {
            if (null == cable)
            {
                throw new ArgumentNullException("cable");
            }

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

            List<Connection> result = new List<Connection>();
            List<int> ports = new List<int>();
            List<int> strands = new List<int>();

            using (ESRI.ArcGIS.ADF.ComReleaser releaser = new ESRI.ArcGIS.ADF.ComReleaser())
            {
                ESRI.ArcGIS.Geodatabase.IFeatureClass deviceFtClass = device.Feature.Class as ESRI.ArcGIS.Geodatabase.IFeatureClass;
                ESRI.ArcGIS.Geodatabase.IRelationshipClass deviceHasPorts = ConfigUtil.GetPortRelationship(deviceFtClass);
                if (null == deviceHasPorts)
                {
                    throw new Exception("Unable to find port relationship class.");
                }

                ESRI.ArcGIS.Geodatabase.ITable portTable = deviceHasPorts.DestinationClass as ESRI.ArcGIS.Geodatabase.ITable;
                if (null == portTable)
                {
                    throw new Exception("Invalid destination on port relationship class.");
                }

                ESRI.ArcGIS.Geodatabase.IQueryFilter filter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
                releaser.ManageLifetime(filter);
                filter.WhereClause = string.Format("{0}='{1}' AND {2}='{3}' AND {4}='{5}' AND {6}='{7}' AND {8} IS NOT NULL AND {9} IS NOT NULL",
                    deviceHasPorts.OriginForeignKey,
                    device.Feature.get_Value(deviceFtClass.FindField(deviceHasPorts.OriginPrimaryKey)),
                    ConfigUtil.ConnectedCableFieldName,
                    cable.IPID,
                    ConfigUtil.PortTypeFieldName,
                    (PortType.Input == portType ? "1" : "2"),
                    ConfigUtil.ConnectedEndFieldName,
                    (isFromEnd ? "T" : "F"),
                    ConfigUtil.ConnectedFiberFieldName,
                    ConfigUtil.PortIdFieldName);

                // ORDER BY does not work outside of SDE.
                // Removing for now, should not be important.
                string orderFormat = "ORDER BY {0}";
                if (PortType.Input == portType)
                {
            //                    ((ESRI.ArcGIS.Geodatabase.IQueryFilterDefinition2)filter).PostfixClause = string.Format(orderFormat, ConfigUtil.ConnectedFiberFieldName);
                }
                else
                {
            //                    ((ESRI.ArcGIS.Geodatabase.IQueryFilterDefinition2)filter).PostfixClause = string.Format(orderFormat, ConfigUtil.PortIdFieldName);
                }

                ESRI.ArcGIS.Geodatabase.ICursor portCursor = portTable.Search(filter, true);
                ESRI.ArcGIS.Geodatabase.IRow portRow = portCursor.NextRow();

                int portIdIdx = portTable.FindField(ConfigUtil.PortIdFieldName);
                int fiberNumberIdx = portTable.FindField(ConfigUtil.ConnectedFiberFieldName);

                while (null != portRow)
                {
                    ports.Add((int)portRow.get_Value(portIdIdx));
                    strands.Add((int)portRow.get_Value(fiberNumberIdx));

                    ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(portRow);
                    portRow = portCursor.NextRow();
                }

                ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(portCursor);
            }

            List<Range> portRanges = SpliceAndConnectionUtils.MergeRanges(ports);
            List<Range> strandRanges = SpliceAndConnectionUtils.MergeRanges(strands);

            if (PortType.Input == portType)
            {
                result = SpliceAndConnectionUtils.MatchUp(strandRanges, portRanges);
            }
            else
            {
                result = SpliceAndConnectionUtils.MatchUp(portRanges, strandRanges);
            }

            return result;
        }
        /// <summary>
        /// Gets a list of all strand numbers from a cable that are connected on the given end
        /// </summary>
        /// <param name="cable">Cable to check</param>
        /// <param name="isFromEnd">True to check from end, False to check to end</param>
        /// <returns>List of int</returns>
        private static List<int> GetConnectedStrands(FiberCableWrapper cable, bool isFromEnd)
        {
            #region Validation
            if (null == cable)
            {
                throw new ArgumentNullException("cable");
            }

            #endregion

            List<int> result = new List<int>();

            using (ESRI.ArcGIS.ADF.ComReleaser releaser = new ESRI.ArcGIS.ADF.ComReleaser())
            {
                ESRI.ArcGIS.Geodatabase.IFeatureClass cableFtClass = cable.Feature.Class as ESRI.ArcGIS.Geodatabase.IFeatureClass;

                string[] deviceClassNames = ConfigUtil.DeviceFeatureClassNames;
                for (int i = 0; i < deviceClassNames.Length; i++)
                {
                    string deviceClassName = deviceClassNames[i];
                    ESRI.ArcGIS.Geodatabase.IFeatureClass deviceFtClass = TelecomWorkspaceHelper.Instance().FindFeatureClass(deviceClassName);
            //                    ESRI.ArcGIS.Geodatabase.IFeatureClass deviceFtClass = GdbUtils.GetFeatureClass(cableFtClass, deviceClassName);
                    if (null != deviceFtClass)
                    {
                        ESRI.ArcGIS.Geodatabase.IRelationshipClass deviceHasPorts = ConfigUtil.GetPortRelationship(deviceFtClass);
                        if (null != deviceHasPorts)
                        {
                            ESRI.ArcGIS.Geodatabase.ITable portTable = deviceHasPorts.DestinationClass as ESRI.ArcGIS.Geodatabase.ITable;
                            if (null != portTable)
                            {
                                ESRI.ArcGIS.Geodatabase.IQueryFilter filter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
                                releaser.ManageLifetime(filter);

                                filter.WhereClause = string.Format("{0}='{1}' AND {2}='{3}' AND {4} IS NOT NULL",
                                    ConfigUtil.ConnectedCableFieldName,
                                    cable.IPID,
                                    ConfigUtil.ConnectedEndFieldName,
                                    (isFromEnd ? "T" : "F"),
                                    ConfigUtil.ConnectedFiberFieldName);

                                ESRI.ArcGIS.Geodatabase.ICursor portCursor = portTable.Search(filter, true);
                                ESRI.ArcGIS.Geodatabase.IRow portRow = portCursor.NextRow();
                                int fiberIdIdx = portTable.FindField(ConfigUtil.ConnectedFiberFieldName);

                                while (null != portRow)
                                {
                                    result.Add((int)portRow.get_Value(fiberIdIdx));

                                    ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(portRow);
                                    portRow = portCursor.NextRow();
                                }

                                ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(portCursor);
                            }
                        }
                    }
                }
            }

            return result;
        }
        /// <summary>
        /// Break all connections for a given cable
        /// </summary>
        /// <param name="cable">Cable to break connections</param>
        /// <param name="isExistingOperation">Should we start/stop the edit operation, or are we already in one?</param>
        /// <returns>Success</returns>
        public bool BreakAllConnections(FiberCableWrapper cable, bool isExistingOperation)
        {
            bool success = false;
            bool isOperationOpen = false;

            #region Validation
            if (null == cable)
            {
                throw new ArgumentNullException("cable");
            }

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

            if (!isExistingOperation)
            {
                _editor.StartOperation();
                isOperationOpen = true;
            }

            try
            {
                using (ESRI.ArcGIS.ADF.ComReleaser releaser = new ESRI.ArcGIS.ADF.ComReleaser())
                {
                    ESRI.ArcGIS.Geodatabase.IFeatureClass cableFtClass = cable.Feature.Class as ESRI.ArcGIS.Geodatabase.IFeatureClass;
                    ESRI.ArcGIS.Geodatabase.IQueryFilter filter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
                    releaser.ManageLifetime(filter);

                    filter.WhereClause = string.Format("{0}='{1}'", ConfigUtil.ConnectedCableFieldName, cable.IPID);
                    filter.SubFields = string.Format("{0},{1},{2}", ConfigUtil.ConnectedCableFieldName, ConfigUtil.ConnectedFiberFieldName, ConfigUtil.ConnectedEndFieldName);

                    string[] deviceClassNames = ConfigUtil.DeviceFeatureClassNames;
                    for (int i = 0; i < deviceClassNames.Length; i++)
                    {
                        string deviceClassName = deviceClassNames[i];

                        ESRI.ArcGIS.Geodatabase.IFeatureClass deviceFtClass = _wkspHelper.FindFeatureClass(deviceClassName);
            //                        ESRI.ArcGIS.Geodatabase.IFeatureClass deviceFtClass = GdbUtils.GetFeatureClass(cableFtClass, deviceClassName);
                        ESRI.ArcGIS.Geodatabase.ITable portTable = ConfigUtil.GetPortTable(deviceFtClass);
                        ESRI.ArcGIS.Geodatabase.IRowBuffer buffer = portTable.CreateRowBuffer();
                        // We want to set them to null, so we can just send the empty buffer
                        portTable.UpdateSearchedRows(filter, buffer);

                        ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(buffer);
                    }

                    if (isOperationOpen)
                    {
                        _editor.StopOperation("Break Connections");
                        isOperationOpen = false;
                    }

                    success = true;
                }
            }
            catch
            {
                if (isOperationOpen)
                {
                    _editor.AbortOperation();
                }

                success = false;
            }

            return success;
        }
        /// <summary>
        /// Gets a list of all strand numbers from a cable that are spliced on the given end
        /// </summary>
        /// <param name="cable">Cable to check</param>
        /// <param name="isFromEnd">True to check from end, False to check to end</param>
        /// <returns>List of int</returns>
        private static List<int> GetSplicedStrands(FiberCableWrapper cable, bool isFromEnd)
        {
            if (null == cable)
            {
                throw new ArgumentNullException("cable");
            }

            List<int> result = new List<int>();

            using (ESRI.ArcGIS.ADF.ComReleaser releaser = new ESRI.ArcGIS.ADF.ComReleaser())
            {

                ESRI.ArcGIS.Geodatabase.ITable fiberSpliceTable = TelecomWorkspaceHelper.Instance().FindTable(ConfigUtil.FiberSpliceTableName);
            //                ESRI.ArcGIS.Geodatabase.ITable fiberSpliceTable = GdbUtils.GetTable(cable.Feature.Class, ConfigUtil.FiberSpliceTableName);
                ESRI.ArcGIS.Geodatabase.IQueryFilter filter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
                releaser.ManageLifetime(filter);
                filter.WhereClause = string.Format("{0}='{1}' AND {2}='{3}'",
                    ConfigUtil.ACableIdFieldName,
                    cable.IPID,
                    ConfigUtil.IsAFromEndFieldName,
                    (isFromEnd ? "T" : "F"));

                ESRI.ArcGIS.Geodatabase.ICursor spliceCursor = fiberSpliceTable.Search(filter, true);
                ESRI.ArcGIS.Geodatabase.IRow spliceRow = spliceCursor.NextRow();
                int fiberIdIdx = fiberSpliceTable.FindField(ConfigUtil.AFiberNumberFieldName);

                while (null != spliceRow)
                {
                    result.Add((int)spliceRow.get_Value(fiberIdIdx));

                    ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(spliceRow);
                    spliceRow = spliceCursor.NextRow();
                }

                ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(spliceCursor);

                filter.WhereClause = string.Format("{0}='{1}' AND {2}='{3}'",
                                    ConfigUtil.BCableIdFieldName,
                                    cable.IPID,
                                    ConfigUtil.IsBFromEndFieldName,
                                    (isFromEnd ? "T" : "F")); spliceCursor = fiberSpliceTable.Search(filter, true);

                spliceRow = spliceCursor.NextRow();
                fiberIdIdx = fiberSpliceTable.FindField(ConfigUtil.BFiberNumberFieldName);

                while (null != spliceRow)
                {
                    result.Add((int)spliceRow.get_Value(fiberIdIdx));

                    ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(spliceRow);
                    spliceRow = spliceCursor.NextRow();
                }

                ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(spliceCursor);
            }

            return result;
        }
        /*
         * private String fiberOrBufferColorLookup(int number)
         * {
         *  switch (number)
         *  {
         *      case 1:
         *          return "Blue";
         *      case 2:
         *          return "Orange";
         *      case 3:
         *          return "Green";
         *      case 4:
         *          return "Brown";
         *      case 5:
         *          return "Slate";
         *      case 6:
         *          return "White";
         *      case 7:
         *          return "Red";
         *      case 8:
         *          return "Black";
         *      case 9:
         *          return "Yellow";
         *      case 10:
         *          return "Violet";
         *      case 11:
         *          return "Rose";
         *      case 12:
         *          return "Aqua";
         *      default:
         *          return string.Empty;
         *  }
         * }
         */

        /// <summary>
        /// Generates a number of buffer tubes and fiber records for a fiber cable, given a configuration.
        /// </summary>
        /// <param name="feature">IFeature to generate for</param>
        /// <param name="configuration">Specification of buffer and fiber counts</param>
        /// <param name="progressDialog">Progress dialog for user notification</param>
        /// <param name="trackCancel">TrackCancel used in the progress dialog</param>
        /// <returns>Success</returns>
        private bool GenerateUnits(ESRI.ArcGIS.Geodatabase.IFeature feature, FiberCableConfiguration configuration, ESRI.ArcGIS.Framework.IProgressDialog2 progressDialog, ESRI.ArcGIS.esriSystem.ITrackCancel trackCancel)
        {
            bool isComplete  = false;
            bool isCancelled = false;
            Guid g;

            ESRI.ArcGIS.esriSystem.IStepProgressor stepProgressor = (ESRI.ArcGIS.esriSystem.IStepProgressor)progressDialog;
            ESRI.ArcGIS.Geodatabase.IObjectClass   ftClass        = feature.Class;

            using (ESRI.ArcGIS.ADF.ComReleaser releaser = new ESRI.ArcGIS.ADF.ComReleaser())
            {
                ESRI.ArcGIS.Geodatabase.IRelationshipClass cableHasBuffer = GdbUtils.GetRelationshipClass(ftClass, ConfigUtil.FiberCableToBufferRelClassName);
                releaser.ManageLifetime(cableHasBuffer);
                ESRI.ArcGIS.Geodatabase.IRelationshipClass cableHasFiber = GdbUtils.GetRelationshipClass(ftClass, ConfigUtil.FiberCableToFiberRelClassName);
                releaser.ManageLifetime(cableHasFiber);
                ESRI.ArcGIS.Geodatabase.IRelationshipClass bufferHasFiber = GdbUtils.GetRelationshipClass(ftClass, ConfigUtil.BufferToFiberRelClassName);
                releaser.ManageLifetime(bufferHasFiber);

                ESRI.ArcGIS.Geodatabase.ITable bufferTable = cableHasBuffer.DestinationClass as ESRI.ArcGIS.Geodatabase.ITable;
                ESRI.ArcGIS.Geodatabase.ITable fiberTable  = cableHasFiber.DestinationClass as ESRI.ArcGIS.Geodatabase.ITable;

                // Fields to populate on buffer
                int    bufferIpidIdx      = bufferTable.Fields.FindField(ConfigUtil.IpidFieldName);
                int    fiberCountIdx      = bufferTable.Fields.FindField(ConfigUtil.NumberOfFibersFieldName);
                int    bufferToCableIdx   = bufferTable.Fields.FindField(cableHasBuffer.OriginForeignKey);
                object bufferToCableValue = feature.get_Value(feature.Fields.FindField(cableHasBuffer.OriginPrimaryKey));

                // Fields to populate on fiber
                int    fiberIpidIdx          = fiberTable.Fields.FindField(ConfigUtil.IpidFieldName);
                int    fiberNumberIdx        = fiberTable.Fields.FindField(ConfigUtil.Fiber_NumberFieldName);
                int    fiberColorIdx         = fiberTable.Fields.FindField(ConfigUtil.Fiber_ColorFieldName);
                int    fiberToCableIdx       = fiberTable.Fields.FindField(cableHasFiber.OriginForeignKey);
                object fiberToCableValue     = feature.get_Value(feature.Fields.FindField(cableHasFiber.OriginPrimaryKey));
                int    fiberToBufferIdx      = fiberTable.Fields.FindField(bufferHasFiber.OriginForeignKey);
                int    fiberToBufferValueIdx = bufferTable.Fields.FindField(bufferHasFiber.OriginPrimaryKey);

                // Research using InsertCursor for speed.
                int fiberNumber = 0;
                for (int bufferIdx = 1; bufferIdx <= configuration.BufferCount; bufferIdx++)
                {
                    g = Guid.NewGuid();
                    string bufferId = g.ToString("B").ToUpper();

                    ESRI.ArcGIS.Geodatabase.IRow row = bufferTable.CreateRow();
                    releaser.ManageLifetime(row);

                    row.set_Value(bufferIpidIdx, bufferId);
                    row.set_Value(fiberCountIdx, configuration.FibersPerTube);
                    row.set_Value(bufferToCableIdx, bufferToCableValue);
                    row.Store();

                    object fiberToBufferValue = row.get_Value(fiberToBufferValueIdx);

                    // Research using InsertCursor for speed.
                    for (int fiberIdx = 1; fiberIdx <= configuration.FibersPerTube; fiberIdx++)
                    {
                        fiberNumber++;
                        progressDialog.Description = string.Format("Creating fiber {0} of {1}", fiberNumber, configuration.TotalFiberCount);
                        stepProgressor.Step();

                        g = Guid.NewGuid();
                        ESRI.ArcGIS.Geodatabase.IRow fiberRow = fiberTable.CreateRow();
                        releaser.ManageLifetime(fiberRow);


                        fiberRow.set_Value(fiberIpidIdx, g.ToString("B").ToUpper());
                        fiberRow.set_Value(fiberNumberIdx, fiberNumber);

                        // Dangerous if coded values are altered but while
                        // domain type is int Rather than string coded, this
                        // is quickest way to add this
                        // Dont do for fiber groupings of more than 12
                        if (configuration.FibersPerTube <= 12)
                        {
                            fiberRow.set_Value(fiberColorIdx, fiberIdx);
                        }

                        fiberRow.set_Value(fiberToBufferIdx, fiberToBufferValue);
                        fiberRow.set_Value(fiberToCableIdx, fiberToCableValue);

                        fiberRow.Store();

                        if (!trackCancel.Continue())
                        {
                            isCancelled = true;
                            break;
                        }
                    }

                    if (!trackCancel.Continue())
                    {
                        isCancelled = true;
                        break;
                    }
                }

                if (!isCancelled)
                {
                    isComplete = true;
                }
            }

            return(isComplete);
        }
        /// <summary>
        /// Get the existing splice ranges between two cables at an existing closure
        /// </summary>
        /// <param name="cableA">One cable</param>
        /// <param name="cableB">Other cable</param>
        /// <param name="splice">Splice Closure</param>
        /// <returns>List of FiberSplice</returns>
        /// <remarks>Currently only checks A/B as passed, does not check the reverse B/A combination</remarks>
        public static List <FiberSplice> GetSplicedRanges(FiberCableWrapper cableA, FiberCableWrapper cableB, SpliceClosureWrapper splice)
        {
            List <FiberSplice> result      = new List <FiberSplice>();
            string             spliceWhere = string.Format("{0}='{1}' AND {2}='{3}' AND {4}='{5}'",
                                                           ConfigUtil.ACableIdFieldName,
                                                           cableA.IPID,
                                                           ConfigUtil.BCableIdFieldName,
                                                           cableB.IPID,
                                                           ConfigUtil.SpliceClosureIpidFieldName,
                                                           splice.IPID);

            using (ESRI.ArcGIS.ADF.ComReleaser releaser = new ESRI.ArcGIS.ADF.ComReleaser())
            {
                ESRI.ArcGIS.Geodatabase.ITable fiberSpliceTable = TelecomWorkspaceHelper.Instance().FindTable(ConfigUtil.FiberSpliceTableName);
//                ESRI.ArcGIS.Geodatabase.ITable fiberSpliceTable = GdbUtils.GetTable(cableA.Feature.Class, ConfigUtil.FiberSpliceTableName);
                ESRI.ArcGIS.Geodatabase.IQueryFilter filter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
                releaser.ManageLifetime(filter);

                filter.WhereClause = spliceWhere;
                ((ESRI.ArcGIS.Geodatabase.IQueryFilterDefinition)filter).PostfixClause = string.Format("ORDER BY {0}", ConfigUtil.AFiberNumberFieldName);

                int aUnitIdx = fiberSpliceTable.FindField(ConfigUtil.AFiberNumberFieldName);
                int bUnitIdx = fiberSpliceTable.FindField(ConfigUtil.BFiberNumberFieldName);
                int lossIdx  = fiberSpliceTable.FindField(ConfigUtil.LossFieldName);
                int typeIdx  = fiberSpliceTable.FindField(ConfigUtil.TypeFieldName);
                ESRI.ArcGIS.Geodatabase.IField            typeField  = fiberSpliceTable.Fields.get_Field(typeIdx);
                ESRI.ArcGIS.Geodatabase.ICodedValueDomain typeDomain = typeField.Domain as ESRI.ArcGIS.Geodatabase.ICodedValueDomain;

                ESRI.ArcGIS.Geodatabase.ICursor splices = fiberSpliceTable.Search(filter, true);
                releaser.ManageLifetime(splices);

                ESRI.ArcGIS.Geodatabase.IRow spliceRow = splices.NextRow();

                int    lastAUnit = -1;
                int    lastBUnit = -1;
                double?lastLoss  = null;
                object lastType  = Type.Missing;

                int aLow = -1;
                int bLow = -1;

                while (null != spliceRow)
                {
                    // These are not-null columns
                    int aUnit = (int)spliceRow.get_Value(aUnitIdx);
                    int bUnit = (int)spliceRow.get_Value(bUnitIdx);

                    object lossObj = spliceRow.get_Value(lossIdx);
                    double?loss    = null;
                    if (DBNull.Value != lossObj)
                    {
                        loss = (double)lossObj;
                    }

                    object type = spliceRow.get_Value(typeIdx);

                    if (aUnit != (lastAUnit + 1) ||
                        bUnit != (lastBUnit + 1) ||
                        loss != lastLoss ||
                        !type.Equals(lastType))
                    {
                        if (-1 != lastAUnit)
                        {
                            string typeString = string.Empty;
                            if (null != typeString)
                            {
                                if (null != typeDomain)
                                {
                                    typeString = GdbUtils.GetDomainNameForValue(typeDomain, lastType);
                                }
                                else
                                {
                                    typeString = lastType.ToString(); // DBNull.Value will return string.Empty
                                }
                            }

                            result.Add(new FiberSplice(new Range(aLow, lastAUnit), new Range(bLow, lastBUnit), lastLoss, typeString));
                        }

                        aLow = aUnit;
                        bLow = bUnit;
                    }

                    lastAUnit = aUnit;
                    lastBUnit = bUnit;
                    lastLoss  = loss;
                    lastType  = type;

                    ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(spliceRow);
                    spliceRow = splices.NextRow();
                }

                if (-1 < aLow)
                {
                    string typeString = string.Empty;
                    if (null != typeString)
                    {
                        if (null != typeDomain)
                        {
                            typeString = GdbUtils.GetDomainNameForValue(typeDomain, lastType);
                        }
                        else
                        {
                            typeString = lastType.ToString(); // DBNull.Value will return string.Empty
                        }
                    }

                    result.Add(new FiberSplice(new Range(aLow, lastAUnit), new Range(bLow, lastBUnit), lastLoss, typeString));
                }
            }

            return(result);
        }
        /// <summary>
        /// Finds the Device to Port relationship class for a given device feature class
        /// </summary>
        /// <param name="deviceClass">device feature class</param>
        /// <returns>ESRI.ArcGIS.Geodatabase.IRelationshipClass</returns>
        public static ESRI.ArcGIS.Geodatabase.IRelationshipClass GetPortRelationship(ESRI.ArcGIS.Geodatabase.IFeatureClass deviceClass)
        {
            // TODO: Currently returns the first relationship class found whose name ends with "Ports". Should probably make the relationship classes part of the configuration

            ESRI.ArcGIS.Geodatabase.IRelationshipClass result = null;

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

            using (ESRI.ArcGIS.ADF.ComReleaser releaser = new ESRI.ArcGIS.ADF.ComReleaser())
            {
                ESRI.ArcGIS.Geodatabase.IEnumRelationshipClass relClasses = deviceClass.get_RelationshipClasses(ESRI.ArcGIS.Geodatabase.esriRelRole.esriRelRoleOrigin);
                releaser.ManageLifetime(relClasses);

                relClasses.Reset();
                result = relClasses.Next();

                while (null != result)
                {
                    ESRI.ArcGIS.Geodatabase.IDataset relationshipDataset = result as ESRI.ArcGIS.Geodatabase.IDataset;

                    if (relationshipDataset.Name.EndsWith("Ports", StringComparison.CurrentCultureIgnoreCase))
                    {
                        break;
                    }

                    result = relClasses.Next();
                }
            }

            if (null == result)
            {
                throw new Exception("Relationship class from device to port was not found.");
            }

            return result;
        }
        protected override void OnStartup()
        {
            ESRI.ArcGIS.ArcMap.IApplicationStatusEvents_Event appStatusEvents = ArcMap.Application as ESRI.ArcGIS.ArcMap.IApplicationStatusEvents_Event;
            appStatusEvents.Initialized += new ESRI.ArcGIS.ArcMap.IApplicationStatusEvents_InitializedEventHandler(appStatusEvents_Initialized);

            ArcMap.Events.NewDocument += ArcMap_NewOpenDocument;
            ArcMap.Events.OpenDocument += ArcMap_NewOpenDocument;
            ArcMap.Events.CloseDocument += ArcMap_CloseDocument;

            GetConfigSettings();
            if (!_enabledOnStart | AAState._dt == null)
            {
                AAState._PerformUpdates = false;
                //ExecuteToggleAACommands();
                AAState.setIcon();
            }
            IEditor _editor = Globals.getEditor(ArcMap.Application);

            //Wire editor events.
            AAState._editEvents = (IEditEvents_Event)_editor;
            AAState._editEvents.OnStartEditing += OnStartEditing;
            AAState._editEvents.OnStopEditing += OnStopEditing;

            _comReleaser = new ESRI.ArcGIS.ADF.ComReleaser();
            _comReleaser.ManageLifetime(fCursor);

            script = new MSScriptControl.ScriptControlClass();
            script.AllowUI = false;
            script.Language = "VBScript";
            string strScript = "function iif(psdStr, trueStr, falseStr)" + System.Environment.NewLine +
                      "if psdStr then" + System.Environment.NewLine +
                        "iif = trueStr" + System.Environment.NewLine +
                      "else " + System.Environment.NewLine +
                        "iif = falseStr" + System.Environment.NewLine +
                      "end if" + System.Environment.NewLine +
                    "end function" + System.Environment.NewLine;
            script.AddCode(strScript);

            //     strScript = "function trueStr () " + System.Environment.NewLine +
            //     "if trueStr== \"<Null>\" then " + System.Environment.NewLine +
            //     "return(DBNull.value)" + System.Environment.NewLine +
            //     "else" + System.Environment.NewLine +
            //     "return trueStr" + System.Environment.NewLine +
            //     "end function" + System.Environment.NewLine;

            //     script.AddCode(strScript);
            //     strScript = "function falseStr () " + System.Environment.NewLine +
            //"if falseStr== \"<Null>\" then " + System.Environment.NewLine +
            //"return(DBNull.value)" + System.Environment.NewLine +
            //"else" + System.Environment.NewLine +
            //"return falseStr" + System.Environment.NewLine +
            //"end function" + System.Environment.NewLine;

            //     script.AddCode(strScript);
            AAState.WriteLine("                  script to process: " + newValue);
            //_application = _editor.Parent;
            //if (_application != null)
            //{
            //    _document = _application.Document;
            //    _mxDocument = (IMxDocument)_document;
            //    _map = _mxDocument.FocusMap;
            //}
            //else
            //    MessageBox.Show("Couldn't get handle for ArcMap Application");
        }
        /// <summary>
        /// Get the existing splice ranges between two cables at an existing closure
        /// </summary>
        /// <param name="cableA">One cable</param>
        /// <param name="cableB">Other cable</param>
        /// <param name="splice">Splice Closure</param>
        /// <returns>List of FiberSplice</returns>
        /// <remarks>Currently only checks A/B as passed, does not check the reverse B/A combination</remarks>
        public static List<FiberSplice> GetSplicedRanges(FiberCableWrapper cableA, FiberCableWrapper cableB, SpliceClosureWrapper splice)
        {
            List<FiberSplice> result = new List<FiberSplice>();
            string spliceWhere = string.Format("{0}='{1}' AND {2}='{3}' AND {4}='{5}'",
                ConfigUtil.ACableIdFieldName,
                cableA.IPID,
                ConfigUtil.BCableIdFieldName,
                cableB.IPID,
                ConfigUtil.SpliceClosureIpidFieldName,
                splice.IPID);

            using (ESRI.ArcGIS.ADF.ComReleaser releaser = new ESRI.ArcGIS.ADF.ComReleaser())
            {
                ESRI.ArcGIS.Geodatabase.ITable fiberSpliceTable = TelecomWorkspaceHelper.Instance().FindTable(ConfigUtil.FiberSpliceTableName);
            //                ESRI.ArcGIS.Geodatabase.ITable fiberSpliceTable = GdbUtils.GetTable(cableA.Feature.Class, ConfigUtil.FiberSpliceTableName);
                ESRI.ArcGIS.Geodatabase.IQueryFilter filter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
                releaser.ManageLifetime(filter);

                filter.WhereClause = spliceWhere;
                ((ESRI.ArcGIS.Geodatabase.IQueryFilterDefinition)filter).PostfixClause = string.Format("ORDER BY {0}",ConfigUtil.AFiberNumberFieldName);

                int aUnitIdx = fiberSpliceTable.FindField(ConfigUtil.AFiberNumberFieldName);
                int bUnitIdx = fiberSpliceTable.FindField(ConfigUtil.BFiberNumberFieldName);
                int lossIdx = fiberSpliceTable.FindField(ConfigUtil.LossFieldName);
                int typeIdx = fiberSpliceTable.FindField(ConfigUtil.TypeFieldName);
                ESRI.ArcGIS.Geodatabase.IField typeField = fiberSpliceTable.Fields.get_Field(typeIdx);
                ESRI.ArcGIS.Geodatabase.ICodedValueDomain typeDomain = typeField.Domain as ESRI.ArcGIS.Geodatabase.ICodedValueDomain;

                ESRI.ArcGIS.Geodatabase.ICursor splices = fiberSpliceTable.Search(filter, true);
                releaser.ManageLifetime(splices);

                ESRI.ArcGIS.Geodatabase.IRow spliceRow = splices.NextRow();

                int lastAUnit = -1;
                int lastBUnit = -1;
                double? lastLoss = null;
                object lastType = Type.Missing;

                int aLow = -1;
                int bLow = -1;

                while (null != spliceRow)
                {
                    // These are not-null columns
                    int aUnit = (int)spliceRow.get_Value(aUnitIdx);
                    int bUnit = (int)spliceRow.get_Value(bUnitIdx);

                    object lossObj = spliceRow.get_Value(lossIdx);
                    double? loss = null;
                    if (DBNull.Value != lossObj)
                    {
                        loss = (double)lossObj;
                    }

                    object type = spliceRow.get_Value(typeIdx);

                    if (aUnit != (lastAUnit + 1)
                        || bUnit != (lastBUnit + 1)
                        || loss != lastLoss
                        || !type.Equals(lastType))
                    {
                        if (-1 != lastAUnit)
                        {
                            string typeString = string.Empty;
                            if (null != typeString)
                            {
                                if (null != typeDomain)
                                {
                                    typeString = GdbUtils.GetDomainNameForValue(typeDomain, lastType);
                                }
                                else
                                {
                                    typeString = lastType.ToString(); // DBNull.Value will return string.Empty
                                }
                            }

                            result.Add(new FiberSplice(new Range(aLow, lastAUnit), new Range(bLow, lastBUnit), lastLoss, typeString));
                        }

                        aLow = aUnit;
                        bLow = bUnit;
                    }

                    lastAUnit = aUnit;
                    lastBUnit = bUnit;
                    lastLoss = loss;
                    lastType = type;

                    ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(spliceRow);
                    spliceRow = splices.NextRow();
                }

                if (-1 < aLow)
                {
                    string typeString = string.Empty;
                    if (null != typeString)
                    {
                        if (null != typeDomain)
                        {
                            typeString = GdbUtils.GetDomainNameForValue(typeDomain, lastType);
                        }
                        else
                        {
                            typeString = lastType.ToString(); // DBNull.Value will return string.Empty
                        }
                    }

                    result.Add(new FiberSplice(new Range(aLow, lastAUnit), new Range(bLow, lastBUnit), lastLoss, typeString));
                }
            }

            return result;
        }
        private FiberCableWrapper GetConnectedFiber(DeviceWrapper device, int portId, PortType portType, out int fiberNumber)
        {
            FiberCableWrapper result = null;
            fiberNumber = -1;

            ESRI.ArcGIS.Geodatabase.IFeatureClass deviceFtClass = (ESRI.ArcGIS.Geodatabase.IFeatureClass)device.Feature.Class;
            ESRI.ArcGIS.Geodatabase.IRelationshipClass deviceHasPorts = ConfigUtil.GetPortRelationship(deviceFtClass);
            if (null == deviceHasPorts)
            {
                throw new Exception("Device to port relationship is missing or cannot be opened.");
            }

            ESRI.ArcGIS.Geodatabase.ITable portTable = deviceHasPorts.DestinationClass as ESRI.ArcGIS.Geodatabase.ITable;
            if (null == portTable)
            {
                throw new Exception("Port table is missing or cannot be opened.");
            }

            using (ESRI.ArcGIS.ADF.ComReleaser releaser = new ESRI.ArcGIS.ADF.ComReleaser())
            {
                ESRI.ArcGIS.Geodatabase.IQueryFilter filter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
                releaser.ManageLifetime(filter);
                filter.WhereClause = string.Format("{0}='{1}' AND {2}={3} AND {4}='{5}'",
                    deviceHasPorts.OriginForeignKey,
                    device.Feature.get_Value(deviceFtClass.FindField(deviceHasPorts.OriginPrimaryKey)),
                    ConfigUtil.PortIdFieldName,
                    portId,
                    ConfigUtil.PortTypeFieldName,
                    PortType.Input == portType ? 1 : 2);

                ESRI.ArcGIS.Geodatabase.ICursor cursor = portTable.Search(filter, false);
                releaser.ManageLifetime(cursor);
                ESRI.ArcGIS.Geodatabase.IRow portRow = cursor.NextRow();

                if (null != portRow)
                {
                    //releaser.ManageLifetime(portRow);

                    object cableIdValue = portRow.get_Value(portTable.FindField(ConfigUtil.ConnectedCableFieldName));
                    if (DBNull.Value != cableIdValue)
                    {
                        ESRI.ArcGIS.Geodatabase.IFeatureClass cableFtClass = _wkspHelper.FindFeatureClass(ConfigUtil.FiberCableFtClassName);
                        filter.WhereClause = string.Format("{0}='{1}'", ConfigUtil.IpidFieldName, cableIdValue);
                        ESRI.ArcGIS.Geodatabase.IFeatureCursor cableCursor = cableFtClass.Search(filter, false);
                        releaser.ManageLifetime(cableCursor);

                        ESRI.ArcGIS.Geodatabase.IFeature cable = cableCursor.NextFeature();
                        if (null != cable)
                        {
                            result = new FiberCableWrapper(cable);
                            object fiberNumberValue = portRow.get_Value(portTable.FindField(ConfigUtil.ConnectedFiberFieldName));
                            if (DBNull.Value != fiberNumberValue)
                            {
                                int.TryParse(fiberNumberValue.ToString(), out fiberNumber);
                            }
                        }
                    }
                }
            }

            return result;
        }
        /// <summary>
        /// Gets a list of all port numbers from a device that are not connected on the given end
        /// </summary>
        /// <param name="device">Device to check</param>
        /// <param name="portType">Check input or output ports</param>
        /// <returns>List of int</returns>
        private static List<int> GetOpenPorts(DeviceWrapper device, PortType portType)
        {
            #region Validation
            if (null == device)
            {
                throw new ArgumentNullException("device");
            }

            if (null == device.Feature)
            {
                throw new ArgumentException("device.Class cannot be null");
            }
            #endregion

            List<int> result = new List<int>();

            using (ESRI.ArcGIS.ADF.ComReleaser releaser = new ESRI.ArcGIS.ADF.ComReleaser())
            {
                ESRI.ArcGIS.Geodatabase.IFeatureClass deviceFtClass = device.Feature.Class as ESRI.ArcGIS.Geodatabase.IFeatureClass;
                ESRI.ArcGIS.Geodatabase.IRelationshipClass deviceHasPorts = ConfigUtil.GetPortRelationship(deviceFtClass);
                if (null != deviceHasPorts)
                {
                    ESRI.ArcGIS.Geodatabase.ITable portTable = deviceHasPorts.DestinationClass as ESRI.ArcGIS.Geodatabase.ITable;
                    if (null != portTable)
                    {
                        ESRI.ArcGIS.Geodatabase.IQueryFilter filter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
                        releaser.ManageLifetime(filter);
                        filter.WhereClause = string.Format("{0}='{1}' AND {2} IS NULL AND {3}='{4}'",
                            deviceHasPorts.OriginForeignKey,
                            device.Feature.get_Value(deviceFtClass.FindField(deviceHasPorts.OriginPrimaryKey)),
                            ConfigUtil.ConnectedCableFieldName,
                            ConfigUtil.PortTypeFieldName,
                            (PortType.Input == portType ? "1" : "2"));

                        ESRI.ArcGIS.Geodatabase.ICursor portCursor = portTable.Search(filter, true);
                        ESRI.ArcGIS.Geodatabase.IRow portRow = portCursor.NextRow();
                        int portIdIdx = portTable.FindField(ConfigUtil.PortIdFieldName);

                        while (null != portRow)
                        {
                            result.Add((int)portRow.get_Value(portIdIdx));

                            ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(portRow);
                            portRow = portCursor.NextRow();
                        }

                        ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(portCursor);
                    }
                }
            }

            return result;
        }
        /// <summary>
        /// Finds the connected device/port
        /// </summary>
        /// <param name="siblingFtClass">Any feature class from the workspace</param>
        /// <param name="cableId">Cable ID to check connx for</param>
        /// <param name="fiberNumber">Fiber Number to check connx for</param>
        /// <param name="isFromEnd">Whether to check the cable's from or to end</param>
        /// <param name="portRow">(out) result port</param>
        /// <param name="deviceFt">(out) result device</param>
        /// <returns>True if a connx was found</returns>
        private bool GetConnectedPort(ESRI.ArcGIS.Geodatabase.IFeatureClass siblingFtClass, string cableId, int fiberNumber, bool isFromEnd, out ESRI.ArcGIS.Geodatabase.IRow portRow, out ESRI.ArcGIS.Geodatabase.IFeature deviceFt)
        {
            portRow = null;
            deviceFt = null;

            bool result = false;

            string[] portTableNames = ConfigUtil.PortTableNames;
            using (ESRI.ArcGIS.ADF.ComReleaser releaser = new ESRI.ArcGIS.ADF.ComReleaser())
            {
                ESRI.ArcGIS.Geodatabase.IQueryFilter filter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
                filter.WhereClause = string.Format("{0}='{1}' AND {2}={3} AND {4}='{5}'",
                    ConfigUtil.ConnectedCableFieldName,
                    cableId,
                    ConfigUtil.ConnectedFiberFieldName,
                    fiberNumber,
                    ConfigUtil.ConnectedEndFieldName,
                    isFromEnd ? "T" : "F");
                releaser.ManageLifetime(filter);

                for (int i = 0; i < portTableNames.Length; i++)
                {
                    string portTableName = portTableNames[i];
                    ESRI.ArcGIS.Geodatabase.ITable portTable = _wkspHelper.FindTable(portTableName);
                    ESRI.ArcGIS.Geodatabase.ICursor cursor = portTable.Search(filter, false);
                    releaser.ManageLifetime(cursor);

                    portRow = cursor.NextRow();
                    if (null != portRow)
                    {
                        ESRI.ArcGIS.Geodatabase.IRelationshipClass deviceHasPorts = ConfigUtil.GetDeviceRelationship(portTable);
                        if (null == deviceHasPorts)
                        {
                            throw new Exception("Device to port relationship is missing or cannot be opened.");
                        }

                        ESRI.ArcGIS.Geodatabase.IFeatureClass deviceClass = deviceHasPorts.OriginClass as ESRI.ArcGIS.Geodatabase.IFeatureClass;
                        if (null == deviceClass)
                        {
                            throw new Exception("Device feature class is missing or cannot be opened.");
                        }

                        filter.WhereClause = string.Format("{0}='{1}'",
                            deviceHasPorts.OriginPrimaryKey,
                            portRow.get_Value(portTable.FindField(deviceHasPorts.OriginForeignKey)));
                        ESRI.ArcGIS.Geodatabase.IFeatureCursor deviceCursor = deviceClass.Search(filter, false);
                        deviceFt = deviceCursor.NextFeature();

                        result = true;
                        break;
                    }
                }
            }

            return result;
        }
        /// <summary>
        /// Returns a list of connections between the cable and the device, at the cable's given end, to the device's given port type
        /// </summary>
        /// <param name="cable">Cable to check</param>
        /// <param name="device">Device to check</param>
        /// <param name="isFromEnd">Digitized end of cable</param>
        /// <param name="portType">Input or output</param>
        /// <returns>List of Connection</returns>
        public List <Connection> GetConnections(FiberCableWrapper cable, DeviceWrapper device, bool isFromEnd, PortType portType)
        {
            if (null == cable)
            {
                throw new ArgumentNullException("cable");
            }

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

            List <Connection> result  = new List <Connection>();
            List <int>        ports   = new List <int>();
            List <int>        strands = new List <int>();

            using (ESRI.ArcGIS.ADF.ComReleaser releaser = new ESRI.ArcGIS.ADF.ComReleaser())
            {
                ESRI.ArcGIS.Geodatabase.IFeatureClass      deviceFtClass  = device.Feature.Class as ESRI.ArcGIS.Geodatabase.IFeatureClass;
                ESRI.ArcGIS.Geodatabase.IRelationshipClass deviceHasPorts = ConfigUtil.GetPortRelationship(deviceFtClass);
                if (null == deviceHasPorts)
                {
                    throw new Exception("Unable to find port relationship class.");
                }

                ESRI.ArcGIS.Geodatabase.ITable portTable = deviceHasPorts.DestinationClass as ESRI.ArcGIS.Geodatabase.ITable;
                if (null == portTable)
                {
                    throw new Exception("Invalid destination on port relationship class.");
                }

                ESRI.ArcGIS.Geodatabase.IQueryFilter filter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
                releaser.ManageLifetime(filter);
                filter.WhereClause = string.Format("{0}='{1}' AND {2}='{3}' AND {4}='{5}' AND {6}='{7}' AND {8} IS NOT NULL AND {9} IS NOT NULL",
                                                   deviceHasPorts.OriginForeignKey,
                                                   device.Feature.get_Value(deviceFtClass.FindField(deviceHasPorts.OriginPrimaryKey)),
                                                   ConfigUtil.ConnectedCableFieldName,
                                                   cable.IPID,
                                                   ConfigUtil.PortTypeFieldName,
                                                   (PortType.Input == portType ? "1" : "2"),
                                                   ConfigUtil.ConnectedEndFieldName,
                                                   (isFromEnd ? "T" : "F"),
                                                   ConfigUtil.ConnectedFiberFieldName,
                                                   ConfigUtil.PortIdFieldName);


                // ORDER BY does not work outside of SDE.
                // Removing for now, should not be important.
                string orderFormat = "ORDER BY {0}";
                if (PortType.Input == portType)
                {
//                    ((ESRI.ArcGIS.Geodatabase.IQueryFilterDefinition2)filter).PostfixClause = string.Format(orderFormat, ConfigUtil.ConnectedFiberFieldName);
                }
                else
                {
//                    ((ESRI.ArcGIS.Geodatabase.IQueryFilterDefinition2)filter).PostfixClause = string.Format(orderFormat, ConfigUtil.PortIdFieldName);
                }

                ESRI.ArcGIS.Geodatabase.ICursor portCursor = portTable.Search(filter, true);
                ESRI.ArcGIS.Geodatabase.IRow    portRow    = portCursor.NextRow();

                int portIdIdx      = portTable.FindField(ConfigUtil.PortIdFieldName);
                int fiberNumberIdx = portTable.FindField(ConfigUtil.ConnectedFiberFieldName);

                while (null != portRow)
                {
                    ports.Add((int)portRow.get_Value(portIdIdx));
                    strands.Add((int)portRow.get_Value(fiberNumberIdx));

                    ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(portRow);
                    portRow = portCursor.NextRow();
                }

                ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(portCursor);
            }


            List <Range> portRanges   = SpliceAndConnectionUtils.MergeRanges(ports);
            List <Range> strandRanges = SpliceAndConnectionUtils.MergeRanges(strands);

            if (PortType.Input == portType)
            {
                result = SpliceAndConnectionUtils.MatchUp(strandRanges, portRanges);
            }
            else
            {
                result = SpliceAndConnectionUtils.MatchUp(portRanges, strandRanges);
            }

            return(result);
        }
        /// <summary>
        /// Looks for a fiber splice record at one end of a given cable
        /// </summary>
        /// <param name="fiberSpliceTable">Fiber splice table</param>
        /// <param name="cableId">Cable ID of the cable we are checking</param>
        /// <param name="fiberNumber">Fiber Number we are checking</param>
        /// <param name="checkFromEnd">Which end of the cable are we checking?</param>
        /// <param name="nextCableId">(out) Cable ID of the cable spliced on this end, or string.Empty if none</param>
        /// <param name="nextFiberNumber">(out) Fiber Number of spliced on this end, or -1 if none</param>
        /// <param name="spliceClosureIpid">(out) IPID of Splice Closure</param>
        /// <param name="isNextFromEnd">(out) Is the result cable spliced on its from end or its to end?</param>
        /// <returns>The splice record</returns>
        private ESRI.ArcGIS.Geodatabase.IRow GetNextSplice(ESRI.ArcGIS.Geodatabase.ITable fiberSpliceTable, string cableId, int fiberNumber, bool checkFromEnd, out string nextCableId, out int nextFiberNumber, out string spliceClosureIpid, out bool isNextFromEnd)
        {
            ESRI.ArcGIS.Geodatabase.IRow spliceRow = null;

            spliceClosureIpid = string.Empty;
            nextCableId = cableId;
            nextFiberNumber = fiberNumber;
            isNextFromEnd = checkFromEnd;

            using (ESRI.ArcGIS.ADF.ComReleaser releaser = new ESRI.ArcGIS.ADF.ComReleaser())
            {
                ESRI.ArcGIS.Geodatabase.IQueryFilter filter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
                filter.WhereClause = string.Format("({0}='{1}' AND {2}={3} AND {4}='{5}')"
                    + " OR ({6}='{1}' AND {7}={3} AND {8}='{5}')",
                    ConfigUtil.ACableIdFieldName,
                    cableId,
                    ConfigUtil.AFiberNumberFieldName,
                    fiberNumber,
                    ConfigUtil.IsAFromEndFieldName,
                    (checkFromEnd ? "T" : "F"),
                    ConfigUtil.BCableIdFieldName,
                    ConfigUtil.BFiberNumberFieldName,
                    ConfigUtil.IsBFromEndFieldName);

                releaser.ManageLifetime(filter);

                // TODO: should we give a warning if the rowcount is more than 1? We should technically only find one splice
                // record on this end of the fiber...

                ESRI.ArcGIS.Geodatabase.ICursor search = fiberSpliceTable.Search(filter, false);
                releaser.ManageLifetime(search);

                spliceRow = search.NextRow();
                if (null != spliceRow)
                {
                    object scIpidValue = spliceRow.get_Value(_spliceClosureIpidIdx);
                    if (DBNull.Value != scIpidValue)
                    {
                        spliceClosureIpid = scIpidValue.ToString();
                    }

                    string aCableId = spliceRow.get_Value(_aCableIdx).ToString();
                    if (0 == string.Compare(aCableId, cableId))
                    {
                        // b is the one we want to return
                        nextCableId = spliceRow.get_Value(_bCableIdx).ToString();
                        nextFiberNumber = (int)spliceRow.get_Value(_bFiberNumIdx);
                        isNextFromEnd = spliceRow.get_Value(_isBFromIdx).ToString() == "T" ? true : false;
                    }
                    else
                    {
                        // a is the one we want to return
                        nextCableId = aCableId;
                        nextFiberNumber = (int)spliceRow.get_Value(_aFiberNumIdx);
                        isNextFromEnd = spliceRow.get_Value(_isAFromIdx).ToString() == "T" ? true : false;
                    }
                }
            }

            return spliceRow;
        }
        /// <summary>
        /// Deletes given connections between cable to device
        /// </summary>
        /// <param name="cable">Cable</param>
        /// <param name="device">Device</param>
        /// <param name="units">Units to connect</param>
        /// <param name="portType">Input or Output?</param>
        /// <param name="isExistingOperation">Flag to control whether we need to wrap this in an edit operation</param>
        /// <returns>Success</returns>
        public bool BreakConnections(FiberCableWrapper cable, DeviceWrapper device, Dictionary <int, int> units, PortType portType, bool isExistingOperation)
        {
            bool success         = false;
            bool isOperationOpen = false;

            #region Validation
            if (null == cable)
            {
                throw new ArgumentNullException("cable");
            }

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

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

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

            if (0 < units.Count)
            {
                if (!isExistingOperation)
                {
                    _editor.StartOperation();
                    isOperationOpen = true;
                }

                try
                {
                    ESRI.ArcGIS.Geodatabase.IFeatureClass      deviceFtClass  = device.Feature.Class as ESRI.ArcGIS.Geodatabase.IFeatureClass;
                    ESRI.ArcGIS.Geodatabase.IRelationshipClass deviceHasPorts = ConfigUtil.GetPortRelationship(deviceFtClass);
                    if (null == deviceFtClass)
                    {
                        throw new Exception("Unable to find port relationship class.");
                    }

                    ESRI.ArcGIS.Geodatabase.ITable portTable = deviceHasPorts.DestinationClass as ESRI.ArcGIS.Geodatabase.ITable;
                    if (null == portTable)
                    {
                        throw new Exception("Invalid destination on port relationship class.");
                    }

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

                        StringBuilder inList = new StringBuilder(1024);
                        foreach (KeyValuePair <int, int> pair in units)
                        {
                            string appendFormat = "{0},";
                            if (PortType.Input == portType)
                            {
                                inList.AppendFormat(appendFormat, pair.Key);
                            }
                            else
                            {
                                inList.AppendFormat(appendFormat, pair.Value);
                            }
                        }
                        inList.Remove(inList.Length - 1, 1);

                        string format = "{0}='{1}' AND {2}='{3}' AND {4}='{5}' AND {6} IN ({7})";
                        filter.WhereClause = string.Format(format,
                                                           deviceHasPorts.OriginForeignKey,
                                                           device.Feature.get_Value(deviceFtClass.FindField(deviceHasPorts.OriginPrimaryKey)),
                                                           ConfigUtil.ConnectedCableFieldName,
                                                           cable.IPID,
                                                           ConfigUtil.PortTypeFieldName,
                                                           (PortType.Input == portType ? "1" : "2"),
                                                           ConfigUtil.ConnectedFiberFieldName,
                                                           inList.ToString());

                        filter.SubFields = string.Format("{0},{1},{2}", ConfigUtil.ConnectedEndFieldName, ConfigUtil.ConnectedFiberFieldName, ConfigUtil.ConnectedCableFieldName);

                        ESRI.ArcGIS.Geodatabase.IRowBuffer buffer = portTable.CreateRowBuffer();
                        releaser.ManageLifetime(buffer);
                        // We want to set them to null, so we can just send the empty buffer
                        portTable.UpdateSearchedRows(filter, buffer);

                        if (isOperationOpen)
                        {
                            _editor.StopOperation("Break Connections");
                            isOperationOpen = false;
                        }

                        success = true;
                    }
                }
                catch
                {
                    if (isOperationOpen)
                    {
                        _editor.AbortOperation();
                    }

                    success = false;
                }
            }

            return(success);
        }
        ///// <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;
            }
        }
        /// <summary>
        /// Generates a number of buffer tubes and fiber records for a fiber cable, given a configuration. 
        /// </summary>
        /// <param name="feature">IFeature to generate for</param>
        /// <param name="configuration">Specification of buffer and fiber counts</param>
        /// <param name="progressDialog">Progress dialog for user notification</param>
        /// <param name="trackCancel">TrackCancel used in the progress dialog</param>
        /// <returns>Success</returns>
        private bool GenerateUnits(ESRI.ArcGIS.Geodatabase.IFeature feature, FiberCableConfiguration configuration, ESRI.ArcGIS.Framework.IProgressDialog2 progressDialog, ESRI.ArcGIS.esriSystem.ITrackCancel trackCancel)
        {
            bool isComplete = false;
            bool isCancelled = false;
            Guid g;

            ESRI.ArcGIS.esriSystem.IStepProgressor stepProgressor = (ESRI.ArcGIS.esriSystem.IStepProgressor)progressDialog;
            ESRI.ArcGIS.Geodatabase.IObjectClass ftClass = feature.Class;

            using (ESRI.ArcGIS.ADF.ComReleaser releaser = new ESRI.ArcGIS.ADF.ComReleaser())
            {
                ESRI.ArcGIS.Geodatabase.IRelationshipClass cableHasBuffer = GdbUtils.GetRelationshipClass(ftClass, ConfigUtil.FiberCableToBufferRelClassName);
                releaser.ManageLifetime(cableHasBuffer);
                ESRI.ArcGIS.Geodatabase.IRelationshipClass cableHasFiber = GdbUtils.GetRelationshipClass(ftClass, ConfigUtil.FiberCableToFiberRelClassName);
                releaser.ManageLifetime(cableHasFiber);
                ESRI.ArcGIS.Geodatabase.IRelationshipClass bufferHasFiber = GdbUtils.GetRelationshipClass(ftClass, ConfigUtil.BufferToFiberRelClassName);
                releaser.ManageLifetime(bufferHasFiber);

                ESRI.ArcGIS.Geodatabase.ITable bufferTable = cableHasBuffer.DestinationClass as ESRI.ArcGIS.Geodatabase.ITable;
                ESRI.ArcGIS.Geodatabase.ITable fiberTable = cableHasFiber.DestinationClass as ESRI.ArcGIS.Geodatabase.ITable;

                // Fields to populate on buffer
                int bufferIpidIdx = bufferTable.Fields.FindField(ConfigUtil.IpidFieldName);
                int fiberCountIdx = bufferTable.Fields.FindField(ConfigUtil.NumberOfFibersFieldName);
                int bufferToCableIdx = bufferTable.Fields.FindField(cableHasBuffer.OriginForeignKey);
                object bufferToCableValue = feature.get_Value(feature.Fields.FindField(cableHasBuffer.OriginPrimaryKey));

                // Fields to populate on fiber
                int fiberIpidIdx = fiberTable.Fields.FindField(ConfigUtil.IpidFieldName);
                int fiberNumberIdx = fiberTable.Fields.FindField(ConfigUtil.Fiber_NumberFieldName);
                int fiberToCableIdx = fiberTable.Fields.FindField(cableHasFiber.OriginForeignKey);
                object fiberToCableValue = feature.get_Value(feature.Fields.FindField(cableHasFiber.OriginPrimaryKey));
                int fiberToBufferIdx = fiberTable.Fields.FindField(bufferHasFiber.OriginForeignKey);
                int fiberToBufferValueIdx = bufferTable.Fields.FindField(bufferHasFiber.OriginPrimaryKey);

                // Research using InsertCursor for speed.
                int fiberNumber = 0;
                for (int bufferIdx = 1; bufferIdx <= configuration.BufferCount; bufferIdx++)
                {
                    g = Guid.NewGuid();
                    string bufferId = g.ToString("B").ToUpper();

                    ESRI.ArcGIS.Geodatabase.IRow row = bufferTable.CreateRow();
                    releaser.ManageLifetime(row);

                    row.set_Value(bufferIpidIdx, bufferId);
                    row.set_Value(fiberCountIdx, configuration.FibersPerTube);
                    row.set_Value(bufferToCableIdx, bufferToCableValue);

                    row.Store();

                    object fiberToBufferValue = row.get_Value(fiberToBufferValueIdx);

                    // Research using InsertCursor for speed.
                    for (int fiberIdx = 1; fiberIdx <= configuration.FibersPerTube; fiberIdx++)
                    {
                        fiberNumber++;
                        progressDialog.Description = string.Format("Creating fiber {0} of {1}", fiberNumber, configuration.TotalFiberCount);
                        stepProgressor.Step();

                        g = Guid.NewGuid();
                        ESRI.ArcGIS.Geodatabase.IRow fiberRow = fiberTable.CreateRow();
                        releaser.ManageLifetime(fiberRow);

                        fiberRow.set_Value(fiberIpidIdx, g.ToString("B").ToUpper());
                        fiberRow.set_Value(fiberNumberIdx, fiberNumber);
                        fiberRow.set_Value(fiberToBufferIdx, fiberToBufferValue);
                        fiberRow.set_Value(fiberToCableIdx, fiberToCableValue);

                        fiberRow.Store();

                        if (!trackCancel.Continue())
                        {
                            isCancelled = true;
                            break;
                        }
                    }

                    if (!trackCancel.Continue())
                    {
                        isCancelled = true;
                        break;
                    }
                }

                if (!isCancelled)
                {
                    isComplete = true;
                }
            }

            return isComplete;
        }
        /// <summary>
        /// Delete the ports for a given device
        /// </summary>
        /// <param name="device">The device feature</param>
        /// <returns>True if completed</returns>
        private bool DeletePorts(ESRI.ArcGIS.Geodatabase.IFeature device, int highInputPort, int highOutputPort, ESRI.ArcGIS.Framework.IProgressDialog2 progressDialog, ESRI.ArcGIS.esriSystem.ITrackCancel trackCancel)
        {
            bool isCancelled = false;

            ESRI.ArcGIS.esriSystem.IStepProgressor stepProgressor = (ESRI.ArcGIS.esriSystem.IStepProgressor)progressDialog;
            ESRI.ArcGIS.Geodatabase.IRelationshipClass deviceHasPorts = ConfigUtil.GetPortRelationship((ESRI.ArcGIS.Geodatabase.IFeatureClass)device.Class);

            if (null != deviceHasPorts)
            {
                using (ESRI.ArcGIS.ADF.ComReleaser releaser = new ESRI.ArcGIS.ADF.ComReleaser())
                {
                    ESRI.ArcGIS.Geodatabase.ITable portTable = (ESRI.ArcGIS.Geodatabase.ITable)deviceHasPorts.DestinationClass;
                    releaser.ManageLifetime(portTable);

                    ESRI.ArcGIS.Geodatabase.IQueryFilter filter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
                    releaser.ManageLifetime(filter);

                    filter.WhereClause = string.Format("{0}='{1}' AND {2} > {3} AND {4}='{5}'",
                        deviceHasPorts.OriginForeignKey,
                        device.get_Value(device.Fields.FindField(deviceHasPorts.OriginPrimaryKey)),
                        ConfigUtil.PortIdFieldName,
                        highInputPort,
                        ConfigUtil.PortTypeFieldName,
                        1);

                    stepProgressor.Message = "Deleting higher input ports...";
                    int deletedPorts = portTable.RowCount(filter);

                    portTable.DeleteSearchedRows(filter);
                    for (int i = 0; i < deletedPorts; i++)
                    {
                        stepProgressor.Step();
                    }

                    filter.WhereClause = string.Format("{0}='{1}' AND {2} > {3} AND {4}='{5}'",
                        deviceHasPorts.OriginForeignKey,
                        device.get_Value(device.Fields.FindField(deviceHasPorts.OriginPrimaryKey)),
                        ConfigUtil.PortIdFieldName,
                        highOutputPort,
                        ConfigUtil.PortTypeFieldName,
                        2);

                    stepProgressor.Message = "Deleting higher output ports...";
                    deletedPorts = portTable.RowCount(filter);

                    portTable.DeleteSearchedRows(filter);
                    for (int i = 0; i < deletedPorts; i++)
                    {
                        stepProgressor.Step();
                    }
                }
            }

            return !isCancelled;
        }