/*
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 Everest.Library.Mvc;
using Everest.Library.Extjs;
using Everest.Library.Extjs.Tree;
using Everest.Library.Data.Entity;
using Everest.Library.Json;
using Everest.Library.Versioning;
using Everest.Library.Data;
using Everest.Library.Data.Rule;
using Everest.CmsServices.Models;
using Everest.Library.ExtensionMethod;
namespace Everest.CmsServices.Controllers{
[ValidateInput(false)]
public class ValidatorController : CmsExtController
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
#region IStandardActions Members
/// <summary>
/// Gets the list.
/// </summary>
/// <returns></returns>
[PermissionFilter(Permission = FolderType.Validator)]
public ActionResult GetList()
{
int start, limit;
EnsurePaging(out start, out limit);
string application = Request.Form["application"];
var queryable = dataContext.QueryValidators(application);
string name = Request.Form["ValidatorName"];
if (!StringExtensions.IsNullOrEmptyTrim(name))
{
queryable = queryable.Where(cv => cv.ValidatorName.Contains(name));
}
queryable = queryable.OrderByDescending(cv => cv.ValidatorId);
queryable = OrderByRequest(queryable);
var queryResult = queryable.Select<Cms_Validator, object>(c => new
{
c.UUID,
ValidatorId = c.ValidatorId,
ValidatorName = c.ValidatorName,
Application = c.aspnet_Applications.ApplicationName
});
return Json(new ExtJsonReaderObject(queryResult.Skip(start).Take(limit).ToArray(), queryResult.Count()));
}
/// <summary>
/// Gets the details.
/// </summary>
/// <returns></returns>
[PermissionFilter(Permission = FolderType.Validator)]
public ActionResult GetDetail()
{
int id = int.Parse(Request.Form["ValidatorId"]);
var jsonData = GetValidatorDetail(id);
return Json(new JsonResultData() { success = true, data = jsonData });
}
private object GetValidatorDetail(int id)
{
return dataContext.QueryValidator(id).
Select(cv => new
{
cv.UUID,
ValidatorId = cv.ValidatorId,
ValidatorName = cv.ValidatorName,
FormTitle = cv.ValidatorName,
Function = cv.Function
}).First();
}
/// <summary>
/// Submits the specified add.
/// </summary>
/// <param name="add">if set to <c>true</c> [add].</param>
/// <param name="closeForm"></param>
/// <returns></returns>
[PermissionFilter(Permission = FolderType.Validator)]
public ActionResult Submit(bool add, bool? closeForm)
{
JsonResultData resultData = new JsonResultData() { success = true };
try
{
Cms_Validator validator = null;
if (add)
{
string validatorName = Request.Form["ValidatorName"];
string application = Request.Form["Application"];
validator = new Cms_Validator();
validator.UUID = Guid.NewGuid();
validator.ValidatorName = validatorName;
validator.Function = Request.Form["Function"];
validator.aspnet_Applications = dataContext.QueryApplication(application).First();
dataContext.AddToCms_Validator(validator);
}
else
{
int id = int.Parse(Request.Form["oldData.ValidatorId"]);
validator = dataContext.QueryValidator(id).First();
validator.ValidatorName = Request.Form["ValidatorName"];
validator.Function = Request.Form["Function"];
}
dataContext.SaveChanges();
validator.Checkin(User.Identity.Name, "");
if (resultData.success && !closeForm.Value)
{
resultData.closeForm = false;
resultData.data = GetValidatorDetail(validator.ValidatorId);
}
}
catch (RuleViolationException ruleException)
{
dataContext.GetViolations().UpdateResultDataWithViolations(resultData);
Everest.Library.HealthMonitor.HealthMonitoringLogging.LogError(ruleException);
}
return Json(resultData);
}
/// <summary>
/// Deletes this instance.
/// </summary>
/// <returns></returns>
[PermissionFilter(Permission = FolderType.Validator)]
public ActionResult Delete(Guid[] uuid)
{
var resultData = new JsonResultData();
foreach (var guid in uuid)
{
var validator = dataContext.QueryValidator(guid).First();
if (BeReferenced(guid))
{
resultData.message = string.Format(Resources.ItemCouldNotBeDeleted, validator.ValidatorName);
}
else
{
dataContext.DeleteObject(validator);
dataContext.SaveChanges();
}
}
return Json(resultData);
}
private bool BeReferenced(Guid validatorUUID)
{
if (dataContext.QueryValidatorGroupsByValidator(validatorUUID).Exists())
{
return true;
}
return false;
}
#endregion
#region Combobox
/// <summary>
/// Gets the validators for combobox.
/// </summary>
/// <returns></returns>
public ActionResult GetValidatorsForCombobox(string application)
{
var query = dataContext.QueryValidators(application);
var items = query.ToArray().ToComboboxItems(v => v.ValidatorName, v => v.UUID.ToString());
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 groupNodes = new TreeNode();
treeNodes.Add(groupNodes.Attributes);
groupNodes.Text = Resources.ValidatorGroups;
groupNodes.IconCls = FolderType.ValidatorGroup.ToString();
groupNodes.Leaf = false;
groupNodes.Expanded = true;
var groups = dataContext.QueryValidatorGroupsByValidator(uuid)
.Select(vg => new
{
vg.aspnet_Applications.ApplicationName,
vg.ValidateGroupName
});
groupNodes.children = new List<IDictionary<string, object>>();
foreach (var group in groups)
{
var folderNode = new TreeNode();
groupNodes.children.Add(folderNode.Attributes);
folderNode.Text = new UniqueName(CmsGlobal.GetApplicationName(group.ApplicationName), group.ValidateGroupName).ToString();
folderNode.Leaf = true;
}
#endregion
return Json(treeNodes);
}
#endregion
}
}
|