2013-05-31 17:20:56 -10:00
using System ;
using System.Collections.Generic ;
2013-06-20 17:47:14 +10:00
using System.Globalization ;
2013-05-31 17:20:56 -10:00
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 ;
2013-10-08 12:38:27 +11:00
using Umbraco.Web.Models.Trees ;
2013-05-31 17:20:56 -10:00
using Umbraco.Web.WebApi ;
using umbraco.BusinessLogic ;
using umbraco.cms.presentation.Trees ;
2013-07-02 17:47:20 +10:00
using ApplicationTree = Umbraco . Core . Models . ApplicationTree ;
2013-05-31 17:20:56 -10:00
using UrlHelper = System . Web . Http . Routing . UrlHelper ;
namespace Umbraco.Web.Trees
{
internal static class ApplicationTreeExtensions
{
2013-09-04 12:01:41 +10:00
internal static Attempt < Type > TryGetControllerTree ( this ApplicationTree appTree )
2013-05-31 17:20:56 -10:00
{
//get reference to all TreeApiControllers
var controllerTrees = UmbracoApiControllerResolver . Current . RegisteredUmbracoApiControllers
2013-09-26 15:55:38 +10:00
. Where ( TypeHelper . IsTypeAssignableFrom < TreeController > )
2013-05-31 17:20:56 -10:00
. ToArray ( ) ;
//find the one we're looking for
2013-09-10 18:28:45 +10:00
var foundControllerTree = controllerTrees . FirstOrDefault ( x = > x = = appTree . GetRuntimeType ( ) ) ;
2013-05-31 17:20:56 -10:00
if ( foundControllerTree = = null )
{
2013-09-12 12:54:34 +02:00
return Attempt < Type > . Fail ( new InstanceNotFoundException ( "Could not find tree of type " + appTree . Type + " in any loaded DLLs" ) ) ;
2013-06-20 17:47:14 +10:00
}
2013-09-12 12:54:34 +02:00
return Attempt . Succeed ( foundControllerTree ) ;
2013-06-20 17:47:14 +10:00
}
2013-09-04 12:01:41 +10:00
internal static Attempt < TreeNode > TryGetRootNodeFromControllerTree ( this ApplicationTree appTree , FormDataCollection formCollection , HttpControllerContext controllerContext )
2013-06-20 17:47:14 +10:00
{
var foundControllerTreeAttempt = appTree . TryGetControllerTree ( ) ;
if ( foundControllerTreeAttempt . Success = = false )
{
2013-09-12 12:54:34 +02:00
return Attempt < TreeNode > . Fail ( foundControllerTreeAttempt . Exception ) ;
2013-06-20 17:47:14 +10:00
}
var foundControllerTree = foundControllerTreeAttempt . Result ;
//instantiate it, since we are proxying, we need to setup the instance with our current context
2013-09-26 15:55:38 +10:00
var instance = ( TreeController ) DependencyResolver . Current . GetService ( foundControllerTree ) ;
2013-06-20 17:47:14 +10:00
instance . ControllerContext = controllerContext ;
2013-09-04 12:01:41 +10:00
instance . Request = controllerContext . Request ;
2013-06-20 17:47:14 +10:00
//return the root
2013-07-30 13:29:05 +10:00
var node = instance . GetRootNode ( formCollection ) ;
return node = = null
2013-09-12 12:54:34 +02:00
? Attempt < TreeNode > . Fail ( new InvalidOperationException ( "Could not return a root node for tree " + appTree . Type ) )
: Attempt < TreeNode > . Succeed ( node ) ;
2013-06-20 17:47:14 +10:00
}
2013-09-04 12:01:41 +10:00
internal static Attempt < TreeNodeCollection > TryLoadFromControllerTree ( this ApplicationTree appTree , string id , FormDataCollection formCollection , HttpControllerContext controllerContext )
2013-06-20 17:47:14 +10:00
{
var foundControllerTreeAttempt = appTree . TryGetControllerTree ( ) ;
if ( foundControllerTreeAttempt . Success = = false )
{
2013-09-12 12:54:34 +02:00
return Attempt < TreeNodeCollection > . Fail ( foundControllerTreeAttempt . Exception ) ;
2013-05-31 17:20:56 -10:00
}
2013-06-20 17:47:14 +10:00
var foundControllerTree = foundControllerTreeAttempt . Result ;
2013-05-31 17:20:56 -10:00
//instantiate it, since we are proxying, we need to setup the instance with our current context
2013-09-26 15:55:38 +10:00
var instance = ( TreeController ) DependencyResolver . Current . GetService ( foundControllerTree ) ;
2013-05-31 17:20:56 -10:00
instance . ControllerContext = controllerContext ;
2013-09-04 12:01:41 +10:00
instance . Request = controllerContext . Request ;
2013-05-31 17:20:56 -10:00
//return it's data
2013-09-12 12:54:34 +02:00
return Attempt . Succeed ( instance . GetNodes ( id , formCollection ) ) ;
2013-05-31 17:20:56 -10:00
}
2013-07-11 13:26:54 +10:00
internal static Attempt < TreeNode > TryGetRootNodeFromLegacyTree ( this ApplicationTree appTree , FormDataCollection formCollection , UrlHelper urlHelper , string currentSection )
2013-07-09 18:51:45 +10:00
{
var xmlTreeNodeAttempt = TryGetRootXmlNodeFromLegacyTree ( appTree , formCollection , urlHelper ) ;
if ( xmlTreeNodeAttempt . Success = = false )
{
2013-09-12 12:54:34 +02:00
return Attempt < TreeNode > . Fail ( xmlTreeNodeAttempt . Exception ) ;
2013-07-09 18:51:45 +10:00
}
2013-10-28 13:45:38 +11:00
2013-11-15 18:26:40 +11:00
//the root can potentially be null, in that case we'll just return a null success which means it won't be included
if ( xmlTreeNodeAttempt . Result = = null )
{
return Attempt < TreeNode > . Succeed ( null ) ;
}
2013-10-28 13:45:38 +11:00
var legacyController = new LegacyTreeController ( xmlTreeNodeAttempt . Result , appTree . Alias , currentSection , urlHelper ) ;
var newRoot = legacyController . GetRootNode ( formCollection ) ;
return Attempt . Succeed ( newRoot ) ;
2013-07-09 18:51:45 +10:00
}
internal static Attempt < XmlTreeNode > TryGetRootXmlNodeFromLegacyTree ( this ApplicationTree appTree , FormDataCollection formCollection , UrlHelper urlHelper )
2013-05-31 17:20:56 -10:00
{
2013-06-20 17:47:14 +10:00
var treeDefAttempt = appTree . TryGetLegacyTreeDef ( ) ;
if ( treeDefAttempt . Success = = false )
2013-05-31 17:20:56 -10:00
{
2013-09-12 12:54:34 +02:00
return Attempt < XmlTreeNode > . Fail ( treeDefAttempt . Exception ) ;
2013-05-31 17:20:56 -10:00
}
2013-06-20 17:47:14 +10:00
var treeDef = treeDefAttempt . Result ;
var bTree = treeDef . CreateInstance ( ) ;
var treeParams = new LegacyTreeParams ( formCollection ) ;
bTree . SetTreeParameters ( treeParams ) ;
2013-10-28 13:45:38 +11:00
var xmlRoot = bTree . RootNode ;
return Attempt . Succeed ( xmlRoot ) ;
2013-06-20 17:47:14 +10:00
}
2013-05-31 17:20:56 -10:00
2013-09-04 12:01:41 +10:00
internal static Attempt < TreeDefinition > TryGetLegacyTreeDef ( this ApplicationTree appTree )
2013-06-20 17:47:14 +10:00
{
//This is how the legacy trees worked....
var treeDef = TreeDefinitionCollection . Instance . FindTree ( appTree . Alias ) ;
return treeDef = = null
2013-09-12 12:54:34 +02:00
? Attempt < TreeDefinition > . Fail ( new InstanceNotFoundException ( "Could not find tree of type " + appTree . Alias ) )
: Attempt < TreeDefinition > . Succeed ( treeDef ) ;
2013-06-20 17:47:14 +10:00
}
2013-07-11 13:26:54 +10:00
internal static Attempt < TreeNodeCollection > TryLoadFromLegacyTree ( this ApplicationTree appTree , string id , FormDataCollection formCollection , UrlHelper urlHelper , string currentSection )
2013-06-20 17:47:14 +10:00
{
2013-07-09 18:51:45 +10:00
var xTreeAttempt = appTree . TryGetXmlTree ( id , formCollection ) ;
if ( xTreeAttempt . Success = = false )
{
2013-09-12 12:54:34 +02:00
return Attempt < TreeNodeCollection > . Fail ( xTreeAttempt . Exception ) ;
2013-07-09 18:51:45 +10:00
}
2013-10-14 15:36:17 +11:00
return Attempt . Succeed ( LegacyTreeDataConverter . ConvertFromLegacy ( id , xTreeAttempt . Result , urlHelper , currentSection , formCollection ) ) ;
2013-07-09 18:51:45 +10:00
}
internal static Attempt < MenuItemCollection > TryGetMenuFromLegacyTreeRootNode ( this ApplicationTree appTree , FormDataCollection formCollection , UrlHelper urlHelper )
{
var rootAttempt = appTree . TryGetRootXmlNodeFromLegacyTree ( formCollection , urlHelper ) ;
if ( rootAttempt . Success = = false )
{
2013-09-12 12:54:34 +02:00
return Attempt < MenuItemCollection > . Fail ( rootAttempt . Exception ) ;
2013-07-09 18:51:45 +10:00
}
2013-07-11 13:26:54 +10:00
var currentSection = formCollection . GetRequiredString ( "section" ) ;
var result = LegacyTreeDataConverter . ConvertFromLegacyMenu ( rootAttempt . Result , currentSection ) ;
2013-09-12 12:54:34 +02:00
return Attempt . Succeed ( result ) ;
2013-07-09 18:51:45 +10:00
}
internal static Attempt < MenuItemCollection > TryGetMenuFromLegacyTreeNode ( this ApplicationTree appTree , string parentId , string nodeId , FormDataCollection formCollection , UrlHelper urlHelper )
{
var xTreeAttempt = appTree . TryGetXmlTree ( parentId , formCollection ) ;
if ( xTreeAttempt . Success = = false )
{
2013-09-12 12:54:34 +02:00
return Attempt < MenuItemCollection > . Fail ( xTreeAttempt . Exception ) ;
2013-07-09 18:51:45 +10:00
}
2013-07-11 13:26:54 +10:00
var currentSection = formCollection . GetRequiredString ( "section" ) ;
var result = LegacyTreeDataConverter . ConvertFromLegacyMenu ( nodeId , xTreeAttempt . Result , currentSection ) ;
2013-07-09 18:51:45 +10:00
if ( result = = null )
{
2013-09-12 12:54:34 +02:00
return Attempt < MenuItemCollection > . Fail ( new ApplicationException ( "Could not find the node with id " + nodeId + " in the collection of nodes contained with parent id " + parentId ) ) ;
2013-07-09 18:51:45 +10:00
}
2013-09-12 12:54:34 +02:00
return Attempt . Succeed ( result ) ;
2013-07-09 18:51:45 +10:00
}
private static Attempt < XmlTree > TryGetXmlTree ( this ApplicationTree appTree , string id , FormDataCollection formCollection )
{
var treeDefAttempt = appTree . TryGetLegacyTreeDef ( ) ;
2013-06-20 17:47:14 +10:00
if ( treeDefAttempt . Success = = false )
{
2013-09-12 12:54:34 +02:00
return Attempt < XmlTree > . Fail ( treeDefAttempt . Exception ) ;
2013-07-09 18:51:45 +10:00
}
2013-06-20 17:47:14 +10:00
var treeDef = treeDefAttempt . Result ;
//This is how the legacy trees worked....
2013-05-31 17:20:56 -10:00
var bTree = treeDef . CreateInstance ( ) ;
2013-06-20 17:47:14 +10:00
var treeParams = new LegacyTreeParams ( formCollection ) ;
2013-05-31 17:20:56 -10:00
//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 ) ;
2013-09-12 12:54:34 +02:00
return Attempt . Succeed ( xTree ) ;
2013-05-31 17:20:56 -10:00
}
}
}