Permalink
Browse files

New feature: Support to query the Json data from remote API in the da…

…ta rule. #174
  • Loading branch information...
kooboo-jifeng committed Nov 20, 2013
1 parent 228bea0 commit 27ed4c5bd0c18e4c4bd6699a8b579af3b87e6442
Showing with 1,306 additions and 539 deletions.
  1. +2 −2 Kooboo.CMS/Kooboo.CMS.Sites/DataRule/CategoryDataRule.cs
  2. +122 −8 Kooboo.CMS/Kooboo.CMS.Sites/DataRule/DataRuleBase.cs
  3. +2 −113 Kooboo.CMS/Kooboo.CMS.Sites/DataRule/DataRuleExecutor.cs
  4. +4 −3 Kooboo.CMS/Kooboo.CMS.Sites/DataRule/DataRuleType.cs
  5. +3 −3 Kooboo.CMS/Kooboo.CMS.Sites/DataRule/FolderDataRule.cs
  6. +116 −0 Kooboo.CMS/Kooboo.CMS.Sites/DataRule/Http/HttpDataRequest.cs
  7. +22 −0 Kooboo.CMS/Kooboo.CMS.Sites/DataRule/Http/IHttpDataRequest.cs
  8. +21 −0 Kooboo.CMS/Kooboo.CMS.Sites/DataRule/Http/IResponseTextParser.cs
  9. +31 −0 Kooboo.CMS/Kooboo.CMS.Sites/DataRule/Http/JsonTextParser.cs
  10. +86 −0 Kooboo.CMS/Kooboo.CMS.Sites/DataRule/Http/XmlTextParser.cs
  11. +135 −0 Kooboo.CMS/Kooboo.CMS.Sites/DataRule/HttpDataRule.cs
  12. +29 −0 Kooboo.CMS/Kooboo.CMS.Sites/DataRule/IContentDataRule.cs
  13. +9 −10 Kooboo.CMS/Kooboo.CMS.Sites/DataRule/IDataRule.cs
  14. +13 −0 Kooboo.CMS/Kooboo.CMS.Sites/DataRule/ResponseType.cs
  15. +1 −1 Kooboo.CMS/Kooboo.CMS.Sites/DataRule/SchemaDataRule.cs
  16. +11 −0 Kooboo.CMS/Kooboo.CMS.Sites/Kooboo.CMS.Sites.csproj
  17. +2 −2 Kooboo.CMS/Kooboo.CMS.Sites/Models/DataRuleSetting.cs
  18. +2 −2 Kooboo.CMS/Kooboo.CMS.Sites/Persistence/FileSystem/PageProvider.cs
  19. +2 −2 Kooboo.CMS/Kooboo.CMS.Sites/Persistence/FileSystem/ViewProvider.cs
  20. +6 −10 Kooboo.CMS/Kooboo.CMS.Sites/View/FrontHtmlHelper.cs
  21. +1 −1 .../Kooboo.CMS.SitesProviders/Kooboo.CMS.Sites.TemplateEngines.NVelocity/NVelocityListCodeSnippet.cs
  22. +1 −1 Kooboo.CMS/Kooboo.CMS.SitesProviders/Kooboo.CMS.Sites.TemplateEngines.Razor/RazorListCodeSnippet.cs
  23. +1 −1 ....CMS/Kooboo.CMS.SitesProviders/Kooboo.CMS.Sites.TemplateEngines.WebForm/WebFormListCodeSnippet.cs
  24. +3 −0 Kooboo.CMS/Kooboo.CMS.Web/Areas/Sites/ModelBinders/DataRuleBinder.cs
  25. +309 −255 Kooboo.CMS/Kooboo.CMS.Web/Areas/Sites/Views/Shared/DataRules.Scripts.cshtml
  26. +28 −0 Kooboo.CMS/Kooboo.CMS.Web/Areas/Sites/Views/Shared/_ChooseDataRuleType.cshtml
  27. +1 −0 Kooboo.CMS/Kooboo.CMS.Web/Areas/Sites/Views/Shared/_ChooseFolder.cshtml
  28. +15 −1 Kooboo.CMS/Kooboo.CMS.Web/Areas/Sites/Views/Shared/_DataRulesJsonToFormInput.cshtml
  29. +171 −20 Kooboo.CMS/Kooboo.CMS.Web/Areas/Sites/Views/Shared/_EditDataRule.cshtml
  30. +2 −0 Kooboo.CMS/Kooboo.CMS.Web/Areas/Sites/Views/View/DataRules.cshtml
  31. +3 −0 Kooboo.CMS/Kooboo.CMS.Web/Kooboo.CMS.Web.csproj
  32. +3 −1 Kooboo.CMS/Kooboo.CMS.Web/KoobooCMSHttpApplicationEvents.cs
  33. +128 −103 Kooboo.CMS/Kooboo.CMS.Web/Styles/common.css
  34. +21 −0 Kooboo.CMS/Kooboo/Collections/Generic/CKeyValuePair.cs
@@ -23,9 +23,9 @@ namespace Kooboo.CMS.Sites.DataRule
[KnownTypeAttribute(typeof(CategoryDataRule))]
public class CategoryDataRule : FolderDataRule
{
public override Content.Query.IContentQuery<Content.Models.TextContent> Execute(DataRuleContext dataRuleContext)
public override IContentQuery<Content.Models.TextContent> GetContentQuery(DataRuleContext dataRuleContext)
{
var contentQuery = base.Execute(dataRuleContext);
var contentQuery = base.GetContentQuery(dataRuleContext);
if (contentQuery is MediaContentQuery)
{
throw new KoobooException(string.Format("The binary folder '{0}' does not support '{1}'.", FolderName, "CategoryDataRule"));
@@ -8,11 +8,18 @@
#endregion
using Kooboo.CMS.Common.Persistence.Non_Relational;
using Kooboo.CMS.Content.Models;
using Kooboo.CMS.Content.Query;
using Kooboo.CMS.Content.Query.Translator.String;
using Kooboo.CMS.Sites.Models;
using Kooboo.CMS.Content.Caching;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Caching;
using System.Runtime.Serialization;
using System.Text;

namespace Kooboo.CMS.Sites.DataRule
{
public enum SortDirection
@@ -21,17 +28,16 @@ public enum SortDirection
Descending
}


[DataContract(Name = "DataRuleBase")]
[KnownTypeAttribute(typeof(DataRuleBase))]
[KnownTypeAttribute(typeof(FolderDataRule))]
[KnownTypeAttribute(typeof(CategoryDataRule))]
[KnownTypeAttribute(typeof(SchemaDataRule))]
public abstract class DataRuleBase : IDataRule
public abstract class DataRuleBase : IContentDataRule
{
[DataMember(Order = 13)]
public string FolderName { get; set; }

[DataMember(Order = 1)]
public WhereClause[] WhereClauses
{
@@ -89,10 +95,121 @@ public SortDirection SortDirection
[DataMember(Order = 11)]
public string Top { get; set; }

public abstract IContentQuery<Content.Models.TextContent> GetContentQuery(DataRuleContext dataRuleContext);

#region IDataRule Members
public object Execute(DataRuleContext dataRuleContext, TakeOperation operation, int cacheDuration)
{
var contentQuery = this.GetContentQuery(dataRuleContext);
object data = contentQuery;

if (!string.IsNullOrEmpty(this.SortField))
{
if (this.SortDirection == DataRule.SortDirection.Ascending)
{
contentQuery = contentQuery.OrderBy(this.SortField);
}
else
{
contentQuery = contentQuery.OrderByDescending(this.SortField);
}
}
if (this.EnablePaging.Value)
{
string pageIndexParameterName;
var pageIndexValue = ParameterizedFieldValue.GetFieldValue(this.PageIndex, dataRuleContext.ValueProvider, out pageIndexParameterName);
var intPageIndexValue = 1;
int.TryParse(pageIndexValue, out intPageIndexValue);
if (intPageIndexValue < 1)
{
intPageIndexValue = 1;
}

public abstract Content.Query.IContentQuery<Content.Models.TextContent> Execute(DataRuleContext dataRuleContext);
string pageSizeParameterName;
var pageSizeValue = ParameterizedFieldValue.GetFieldValue(this.PageSize, dataRuleContext.ValueProvider, out pageSizeParameterName);
var intPageSize = 10;
int.TryParse(pageSizeValue, out intPageSize);
if (intPageSize < 1)
{
intPageSize = 10;
}

var totalCount = contentQuery.Count();

data = new DataRulePagedList(contentQuery.Skip((intPageIndexValue - 1) * intPageSize).Take(intPageSize)
, intPageIndexValue
, intPageSize
, totalCount)
{
PageIndexParameterName = pageIndexParameterName
};
}
else if (!string.IsNullOrEmpty(this.Top))
{
string fieldName;
var topValue = ParameterizedFieldValue.GetFieldValue(this.Top, dataRuleContext.ValueProvider, out fieldName);
var intTopValue = 1;
int.TryParse(topValue, out intTopValue);

data = contentQuery.Take(intTopValue);
}
else
{
data = contentQuery;
}


if (data is IContentQuery<ContentBase>)
{
data = GetData((IContentQuery<TextContent>)data, operation, cacheDuration);
}

return data;
}
private static object GetData(IContentQuery<TextContent> contentQuery, TakeOperation operation, int cacheDuration)
{
if (cacheDuration > 0)
{
var policy = new CacheItemPolicy() { SlidingExpiration = TimeSpan.FromSeconds(cacheDuration) };
switch (operation)
{
case TakeOperation.First:
var lazyFirst = contentQuery.LazyFirstOrDefault();
return GetCacheData(contentQuery, operation, policy, () => lazyFirst.Value);
case TakeOperation.Count:
var lazyCount = contentQuery.LazyCount();
return GetCacheData(contentQuery, operation, policy, () => lazyCount.Value);
case TakeOperation.List:
default:
return GetCacheData(contentQuery, operation, policy, () => contentQuery.ToArray());
}
}
else
{
switch (operation)
{
case TakeOperation.First:
return contentQuery.FirstOrDefault();
case TakeOperation.Count:
return contentQuery.Count();
case TakeOperation.List:
default:
return contentQuery.ToArray();
}
}
}
private static object GetCacheData(IContentQuery<TextContent> contentQuery, TakeOperation operation, CacheItemPolicy policy, Func<object> getData)
{
string cacheKey = "TakeOperation:" + operation.ToString() + " " + TextTranslator.Translate(contentQuery);
var data = contentQuery.Repository.ObjectCache().Get(cacheKey);
if (data == null)
{
data = getData();
contentQuery.Repository.ObjectCache().AddOrGetExisting(cacheKey, data, policy);
}
return data;
}

public abstract DataRuleType DataRuleType { get; }

public abstract Schema GetSchema(Repository repository);
@@ -101,7 +218,6 @@ public virtual bool IsValid(Repository repository)
var folder = FolderHelper.Parse<TextFolder>(repository, FolderName);
return folder.AsActual() != null;
}
#endregion

public virtual bool HasAnyParameters()
{
@@ -129,8 +245,6 @@ public virtual bool HasAnyParameters()
}
return false;
}



#endregion
}
}
@@ -22,81 +22,12 @@ namespace Kooboo.CMS.Sites.DataRule
{
public static class DataRuleExecutor
{
public static ObjectCache ObjectCache = MemoryCache.Default;
public static string CacheRegionName = null;

public static string ModelName = "model";
public static void Execute(ViewDataDictionary viewData, DataRuleContext dataRuleContext, IEnumerable<DataRuleSetting> dataRules)
{
foreach (var item in dataRules)
{
var contentQuery = item.DataRule.Execute(dataRuleContext);
object data = contentQuery;
if (item.DataRule is DataRuleBase)
{
var dataRuleBase = (DataRuleBase)item.DataRule;
if (!string.IsNullOrEmpty(dataRuleBase.SortField))
{
if (dataRuleBase.SortDirection == DataRule.SortDirection.Ascending)
{
contentQuery = contentQuery.OrderBy(dataRuleBase.SortField);
}
else
{
contentQuery = contentQuery.OrderByDescending(dataRuleBase.SortField);
}
}
if (item.DataRule.EnablePaging.Value)
{
string pageIndexParameterName;
var pageIndexValue = ParameterizedFieldValue.GetFieldValue(dataRuleBase.PageIndex, dataRuleContext.ValueProvider, out pageIndexParameterName);
var intPageIndexValue = 1;
int.TryParse(pageIndexValue, out intPageIndexValue);
if (intPageIndexValue < 1)
{
intPageIndexValue = 1;
}

string pageSizeParameterName;
var pageSizeValue = ParameterizedFieldValue.GetFieldValue(dataRuleBase.PageSize, dataRuleContext.ValueProvider, out pageSizeParameterName);
var intPageSize = 10;
int.TryParse(pageSizeValue, out intPageSize);
if (intPageSize < 1)
{
intPageSize = 10;
}

var totalCount = contentQuery.Count();

data = new DataRulePagedList(contentQuery.Skip((intPageIndexValue - 1) * intPageSize).Take(intPageSize)
, intPageIndexValue
, intPageSize
, totalCount)
{
PageIndexParameterName = pageIndexParameterName
};
}
else if (!string.IsNullOrEmpty(dataRuleBase.Top))
{
string fieldName;
var topValue = ParameterizedFieldValue.GetFieldValue(dataRuleBase.Top, dataRuleContext.ValueProvider, out fieldName);
var intTopValue = 1;
int.TryParse(topValue, out intTopValue);

data = contentQuery.Take(intTopValue);
}
else
{
data = contentQuery;
}
}

if (data is IContentQuery<ContentBase>)
{
data = GetData(item, (IContentQuery<TextContent>)data);
}


var data = item.DataRule.Execute(dataRuleContext, item.TakeOperation, item.CachingDuration);
if (item.DataName.EqualsOrNullEmpty(ModelName, StringComparison.CurrentCultureIgnoreCase))
{
viewData.Model = data;
@@ -105,48 +36,6 @@ public static void Execute(ViewDataDictionary viewData, DataRuleContext dataRule
}
}

private static object GetData(DataRuleSetting dataRuleSetting, IContentQuery<TextContent> contentQuery)
{
if (dataRuleSetting.CachingDuration > 0)
{
var policy = new CacheItemPolicy() { SlidingExpiration = TimeSpan.FromSeconds(dataRuleSetting.CachingDuration) };
switch (dataRuleSetting.TakeOperation)
{
case TakeOperation.First:
var lazyFirst = contentQuery.LazyFirstOrDefault();
return GetCacheData(dataRuleSetting.TakeOperation, contentQuery, policy, () => lazyFirst.Value);
case TakeOperation.Count:
var lazyCount = contentQuery.LazyCount();
return GetCacheData(dataRuleSetting.TakeOperation, contentQuery, policy, () => lazyCount.Value);
case TakeOperation.List:
default:
return GetCacheData(dataRuleSetting.TakeOperation, contentQuery, policy, () => contentQuery.ToArray());
}
}
else
{
switch (dataRuleSetting.TakeOperation)
{
case TakeOperation.First:
return contentQuery.FirstOrDefault();
case TakeOperation.Count:
return contentQuery.Count();
case TakeOperation.List:
default:
return contentQuery.ToArray();
}
}
}
private static object GetCacheData(TakeOperation operation, IContentQuery<TextContent> contentQuery, CacheItemPolicy policy, Func<object> getData)
{
string cacheKey = "TakeOperation:" + operation.ToString() + " " + TextTranslator.Translate(contentQuery);
var data = ObjectCache.Get(cacheKey, CacheRegionName);
if (data == null)
{
data = getData();
ObjectCache.AddOrGetExisting(cacheKey, data, policy, CacheRegionName);
}
return data;
}

}
}
@@ -15,9 +15,10 @@ namespace Kooboo.CMS.Sites.DataRule
{
public enum DataRuleType
{
Folder,
Schema,
Category
Folder = 0,
Schema = 1,
Category = 2,
Http = 3
//Categorizable
}
}
@@ -48,7 +48,7 @@ public WhereClause[] CategoryClauses

//[DataMember(Order = 13)]
//public new string FolderName { get; set; }
public override Content.Query.IContentQuery<Content.Models.TextContent> Execute(DataRuleContext dataRuleContext)
public override IContentQuery<Content.Models.TextContent> GetContentQuery(DataRuleContext dataRuleContext)
{
var site = dataRuleContext.Site;
var repository = Sites.Models.ModelExtensions.GetRepository(site);
@@ -101,10 +101,10 @@ public override Content.Query.IContentQuery<Content.Models.TextContent> Execute(
contentQuery = contentQuery.WhereEquals("Published", true); //default query published=true.
return contentQuery;
}

[DataMember]
public override DataRuleType DataRuleType
{
get { return DataRule.DataRuleType.Folder; }
get { return DataRule.DataRuleType.Folder; }
}

public override Schema GetSchema(Repository repository)
Oops, something went wrong.

0 comments on commit 27ed4c5

Please sign in to comment.