/*
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 CategoryByContentNSchemaQueryable: ContentQueryable<ContentContainer>
{
public CategoryByContentNSchemaQueryable(Guid contentUUID, string schemaName)
: base()
{
this.ContentUUID = contentUUID;
this.SchemaName = schemaName;
}
public CategoryByContentNSchemaQueryable(int contentId, string schemaName)
: base()
{
this.ContentId = contentId;
this.SchemaName = schemaName;
}
internal CategoryByContentNSchemaQueryable(Guid contentUUID, int contentId, string schemaName, Expression expression)
: base(expression)
{
this.ContentUUID = contentUUID;
this.ContentId = contentId;
this.SchemaName = schemaName;
}
public int ContentId { get; private set; }
public Guid ContentUUID { get; private set; }
public string SchemaName { get; private set; }
public override IQueryProvider Provider
{
get { return new CategoryByContentNSchemaQueryProvider(this); }
}
}
internal class CategoryByContentNSchemaQueryProvider : ContentQueryProvider
{
private CategoryByContentNSchemaQueryable queryable;
public CategoryByContentNSchemaQueryProvider(CategoryByContentNSchemaQueryable queryable)
{
this.queryable = queryable;
}
public override IQueryable<TElement> CreateQuery<TElement>(System.Linq.Expressions.Expression expression)
{
return (IQueryable<TElement>)new CategoryByContentNSchemaQueryable(queryable.ContentUUID, queryable.ContentId, queryable.SchemaName, expression);
}
public override IQueryable CreateQuery(System.Linq.Expressions.Expression expression)
{
Type elementType = TypeSystem.GetElementType(expression.Type);
try
{
return (IQueryable)Activator.CreateInstance(typeof(CategoryByContentNSchemaQueryable), new object[] { this, queryable.ContentUUID, queryable.ContentId, queryable.SchemaName, expression });
}
catch (TargetInvocationException tie)
{
throw tie.InnerException;
}
}
private object countValue;
protected override object Count(StatementExpression exp)
{
if (countValue == null)
{
if (queryable.ContentId != 0)
{
countValue = ContentService.GetCategoryCountByContentAndSchema(queryable.ContentId, queryable.SchemaName, exp.QueryStatement, exp.Parameters.ToNameValueCollection(), null);
}
else
{
countValue = ContentService.GetCategoryCountByContentAndSchema(queryable.ContentUUID, queryable.SchemaName, exp.QueryStatement, exp.Parameters.ToNameValueCollection(), null);
}
}
return countValue;
}
private object firstValue;
protected override object First(StatementExpression exp)
{
if (firstValue == null)
{
if (queryable.ContentId != 0)
{
firstValue = new ContentContainer(ContentService.GetCategoriesByContentAndSchema(queryable.ContentId, queryable.SchemaName, exp.QueryStatement, exp.OrderBy,
exp.Parameters.ToNameValueCollection(), exp.SkipCount + 1, 1, null).First());
}
else
{
firstValue = new ContentContainer(ContentService.GetCategoriesByContentAndSchema(queryable.ContentUUID, 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.ContentId != 0)
{
retValue = ContentService.GetCategoriesByContentAndSchema(queryable.ContentId, queryable.SchemaName, exp.QueryStatement, exp.OrderBy, exp.Parameters.ToNameValueCollection(),
exp.SkipCount + 1, 1, null).FirstOrDefault();
}
else
{
retValue = new ContentContainer(ContentService.GetCategoriesByContentAndSchema(queryable.ContentUUID, 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.ContentId != 0)
{
lastValue = new ContentContainer(ContentService.GetCategoriesByContentAndSchema(queryable.ContentId, queryable.SchemaName, exp.QueryStatement, exp.OrderBy,
exp.Parameters.ToNameValueCollection(), startIndex, 1, null).First());
}
else
{
lastValue = new ContentContainer(ContentService.GetCategoriesByContentAndSchema(queryable.ContentUUID, 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.ContentId != 0)
{
retValue = ContentService.GetCategoriesByContentAndSchema(queryable.ContentId, queryable.SchemaName, exp.QueryStatement, exp.OrderBy, exp.Parameters.ToNameValueCollection(), startIndex, 1, null).FirstOrDefault();
}
else
{
retValue = ContentService.GetCategoriesByContentAndSchema(queryable.ContentUUID, 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.ContentId != 0)
{
contents = ContentService.GetCategoriesByContentAndSchema(queryable.ContentId, queryable.SchemaName, exp.QueryStatement, exp.OrderBy, exp.Parameters.ToNameValueCollection(), exp.SkipCount + 1, pageSize, null);
}
else
{
contents = ContentService.GetCategoriesByContentAndSchema(queryable.ContentUUID, 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;
}
}
}
|