/*
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 System.Collections;
using Db4objects.Db4o;
using Db4objects.Db4o.Query;
using Db4objects.Db4o.Config;
using Everest.Library.Versioning;
namespace Everest.Providers.Versioning.DB4O{
public class DataContext : IDisposable
{
IObjectContainer versionDb;
private string dbDirectory;
public DataContext(string dbDirectory)
{
this.dbDirectory = dbDirectory;
IConfiguration objConfig = Db4oFactory.NewConfiguration();
objConfig.ObjectClass(typeof(VersionItem)).ObjectField("<UUID>k__BackingField").Indexed(true);
objConfig.ObjectClass(typeof(VersionItem)).ObjectField("<Revision>k__BackingField").Indexed(true);
versionDb = Db4objects.Db4o.Db4oFactory.OpenFile(objConfig, Path.Combine(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, dbDirectory), "Versioning.config"));
}
public IEnumerable<IVersionItem> GetVersions(Guid uuid)
{
IQuery query = QueryVersions(uuid);
IObjectSet result = query.Execute();
IList<IVersionItem> list = new List<IVersionItem>();
foreach (var item in result)
{
list.Add((VersionItem)item);
}
return list;
}
private IQuery QueryVersions(Guid uuid)
{
IQuery query = versionDb.Query();
query.Constrain(typeof(VersionItem));
query.Descend("<UUID>k__BackingField").Constrain(uuid).Equal();
query.Descend("<Revision>k__BackingField").OrderDescending();
return query;
}
public VersionItem GetLastestVersion(Guid uuid)
{
IQuery query = QueryVersions(uuid);
IObjectSet result = query.Execute();
if (result.Count > 0)
{
return (VersionItem)result[0];
}
return null;
}
public VersionItem GetVersion(Guid uuid, int revision)
{
IQuery query = versionDb.Query();
query.Constrain(typeof(VersionItem));
query.Descend("<UUID>k__BackingField").Constrain(uuid).Equal();
query.Descend("<Revision>k__BackingField").Constrain(revision).Equal();
IObjectSet result = query.Execute();
if (result.Count > 0)
{
return (VersionItem)result[0];
}
return null;
}
public void SaveVersion(VersionItem versionItem)
{
versionDb.Store(versionItem);
versionDb.Commit();
}
public void DeleteVersions(Guid uuid)
{
IQuery query = QueryVersions(uuid);
IObjectSet result = query.Execute();
foreach (var item in result)
{
versionDb.Delete(item);
}
versionDb.Commit();
}
#region IDisposable Members
public void Dispose()
{
versionDb.Close();
}
#endregion
}
}
|