/*
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.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Everest.Library.Data.Entity;
using Everest.Library.ExtensionMethod;
using Everest.Library.Data.Rule;
using Everest.Library.Data;
using Everest.CmsServices.Services;
namespace Everest.CmsServices.Models{
/// <summary>
///
/// </summary>
public partial class Cms_Folder : IRuleEntity
{
#region Constructor
/// <summary>
/// Initializes a new instance of the <see cref="Cms_Folder"/> class.
/// </summary>
public Cms_Folder()
{
this.UUID = Guid.NewGuid();
this.OriginalUUID = this.UUID;
}
#endregion
#region get bases
//IList<Cms_Folder> baseFolders;
List<string> baseApplications;
/// <summary>
/// Gets all its and inherit folders.
/// </summary>
/// <returns></returns>
public IEnumerable<Cms_Folder> GetBaseFoldersRecusive()
{
return CachedData.GetAllBase(this.UUID);
//if (baseFolders == null)
//{
// IList<Cms_Folder> folders = new List<Cms_Folder>();
// //add its and its parents cascading.
// folders.Add(this);
// this.ParentReference.Load();
// Cms_Folder parent = this.Parent;
// while (parent != null && parent.CanInherit)
// {
// //if (parent.FolderType == this.FolderType)
// {
// folders.Add(parent);
// parent.ParentReference.Load();
// parent = parent.Parent;
// }
// //else
// //{
// // break;
// //}
// }
// //get all base folders
// baseFolders = new List<Cms_Folder>();
// foreach (var item in folders)
// {
// baseFolders.Add(item);
// item.BaseReference.Load();
// Cms_Folder baseFolder = item.Base;
// while (baseFolder != null && !baseFolders.Contains(baseFolder))
// {
// baseFolders.Add(baseFolder);
// baseFolder.BaseReference.Load();
// baseFolder = baseFolder.Base;
// }
// }
//}
//return baseFolders;
}
///// <summary>
///// Gets the base applications.
///// </summary>
///// <returns></returns>
//public IEnumerable<string> GetBaseApplicationsRecusive()
//{
// if (baseApplications == null)
// {
// IEnumerable<Cms_Folder> baseFolders = GetBaseFoldersRecusive();
// baseApplications = new List<string>();
// foreach (var item in baseFolders)
// {
// if (!item.aspnet_ApplicationsReference.IsLoaded)
// {
// item.aspnet_ApplicationsReference.Load();
// }
// if (!baseApplications.Contains(item.aspnet_Applications.ApplicationName))
// {
// baseApplications.Add(item.aspnet_Applications.ApplicationName);
// }
// }
// }
// return baseApplications;
//}
#endregion
#region Get Folder file path
private string ContentFileRelativePath
{
get
{
return Path.Combine(ContentFileManager.ContentFileDirName, this.FolderPath);
}
}
/// <summary>
/// Gets the absolute folder path.
/// </summary>
/// <returns></returns>
public string GetAbsoluteFolderContentFilePath(string path)
{
if ((Everest.CmsServices.Models.FolderType)FolderType == Everest.CmsServices.Models.FolderType.ContentView)
{
return string.Empty;
}
string absoluteFolderPath = Path.Combine(CmsGlobal.BaseDirPath, ContentFileRelativePath);
if (!StringExtensions.IsNullOrEmptyTrim(path))
{
absoluteFolderPath = Path.Combine(absoluteFolderPath, path);
}
if (!Directory.Exists(absoluteFolderPath) && !File.Exists(absoluteFolderPath))
{
Directory.CreateDirectory(absoluteFolderPath);
}
return absoluteFolderPath;
}
#endregion
#region Properties
public Guid? SchemaUUIDReference
{
get
{
return this.Cms_SchemaReference.EntityKey.GetKeyValue<Guid?>("UUID");
}
}
public Guid? WorkflowUUIDReference
{
get
{
return this.Cms_WorkflowReference.EntityKey.GetKeyValue<Guid?>("UUID");
}
}
/// <summary>
/// Gets or sets the condition.
///
/// ContentView query condition. Using the FolderPath field.
/// </summary>
/// <value>The condition.</value>
public string Condition
{
get
{
if ((Everest.CmsServices.Models.FolderType)FolderType == Everest.CmsServices.Models.FolderType.ContentView)
{
return this.FolderPath;
}
return string.Empty;
}
set
{
if ((Everest.CmsServices.Models.FolderType)FolderType == Everest.CmsServices.Models.FolderType.ContentView)
{
this.FolderPath = value;
}
}
}
#endregion
#region override object
public override int GetHashCode()
{
return base.GetHashCode();
}
public override bool Equals(object obj)
{
if (((Cms_Folder)obj).UUID == this.UUID)
{
return true;
}
return base.Equals(obj);
}
#endregion
#region IRuleEntity Members
public IEnumerable<RuleViolation> GetRuleViolations()
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
List<RuleViolation> violations = new List<RuleViolation>();
if (StringExtensions.IsNullOrEmptyTrim(this.FolderName))
{
violations.Add(new RuleViolation("FolderName", "FolderName", string.Format(Resources.FieldIsRequired, "FolderName")));
}
this.aspnet_ApplicationsReference.Load(this.aspnet_Applications, this.EntityState);
if (dataContext.IsFolderExists(this.aspnet_Applications.ApplicationName, this.FolderName, this.UUID, this.FolderType).Exists())
{
violations.Add(new RuleViolation("FolderName", this.FolderName, Resources.FolderIsAlreadyExists));
}
if (((FolderType)FolderType == Everest.CmsServices.Models.FolderType.TextContent) && this.EntityState == System.Data.EntityState.Added && this.Cms_Schema == null)
{
violations.Add(new RuleViolation("Schema", "null", string.Format(Resources.FieldIsRequired, "Schema")));
}
if (this.OriginalUUID != this.UUID && this.EntityState == System.Data.EntityState.Added)
{
if (dataContext.IsFolderExists(this.aspnet_Applications.ApplicationName, this.OriginalUUID).Exists())
{
violations.Add(new RuleViolation("Base", this.OriginalUUID.ToString(), string.Format(Resources.FolderInheritanceAlreadyExists, this.Base.FolderName)));
}
}
return violations;
}
#endregion
#region Serialize
public static Cms_Folder DeserializeFromNode(XmlNode parentNode)
{
var folder = new Cms_Folder();
XmlNode folderNode = parentNode.SelectSingleNode("folder");
if (folderNode != null)
{
folder.UUID = new Guid(folderNode.Attributes["UUID"].Value);
folder.FolderName = folderNode.Attributes["FolderName"].Value;
return folder;
}
return null;
}
public void SerializeAsNode(XmlNode parentNode)
{
var xmlDom = parentNode.OwnerDocument;
//add folderNode
XmlNode folderNode = parentNode.OwnerDocument.CreateElement("folder");
parentNode.AppendChild(folderNode);
XmlAttribute folderUUIDAtt = xmlDom.CreateAttribute("UUID");
folderUUIDAtt.Value = this.UUID.ToString();
folderNode.Attributes.Append(folderUUIDAtt);
XmlAttribute folderNameAtt = xmlDom.CreateAttribute("FolderName");
folderNameAtt.Value = this.FolderName;
folderNode.Attributes.Append(folderNameAtt);
}
#endregion
}
}
|