[longomatch] Make the templates use the new IStorage interface
- From: Andoni Morales Alastruey <amorales src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [longomatch] Make the templates use the new IStorage interface
- Date: Wed, 18 Mar 2015 14:46:29 +0000 (UTC)
commit 9e78806453484083c82f8043c5a7b8b9283e5233
Author: Jorge Zapata <jorgeluis zapata gmail com>
Date: Mon Mar 16 19:51:05 2015 +0100
Make the templates use the new IStorage interface
For now we just use a FileStorage as the IStorage type for templates.
Old templates will not be accessible, given that the old path is different
than the new one. We can create a migration tool for that or just wait
until thre whole DBStorage is finished.
LongoMatch.Core/Interfaces/ITemplates.cs | 9 +-
LongoMatch.Services/Services/CoreServices.cs | 3 +-
LongoMatch.Services/Services/TemplatesService.cs | 151 +++++++++-------------
Tests/Services/TestTemplatesService.cs | 67 ++++++++++
Tests/Tests.csproj | 1 +
5 files changed, 136 insertions(+), 95 deletions(-)
---
diff --git a/LongoMatch.Core/Interfaces/ITemplates.cs b/LongoMatch.Core/Interfaces/ITemplates.cs
index 621e0fd..655eacc 100644
--- a/LongoMatch.Core/Interfaces/ITemplates.cs
+++ b/LongoMatch.Core/Interfaces/ITemplates.cs
@@ -22,14 +22,13 @@ using LongoMatch.Core.Store.Templates;
namespace LongoMatch.Core.Interfaces
{
- public interface ITemplate: IIDObject
+ public interface ITemplate: IStorable
{
string Name {get; set;}
}
-
+
public interface ITemplateProvider
{
- void CheckDefaultTemplate();
List<string> TemplatesNames {get;}
bool Exists(string name);
void Copy (string orig, string copy);
@@ -42,8 +41,8 @@ namespace LongoMatch.Core.Interfaces
List<T> Templates {get;}
T Load (string name);
T LoadFile (string filename);
- void Save (ITemplate template);
- void Update (ITemplate template);
+ void Save (T template);
+ void Update (T template);
void Register (T template);
}
diff --git a/LongoMatch.Services/Services/CoreServices.cs b/LongoMatch.Services/Services/CoreServices.cs
index 3a6359d..ffffa42 100644
--- a/LongoMatch.Services/Services/CoreServices.cs
+++ b/LongoMatch.Services/Services/CoreServices.cs
@@ -24,6 +24,7 @@ using LongoMatch.Core.Interfaces;
using LongoMatch.Core.Interfaces.GUI;
using LongoMatch.Core.Interfaces.Multimedia;
using Mono.Unix;
+using LongoMatch.Services.Services;
#if OSTYPE_WINDOWS
using System.Runtime.InteropServices;
@@ -83,7 +84,7 @@ namespace LongoMatch.Services
public static void StartServices (IGUIToolkit guiToolkit, IMultimediaToolkit
multimediaToolkit)
{
- ts = new TemplatesService ();
+ ts = new TemplatesService (new FileStorage(Config.DBDir));
Config.TeamTemplatesProvider = ts.TeamTemplateProvider;
Config.CategoriesTemplatesProvider = ts.CategoriesTemplateProvider;
diff --git a/LongoMatch.Services/Services/TemplatesService.cs
b/LongoMatch.Services/Services/TemplatesService.cs
index 0cb597a..a3ebab2 100644
--- a/LongoMatch.Services/Services/TemplatesService.cs
+++ b/LongoMatch.Services/Services/TemplatesService.cs
@@ -25,6 +25,7 @@ using LongoMatch.Core.Interfaces;
using LongoMatch.Core.Store;
using LongoMatch.Core.Store.Templates;
using Mono.Unix;
+using LongoMatch.Services.Services;
namespace LongoMatch.Services
{
@@ -32,19 +33,13 @@ namespace LongoMatch.Services
{
private Dictionary<Type, ITemplateProvider> dict;
- public TemplatesService ()
+ public TemplatesService (IStorage storage)
{
dict = new Dictionary<Type, ITemplateProvider> ();
- dict.Add (typeof(Team),
- new TeamTemplatesProvider (Config.TeamsDir));
- dict.Add (typeof(Dashboard), new CategoriesTemplatesProvider (Config.AnalysisDir));
- CheckDefaultTemplates ();
- }
-
- private void CheckDefaultTemplates ()
- {
- foreach (ITemplateProvider t in dict.Values)
- t.CheckDefaultTemplate ();
+ dict.Add (typeof(TeamTemplate),
+ new TeamTemplatesProvider (storage));
+ dict.Add (typeof(Dashboard),
+ new CategoriesTemplatesProvider (storage));
}
public ITemplateProvider<T> GetTemplateProvider<T> () where T: ITemplate
@@ -69,57 +64,58 @@ namespace LongoMatch.Services
public class TemplatesProvider<T>: ITemplateProvider<T> where T: ITemplate
{
- readonly string basePath;
- readonly string extension;
readonly MethodInfo methodDefaultTemplate;
List<T> systemTemplates;
+ IStorage storage;
- public TemplatesProvider (string basePath, string extension)
+ public TemplatesProvider (IStorage storage)
{
- this.basePath = basePath;
- this.extension = extension;
methodDefaultTemplate = typeof(T).GetMethod ("DefaultTemplate");
systemTemplates = new List<T> ();
- }
-
- public virtual void CheckDefaultTemplate ()
- {
- string path;
-
- path = GetPath ("default");
- if (!File.Exists (path)) {
- Create ("default", 20);
- }
+ this.storage = storage;
+ // Create the default template, it will be added to the list
+ // of system templates to make it always available on the app
+ // and also read-only
+ Create ("default", 20);
}
public bool Exists (string name)
{
- return File.Exists (GetPath (name));
+ // FIXME we can add an Exist(Dictionary args) method on the IStorage?
+ Dictionary<string, object> dict = new Dictionary<string, object> ();
+ dict.Add ("Name", name);
+
+ List<T> list = storage.Retrieve<T>(dict);
+ if (list.Count == 0)
+ return false;
+ else
+ return true;
}
+ /// <summary>
+ /// Gets the templates.
+ /// </summary>
+ /// <value>The templates.</value>
public List<T> Templates {
get {
- List<T> templates = new List<T> ();
-
- foreach (string file in TemplatesNames) {
- try {
- templates.Add (Load (file));
- } catch (Exception ex) {
- Log.Exception (ex);
- }
- }
- return templates;
+ return storage.RetrieveAll<T>();
}
}
+ /// <summary>
+ /// Gets the templates names.
+ /// For that we need to get every ITemplate from the Storage, create a new list
+ /// based on every ITemplate name, and return it.
+ /// </summary>
+ /// <value>The templates names.</value>
public List<string> TemplatesNames {
+ // FIXME we really need to avoid this. Too many roundtrips
get {
List<string> l = new List<string> ();
- if (!Directory.Exists (basePath)) {
- Directory.CreateDirectory (basePath);
- }
- foreach (string path in Directory.GetFiles (basePath, "*" + extension)) {
- l.Add (Path.GetFileNameWithoutExtension (path));
+ List<T> templates = Templates;
+ foreach (T template in templates)
+ {
+ l.Add(template.Name);
}
return l.Concat (systemTemplates.Select (t => t.Name)).ToList ();
}
@@ -134,48 +130,35 @@ namespace LongoMatch.Services
// Return a copy to prevent modification of system templates.
return Cloner.Clone (template);
} else {
- Log.Information ("Loading template " + name);
- template = (T)Serializer.LoadSafe<T>(GetPath(name));
- template.Name = name;
- return template;
+ Dictionary<string, object> dict = new Dictionary<string, object> ();
+ dict.Add ("Name", name);
+
+ List<T> list = storage.Retrieve<T>(dict);
+ if (list.Count == 0)
+ throw new TemplateNotFoundException (name);
+ else
+ return list[0];
}
}
public T LoadFile (string filename)
{
- T template;
-
Log.Information ("Loading template file " + filename);
- template = (T)Serializer.LoadSafe<T>(filename);
+ T template = FileStorage.RetrieveFrom<T>(filename);
return template;
}
- public void Save (ITemplate template)
+ public void Save (T template)
{
CheckInvalidChars (template.Name);
- string filename = GetPath (template.Name);
-
- Log.Information ("Saving template " + filename);
-
- if (File.Exists (filename)) {
- throw new Exception (Catalog.GetString ("A template already exists with " +
- "the name: ") + filename);
- }
-
- if (!Directory.Exists (Path.GetDirectoryName (filename))) {
- Directory.CreateDirectory (Path.GetDirectoryName (filename));
- }
-
- /* Don't cach the Exception here to chain it up */
- Serializer.Save<T>((T)template, filename);
+ Log.Information ("Saving template " + template.Name);
+ storage.Store<T>(template);
}
- public void Update (ITemplate template)
+ public void Update (T template)
{
CheckInvalidChars (template.Name);
- string filename = GetPath (template.Name);
- Log.Information ("Updating template " + filename);
- /* Don't cach the Exception here to chain it up */
+ Log.Information ("Updating template " + template.Name);
Save(template);
}
@@ -188,11 +171,6 @@ namespace LongoMatch.Services
{
T template;
- if (File.Exists (copy)) {
- throw new Exception (Catalog.GetString ("A template already exists with " +
- "the name: ") + copy);
- }
-
CheckInvalidChars (copy);
Log.Information (String.Format ("Copying template {0} to {1}", orig, copy));
@@ -212,7 +190,9 @@ namespace LongoMatch.Services
{
try {
Log.Information ("Deleting template " + templateName);
- File.Delete (GetPath (templateName));
+ T template = Load(templateName);
+ if (template != null)
+ storage.Delete<T>(template);
} catch (Exception ex) {
Log.Exception (ex);
}
@@ -225,9 +205,12 @@ namespace LongoMatch.Services
if (list.Length == 0)
list = new object[] { 0 };
Log.Information (String.Format ("Creating default {0} template", typeof(T)));
- ITemplate t = (ITemplate)methodDefaultTemplate.Invoke (null, list);
+ T t = (T)methodDefaultTemplate.Invoke (null, list);
t.Name = templateName;
- Save (t);
+ // TODO split the registration from the creation, i.e: this function must return T
+ // and let the constructor register the returned template. For that we need to
refactor
+ // the ITemplateProvider and ITemplateProvider<T>, no need to have them separated
+ Register (t);
}
void CheckInvalidChars (string name)
@@ -239,29 +222,19 @@ namespace LongoMatch.Services
throw new InvalidTemplateFilenameException (invalidChars);
}
}
-
- string GetPath (string templateName)
- {
- return System.IO.Path.Combine (basePath, templateName) + extension;
- }
}
public class TeamTemplatesProvider: TemplatesProvider<Team>, ITeamTemplatesProvider
{
- public TeamTemplatesProvider (string basePath): base (basePath, Constants.TEAMS_TEMPLATE_EXT)
+ public TeamTemplatesProvider (IStorage storage): base (storage)
{
}
}
public class CategoriesTemplatesProvider : TemplatesProvider<Dashboard>, ICategoriesTemplatesProvider
{
- public CategoriesTemplatesProvider (string basePath): base (basePath,
Constants.CAT_TEMPLATE_EXT)
- {
- }
-
- public override void CheckDefaultTemplate ()
+ public CategoriesTemplatesProvider (IStorage storage) : base(storage)
{
- /* Do nothing now that we have a plugin that creates the default template */
}
}
}
diff --git a/Tests/Services/TestTemplatesService.cs b/Tests/Services/TestTemplatesService.cs
new file mode 100644
index 0000000..0405d9b
--- /dev/null
+++ b/Tests/Services/TestTemplatesService.cs
@@ -0,0 +1,67 @@
+//
+// Copyright (C) 2015 jl
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// 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, write to the Free Software
+// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
+//
+using NUnit.Framework;
+using System;
+using LongoMatch.Services;
+using LongoMatch.Services.Services;
+using System.IO;
+using LongoMatch.Core.Store.Templates;
+using LongoMatch.Core.Common;
+using LongoMatch.Core.Interfaces;
+
+namespace Tests.Services
+{
+ [TestFixture()]
+ public class TestTemplatesService
+ {
+ [TearDown]
+ public void RemoveStorage ()
+ {
+ try {
+ Directory.Delete (Path.Combine (Path.GetTempPath (), "TestTemplatesService"),
true);
+ } catch {
+ }
+ }
+
+ [Test()]
+ public void TestSystemTemplates ()
+ {
+ FileStorage fs = new FileStorage (
+ Path.Combine (Path.GetTempPath (), "TestTemplatesService"));
+ TemplatesService ts = new TemplatesService (fs);
+ ICategoriesTemplatesProvider ctp = ts.CategoriesTemplateProvider;
+
+ // We must have at least one template provider called 'Default'
+ Dashboard dash = ctp.Load ("default");
+ Assert.AreNotSame (dash, null);
+
+ // Test we dont have a template
+ bool found = ctp.Exists ("NonExistingTemplate");
+ Assert.AreEqual (found, false);
+
+ // Test saving the default template
+ dash.Name = "NewDefault";
+ ctp.Save (dash);
+
+ // Test loading a template from a file
+ Dashboard newDefault = ctp.Load ("NewDefault");
+ Assert.AreEqual (newDefault.Name, "NewDefault");
+ }
+ }
+}
+
diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj
index 10b5876..3d44a0b 100644
--- a/Tests/Tests.csproj
+++ b/Tests/Tests.csproj
@@ -78,6 +78,7 @@
<Compile Include="Services\TestFileStorage.cs" />
<Compile Include="Core\Store\TestCoordinates.cs" />
<Compile Include="DB\TestStorage.cs" />
+ <Compile Include="Services\TestTemplatesService.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LongoMatch.Core\LongoMatch.Core.csproj">
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]