public ActionResult AsSubjectReport()
        {
            var subjectLinks = AsRepo.GetLinkedSubjects();
            var authority    = GetSubjectAllDocs();

            var csv = new StringBuilder();

            csv.AppendLine("sid,rid,repo,subject,type,found,auth_id_exists");

            foreach (var link in subjectLinks)
            {
                Row found;
                var match     = false;
                var authMatch = false;
                var repo      = link.repo_id == 2 ? "Archives" : link.repo_id == 3 ? "Manuscripts" : "Laupus";
                if (link.type == "topical")
                {
                    found = authority.Find(x => x.doc.authoritativeLabel == link.subject && x.doc.topic.Contains("http://archivesspace.ecu.edu/resources/" + link.resource_id) && link.type == "topical");
                    if (found != null)
                    {
                        match     = true;
                        authMatch = link.authority_id == found.doc._id;
                    }
                    csv.AppendLine($"{link.id},{link.resource_id},{repo},\"{link.subject}\",{link.type},{match},{authMatch}");
                }
                else if (link.type == "geographic")
                {
                    found = authority.Find(x => x.doc.authoritativeLabel == link.subject && x.doc.geographic.Contains("http://archivesspace.ecu.edu/resources/" + link.resource_id) && link.type == "geographic");
                    if (found != null)
                    {
                        match     = true;
                        authMatch = link.authority_id == found.doc._id;
                    }
                    csv.AppendLine($"{link.id},{link.resource_id},{repo},\"{link.subject}\",{link.type},{match},{authMatch}");
                }
                else if (link.type == "personal")
                {
                    found = authority.Find(x => x.doc.authoritativeLabel == link.subject && x.doc.personalNameSubject.Contains("http://archivesspace.ecu.edu/resources/" + link.resource_id) && link.type == "personal");
                    if (found != null)
                    {
                        match     = true;
                        authMatch = link.authority_id == found.doc._id;
                    }
                    csv.AppendLine($"{link.id},{link.resource_id},{repo},\"{link.subject}\",{link.type},{match},{authMatch}");
                }
                else if (link.type == "family")
                {
                    found = authority.Find(x => x.doc.authoritativeLabel == link.subject && x.doc.familyNameSubject.Contains("http://archivesspace.ecu.edu/resources/" + link.resource_id) && link.type == "family");
                    if (found != null)
                    {
                        match     = true;
                        authMatch = link.authority_id == found.doc._id;
                    }
                    csv.AppendLine($"{link.id},{link.resource_id},{repo},\"{link.subject}\",{link.type},{match},{authMatch}");
                }
                else if (link.type == "corporate")
                {
                    found = authority.Find(x => x.doc.authoritativeLabel == link.subject && x.doc.corporateNameSubject.Contains("http://archivesspace.ecu.edu/resources/" + link.resource_id) && link.type == "corporate");
                    if (found != null)
                    {
                        match     = true;
                        authMatch = link.authority_id == found.doc._id;
                    }
                    csv.AppendLine($"{link.id},{link.resource_id},{repo},\"{link.subject}\",{link.type},{match},{authMatch}");
                }
                else if (link.type == "meeting")
                {
                    found = authority.Find(x => x.doc.authoritativeLabel == link.subject && x.doc.meeting.Contains("http://archivesspace.ecu.edu/resources/" + link.resource_id) && link.type == "meeting");
                    if (found != null)
                    {
                        match     = true;
                        authMatch = link.authority_id == found.doc._id;
                    }
                    csv.AppendLine($"{link.id},{link.resource_id},{repo},\"{link.subject}\",{link.type},{match},{authMatch}");
                }
                else if (link.type == "uniform_title")
                {
                    found = authority.Find(x => x.doc.authoritativeLabel == link.subject && x.doc.uniformTitle.Contains("http://archivesspace.ecu.edu/resources/" + link.resource_id) && link.type == "uniform_title");
                    if (found != null)
                    {
                        match     = true;
                        authMatch = link.authority_id == found.doc._id;
                    }
                    csv.AppendLine($"{link.id},{link.resource_id},{repo},\"{link.subject}\",{link.type},{match},{authMatch}");
                }
                else
                {
                    csv.AppendLine($"{link.id},{link.resource_id},{repo},\"{link.subject}\",{link.type},{match},{authMatch}");
                }
            }

            System.IO.File.WriteAllText(Server.MapPath("~/Download/AsSubjectReport.csv"), csv.ToString(), Encoding.UTF8);
            return(Redirect("~/Download/AsSubjectReport.csv"));
        }
        public ActionResult SubjectAuthorities()
        {
            var authority    = GetSubjectAllDocs();
            var subjectLinks = AsRepo.GetLinkedSubjects();

            var csv = new StringBuilder();

            csv.AppendLine("id,authoritativeLabel,externalAuthorityUri,archivesSpaceUri,type,rid,found,auth_id_exists");

            SubjectGroup found;

            foreach (var result in authority)
            {
                var match     = false;
                var authMatch = false;
                //if (result.doc._id == "3bd545c1c309a78af7f2d6875928aabe")
                //{
                //    var test = "adf";
                //    test.ToUpper();
                //}
                if (!result.doc._id.StartsWith("_"))
                {
                    var externalUri = string.Empty;
                    if (result.doc.externalAuthorityUri != null)
                    {
                        externalUri = result.doc.externalAuthorityUri;
                    }
                    else if (result.doc.substrings != null && result.doc.substrings.Count > 0)
                    {
                        externalUri = string.Join(";", result.doc.substrings?.Select(x => x.externalAuthorityUri));
                    }

                    if (result.doc.archivesSpaceRelations != null)
                    {
                        foreach (var item in result.doc.archivesSpaceRelations)
                        {
                            found = subjectLinks.Find(x => x.subject == result.doc.authoritativeLabel && x.resource_id.ToString() == item.Replace("http://archivesspace.ecu.edu/resources/", ""));
                            if (found != null)
                            {
                                match     = true;
                                authMatch = found.authority_id == result.doc._id;
                            }
                            csv.AppendLine($"{result.doc._id},\"{result.doc.authoritativeLabel}\",{externalUri},{result.doc.archivesSpaceUri},{result.doc.type},{item.Replace("http://archivesspace.ecu.edu/resources/", "")},{match},{authMatch}");
                        }
                    }

                    //if (result.doc.topic != null)
                    //{
                    //    foreach (var topic in result.doc.topic)
                    //    {
                    //        //if (result.doc.authoritativeLabel.StartsWith("Inchon"))
                    //        //{
                    //        //    var x = 33;
                    //        //}
                    //        found = subjectLinks.Find(x => x.subject == result.doc.authoritativeLabel && x.resource_id.ToString() == topic.Replace("http://archivesspace.ecu.edu/resources/", ""));// && x.type == "topical");
                    //        if (found != null)
                    //        {
                    //            match = true;
                    //            authMatch = found.authority_id == result.doc._id;
                    //        }

                    //        csv.AppendLine($"{result.doc._id},\"{result.doc.authoritativeLabel}\",{externalUri},{result.doc.archivesSpaceUri},topical,{topic.Replace("http://archivesspace.ecu.edu/resources/", "")},{match},{authMatch}");
                    //    }
                    //}
                    //if (result.doc.geographic != null)
                    //{
                    //    foreach (var item in result.doc.geographic)
                    //    {
                    //        found = subjectLinks.Find(x => x.subject == result.doc.authoritativeLabel && x.resource_id.ToString() == item.Replace("http://archivesspace.ecu.edu/resources/", ""));// && x.type == "geographic");
                    //        if (found != null)
                    //        {
                    //            match = true;
                    //            authMatch = found.authority_id == result.doc._id;
                    //        }
                    //        csv.AppendLine($"{result.doc._id},\"{result.doc.authoritativeLabel}\",{externalUri},{result.doc.archivesSpaceUri},geographic,{item.Replace("http://archivesspace.ecu.edu/resources/", "")},{match},{authMatch}");
                    //    }
                    //}
                    //if (result.doc.personalNameSubject != null)
                    //{
                    //    foreach (var item in result.doc.personalNameSubject)
                    //    {
                    //        found = subjectLinks.Find(x => x.subject == result.doc.authoritativeLabel && x.resource_id.ToString() == item.Replace("http://archivesspace.ecu.edu/resources/", ""));// && x.type == "personal");
                    //        if (found != null)
                    //        {
                    //            match = true;
                    //            authMatch = found.authority_id == result.doc._id;
                    //        }
                    //        csv.AppendLine($"{result.doc._id},\"{result.doc.authoritativeLabel}\",{externalUri},{result.doc.archivesSpaceUri},personal,{item.Replace("http://archivesspace.ecu.edu/resources/", "")},{match},{authMatch}");
                    //    }
                    //}
                    //if (result.doc.familyNameSubject != null)
                    //{
                    //    foreach (var item in result.doc.familyNameSubject)
                    //    {
                    //        found = subjectLinks.Find(x => x.subject == result.doc.authoritativeLabel && x.resource_id.ToString() == item.Replace("http://archivesspace.ecu.edu/resources/", ""));// && x.type == "family");
                    //        if (found != null)
                    //        {
                    //            match = true;
                    //            authMatch = found.authority_id == result.doc._id;
                    //        }
                    //        csv.AppendLine($"{result.doc._id},\"{result.doc.authoritativeLabel}\",{externalUri},{result.doc.archivesSpaceUri},family,{item.Replace("http://archivesspace.ecu.edu/resources/", "")},{match},{authMatch}");
                    //    }
                    //}
                    //if (result.doc.corporateNameSubject != null)
                    //{
                    //    foreach (var item in result.doc.corporateNameSubject)
                    //    {
                    //        found = subjectLinks.Find(x => x.subject == result.doc.authoritativeLabel && x.resource_id.ToString() == item.Replace("http://archivesspace.ecu.edu/resources/", ""));// && x.type == "corporate");
                    //        if (found != null)
                    //        {
                    //            match = true;
                    //            authMatch = found.authority_id == result.doc._id;
                    //        }
                    //        csv.AppendLine($"{result.doc._id},\"{result.doc.authoritativeLabel}\",{externalUri},{result.doc.archivesSpaceUri},corporate,{item.Replace("http://archivesspace.ecu.edu/resources/", "")},{match},{authMatch}");
                    //    }
                    //}
                    //if (result.doc.meeting != null)
                    //{
                    //    foreach (var item in result.doc.meeting)
                    //    {
                    //        found = subjectLinks.Find(x => x.subject == result.doc.authoritativeLabel && x.resource_id.ToString() == item.Replace("http://archivesspace.ecu.edu/resources/", ""));// && x.type == "meeting");
                    //        if (found != null)
                    //        {
                    //            match = true;
                    //            authMatch = found.authority_id == result.doc._id;
                    //        }
                    //        csv.AppendLine($"{result.doc._id},\"{result.doc.authoritativeLabel}\",{externalUri},{result.doc.archivesSpaceUri},meeting,{item.Replace("http://archivesspace.ecu.edu/resources/", "")},{match},{authMatch}");
                    //    }
                    //}
                    //if (result.doc.uniformTitle != null)
                    //{
                    //    foreach (var item in result.doc.uniformTitle)
                    //    {
                    //        found = subjectLinks.Find(x => x.subject == result.doc.authoritativeLabel && x.resource_id.ToString() == item.Replace("http://archivesspace.ecu.edu/resources/", ""));// && x.type == "uniform_title");
                    //        if (found != null)
                    //        {
                    //            match = true;
                    //            authMatch = found.authority_id == result.doc._id;
                    //        }
                    //        csv.AppendLine($"{result.doc._id},\"{result.doc.authoritativeLabel}\",{externalUri},{result.doc.archivesSpaceUri},title,{item.Replace("http://archivesspace.ecu.edu/resources/", "")},{match},{authMatch}");
                    //    }
                    //}
                }
            }

            System.IO.File.WriteAllText(Server.MapPath("~/Download/subjectAuths.csv"), csv.ToString(), Encoding.UTF8);
            return(Redirect("~/Download/subjectAuths.csv"));
        }