Ejemplo n.º 1
0
        /// <summary>
        /// Gets the type of the resource.
        /// </summary>
        /// <param name="typeName">Name of the type.</param>
        /// <returns>The <see cref="ResourceType"/>.</returns>
        internal ResourceType GetResourceType(string typeName)
        {
            if (string.IsNullOrEmpty(typeName))
            {
                return(null);
            }

            ResourceType typeInfo = null;

            using (Core.ZentityContext context = CoreHelper.CreateZentityContext(entityConnectionString))
            {
                var resourceTypes = context.DataModel.Modules
                                    .SelectMany(tuple => tuple.ResourceTypes);

                if (typeName.Contains("."))
                {
                    typeInfo = resourceTypes.FirstOrDefault(type => type.FullName.ToUpperInvariant() == typeName.ToUpperInvariant());
                }
                else
                {
                    typeInfo = resourceTypes.FirstOrDefault(type => type.Name.ToUpperInvariant() == typeName.ToUpperInvariant());
                }
            }

            return(typeInfo);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Retrieves a collection of all the resources that have recently been added or modified
        /// </summary>
        /// <typeparam name="T">Type of Resource which derive from
        /// <typeref name="Zentity.Core.Resource"/></typeparam>
        /// <param name="isForDateAdded">
        /// boolean indicating if we want to retrieve resources that have been currently added
        /// or those resources that have been recently modified</param>
        /// <param name="count">The number of resources that are to be retrieved</param>
        /// <param name="date">The date on which resources have been added or modified</param>
        /// <param name="connectionString">Data storage connection string.</param>
        /// <returns>List of resources</returns>
        internal static List <T> GetResourcesByDate <T>(bool isForDateAdded,
                                                        ushort count,
                                                        DateTime date,
                                                        string connectionString) where T : Core.Resource
        {
            if (0 >= count)
            {
                throw new ArgumentException(Properties.Resources.EXCEPTION_ARGUMENTINVALID, "count");
            }

            if (count > PlatformSettings.MaximumFeedCount)
            {
                count = PlatformSettings.MaximumFeedCount;
            }

            List <T>       listOfResources = null;
            Func <T, bool> predicate       = null;

            if (isForDateAdded)
            {
                predicate = resource => resource.DateAdded >= date;
            }
            else
            {
                predicate = resource => resource.DateModified >= date;
            }

            try
            {
                using (Core.ZentityContext zentityContext = CoreHelper.CreateZentityContext(connectionString))
                {
                    zentityContext.CommandTimeout = PlatformSettings.ConnectionTimeout;
                    listOfResources = zentityContext.Resources
                                      .OfType <T>()
                                      .Where(predicate)
                                      .OrderByDescending(resource => resource.DateModified)
                                      .AsEnumerable()
                                      .Where(resource => !(resource is Contact || resource is Tag || resource is CategoryNode))
                                      .Take(count)
                                      .ToList();
                    LoadResources <T>(listOfResources);
                }
            }
            catch (ArgumentNullException)
            {
                throw;
            }

            return(listOfResources);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Retrieves a collection of all the resources owned by the author
        /// </summary>
        /// <typeparam name="T">Type of Resource which derive from
        /// <typeref name="Zentity.Core.Resource"/></typeparam>
        /// <param name="firstName">The associated author's first name</param>
        /// <param name="lastName">The associated author's last name</param>
        /// <param name="count">The number of resources that are to be retrieved</param>
        /// <param name="connectionString">Data storage connection string.</param>
        /// <returns>A collection of key value pairs of author and his associated resources</returns>
        internal static List <T> GetResourcesByAuthor <T>(string firstName,
                                                          string lastName,
                                                          ushort count,
                                                          string connectionString) where T : ScholarlyWorks.ScholarlyWork
        {
            if (string.IsNullOrEmpty(firstName))
            {
                throw new ArgumentNullException("firstName");
            }
            if (string.IsNullOrEmpty(lastName))
            {
                throw new ArgumentNullException("lastName");
            }
            if (0 >= count)
            {
                throw new ArgumentException(Properties.Resources.EXCEPTION_ARGUMENTINVALID, "count");
            }


            if (count > PlatformSettings.MaximumFeedCount)
            {
                count = PlatformSettings.MaximumFeedCount;
            }

            List <T> listOfResources = null;

            try
            {
                using (Core.ZentityContext zentityContext = CoreHelper.CreateZentityContext(connectionString))
                {
                    zentityContext.CommandTimeout = PlatformSettings.ConnectionTimeout;
                    listOfResources = zentityContext.People()
                                      .Where(author => author.FirstName == firstName &&
                                             author.LastName == lastName)
                                      .SelectMany(author => author.AuthoredWorks)
                                      .OfType <T>()
                                      .OrderByDescending(resource => resource.DateModified)
                                      .Take(count)
                                      .ToList();
                    LoadResources <T>(listOfResources);
                }
            }
            catch (ArgumentNullException)
            {
                throw;
            }

            return(listOfResources);
        }
Ejemplo n.º 4
0
        /// <summary>
        ///     retrieves the when was first resource added to core
        /// </summary>
        /// <returns>datestamp of first resource added</returns>
        internal string GetEarliestdateStamp()
        {
            using (Core.ZentityContext zentityContext = CoreHelper.CreateZentityContext())
            {
                DateTime?earliestScholarlyWorkDateStamp = getEarliestDateStamp(zentityContext);

                if (earliestScholarlyWorkDateStamp.HasValue)
                {
                    return(earliestScholarlyWorkDateStamp.Value.ToUniversalTime().
                           ToString(MetadataProviderHelper.DateTimeGranularity, CultureInfo.InvariantCulture));
                }
                else
                {
                    return(string.Empty);
                }
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Checks if specified resource exist
        /// </summary>
        /// <param name="identifier"> resource identifier </param>
        /// <returns> boolean indicating whether resource exist or not </returns>
        internal bool CheckIfResourceExists(Guid identifier)
        {
            bool exists = false;

            using (Core.ZentityContext zentityContext = CoreHelper.CreateZentityContext())
            {
                var returnedResource = zentityContext.Resources
                                       .Where(resource => resource.Id == identifier)
                                       .FirstOrDefault();

                if (returnedResource != null &&
                    returnedResource.Authorize("Read", zentityContext, GetAuthenticationToken()))
                {
                    exists = true;
                }
            }

            return(exists);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// retrieves resource object specific to resource id
        /// </summary>
        /// <param name="resourceId">id for retrieving specific object</param>
        /// <returns>resource object</returns>
        internal Core.Resource GetResource(Guid resourceId)
        {
            Core.Resource coreResource = null;

            using (Core.ZentityContext zentityContext = CoreHelper.CreateZentityContext())
            {
                var returnedResource = zentityContext.Resources
                                       .Where(resource => resource.Id == resourceId)
                                       .FirstOrDefault();

                if (returnedResource != null &&
                    returnedResource.Authorize("Read", zentityContext, GetAuthenticationToken()))
                {
                    coreResource = LoadResource(returnedResource);
                }
            }

            return(coreResource);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Retrieves a collection of all the resources having with the specified label for the tag
        /// </summary>
        /// <typeparam name="T">Type of Resource which derive from
        /// <typeref name="Zentity.Core.Resource"/></typeparam>
        /// <param name="tagLabel">The associated tag label</param>
        /// <param name="count">The number of resources that are to be retrieved</param>
        /// <param name="connectionString">Data storage connection string.</param>
        /// <returns>A key value pair of tag and its resources</returns>
        internal static List <T> GetResourcesByCategory <T>(string tagLabel,
                                                            ushort count,
                                                            string connectionString) where T : ScholarlyWorks.ScholarlyWorkItem
        {
            if (string.IsNullOrEmpty(tagLabel))
            {
                throw new ArgumentNullException("tagLabel");
            }
            if (0 >= count)
            {
                throw new ArgumentException(Properties.Resources.EXCEPTION_ARGUMENTINVALID, "count");
            }

            if (count > PlatformSettings.MaximumFeedCount)
            {
                count = PlatformSettings.MaximumFeedCount;
            }

            List <T> listOfResources = null;

            try
            {
                using (Core.ZentityContext zentityContext = CoreHelper.CreateZentityContext(connectionString))
                {
                    zentityContext.CommandTimeout = PlatformSettings.ConnectionTimeout;
                    listOfResources = zentityContext.CategoryNodes()
                                      .Where(tag => tag.Title == tagLabel)
                                      .SelectMany(tag => tag.ScholarlyWorkItems)
                                      .OfType <T>()
                                      .OrderByDescending(resource => resource.DateModified)
                                      .Take(count)
                                      .ToList();
                    LoadResources <T>(listOfResources);
                }
            }

            catch (ArgumentNullException)
            {
                throw;
            }

            return(listOfResources);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Retrieves a collection of all the resources whose contributor's email Id has been specified
        /// </summary>
        /// <typeparam name="T">Type of Resource which derive from
        /// <typeref name="Zentity.Core.Resource"/></typeparam>
        /// <param name="emailId">The associated contributor's email id</param>
        /// <param name="count">The number of resources that are to be retrieved</param>
        /// <param name="connectionString">Data storage connection string.</param>
        /// <returns>A collection of key value pairs of author and his associated resources</returns>
        internal static List <T> GetResourcesByContributor <T>(string emailId,
                                                               ushort count, string connectionString) where T : ScholarlyWorks.ScholarlyWork
        {
            if (string.IsNullOrEmpty(emailId))
            {
                throw new ArgumentNullException("emailId");
            }
            if (0 >= count)
            {
                throw new ArgumentException(Properties.Resources.EXCEPTION_ARGUMENTINVALID, "count");
            }

            if (count > PlatformSettings.MaximumFeedCount)
            {
                count = PlatformSettings.MaximumFeedCount;
            }

            List <T> listOfResources = null;

            try
            {
                using (Core.ZentityContext zentityContext = CoreHelper.CreateZentityContext(connectionString))
                {
                    zentityContext.CommandTimeout = PlatformSettings.ConnectionTimeout;
                    listOfResources = zentityContext.Contacts()
                                      .Where(contributor => contributor.Email == emailId)
                                      .SelectMany(contributor => contributor.ContributionInWorks)
                                      .OfType <T>()
                                      .OrderByDescending(resource => resource.DateModified)
                                      .Take(count)
                                      .ToList();
                    LoadResources <T>(listOfResources);
                }
            }
            catch (ArgumentNullException)
            {
                throw;
            }

            return(listOfResources);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Gets the resource type hierarchy.
        /// </summary>
        /// <param name="resourceTypeInfo">The resource type info.</param>
        /// <returns>List of resource type by heirarchy.</returns>
        internal List <string> GetResourceTypeHierarchy(ResourceType resourceTypeInfo)
        {
            List <string> resourceTypeList = new List <string>();

            if (null == resourceTypeInfo)
            {
                return(resourceTypeList);
            }

            if (CoreHelper.resourceTypeHierarchy.ContainsKey(resourceTypeInfo.Name))
            {
                return(CoreHelper.resourceTypeHierarchy[resourceTypeInfo.Name]);
            }

            ResourceType orginalResourceType = resourceTypeInfo;

            resourceTypeList.Add(resourceTypeInfo.Name);

            using (Core.ZentityContext context = CoreHelper.CreateZentityContext(entityConnectionString))
            {
                while (resourceTypeInfo.BaseType != null)
                {
                    ResourceType resourceType = null;
                    foreach (ResourceType resType in CoreHelper.GetResourceTypes(context))
                    {
                        if ((resourceTypeInfo.BaseType.FullName.EndsWith(resType.Name, StringComparison.Ordinal) &&
                             resourceTypeInfo.BaseType.FullName.StartsWith(resType.Parent.NameSpace, StringComparison.Ordinal)))
                        {
                            resourceType = resType;
                            break;
                        }
                    }

                    resourceTypeInfo = resourceType;
                    resourceTypeList.Add(resourceTypeInfo.Name);
                }
            }
            CoreHelper.resourceTypeHierarchy.Add(orginalResourceType.Name, resourceTypeList);
            return(resourceTypeList);
        }
Ejemplo n.º 10
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);
        }