/*
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.Collections.Specialized;
using System.Linq;
using System.Text;
using Everest.Library;
using Everest.Library.Json;
using Everest.Library.ExtensionMethod;
using Everest.Library.Data;
using Everest.CmsServices.Models;
using Everest.CmsServices.Providers;
using Microsoft.Data.Extensions;
namespace Everest.CmsServices.Services{
public class FolderService
{
#region CreateFolder
/// <summary>
/// Creates the folder.
/// </summary>
/// <param name="parentFolderUUID">The parent folder UUID.</param>
/// <param name="values">The values.</param>
/// <param name="folderType">Type of the folder.</param>
/// <param name="schema">The schema.</param>
/// <returns></returns>
public Cms_Folder CreateFolder(Guid parentFolderUUID, NameValueCollection values, FolderType folderType)
{
var dataContext = EverestCmsEntities.GetDataContext();
var folder = new Cms_Folder();
folder.FolderName = values["FolderName"];
folder.DisplayName = values["DisplayName"];
folder.Parent = dataContext.QueryFolder(parentFolderUUID).First();
if (!StringExtensions.IsNullOrEmptyTrim(values["BaseUUID"]))
{
var baseUUID = new Guid(values["BaseUUID"]);
folder.Base = dataContext.QueryFolder(baseUUID).First();
folder.FolderLevel = folder.Base.FolderLevel + 1;
folder.OriginalUUID = folder.Base.OriginalUUID;
}
else
{
folder.FolderLevel = 1;
}
folder.CanInherit = true;
folder.Parent.aspnet_ApplicationsReference.Load();
folder.aspnet_Applications = folder.Parent.aspnet_Applications;
folder.ApplicationLevel = folder.aspnet_Applications.ApplicationLevel;
folder.IconCls = folder.FolderType.ToString();
folder.FolderPath = System.IO.Path.Combine(folder.Parent.FolderPath, folder.FolderName);
if (!string.IsNullOrEmpty(values["SchemaUUID"]))
{
var schemaUUID = new Guid(values["SchemaUUID"]);
folder.Cms_Schema = dataContext.QuerySchema(schemaUUID).First();
if (folderType == FolderType.ContentView)
{
folder.SchemaName = "ContentView";
folder.FolderType = (int)folderType;
folder.Condition = values["Condition"];
}
else
{
if ((SchemaType)folder.Cms_Schema.SchemaType == SchemaType.Text)
{
folder.SchemaName = folder.Cms_Schema.UUID.ToString();
folder.FolderType = (int)FolderType.TextContent;
}
else
{
folder.SchemaName = "BinaryContent";
folder.FolderType = (int)FolderType.BinaryContent;
}
}
string workflowUUID = values["WorkflowUUID"];
if (!StringExtensions.IsNullOrEmptyTrim(workflowUUID))
{
folder.Cms_Workflow = dataContext.QueryWorkflow(new Guid(workflowUUID)).First();
}
}
else
{
folder.FolderType = (int)FolderType.BlankFolder;
folder.CanInherit = false;
}
folder.IncludeBase = StringExtensions.IsTrue(values["IncludeItems"]);
dataContext.AddToCms_Folder(folder);
dataContext.SaveChanges();
//SaveProperties(folder, values);
if (StringExtensions.IsTrue(values["IncludeItems"]) && folder.Base != null)
{
UnityManager.Resolve<IContentProvider>().SystemManager.IncludeFolderItems(dataContext, folder.UUID, folder.Base.UUID);
}
return folder;
}
/// <summary>
/// Creates the folder.
/// </summary>
/// <param name="parentFolderUUID">The parent folder UUID.</param>
/// <param name="folderName">Name of the folder.</param>
/// <param name="application">The application.</param>
/// <param name="schemaApplication">The schema application.</param>
/// <param name="schemaName">Name of the schema.</param>
/// <returns></returns>
public Cms_Folder CreateFolder(Guid parentFolderUUID, string folderName, string application, string schemaApplication, string schemaName)
{
var dataContext = EverestCmsEntities.GetDataContext();
NameValueCollection nameValues = new NameValueCollection();
nameValues["FolderName"] = folderName;
if (!StringExtensions.IsNullOrEmptyTrim(schemaName))
{
var schema = dataContext.QuerySchema(schemaApplication, schemaName).FirstOrDefault();
if (schema != null)
{
nameValues["SchemaUUID"] = schema.UUID.ToString();
}
}
return CreateFolder(parentFolderUUID, nameValues, FolderType.Content);
}
#endregion
#region Save
public Cms_Folder SaveFolder(Guid folderUUID, NameValueCollection values)
{
var dataContext = EverestCmsEntities.GetDataContext();
var folder = dataContext.QueryFolder(folderUUID).First();
if (!StringExtensions.IsNullOrEmptyTrim(values["BaseUUID"]))
{
var baseUUID = new Guid(values["BaseUUID"]);
folder.Base = dataContext.QueryFolder(baseUUID).First();
folder.FolderLevel = folder.Base.FolderLevel + 1;
folder.OriginalUUID = folder.Base.OriginalUUID;
}
string workflowUUID = values["WorkflowUUID"];
if (!StringExtensions.IsNullOrEmptyTrim(workflowUUID))
{
folder.Cms_Workflow = dataContext.QueryWorkflow(new Guid(workflowUUID)).First();
}
if (((FolderType)folder.FolderType) == FolderType.ContentView)
{
folder.Condition = values["Condition"];
}
folder.DisplayName = values["DisplayName"];
folder.IncludeBase = StringExtensions.IsTrue(values["IncludeItems"]);
dataContext.SaveChanges();
if (StringExtensions.IsTrue(values["IncludeItems"]) && folder.Base != null)
{
UnityManager.Resolve<IContentProvider>().SystemManager.IncludeFolderItems(dataContext, folder.UUID, folder.Base.UUID);
}
//SaveProperties(folder, values);
return folder;
}
/// <summary>
/// Saves the properties.
/// </summary>
/// <param name="folder">The folder.</param>
//public void SaveProperties(Cms_Folder folder, NameValueCollection values)
//{
// var dataContext = EverestCmsEntities.GetDataContext();
// var folderUUID = folder.UUID;
// Dictionary<string, string> propertyValues = null;
// Cms_Properties property = dataContext.QueryProperty(folderUUID).FirstOrDefault();
// if (property == null)
// {
// property = new Cms_Properties();
// property.Cms_Folder = folder;
// dataContext.AddToCms_Properties(property);
// }
// else
// {
// propertyValues = property.PropertiesBody.DeserializeJSON<Dictionary<string, string>>();
// }
// if (propertyValues == null)
// {
// propertyValues = new Dictionary<string, string>();
// }
// #region Roles
// ////save the folder in the roles
// //if (!StringExtensions.IsNullOrEmptyTrim(values["RolesId"]))
// //{
// // if (!values.ContainsKey("RolesId") || values["RolesId"] != values["RolesId"])
// // {
// // //clear roles
// // folder.ClearRoles(dataContext);
// // string[] rolesIdArr = values["RolesId"].Split(',');
// // foreach (var roleId in rolesIdArr)
// // {
// // var roleGuid = new Guid(roleId);
// // var roleQuery = from r in dataContext.aspnet_Roles
// // where r.RoleId == roleGuid
// // select r;
// // folder.aspnet_Roles.Add(roleQuery.First());
// // }
// // }
// //}
// #endregion
// //set the base folder
// if (!StringExtensions.IsNullOrEmptyTrim(values["BaseApplication"]) && !StringExtensions.IsNullOrEmptyTrim(values["folderType"]))
// {
// FolderType folderType = (FolderType)Enum.Parse(typeof(FolderType), values["folderType"]);
// Cms_Folder baseFolder = CachedData.GetFolder(values["BaseApplication"], folderType);
// folder.Base = baseFolder;
// }
// foreach (var key in values.AllKeys)
// {
// if (!StringExtensions.IsNullOrEmptyTrim(key) && !key.StartsWith("oldData."))
// {
// propertyValues[key] = values[key];
// }
// }
// property.PropertyValues = propertyValues;
// dataContext.SaveChanges();
//}
#endregion
#region DeleteFolder
/// <summary>
/// Deletes the folder.
/// </summary>
/// <param name="folderUUID">The folder UUID.</param>
public void DeleteFolder(Guid folderUUID)
{
var dataContext = EverestCmsEntities.GetDataContext();
var folder = dataContext.QueryFolder(folderUUID).FirstOrDefault();
if (folder == null)
{
return;
}
if (BeUsed(dataContext, folderUUID))
{
throw new Exception(string.Format(Resources.ItemCouldNotBeDeleted, folder.FolderName));
}
else
{
folder.Children.Load();
while (folder.Children.Count > 0)
{
DeleteFolder(folder.Children.First().UUID);
}
string folderDir = folder.GetAbsoluteFolderContentFilePath("");
using (var conn = dataContext.ObjectContext.Connection.CreateConnectionScope())
{
using (var dbTrans = dataContext.ObjectContext.Connection.BeginTransaction())
{
var properties = dataContext.QueryProperty(folderUUID).FirstOrDefault();
if (properties != null)
{
dataContext.DeleteObject(properties);
}
UnityManager.Resolve<IContentProvider>().TextContentManager.DeleteContentsByFolder(dataContext, folder);
dataContext.DeleteObject(folder);
dataContext.SaveChanges();
dbTrans.Commit();
}
}
if (!string.IsNullOrEmpty(folderDir) && System.IO.Directory.Exists(folderDir))
{
System.IO.Directory.Delete(folderDir, true);
}
}
}
/// <summary>
/// Bes the used.
/// </summary>
/// <param name="dataContext">The data context.</param>
/// <param name="folderUUID">The folder UUID.</param>
/// <returns></returns>
private bool BeUsed(IEverestCmsDataContext dataContext, Guid folderUUID)
{
if (dataContext.QuerySearchSettings(folderUUID).Exists())
{
return true;
}
if (dataContext.QueryDataRulesByFolder(folderUUID).Exists())
{
return true;
}
if (dataContext.QueryFoldersByBase(folderUUID).Exists())
{
return true;
}
return false;
}
#endregion
}
}
|