/*
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 CategoryByContentNFolderQueryable: ContentQueryable<ContentContainer>
{
public CategoryByContentNFolderQueryable(Guid contentUUID, string folderName)
: base()
{
this.ContentUUID = contentUUID;
this.FolderName = folderName;
}
public CategoryByContentNFolderQueryable(int contentId, string folderName)
: base()
{
this.ContentId = contentId;
this.FolderName = folderName;
}
internal CategoryByContentNFolderQueryable(Guid contentUUID, int contentId, string folderName, Expression expression)
: base(expression)
{
this.ContentUUID = contentUUID;
this.ContentId = contentId;
this.FolderName = folderName;
}
public int ContentId { get; private set; }
public Guid ContentUUID { get; private set; }
public string FolderName { get; private set; }
public override IQueryProvider Provider
{
get { return new CategoryByContentNFolderQueryProvider(this); }
}
}
internal class CategoryByContentNFolderQueryProvider : ContentQueryProvider
{
private CategoryByContentNFolderQueryable queryable;
public CategoryByContentNFolderQueryProvider(CategoryByContentNFolderQueryable queryable)
{
this.queryable = queryable;
}
public override IQueryable<TElement> CreateQuery<TElement>(System.Linq.Expressions.Expression expression)
{
return (IQueryable<TElement>)new CategoryByContentNFolderQueryable(queryable.ContentUUID, queryable.ContentId, queryable.FolderName, expression);
}
public override IQueryable CreateQuery(System.Linq.Expressions.Expression expression)
{
Type elementType = TypeSystem.GetElementType(expression.Type);
try
{
return (IQueryable)Activator.CreateInstance(typeof(CategoryByContentNFolderQueryable), new object[] { this, queryable.ContentUUID, queryable.ContentId, queryable.FolderName, 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.GetCategoryCountByContentAndFolder(queryable.ContentId, queryable.FolderName, exp.QueryStatement, exp.Parameters.ToNameValueCollection(), null);
}
else
{
countValue = ContentService.GetCategoryCountByContentAndFolder(queryable.ContentUUID, queryable.FolderName, 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.GetCategoriesByContentAndFolder(queryable.ContentId, queryable.FolderName, exp.QueryStatement, exp.OrderBy,
exp.Parameters.ToNameValueCollection(), exp.SkipCount + 1, 1, null).First());
}
else
{
firstValue = new ContentContainer(ContentService.GetCategoriesByContentAndFolder(queryable.ContentUUID, queryable.FolderName, 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.GetCategoriesByContentAndFolder(queryable.ContentId, queryable.FolderName, exp.QueryStatement, exp.OrderBy, exp.Parameters.ToNameValueCollection(),
exp.SkipCount + 1, 1, null).FirstOrDefault();
}
else
{
retValue = new ContentContainer(ContentService.GetCategoriesByContentAndFolder(queryable.ContentUUID, queryable.FolderName, 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.GetCategoriesByContentAndFolder(queryable.ContentId, queryable.FolderName, exp.QueryStatement, exp.OrderBy,
exp.Parameters.ToNameValueCollection(), startIndex, 1, null).First());
}
else
{
lastValue = new ContentContainer(ContentService.GetCategoriesByContentAndFolder(queryable.ContentUUID, queryable.FolderName, 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.GetCategoriesByContentAndFolder(queryable.ContentId, queryable.FolderName, exp.QueryStatement, exp.OrderBy, exp.Parameters.ToNameValueCollection(), startIndex, 1, null).FirstOrDefault();
}
else
{
retValue = ContentService.GetCategoriesByContentAndFolder(queryable.ContentUUID, queryable.FolderName, 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.GetCategoriesByContentAndFolder(queryable.ContentId, queryable.FolderName, exp.QueryStatement, exp.OrderBy, exp.Parameters.ToNameValueCollection(), exp.SkipCount + 1, pageSize, null);
}
else
{
contents = ContentService.GetCategoriesByContentAndFolder(queryable.ContentUUID, queryable.FolderName, 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;
}
}
}
|