Beispiel #1
0
        public IHttpActionResult SecureListDocs()
        {
            var parms = new ParamCollection(Request.RequestUri.ParseQueryString());

            var osuId = parms.Get("osuId", 0);

            if (osuId == 0)
            {
                return(BadRequest("A valid OSUID is required."));
            }

            var timeToLive = parms.Get("timeToLive", DefaultTimeToLive);

            long startDocId   = 0;
            var  startDocHash = parms.Get("filter[startDocHash]", DefaultStartDocId);

            var filter = new DocListFilter
            {
                IndexKey       = parms.Get("filter[indexKey]", DefaultIndexKey),
                DocTypeGroup   = parms.Get("filter[docTypeGroup]", DefaultTypeGroup),
                DocType        = parms.Get("filter[docType]", DefaultDocType),
                StartDocId     = startDocId,
                PageSize       = parms.Get("filter[pageSize]", DefaultPageSize),
                KeywordsHasAll = parms.Get("filter[keywords][hasAll]", DefaultKeywords),
            };

            return(ListDocs <SecureDocumentAttributes>(filter, d => SecureDocumentResource(osuId, timeToLive, d)));
        }
Beispiel #2
0
        public IHttpActionResult ListDocs()
        {
            var parms = new ParamCollection(Request.RequestUri.ParseQueryString());

            var filter = new DocListFilter
            {
                IndexKey       = parms.Get("filter[indexKey]", DefaultIndexKey),
                DocTypeGroup   = parms.Get("filter[typeGroup]", DefaultTypeGroup),
                DocType        = parms.Get("filter[type]", DefaultDocType),
                StartDocId     = parms.Get("filter[startDocId]", DefaultStartDocId),
                PageSize       = parms.Get("filter[pageSize]", DefaultPageSize),
                KeywordsHasAll = parms.Get("filter[keywords][hasAll]", DefaultKeywords),
            };

            return(ListDocs <DocumentAttributes>(filter, d => DocumentResource(d)));
        }
Beispiel #3
0
        IHttpActionResult ListDocs <T>(DocListFilter filter, Func <Document, DataResource <T> > resourceFactory)
        {
            // We bundle the bad requests in one response.
            var badRequestErrors = new List <Error>();

            //
            // Read the keywords.
            //
            var filterKeywords = new Dictionary <string, string>();

            if (!string.IsNullOrWhiteSpace(filter.KeywordsHasAll))
            {
                foreach (var kw in filter.KeywordsHasAll.Split('|'))
                {
                    var parts = kw.Split(':');
                    if (parts.Length == 2)
                    {
                        filterKeywords[parts[0]] = parts[1];
                    }
                    else
                    {
                        badRequestErrors.Add(BadRequestError("The filter[keywords][hasAll] parameter is not valid."));
                    }
                }
            }

            if (string.IsNullOrWhiteSpace(filter.IndexKey) && string.IsNullOrWhiteSpace(filter.DocType))
            {
                badRequestErrors.Add(BadRequestError("One of filter[indexKey] or filter[type] parameters is required."));
            }

            var config = Global.Config;

            return(TryHandleRequest(app =>
            {
                DocumentTypeGroup docTypeGroup = null;
                if (!string.IsNullOrWhiteSpace(filter.DocTypeGroup))
                {
                    docTypeGroup = app.Core.DocumentTypeGroups.Find(filter.DocTypeGroup);
                    if (docTypeGroup == null)
                    {
                        badRequestErrors.Add(BadRequestError($"The document type group '{filter.DocTypeGroup}' could not be found."));
                    }
                }

                DocumentType docType = null;
                if (!string.IsNullOrWhiteSpace(filter.DocType))
                {
                    docType = app.Core.DocumentTypes.Find(filter.DocType);
                    if (docType == null)
                    {
                        badRequestErrors.Add(BadRequestError($"The document type '{filter.DocType}' could not be found."));
                    }
                }

                var query = app.Core.CreateDocumentQuery();
                if (query == null)
                {
                    badRequestErrors.Add(BadRequestError("Unable to create document query."));
                }

                // Check if there are any bad request errors. If there are then return them.
                if (badRequestErrors.Any())
                {
                    return BadRequestResult(badRequestErrors);
                }

                if (docTypeGroup != null)
                {
                    query.AddDocumentTypeGroup(docTypeGroup);
                }
                if (docType != null)
                {
                    query.AddDocumentType(docType);
                }
                if (!string.IsNullOrWhiteSpace(filter.IndexKey))
                {
                    query.AddKeyword(config.DocIndexKeyName, filter.IndexKey);
                }

                // Add the keywords to the query.
                foreach (var keyword in filterKeywords)
                {
                    query.AddKeyword(keyword.Key, keyword.Value);
                }

                // The OnBase API does not support a method for paging. The closest
                // we can get is to use a starting document ID.
                query.AddSort(DocumentQuery.SortAttribute.DocumentID, true);
                query.AddDocumentRange(filter.StartDocId, long.MaxValue);
                var queryResults = query.Execute(filter.PageSize);
                if (queryResults == null)
                {
                    return InternalErrorResult("Document query returned null.");
                }

                var docs = new List <DataResource <T> >();
                foreach (var doc in queryResults)
                {
                    docs.Add(resourceFactory(doc));
                }

                // Generate the query string for this request.
                var builder = new QueryStringBuilder();
                builder.Add("filter[indexKey]", filter.IndexKey, DefaultIndexKey);
                builder.Add("filter[docTypeGroup]", filter.DocTypeGroup, DefaultTypeGroup);
                builder.Add("filter[docType]", filter.DocType, DefaultDocType);
                builder.Add("filter[startDocId]", filter.StartDocId, DefaultStartDocId);
                builder.Add("filter[pageSize]", filter.PageSize, DefaultPageSize);
                builder.Add("filter[keywords][hasAll]", filter.KeywordsHasAll, DefaultKeywords);
                var queryStr = builder.ToString();

                return Ok(new ListResult <T>
                {
                    Data = docs,
                    Links = new DataLinks
                    {
                        Self = $"{config.ApiHost}/{config.ApiBasePath}{queryStr}",
                    }
                });
            }));
        }