EncompDEV
LoanDebugger2

Admin Form - Check Duplicates / Process Loan Guids

Click here to download this form    
This form will allow you to:
1) Check if any Loan Numbers in your Encompass are duplicated.
2) Process a list of Loan Guids into a list of Guids with Loan Numbers / Folders / ...

Code on this form is in C# and not VB.net and does the following:
1) Enumerates all loans in all folders except for Trash.
2) Builds maps of Guid -> Loan Number + ..., LoanNumber -> Guid + ...
3) Builds maps (lists) of duplicated Loan Guids and Loan Numbers
4) Checks input list of Guids against Guid -> Loan map
5) Output lists into textboxes

// busy cursor while processing
System.Windows.Forms.Form.ActiveForm.Cursor = System.Windows.Forms.Cursors.WaitCursor;
try
{
    // 1.1. Map: GUID -> LoanNumber TAB LoanFolder TAB LastName TAB ORGID
    System.Collections.Generic.Dictionary dictGuidToLoanNumber =
        new System.Collections.Generic.Dictionary();
    // 1.2. Map: LoanNumber -> LoanFolder TAB LastName TAB GUID TAB ORGID
    System.Collections.Generic.Dictionary dictUniqueLoanNumbers =
        new System.Collections.Generic.Dictionary();
    // 1.3. Map: Duplicated GUIDs (this is really bad) 
    //           INDEX TAB GUID TAB LoanNumber TAB LoanFolder TAB LastName TAB ORGID
    System.Collections.Generic.Dictionary dictDuplicatedGuids =
        new System.Collections.Generic.Dictionary();
    // 1.4. Map: Duplicated LoanNumbers (can be fixed) 
    //           INDEX TAB LoanNumber TAB LoanFolder TAB LastName TAB GUID TAB ORGID
    System.Collections.Generic.Dictionary dictDuplicatedLoanNumbers =
        new System.Collections.Generic.Dictionary();

    // 2.1. query for all loans (without TRASH)
    EllieMae.Encompass.Query.QueryCriterion query = null;
    // 2.2. enum all loan folders without trash
    foreach (LoanFolder lf in Loan.Session.Loans.Folders)
    {
        if (!lf.IsTrash)
        {
            EllieMae.Encompass.Query.StringFieldCriterion cr = new EllieMae.Encompass.Query.StringFieldCriterion
                ("Loan.LoanFolder", lf.Name, EllieMae.Encompass.Query.StringFieldMatchType.CaseInsensitive, true);
            if (query == null)
            {
                query = cr;
            }
            else
            {
                query = query.Or(cr);
            }
        }
    }
    // 2.3. fields that we need
    EllieMae.Encompass.Collections.StringList fieldIDs = new EllieMae.Encompass.Collections.StringList();
    fieldIDs.Add("Loan.LoanNumber");
    fieldIDs.Add("Loan.LoanFolder");
    fieldIDs.Add("Fields.4000");
    fieldIDs.Add("Fields.ORGID");
    // 2.4. do query
    EllieMae.Encompass.Reporting.LoanReportCursor cursor = Loan.Session.Reports.OpenReportCursor(fieldIDs, query);
    // 2.5. enum all loans and build maps
    for (int i = 0; i < cursor.Count; i++)
    {
        // read loan data
        EllieMae.Encompass.Reporting.LoanReportData data = cursor.GetItem(i);
        string LoanGuid = data.Guid.ToLower().Trim(); // LCASE comparison
        string LoanNumber = data["Loan.LoanNumber"].ToString().ToUpper().Trim(); // UCASE comparison
        string LoanFolder = data["Loan.LoanFolder"].ToString();
        string LastName = data["Fields.4000"].ToString();
        string OrgID = data["Fields.ORGID"].ToString();
        string sLoanNumberFolderName = LoanNumber + "\t" + LoanFolder + "\t" + LastName + "\t" + OrgID;
        string sLoanFolderNameGuid = LoanFolder + "\t" + LastName + "\t" + LoanGuid + "\t" + OrgID;
        // Check GUID
        if (!dictGuidToLoanNumber.ContainsKey(LoanGuid))
        {
            // this GUID is unique
            dictGuidToLoanNumber.Add(LoanGuid, sLoanNumberFolderName);
        }
        else
        {
            // this GUID is not unique
            string sKey1 = "REF" + "\t" + LoanGuid + "\t" + dictGuidToLoanNumber[LoanGuid];
            // first "reference" key may already be present if we have more than 2 duplicates
            if (!dictDuplicatedGuids.ContainsKey(sKey1))
            {
                dictDuplicatedGuids.Add(sKey1, sKey1);
            }
            // second key is based on index, so it won't exist
            string sKey2 = i.ToString() + "\t" + LoanGuid + "\t" + sLoanNumberFolderName;
            dictDuplicatedGuids.Add(sKey2, sKey2);
        }
        // Check LoanNumber
        if (!dictUniqueLoanNumbers.ContainsKey(LoanNumber))
        {
            dictUniqueLoanNumbers.Add(LoanNumber, sLoanFolderNameGuid);
        }
        else
        {
            // this LoanNumber is not unique
            string sKey1 = "REF" + "\t" + LoanNumber + "\t" + dictUniqueLoanNumbers[LoanNumber];
            // first "reference" key may already be present if we have more than 2 duplicates
            if (!dictDuplicatedLoanNumbers.ContainsKey(sKey1))
            {
                dictDuplicatedLoanNumbers.Add(sKey1, sKey1);
            }
            // second key is based on index, so it won't exist
            string sKey2 = i.ToString() + "\t" + LoanNumber + "\t" + sLoanFolderNameGuid;
            dictDuplicatedLoanNumbers.Add(sKey2, sKey2);
        }
    }

    // 3.1. read supplied GUIDs and resolve Loan Numbers
    string sAllGuids = txtInputGuids.Text;
    string[] arrGuids = sAllGuids.Split(
        new string[] { "\r", "\n", "\t", " " }, StringSplitOptions.RemoveEmptyEntries);
    System.Collections.Generic.List lstOutput = new System.Collections.Generic.List();
    foreach (string sGuid in arrGuids)
    {
        string sGuidKey = sGuid.ToLower().Trim(); // LCASE comparison
        string sResult = "";
        if (dictGuidToLoanNumber.ContainsKey(sGuidKey))
        {
            sResult = dictGuidToLoanNumber[sGuidKey];
        }
        else
        {
            sResult = "LOAN_NOT_FOUND";
        }
        lstOutput.Add(sGuidKey + "\t" + sResult);
    }

    // 4.1. show results
    if (lstOutput.Count > 0)
    {
        txtOutputResults.Text =
            "LoanGuid\tLoanNumber\tLoanFolder\tLastName\tORGID\r\n" +
            String.Join("\r\n", lstOutput.ToArray());
    }
    else
    {
        txtOutputResults.Text = "EMPTY";
    }
    if (dictDuplicatedGuids.Count > 0)
    {
        txtOutputBadGuids.Text =
            "ID\tLoanGuid\tLoanNumber\tLoanFolder\tLastName\tORGID\r\n" +
            String.Join("\r\n", dictDuplicatedGuids.Keys);
    }
    else
    {
        txtOutputBadGuids.Text = "EMPTY";
    }
    if (dictDuplicatedLoanNumbers.Count > 0)
    {
        txtOutputBadNumbers.Text =
            "ID\tLoanNumber\tLoanFolder\tLastName\tLoanGuid\tORGID\r\n" +
            String.Join("\r\n", dictDuplicatedLoanNumbers.Keys);
    }
    else
    {
        txtOutputBadNumbers.Text = "EMPTY";
    }
}
catch (Exception ex)
{
    System.Windows.Forms.MessageBox.Show("Error: " + ex.Message);
}
finally
{
    // restore cursor at the end
    System.Windows.Forms.Form.ActiveForm.Cursor = System.Windows.Forms.Cursors.Default;
}