/*
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;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Everest.Library;
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.ExtensionMethod;
using Everest.Library.Versioning;
using Everest.Library.Data.Rule;
using Everest.Library.Data;
using Everest.CmsServices.Services;
using Everest.CmsServices.Models;
using Everest.CmsServices.Providers;
namespace Everest.CmsServices.Controllers{
public class BinarySchemaController : CmsExtController
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
#region IStandardActions Members
[PermissionFilter(Permission = FolderType.BinarySchema)]
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.QuerySchemas(application, SchemaType.Binary);
string name = Request.Form["SchemaName"];
if (!StringExtensions.IsNullOrEmptyTrim(name))
{
queryable = queryable.Where(s => s.SchemaName.Contains(name));
}
queryable = queryable.OrderByDescending(p => p.SchemaId);
queryable = OrderByRequest(queryable);
var queryResult = queryable.Select<Cms_Schema, object>(c => new
{
c.UUID,
SchemaId = c.SchemaId,
SchemaName = c.SchemaName,
ModifiedDate = c.ModifiedDate,
UserName = c.UserName,
c.Extensions,
MaxSize = c.MaxSize,
Application = c.aspnet_Applications.ApplicationName
});
return Json(new ExtJsonReaderObject(queryResult.Skip(start).Take(limit).ToArray(), queryResult.Count()));
}
[PermissionFilter(Permission = FolderType.BinarySchema)]
public ActionResult GetDetail(int schemaId)
{
object data = GetFileSchemaDetail(schemaId);
return Json(new JsonResultData() { success = true, data = data });
}
/// <summary>
/// Gets the file schema details.
/// </summary>
/// <param name="Id">The id.</param>
/// <returns></returns>
private object GetFileSchemaDetail(int id)
{
List<Cms_Column> columns = new List<Cms_Column>();
var query = dataContext.QuerySchema(id)
.Select(s => new
{
s.UUID,
SchemaId = s.SchemaId,
SchemaName = s.SchemaName,
FormTitle = s.SchemaName,
ModifiedDate = s.ModifiedDate,
UserName = s.UserName,
s.Extensions,
MaxSize = s.MaxSize
});
object data = query.First();
return data;
}
[PermissionFilter(Permission = FolderType.BinarySchema)]
public ActionResult Submit(bool add, bool? closeForm)
{
JsonResultData resultData = new JsonResultData() { success = true };
try
{
Cms_Schema schema;
if (add)
{
schema = new Cms_Schema();
schema.UUID = Guid.NewGuid();
schema.SchemaName = Request.Form["SchemaName"];
schema.UserName = User.Identity.Name;
schema.ModifiedDate = DateTime.Now;
schema.SchemaType = (int)SchemaType.Binary;
schema.Extensions = Request.Form["Extensions"];
schema.MaxSize = StringExtensions.IsNullOrEmptyTrim(Request.Form["MaxSize"]) ? 0 : int.Parse(Request.Form["MaxSize"]);
string application = Request.Form["Application"];
schema.aspnet_Applications = dataContext.QueryApplication(application).First();
dataContext.AddToCms_Schema(schema);
dataContext.SaveChanges();
}
else
{
int schemaId = int.Parse(Request.Form["oldData.SchemaId"]);
var query = dataContext.QuerySchema(schemaId);
schema = query.First();
schema.ModifiedDate = DateTime.Now;
schema.SchemaName = Request.Form["SchemaName"];
schema.Extensions = Request.Form["Extensions"];
schema.MaxSize = StringExtensions.IsNullOrEmptyTrim(Request.Form["MaxSize"]) ? 0 : int.Parse(Request.Form["MaxSize"]);
dataContext.SaveChanges();
}
//check in to save history
schema.Checkin(User.Identity.Name, "");
if (closeForm == false && resultData.success)
{
resultData.closeForm = false;
resultData.data = GetFileSchemaDetail(schema.SchemaId);
}
}
catch (RuleViolationException ruleException)
{
ruleException.Issues.UpdateResultDataWithViolations(resultData);
//Everest.Library.HealthMonitor.HealthMonitoringLogging.LogError(ruleException);
}
return Json(resultData);
}
[PermissionFilter(Permission = FolderType.BinarySchema)]
public ActionResult Delete(Guid[] uuid)
{
var resultData = new JsonResultData();
foreach (var guid in uuid)
{
var schema = dataContext.QuerySchema(guid).First();
if (BeReferenced(guid))
{
resultData.message = string.Format(Resources.ItemCouldNotBeDeleted, schema.SchemaName);
}
else
{
dataContext.DeleteObject(schema);
schema.ClearVersions();
dataContext.SaveChanges();
}
}
return Json(resultData);
}
private bool BeReferenced(Guid schemaUUID)
{
string strSchemaUUID = schemaUUID.ToString();
if (dataContext.QueryFoldersBySchema(schemaUUID).Exists())
{
return true;
}
if (dataContext.Cms_Schema.Where(s => s.BinarySchemas.Contains(strSchemaUUID)).Exists())
{
return true;
}
return false;
}
#endregion
#region Combobox
/// <summary>
/// Gets the binary schemas for combobox.
/// </summary>
/// <returns></returns>
public ActionResult GetBinarySchemasForCombobox(string application)
{
var queryable = dataContext.QuerySchemas(application, SchemaType.Binary);
var items = queryable.ToComboboxItems<Cms_Schema>(s => s.SchemaName, s => s.UUID.ToString());
return Json(new ExtJsonReaderObject(items, items.Count));
}
/// <summary>
/// Gets the binary for create folder.
/// </summary>
/// <param name="application">The application.</param>
/// <returns></returns>
//public ActionResult GetBinarySchemaForCreateFolder(string application)
//{
// var query = dataContext.QuerySchemas(application, FolderType.BinarySchema, SchemaType.Binary);
// var items = query.ToComboboxItems<Cms_Schema>(s => s.SchemaName, s => s.UUID.ToString(), false);
// 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 BinaryFolders
var foldersNode = new TreeNode();
treeNodes.Add(foldersNode.Attributes);
foldersNode.Text = Resources.UsedByFolders;
foldersNode.Expanded = true;
foldersNode.IconCls = FolderType.BinaryContent.ToString();
var folders = dataContext.QueryFoldersBySchema(uuid).
Select(f => new
{
f.aspnet_Applications.ApplicationName,
Folder = f
});
foldersNode.children = new List<IDictionary<string, object>>();
foreach (var folder in folders)
{
var folderNode = new TreeNode();
foldersNode.children.Add(folderNode.Attributes);
folderNode.Text = new UniqueName(CmsGlobal.GetApplicationName(folder.ApplicationName), folder.Folder.FolderName).ToString();
folderNode.Leaf = true;
folderNode.IconCls = FolderType.BinaryContent.ToString();
folderNode.AddAttribute("UUID", folder.Folder.UUID.ToString());
folderNode.AddAttribute("dataUrl", "folder/usedby");
}
#endregion
#region Included by others
var includedByNode = new TreeNode();
treeNodes.Add(includedByNode.Attributes);
includedByNode.Text = Resources.IncludedBy;
includedByNode.IconCls = FolderType.TextSchema.ToString();
includedByNode.Expanded = true;
var strUUID = uuid.ToString();
var includingSchemas = dataContext.Cms_Schema.Where(s => s.BinarySchemas.Contains(strUUID))
.Select(s => new
{
s.aspnet_Applications.ApplicationName,
Schema = s
});
includedByNode.children = new List<IDictionary<string, object>>();
foreach (var including in includingSchemas)
{
var schemaNode = new TreeNode();
includedByNode.children.Add(schemaNode.Attributes);
schemaNode.Text = new UniqueName(CmsGlobal.GetApplicationName(including.ApplicationName), including.Schema.SchemaName).ToString();
schemaNode.Attributes.Add("uuid", including.Schema.UUID);
schemaNode.Attributes.Add("dataUrl", "Kooboo_TextSchema/UsedBy");
schemaNode.IconCls = FolderType.TextSchema.ToString();
}
#endregion
return Json(treeNodes);
}
#endregion
}
}
|