private void lbConflictResolutionColumns_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                int indexFromPoint = lbConflictResolutionColumns.IndexFromPoint(e.Location);

                if (indexFromPoint != -1)
                {
                    var RightClickMenu = new ContextMenuStrip();

                    IResolveDuplication resolver =
                        (IResolveDuplication)lbConflictResolutionColumns.Items[indexFromPoint];

                    string target    = resolver.DuplicateRecordResolutionIsAscending ? "DESC" : "ASC";
                    string currently = resolver.DuplicateRecordResolutionIsAscending ? "ASC" : "DESC";

                    RightClickMenu.Items.Add(
                        "Set " + resolver.GetRuntimeName() + " to " + target + " (Currently resolution order is " +
                        currently + ")", null, delegate
                    {
                        //flip its bit
                        resolver.DuplicateRecordResolutionIsAscending =
                            !resolver.DuplicateRecordResolutionIsAscending;
                        //save it to database
                        resolver.SaveToDatabase();
                        //refresh UI
                        RefreshUIFromDatabase();
                    });

                    RightClickMenu.Show(lbConflictResolutionColumns.PointToScreen(e.Location));
                }
            }
        }
        private void listBox1_DragDrop(object sender, DragEventArgs e)
        {
            Point point = lbConflictResolutionColumns.PointToClient(new Point(e.X, e.Y));
            int   index = this.lbConflictResolutionColumns.IndexFromPoint(point);

            //if they are dragging it way down the bottom of the list
            if (index < 0)
            {
                index = this.lbConflictResolutionColumns.Items.Count;
            }

            //get the thing they are dragging
            IResolveDuplication data =
                (IResolveDuplication)
                (e.Data.GetData(typeof(ColumnInfo)) ?? e.Data.GetData(typeof(PreLoadDiscardedColumn)));

            //find original index because if we are dragging down then we will want to adjust the index so that insert point is correct even after removing the object further up the list
            int originalIndex = this.lbConflictResolutionColumns.Items.IndexOf(data);

            this.lbConflictResolutionColumns.Items.Remove(data);

            if (originalIndex < index)
            {
                this.lbConflictResolutionColumns.Items.Insert(Math.Max(0, index - 1), data);
            }
            else
            {
                this.lbConflictResolutionColumns.Items.Insert(index, data);
            }

            SaveOrderIntoDatabase();
        }
        private string AppendRelevantOrderBySql(string sql, IResolveDuplication col)
        {
            string colname = _querySyntaxHelper.EnsureWrapped(col.GetRuntimeName(LoadStage.AdjustRaw));

            string direction = col.DuplicateRecordResolutionIsAscending ? " ASC" : " DESC";

            //dont bother adding these because they are hic generated
            if (SpecialFieldNames.IsHicPrefixed(colname))
            {
                return(sql);
            }

            ValueType valueType = GetDataType(col.Data_type);

            if (valueType == ValueType.CharacterString)
            {
                //character strings are compared first by LENGTH (to prefer longer data)
                //then by alphabetical comparison to prefer things towards the start of the alphabet (because this makes sense?!)
                return
                    (sql +
                     "LEN(ISNULL(" + colname + "," + GetNullSubstituteForComparisonsWithDataType(col.Data_type, true) + "))" + direction + "," + Environment.NewLine +
                     "ISNULL(" + colname + "," + GetNullSubstituteForComparisonsWithDataType(col.Data_type, true) + ")" + direction + "," + Environment.NewLine);
            }

            return(sql + "ISNULL(" + colname + "," + GetNullSubstituteForComparisonsWithDataType(col.Data_type, true) + ")" + direction + "," + Environment.NewLine);
        }
        private void SaveOrderIntoDatabase()
        {
            for (int i = 0; i < lbConflictResolutionColumns.Items.Count; i++)
            {
                IResolveDuplication extractionInformation = (IResolveDuplication)lbConflictResolutionColumns.Items[i];
                extractionInformation.DuplicateRecordResolutionOrder = i;
                extractionInformation.SaveToDatabase();
            }

            RefreshUIFromDatabase();
        }