private async Task <CombinedBarcodeResponseItem> FormatTcEntryAsync(TopContainer container,
                                                                            ArchivesSpaceService activeService)
        {
            if (container == null)
            {
                return(new CombinedBarcodeResponseItem());
            }
            var location = await GetContainerLocationDisplayString(container, activeService);

            var resourceManager = new ArchivesSpaceResourceManager(activeService);

            var resource = await resourceManager.GetResourceByIdAsync(container.Collection.First().RefStrippedId);

            var hasSeries      = container.Series.Count > 0;
            var seriesDivision = hasSeries ? container.Series.First().Identifier.FormatSeriesDivision() : "";
            var seriesTitle    = hasSeries ? container.Series.First().DisplayString : "";
            var tcEntry        = new CombinedBarcodeResponseItem
            {
                Origin         = "ArchivesSpace",
                Author         = "",
                BoxNumber      = Helpers.FormatBoxText(container.Indicator),
                CallNumber     = Helpers.FormatCallNumber(container.Collection.First().Identifier),
                Location       = location,
                ResourceTitle  = container.Collection.First().DisplayString,
                ResourceId     = container.Collection.First().RefStrippedId,
                Handle         = resource.EadLocation, //This seems to be null pretty often even if EadId (not a URL) is not
                Restriction    = (container.Restricted ? "Y" : "N"),
                SeriesDivision = seriesDivision,
                SeriesTitle    = seriesTitle
            };

            return(tcEntry);
        }
        public static async Task TestResources(ArchivesSpaceService aspaceService, int resourceId)
        {
            var randIntGen = new Random();

            Console.WriteLine("Getting all resources:");
            var resourceManager = new ArchivesSpaceResourceManager(aspaceService);


            if (resourceId == 0)
            {
                Console.WriteLine("No resource specified, fetching random from list of all resources: ");
                var resourceList = await resourceManager.GetAllResourceIdsAsync();

                Console.WriteLine(Serialize(resourceList));

                var randInt = randIntGen.Next(0, resourceList.Count - 1);

                resourceId = resourceList[randInt];
                Console.WriteLine("ID [ {0} ] selected at random", resourceId);
            }
            Console.WriteLine("Getting resource with ID [ {0} ]", resourceId);
            var singleResource = await resourceManager.GetResourceByIdAsync(resourceId);

            Console.WriteLine(Serialize(singleResource));

            Console.WriteLine("Get the top level series archival objects for this resource:");
            var seriesCollection = await resourceManager.GetTopLevelSeriesArchivalObjects(resourceId);

            Console.WriteLine(Serialize(seriesCollection));


            Console.WriteLine("Fetch the small tree of this resource: ");
            var smallTree = await resourceManager.GetSmallResourceTreeAsync(resourceId);

            Console.WriteLine(Serialize(smallTree));
        }
        public async Task <HttpResponseMessage> Get(string repo = ""
                                                    , [FromUri(Name = "bib_id")] int?bibIdNullable  = 0
                                                    , [FromUri(Name = "call_no")] string callNumber = "")
        {
            var reqId = Guid.NewGuid().ToString("D");
            var sw    = new Stopwatch();

            sw.Start();
            _logger.Trace("Request for URI [ {0} ] received and assigned ID [ {1} ]", Request.RequestUri, reqId);
            var bibId = bibIdNullable ?? 0;

            if (repo == "")
            {
                repo = ASpace_Aeon_Middleware.Configuration.DefaultSite;
            }
            var resourceId = bibId; //parameters have to be named so for querystring binding with legacy client
            var resultList = new List <SeriesResponseItem>();
            ArchivesSpaceService activeService;

            try
            {
                activeService = await _serviceHandler.GetArchivesSpaceServiceAsync(repo);
            }
            catch (InvalidOperationException ex)
            {
                _logger.Trace("Request [ {0} ] finished in [ {1} ] ms", reqId, sw.ElapsedMilliseconds);
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, String.Format("repository [ {0} ] is not a valid repository", repo), ex));
            }
            if (resourceId == 0)
            {
                if (callNumber == "" || callNumber == String.Empty)
                {
                    _logger.Trace("Request [ {0} ] finished in [ {1} ] ms", reqId, sw.ElapsedMilliseconds);
                    return(Request.CreateResponse(HttpStatusCode.OK, resultList));
                }
                var aspaceHelper = new AspaceHelpers(activeService);
                var searchResult = await aspaceHelper.GetResourceIdFromCallNumber(callNumber);

                if (searchResult == 0)
                {
                    _logger.Trace("Request [ {0} ] finished in [ {1} ] ms", reqId, sw.ElapsedMilliseconds);
                    return(Request.CreateResponse(HttpStatusCode.OK, resultList));
                }
                resourceId = searchResult;
            }
            var resourceService    = new ArchivesSpaceResourceManager(activeService);
            var resourceRecordTask = resourceService.GetResourceByIdAsync(resourceId);
            var seriesRecords      = await resourceService.GetTopLevelSeriesArchivalObjects(resourceId);

            if (seriesRecords.Count > 0)
            {
                var resourceRecord = await resourceRecordTask;
                foreach (var archivalObject in seriesRecords)
                {
                    var resultEntry = new SeriesResponseItem
                    {
                        CollectionTitle = resourceRecord.Title,
                        EadLocation     = resourceRecord.EadLocation,
                        SeriesId        = archivalObject.Id,
                        SeriesDiv       = archivalObject.ComponentId.FormatSeriesDivision(),
                        SeriesTitle     = archivalObject.DisplayString //The title omits some information
                    };
                    resultList.Add(resultEntry);
                }
            }
            _logger.Trace("Request [ {0} ] finished in [ {1} ] ms", reqId, sw.ElapsedMilliseconds);
            return(Request.CreateResponse(HttpStatusCode.OK, resultList));
        }