/// <summary>
        /// Creates string of tags related to resource
        /// </summary>
        /// <param name="metadata">the associated metadata</param>
        /// <param name="resource"> core.resource  </param>
        private static void AddResourceTags(XElement metadata, ScholarlyWorks.ScholarlyWorkItem resource)
        {
            if (null == metadata || null == resource)
            {
                return;
            }

            resource.Tags.ToList().ForEach(delegate(ScholarlyWorks.Tag tag)
            {
                if (!String.IsNullOrEmpty(tag.Name))
                {
                    metadata.Add(MetadataProviderHelper.GetElement(MetadataProviderHelper.Metadata_Subject, tag.Name));
                }
            });
        }
        /// <summary>
        /// Generates record element for ListRecord verb
        /// </summary>
        /// <param name="resourceList"> list of resources </param>
        /// <returns> an instance of XElement </returns>
        internal XElement GetRecordElement(List <Core.Resource> resourceList)
        {
            if (null == resourceList || resourceList.Count <= 0)
            {
                throw new ArgumentException(MetadataProviderHelper.Error_Message_NoRecords, "resourceList");
            }

            XElement listRecords = null;

            listRecords = new XElement(Verb_ListRecords);
            foreach (Core.Resource resource in resourceList)
            {
                XElement record = new XElement(Record);
                record.Add(this.GetHeaderElement(resource));
                record.Add(MetadataProviderHelper.GetMetadataElement(resource));
                listRecords.Add(record);
            }
            return(listRecords);
        }
        /// <summary>
        /// Creates header element
        /// </summary>
        /// <param name="resource"> resource object</param>
        /// <returns> instance of XElement </returns>
        internal XElement GetHeaderElement(Core.Resource resource)
        {
            XElement header = null;

            header = new XElement(Header);

            header.Add(MetadataProviderHelper.GetElement(Header_Identifier, resource.Id.ToString()));
            header.Add(MetadataProviderHelper.GetElement(Header_DateStamp, resource.DateModified.Value.ToString(MetadataProviderHelper.DateTimeGranularity, CultureInfo.InvariantCulture)));

            ResourceType resourceTypeInfo = new CoreHelper(entityConnectionString).GetResourceType(resource);

            if (resourceTypeInfo != null)
            {
                List <string> listOfResourceTypeInfo = new CoreHelper(entityConnectionString).GetResourceTypeHierarchy(resourceTypeInfo);
                listOfResourceTypeInfo.ForEach(delegate(string resourceType)
                {
                    header.Add(MetadataProviderHelper.GetElement(Header_SetSpec, resourceType));
                });
            }
            return(header);
        }
        /// <summary>
        /// Creates metadata element
        /// </summary>
        /// <param name="resource"> resource object </param>
        /// <returns> an instance of XElement </returns>
        internal static XElement GetMetadataElement(Core.Resource resource)
        {
            XElement metadata = null;

            metadata = new XElement(Metadata);

            if (!String.IsNullOrEmpty(resource.Title))
            {
                metadata.Add(MetadataProviderHelper.GetElement(Metadata_Title, resource.Title));
            }

            ScholarlyWorks.ScholarlyWork scholarlyWorks = resource as ScholarlyWorks.ScholarlyWork;
            if (null != scholarlyWorks)
            {
                if (scholarlyWorks.Authors != null)
                {
                    MetadataProviderHelper.AddResourceAuthors(metadata, scholarlyWorks);
                }
                if (scholarlyWorks.Contributors != null)
                {
                    MetadataProviderHelper.AddResourceContributors(metadata, scholarlyWorks);
                }
            }

            // TODO : 2.Currently Tags for a given resource are retrieved without
            //          checking any relationships, but the implementation needs to be changed if required
            ScholarlyWorks.ScholarlyWorkItem scholarlyWorkItem = resource as ScholarlyWorks.ScholarlyWorkItem;
            if (scholarlyWorkItem != null && scholarlyWorkItem.Tags != null)
            {
                MetadataProviderHelper.AddResourceTags(metadata, scholarlyWorkItem);
            }

            metadata.Add(MetadataProviderHelper.GetElement(Metadata_Description, resource.Description));

            if (!String.IsNullOrEmpty(resource.DateModified.Value.ToString()))
            {
                metadata.Add(MetadataProviderHelper.GetElement(Metadata_Date, resource.DateModified.Value.ToString(DateTimeGranularity, CultureInfo.InvariantCulture)));
            }
            return(metadata);
        }
        /// <summary>
        /// retrieves contributors of resource and adds them to the associated metadata element
        /// </summary>
        /// <param name="metadata">the associated metadata</param>
        /// <param name="scholarlyWorks">scholarlyWorks object whose contributors to be retrieved</param>
        private static void AddResourceContributors(XElement metadata, ScholarlyWorks.ScholarlyWork scholarlyWorks)
        {
            if (null == metadata || null == scholarlyWorks)
            {
                return;
            }

            scholarlyWorks.Contributors.ToList().ForEach(delegate(ScholarlyWorks.Contact contact)
            {
                ScholarlyWorks.Person person = contact as ScholarlyWorks.Person;
                string personDetails         = string.Empty;
                if (null != person)
                {
                    personDetails = CoreHelper.GetCompleteName(person.FirstName, person.MiddleName, person.LastName);
                    //In case if any other details are to be added then they can be added here
                }

                if (!string.IsNullOrEmpty(personDetails))
                {
                    metadata.Add(MetadataProviderHelper.GetElement(MetadataProviderHelper.Metadata_Contributor, personDetails));
                }
            });
        }
 /// <summary>
 /// Calculates pending records
 /// </summary>
 /// <param name="totalRecords">complete list size</param>
 /// <returns>returns pending records count</returns>
 internal static int GetPendingRecords(int totalRecords)
 {
     return(MetadataProviderHelper.GetRecordsCount(totalRecords, PlatformSettings.MaximumHarvestCount));
 }
示例#7
0
        private List <Core.Resource> GetResourceList <T>(
            Hashtable queryParams,
            DateTime initialQueryExecutionTime,
            bool isListRecords,
            bool isResumptionTokenSpecified,
            MetadataProviderHelper.TokenDetails token) where T : Core.Resource
        {
            List <Core.Resource> listOfResources = new List <Core.Resource>();
            IQueryable <T>       resourceQuery   = null;

            // 1) Build the query
            DateTime from  = DateTime.MinValue;
            DateTime until = DateTime.MinValue;

            if (queryParams.ContainsKey("from"))
            {
                from = ValidateDate(queryParams["from"], false);
            }
            if (queryParams.ContainsKey("until"))
            {
                until = ValidateDate(queryParams["until"], true);
            }

            using (Core.ZentityContext context = CoreHelper.CreateZentityContext())
            {
                AuthenticatedToken authenticatedToken = GetAuthenticationToken();

                if (from != DateTime.MinValue)
                {
                    if ((until != DateTime.MinValue))
                    {
                        if (from > until)
                        {
                            throw new ArgumentException(MetadataProviderHelper.Error_Message_Invalid_QueryParams, "queryParams");
                        }
                        resourceQuery = FilterQuery(context).OfType <T>().
                                        Where(resource =>
                                              resource.DateModified >= from &&
                                              resource.DateModified <= until &&
                                              resource.DateModified < initialQueryExecutionTime)
                                        .Authorize("Read", context, authenticatedToken)
                                        .OrderByDescending(resource => resource.DateModified);
                    }
                    else
                    {
                        resourceQuery = FilterQuery(context).OfType <T>().
                                        Where(resource =>
                                              resource.DateModified >= from &&
                                              resource.DateModified < initialQueryExecutionTime)
                                        .Authorize("Read", context, authenticatedToken)
                                        .OrderByDescending(resource => resource.DateModified);
                    }
                }
                else
                {
                    resourceQuery = FilterQuery(context).OfType <T>().
                                    Where(resource =>
                                          resource.DateModified < initialQueryExecutionTime)
                                    .Authorize("Read", context, authenticatedToken)
                                    .OrderByDescending(resource => resource.DateModified);
                }

                // 2) Retrieve resources count
                this.totalResourceCount = this.actualRecordCount = resourceQuery.Count();

                if (this.totalResourceCount <= 0)
                {
                    throw new ArgumentException(MetadataProviderHelper.Error_Message_NoRecords,
                                                "queryParams");
                }
                if (!isResumptionTokenSpecified)
                {
                    // 3) If count greater than maxharvestcount then
                    //       retrieve resources according to harvest count
                    //    else
                    //       retrieve resources according to original count
                    if (MetadataProviderHelper.IsResumptionRequired(this.totalResourceCount))
                    {
                        listOfResources = CoreHelper.FilterResources <T>(
                            resourceQuery.Take(PlatformSettings.MaximumHarvestCount),
                            isListRecords);
                        this.actualHarvestedCount = listOfResources.Count;
                    }
                    else
                    {
                        listOfResources = CoreHelper.FilterResources <T>(resourceQuery, isListRecords);
                    }
                }
                else
                {
                    // 4) retrieve pending resources i.e : according to maximum harvest count
                    int noOfRecordsToSkip = token.ActualHarvestedRecords;

                    listOfResources = CoreHelper.FilterResources <T>(resourceQuery.
                                                                     Skip(noOfRecordsToSkip).
                                                                     Take(PlatformSettings.MaximumHarvestCount), isListRecords);

                    this.actualHarvestedCount = noOfRecordsToSkip + listOfResources.Count;
                }
            }
            return(listOfResources);
        }