Updated trees to get children nodes from the server... nearly working and also works with proxying

to legacy trees. In fact there are not new trees created yet, just focusing on adapting for legacy trees
which so far seems to be good.
This commit is contained in:
Shannon Deminick
2013-05-31 17:20:56 -10:00
parent 4a57dcfd01
commit f3fbae7173
9 changed files with 294 additions and 130 deletions

View File

@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Instrumentation;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http.Controllers;
using System.Web.Mvc;
using Umbraco.Core;
using Umbraco.Web.WebApi;
using umbraco.BusinessLogic;
using umbraco.cms.presentation.Trees;
using UrlHelper = System.Web.Http.Routing.UrlHelper;
namespace Umbraco.Web.Trees
{
internal static class ApplicationTreeExtensions
{
internal static Attempt<TreeNodeCollection> TryLoadFromControllerTree(this ApplicationTree appTree, string id, FormDataCollection formCollection, HttpControllerContext controllerContext, HttpRequestMessage request)
{
//get reference to all TreeApiControllers
var controllerTrees = UmbracoApiControllerResolver.Current.RegisteredUmbracoApiControllers
.Where(TypeHelper.IsTypeAssignableFrom<TreeApiController>)
.ToArray();
//find the one we're looking for
var foundControllerTree = controllerTrees.FirstOrDefault(x => x.GetFullNameWithAssembly() == appTree.Type);
if (foundControllerTree == null)
{
return new Attempt<TreeNodeCollection>(new InstanceNotFoundException("Could not find tree of type " + appTree.Type + " in any loaded DLLs"));
}
//instantiate it, since we are proxying, we need to setup the instance with our current context
var instance = (TreeApiController)DependencyResolver.Current.GetService(foundControllerTree);
instance.ControllerContext = controllerContext;
instance.Request = request;
//return it's data
return new Attempt<TreeNodeCollection>(true, instance.GetNodes(id, formCollection));
}
internal static Attempt<TreeNodeCollection> TryLoadFromLegacyTree(this ApplicationTree appTree, string id, FormDataCollection formCollection, UrlHelper urlHelper)
{
//This is how the legacy trees worked....
var treeDef = TreeDefinitionCollection.Instance.FindTree(appTree.Alias);
if (treeDef == null)
{
return new Attempt<TreeNodeCollection>(new InstanceNotFoundException("Could not find tree of type " + appTree.Alias));
}
var bTree = treeDef.CreateInstance();
var treeParams = new ApplicationTreeApiController.TreeParams();
//we currently only support an integer id or a string id, we'll refactor how this works
//later but we'll get this working first
int startId;
if (int.TryParse(id, out startId))
{
treeParams.StartNodeID = startId;
}
else
{
treeParams.NodeKey = id;
}
var xTree = new XmlTree();
bTree.SetTreeParameters(treeParams);
bTree.Render(ref xTree);
return new Attempt<TreeNodeCollection>(true, LegacyTreeDataAdapter.ConvertFromLegacy(xTree, urlHelper));
}
}
}