/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.
This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.Linq;
using System.Text;
using System.Data;
using System.Web.Mvc;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Collections.Specialized;
using Everest.Library.Data.Entity;
using Everest.Library.ExtensionMethod;
using Everest.CmsServices.Models;
using Everest.Library.Providers.Caching;
namespace Everest.CmsServices.DataRule{
/// <summary>
/// data rule eveluator
/// </summary>
public class DataRuleEvaluator
{
static IDictionary<DataRuleValueType, IDataRuleValueEvaluator> dataRuleValueExpressions = new Dictionary<DataRuleValueType, IDataRuleValueEvaluator>();
static DataRuleEvaluator()
{
IDataRuleValueEvaluator expression = new ConstValueEvaluator();
dataRuleValueExpressions.Add(expression.RuleType, expression);
expression = new ListValueEvaluator();
dataRuleValueExpressions.Add(expression.RuleType, expression);
expression = new ObjectValueEvaluator();
dataRuleValueExpressions.Add(expression.RuleType, expression);
expression = new CountValueEvaluator();
dataRuleValueExpressions.Add(expression.RuleType, expression);
var referencingListEvaluator = new ReferencingListEvaluator();
dataRuleValueExpressions.Add(referencingListEvaluator.RuleType, referencingListEvaluator);
var referencingCountEvaluator = new ReferencingCountEvaluator();
dataRuleValueExpressions.Add(referencingCountEvaluator.RuleType, referencingCountEvaluator);
var referencedListEvaluator = new ReferencedListEvaluator();
dataRuleValueExpressions.Add(referencedListEvaluator.RuleType, referencedListEvaluator);
var referencedCountEvaluator = new ReferencedCountEvaluator();
dataRuleValueExpressions.Add(referencedCountEvaluator.RuleType, referencedCountEvaluator);
}
/// <summary>
/// Evaluates the specified data rule context.
/// the data rule like as: Type=const:1&News=list: News:Name = {Name }&New=object:News:id={id};
/// </summary>
/// <param name="dataRuleContext">The data rule context.</param>
/// <param name="dataRules">The data rules.</param>
public static void Evaluate(DataRuleContext dataRuleContext, IEnumerable<Cms_DataRule> dataRules)
{
var oldQueryString = dataRuleContext.QueryString;
var userControl = dataRuleContext.UserControl;
foreach (var item in dataRules)
{
dataRuleContext.QueryString = new NameValueCollection(oldQueryString);
if (userControl != null)
{
//get the control property value for the queryStatement...
ContentQueryStatementInterpreter.CombinePropertyValues(userControl, dataRuleContext.QueryString, item.ValueRule);
}
var expression = dataRuleValueExpressions[(DataRuleValueType)item.ValueType];
if (expression != null)
{
object value = null;
TimeSpan? cacheTime = null;
if (!StringExtensions.IsNullOrEmptyTrim(item.Caching))
{
cacheTime = TimeSpan.Parse(item.Caching);
}
value = expression.Evaluate(dataRuleContext, item, cacheTime);
if (!dataRuleContext.ViewData.ContainsKey(item.DataName))
{
dataRuleContext.ViewData.Add(item.DataName, value);
}
else
{
dataRuleContext.ViewData[item.DataName] = value;
}
}
}
//reset the usercontrol
dataRuleContext.QueryString = oldQueryString;
}
}
}
|