/*
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 Microsoft.Data.Extensions;
using Everest.Library;
using Everest.Library.Data;
using Everest.Library.Data.Entity;
using Everest.Library.Versioning;
using Everest.CmsServices.Providers;
using Everest.CmsServices.Models;
namespace Everest.CmsServices.Services{
public class SchemaService
{
public void AddSchema(IEverestCmsDataContext dataContext, Cms_Schema schema, string userName)
{
using (var conn = dataContext.ObjectContext.Connection.CreateConnectionScope())
{
using (var dbTrans = dataContext.ObjectContext.Connection.BeginTransaction())
{
dataContext.AddToCms_Schema(schema);
dataContext.SaveChanges();
//ApplicationSchemaaspnet_Applications
//
schema.aspnet_ApplicationsReference.Load(schema.aspnet_Applications, schema.EntityState);
IContentProvider contentProvider = UnityManager.Resolve<IContentProvider>();
contentProvider.TextContentManager.CreateSchema(dataContext, schema);
dbTrans.Commit();
}
}
//check in to save history
schema.Checkin(userName, "");
}
public void UpdateSchema(IEverestCmsDataContext dataContext, Cms_Schema schema, string userName, string oldSchemaName, List<Cms_Column> olderColumns)
{
using (var conn = dataContext.ObjectContext.Connection.CreateConnectionScope())
{
using (var dbTrans = dataContext.ObjectContext.Connection.BeginTransaction())
{
dataContext.SaveChanges();
IContentProvider contentProvider = UnityManager.Resolve<IContentProvider>();
contentProvider.TextContentManager.UpdateSchema(dataContext, schema, oldSchemaName, olderColumns);
dbTrans.Commit();
}
}
//check in to save history
schema.Checkin(userName, "");
}
public void DeleteSchema(Guid schemaUUID)
{
IEverestCmsDataContext dataContext = EverestCmsEntities.GetDataContext();
var schema = dataContext.QuerySchema(schemaUUID).FirstOrDefault();
if (schema != null)
{
if (BeReferenced(dataContext, schemaUUID))
{
throw new Exception(string.Format(Resources.ItemCouldNotBeDeleted, schema.SchemaName));
}
else
{
using (var conn = dataContext.ObjectContext.Connection.CreateConnectionScope())
{
using (var dbTrans = dataContext.ObjectContext.Connection.BeginTransaction())
{
UnityManager.Resolve<IContentProvider>().TextContentManager.DeleteSchema(dataContext, schema);
schema.ClearColumns(dataContext);
dataContext.DeleteObject(schema);
dataContext.SaveChanges();
dbTrans.Commit();
}
}
schema.ClearVersions();
}
}
}
private bool BeReferenced(IEverestCmsDataContext dataContext, Guid schemaUUID)
{
string strSchemaUUID = schemaUUID.ToString();
if (dataContext.QueryFoldersBySchema(schemaUUID).Exists())
{
return true;
}
if (dataContext.Cms_Schema.Where(s => s.ChildSchemas.Contains(strSchemaUUID)).Exists())
{
return true;
}
if (dataContext.Cms_Schema.Where(s => s.ReferencingSchemas.Contains(strSchemaUUID)).Exists())
{
return true;
}
return false;
}
}
}
|