/*
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.Linq.Expressions;
using System.Text;
using System.Web.Mvc;
using System.Web;
using Everest.Library.Mvc;
using Everest.Library.Extjs;
using Everest.Library.Extjs.Tree;
using Everest.Library.Data.Entity;
using Everest.Library.Json;
using Everest.Library.Data.Rule;
using Everest.Library.ExtensionMethod;
using Everest.Library.Data;
using Everest.CmsServices.Models;
namespace Everest.CmsServices.Controllers{
public class ValidatorGroupController : CmsExtController
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
#region IStandardActions Members
[PermissionFilter(Permission = FolderType.ValidatorGroup)]
public ActionResult GetList()
{
int start, limit;
EnsurePaging(out start, out limit);
string application = Request.Form["application"];
var folderUUID = new Guid(Request.Form["folderUUID"]);
var queryable = dataContext.QueryValidatorGroups(application);
string name = Request.Form["ValidateGroupName"];
if (!StringExtensions.IsNullOrEmptyTrim(name))
{
queryable = queryable.Where(g => g.ValidateGroupName.Contains(name));
}
queryable = queryable.OrderByDescending(g => g.ValidateGroupId);
queryable = OrderByRequest(queryable);
var queryResult = queryable.Select<Cms_ValidatorGroup, object>(c => new
{
c.UUID,
ValidateGroupId = c.ValidateGroupId,
ValidateGroupName = c.ValidateGroupName,
Application = c.aspnet_Applications.ApplicationName
});
return Json(new ExtJsonReaderObject(queryResult.Skip(start).Take(limit).ToArray(), queryResult.Count()));
}
[PermissionFilter(Permission = FolderType.ValidatorGroup)]
public ActionResult GetDetail()
{
int id = int.Parse(Request.Form["ValidateGroupId"]);
object jsonData = GetGroupDetail(id);
return Json(new JsonResultData() { success = true, data = jsonData });
}
private object GetGroupDetail(int id)
{
var data = dataContext.QueryValidatorGroup(id).First();
data.Cms_Validator.Load();
List<string> validatorId = new List<string>();
foreach (var item in data.Cms_Validator)
{
validatorId.Add(item.UUID.ToString());
}
object jsonData = new
{
ValidateGroupId = data.ValidateGroupId,
ValidateGroupName = data.ValidateGroupName,
FormTitle = data.ValidateGroupName,
Description = data.Description,
Validators = string.Join(",", validatorId.ToArray())
};
return jsonData;
}
[PermissionFilter(Permission = FolderType.ValidatorGroup)]
public ActionResult Submit(bool add, bool? closeForm)
{
JsonResultData resultData = new JsonResultData() { success = true };
try
{
Cms_ValidatorGroup validateGroup = null;
if (add)
{
string groupName = Request.Form["ValidateGroupName"];
string application = Request.Form["Application"];
validateGroup = new Cms_ValidatorGroup();
validateGroup.ValidateGroupName = groupName;
validateGroup.Description = Request.Form["Description"];
validateGroup.aspnet_Applications = dataContext.QueryApplication(application).First();
dataContext.AddToCms_ValidatorGroup(validateGroup);
}
else
{
int id = int.Parse(Request.Form["oldData.ValidateGroupId"]);
validateGroup = dataContext.QueryValidatorGroup(id).First();
validateGroup.Cms_Validator.Load();
validateGroup.Cms_Validator.Clear();
dataContext.SaveChanges();
validateGroup.ValidateGroupName = Request.Form["ValidateGroupName"];
validateGroup.Description = Request.Form["Description"];
}
string[] validatorId = Request.Form["Validators"].Split(',');
List<Guid> ids = new List<Guid>();
foreach (var item in validatorId)
{
if (!StringExtensions.IsNullOrEmptyTrim(item))
{
ids.Add(new Guid(item));
}
}
var validators = dataContext.QueryValidators(ids.ToArray());
foreach (var item in validators)
{
validateGroup.Cms_Validator.Add(item);
}
dataContext.SaveChanges();
if (closeForm.Value == false && resultData.success)
{
resultData.closeForm = false;
resultData.data = GetGroupDetail(validateGroup.ValidateGroupId);
}
}
catch (RuleViolationException ruleException)
{
ruleException.Issues.UpdateResultDataWithViolations(resultData);
}
return Json(resultData);
}
[PermissionFilter(Permission = FolderType.ValidatorGroup)]
public ActionResult Delete(Guid[] uuid)
{
var resultData = new JsonResultData();
foreach (var guid in uuid)
{
var validateGroup = dataContext.QueryValidatorGroup(guid).First();
if (BeReferenced(guid))
{
resultData.message = string.Format(Resources.ItemCouldNotBeDeleted, validateGroup.ValidateGroupName);
}
else
{
validateGroup.Cms_Validator.Load();
validateGroup.Cms_Validator.Clear();
dataContext.DeleteObject(validateGroup);
dataContext.SaveChanges();
}
}
return Json(resultData);
}
private bool BeReferenced(Guid validatorGroupUUID)
{
if (dataContext.QueryColumnsByValidatorGroup(validatorGroupUUID).Exists())
{
return true;
}
return false;
}
#endregion
#region Combobox
/// <summary>
/// Gets the validate group for combobox.
/// </summary>
/// <returns></returns>
public ActionResult GetValidateGroupForCombobox(string application)
{
var query = dataContext.QueryValidatorGroups(application).
Select(vg => new UniqueName()
{
ApplicationName = vg.aspnet_Applications.ApplicationName,
ItemName = vg.ValidateGroupName
});
var items = query.ToArray().ToComboboxItems(v => v.ToString(), v => v.ToString(), true);
return Json(new ExtJsonReaderObject(items, items.Count));
}
#endregion
#region Relations
public ActionResult UsedBy(Guid uuid)
{
IList<Dictionary<string, object>> treeNodes = new List<Dictionary<string, object>>();
#region Use in ValidateGroup
var columnsNode = new TreeNode();
treeNodes.Add(columnsNode.Attributes);
columnsNode.Text = Resources.Columns;
columnsNode.Leaf = false;
columnsNode.Expanded = true;
var columns = dataContext.QueryColumnsByValidatorGroup(uuid)
.Select(col => new
{
col.Cms_Schema.aspnet_Applications.ApplicationName,
SchemaName = col.Cms_Schema.SchemaName,
col.ColumnName
});
columnsNode.children = new List<IDictionary<string, object>>();
foreach (var column in columns)
{
var folderNode = new TreeNode();
columnsNode.children.Add(folderNode.Attributes);
folderNode.Text = new UniqueName(new UniqueName(CmsGlobal.GetApplicationName(column.ApplicationName),
column.SchemaName).ToString(), column.ColumnName).ToString();
folderNode.Leaf = true;
}
#endregion
return Json(treeNodes);
}
#endregion
}
}
|