/*
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.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Reflection;
using Everest.Library.ExtensionMethod;
using Everest.CmsServices.MvcHelper;
namespace Everest.CmsServices.LINQ{
public class ContentByCategoryNSchemaQueryable : ContentQueryable<ContentContainer>
{
public ContentByCategoryNSchemaQueryable(Guid categoryUUID, string schemaName)
: this(new Guid[] { categoryUUID }, schemaName)
{
}
public ContentByCategoryNSchemaQueryable(IEnumerable<Guid> categoryUUID, string schemaName)
: base()
{
this.CategoryUUID = categoryUUID;
this.SchemaName = schemaName;
}
public ContentByCategoryNSchemaQueryable(int categoryId, string schemaName)
: this(new int[] { categoryId }, schemaName)
{
}
public ContentByCategoryNSchemaQueryable(IEnumerable<int> categoryId, string schemaName)
: base()
{
this.CategoryId = categoryId;
this.SchemaName = schemaName;
}
internal ContentByCategoryNSchemaQueryable(IEnumerable<Guid> categoryUUID, IEnumerable<int> categoryId, string schemaName, Expression expression)
: base(expression)
{
this.CategoryUUID = categoryUUID;
this.CategoryId = categoryId;
this.SchemaName = schemaName;
}
public IEnumerable<int> CategoryId { get; private set; }
public IEnumerable<Guid> CategoryUUID { get; private set; }
public string SchemaName { get; private set; }
public override IQueryProvider Provider
{
get { return new ContentByCategoryNScehmaQueryProvider(this); }
}
}
internal class ContentByCategoryNScehmaQueryProvider : ContentQueryProvider
{
private ContentByCategoryNSchemaQueryable queryable;
public ContentByCategoryNScehmaQueryProvider(ContentByCategoryNSchemaQueryable queryable)
{
this.queryable = queryable;
}
public override IQueryable<TElement> CreateQuery<TElement>(System.Linq.Expressions.Expression expression)
{
return (IQueryable<TElement>)new ContentByCategoryNSchemaQueryable(queryable.CategoryUUID, queryable.CategoryId, queryable.SchemaName, expression);
}
public override IQueryable CreateQuery(System.Linq.Expressions.Expression expression)
{
Type elementType = TypeSystem.GetElementType(expression.Type);
try
{
return (IQueryable)Activator.CreateInstance(typeof(ContentByCategoryNSchemaQueryable), new object[] { this, queryable.CategoryUUID, queryable.CategoryId, queryable.SchemaName, expression });
}
catch (TargetInvocationException tie)
{
throw tie.InnerException;
}
}
private object countValue;
protected override object Count(StatementExpression exp)
{
if (countValue == null)
{
if (queryable.CategoryId != null)
{
countValue = ContentService.GetContentCountByCategoryAndSchema(queryable.CategoryId, queryable.SchemaName, exp.QueryStatement, exp.Parameters.ToNameValueCollection(), null);
}
else
{
countValue = ContentService.GetContentCountByCategoryAndSchema(queryable.CategoryUUID, queryable.SchemaName, exp.QueryStatement, exp.Parameters.ToNameValueCollection(), null);
}
}
return countValue;
}
private object firstValue;
protected override object First(StatementExpression exp)
{
if (firstValue == null)
{
if (queryable.CategoryId != null)
{
firstValue = new ContentContainer(ContentService.GetContentsByCategoryAndSchema(queryable.CategoryId, queryable.SchemaName, exp.QueryStatement, exp.OrderBy,
exp.Parameters.ToNameValueCollection(), exp.SkipCount + 1, 1, null).First());
}
else
{
firstValue = new ContentContainer(ContentService.GetContentsByCategoryAndSchema(queryable.CategoryUUID, queryable.SchemaName, exp.QueryStatement, exp.OrderBy,
exp.Parameters.ToNameValueCollection(), exp.SkipCount + 1, 1, null).First());
}
}
return firstValue;
}
private object firstOrDefaultValue;
protected override object FirstOrDefault(StatementExpression exp)
{
if (firstOrDefaultValue == null)
{
IDictionary<string, object> retValue = null;
if (queryable.CategoryId != null)
{
retValue = ContentService.GetContentsByCategoryAndSchema(queryable.CategoryId, queryable.SchemaName, exp.QueryStatement, exp.OrderBy, exp.Parameters.ToNameValueCollection(),
exp.SkipCount + 1, 1, null).FirstOrDefault();
}
else
{
retValue = new ContentContainer(ContentService.GetContentsByCategoryAndSchema(queryable.CategoryUUID, queryable.SchemaName, exp.QueryStatement, exp.OrderBy,
exp.Parameters.ToNameValueCollection(), exp.SkipCount + 1, 1, null).First());
}
if (retValue != null)
{
firstOrDefaultValue = new ContentContainer(retValue);
}
else
{
firstOrDefaultValue = retValue;
}
}
return firstOrDefaultValue;
}
private object lastValue;
protected override object Last(StatementExpression exp)
{
if (lastValue == null)
{
var startIndex = exp.SkipCount;
if (exp.TakeCount == 0)
{
startIndex = startIndex + (int)Count(exp);
}
else
{
startIndex = startIndex + exp.TakeCount;
}
if (queryable.CategoryId != null)
{
lastValue = new ContentContainer(ContentService.GetContentsByCategoryAndSchema(queryable.CategoryId, queryable.SchemaName, exp.QueryStatement, exp.OrderBy,
exp.Parameters.ToNameValueCollection(), startIndex, 1, null).First());
}
else
{
lastValue = new ContentContainer(ContentService.GetContentsByCategoryAndSchema(queryable.CategoryUUID, queryable.SchemaName, exp.QueryStatement, exp.OrderBy,
exp.Parameters.ToNameValueCollection(), exp.SkipCount + 1, 1, null).First());
}
}
return lastValue;
}
private object lastOrDefaultValue;
protected override object LastOrDefault(StatementExpression exp)
{
if (lastValue == null)
{
var startIndex = exp.SkipCount;
if (exp.TakeCount == 0)
{
startIndex = startIndex + (int)Count(exp);
}
else
{
startIndex = startIndex + exp.TakeCount;
}
IDictionary<string, object> retValue = null;
if (queryable.CategoryId != null)
{
retValue = ContentService.GetContentsByCategoryAndSchema(queryable.CategoryId, queryable.SchemaName, exp.QueryStatement, exp.OrderBy, exp.Parameters.ToNameValueCollection(), startIndex, 1, null).FirstOrDefault();
}
else
{
retValue = ContentService.GetContentsByCategoryAndSchema(queryable.CategoryUUID, queryable.SchemaName, exp.QueryStatement, exp.OrderBy, exp.Parameters.ToNameValueCollection(), startIndex, 1, null).FirstOrDefault();
}
if (retValue != null)
{
lastOrDefaultValue = new ContentContainer(retValue);
}
else
{
lastOrDefaultValue = retValue;
}
}
return lastValue;
}
private object value;
protected override object ExecuteCore(StatementExpression exp)
{
if (value == null)
{
var pageSize = exp.TakeCount;
if (pageSize == 0)
{
pageSize = int.MaxValue;
}
IList<ContentContainer> list = new List<ContentContainer>();
IEnumerable<IDictionary<string, object>> contents = null;
if (queryable.CategoryId != null)
{
contents = ContentService.GetContentsByCategoryAndSchema(queryable.CategoryId, queryable.SchemaName, exp.QueryStatement, exp.OrderBy, exp.Parameters.ToNameValueCollection(), exp.SkipCount + 1, pageSize, null);
}
else
{
contents = ContentService.GetContentsByCategoryAndSchema(queryable.CategoryUUID, queryable.SchemaName, exp.QueryStatement, exp.OrderBy, exp.Parameters.ToNameValueCollection(), exp.SkipCount + 1, pageSize, null);
}
foreach (var item in contents)
{
list.Add(new ContentContainer(item));
}
value = list;
}
return value;
}
}
}
|