/*
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.Text;
using System.IO;
using Everest.Library.ExtensionMethod;
using Everest.CmsServices.Models;
using Everest.Library.Data.Rule;
using Everest.Library.Web;
namespace Everest.CmsServices.Services{
public class StaticCodeService
{
const string Register = "<%@ Control Language=\"C#\" Inherits=\"Everest.CmsServices.MvcHelper.CmsUserControl\" %>\n<script runat=\"server\">\n";
const string EndRegister = "\n</script>";
/// <summary>
/// Adds the static code.
/// </summary>
/// <param name="application">The application.</param>
/// <param name="name">The name.</param>
/// <param name="body">The body.</param>
public void AddStaticCode(string application, string name, string body)
{
string staticCodeDir = TemplateFileManager.GetAbsoluteTemplateFolderPath(application, FolderType.StaticCode);
FileExtensions.EnsureDirtectoryExists(staticCodeDir);
var existsCodes = GetStaticCodes(application, name, false);
if (existsCodes.Count() > 0)
{
RuleViolation[] violations = new RuleViolation[] {
new RuleViolation("Name",name,Resources.StaticCodeAlreadyExists)
};
throw new RuleViolationException(violations);
}
string staticCodeFile = Path.Combine(staticCodeDir, name + ".ascx");
FileExtensions.SaveFileBody(staticCodeFile, AddRegister(body));
}
/// <summary>
/// Updates the static code.
/// </summary>
/// <param name="application">The application.</param>
/// <param name="name">The name.</param>
/// <param name="oldName">The old name.</param>
/// <param name="body">The body.</param>
public void UpdateStaticCode(string application, string name, string oldName, string body)
{
string staticCodeDir = TemplateFileManager.GetAbsoluteTemplateFolderPath(application, FolderType.StaticCode);
FileExtensions.EnsureDirtectoryExists(staticCodeDir);
string staticCodeFile = Path.Combine(staticCodeDir, name + ".ascx");
if (!name.Equals(oldName, StringComparison.InvariantCultureIgnoreCase))
{
if (File.Exists(staticCodeFile))
{
RuleViolation[] violations = new RuleViolation[] {
new RuleViolation("Name",name,Resources.StaticCodeAlreadyExists)
};
throw new RuleViolationException(violations);
}
string oldStaticCodeFile = Path.Combine(staticCodeDir, oldName + ".ascx");
if (File.Exists(oldStaticCodeFile))
{
File.Delete(oldStaticCodeFile);
}
}
FileExtensions.SaveFileBody(staticCodeFile, AddRegister(body));
}
/// <summary>
/// Gets the static codes.
/// </summary>
/// <param name="application">The application.</param>
/// <returns></returns>
public IEnumerable<StaticCode> GetStaticCodes(string application)
{
return this.GetStaticCodes(application, string.Empty, false);
}
/// <summary>
/// Gets the static codes.
/// </summary>
/// <param name="application">The application.</param>
/// <param name="filterName">Name of the filter.</param>
/// <param name="fuzzySearch">if set to <c>true</c> [fuzzy search].</param>
/// <returns></returns>
public IEnumerable<StaticCode> GetStaticCodes(string application, string filterName, bool fuzzySearch)
{
IEnumerable<string> baseApps = CachedData.GetBaseApplications(application);
List<StaticCode> staticCodes = new List<StaticCode>();
if (!string.IsNullOrEmpty(filterName))
{
if (fuzzySearch)
{
filterName = "*" + filterName + "*";
}
else
{
filterName = filterName + ".ascx";
}
}
foreach (var app in baseApps)
{
string staticCodeDir = TemplateFileManager.GetAbsoluteTemplateFolderPath(app, FolderType.StaticCode);
if (Directory.Exists(staticCodeDir))
{
IEnumerable<string> staticFiles;
if (StringExtensions.IsNullOrEmptyTrim(filterName))
{
staticFiles = Directory.GetFiles(staticCodeDir);
}
else
{
staticFiles = Directory.GetFiles(staticCodeDir, filterName);
}
foreach (var file in staticFiles)
{
staticCodes.Add(new StaticCode() { Name = Path.GetFileNameWithoutExtension(file), Application = app });
}
}
}
return staticCodes;
}
/// <summary>
/// Gets the static code.
/// </summary>
/// <param name="application">The application.</param>
/// <param name="name">The name.</param>
/// <returns></returns>
public StaticCode GetStaticCode(string application, string name)
{
string staticCodeDir = TemplateFileManager.GetAbsoluteTemplateFolderPath(application, FolderType.StaticCode);
FileExtensions.EnsureDirtectoryExists(staticCodeDir);
string staticCodeFile = Path.Combine(staticCodeDir, name + ".ascx");
if (File.Exists(staticCodeFile))
{
var body = RemoveRegister(FileExtensions.GetFileBody(staticCodeFile));
var staticCode = new StaticCode()
{
Application = application,
Name = name,
Body = body
};
return staticCode;
}
return null;
}
public void DeleteStaticCode(string application, string name)
{
string staticCodeDir = TemplateFileManager.GetAbsoluteTemplateFolderPath(application, FolderType.StaticCode);
FileExtensions.EnsureDirtectoryExists(staticCodeDir);
string staticCodeFile = Path.Combine(staticCodeDir, name + ".ascx");
if (File.Exists(staticCodeFile))
{
File.Delete(staticCodeFile);
}
}
private static string AddRegister(string body)
{
return string.Format("{0}{1}{2}", Register, body, EndRegister);
}
private static string RemoveRegister(string body)
{
return body.Replace(Register, "").Replace(EndRegister, "");
}
/// <summary>
/// Gets the static code virtual path.
/// </summary>
/// <param name="application">The application.</param>
/// <param name="name">The name.</param>
/// <returns></returns>
public static string GetStaticCodeVirtualPath(string application, string name)
{
string staticCodeDir = TemplateFileManager.GetAbsoluteTemplateFolderPath(application, FolderType.StaticCode);
string staticCodeFile = Path.Combine(staticCodeDir, name + ".ascx");
if (!File.Exists(staticCodeFile))
{
throw new Exception(string.Format(Resources.StaticCodeNotFound, application + "." + name));
}
return UrlConvertor.AbsolutePathToRelativeUrl(staticCodeFile);
}
}
}
|