using System;
using System.Diagnostics;
using System.Xml;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using umbraco.interfaces;
using Umbraco.Core;
namespace Umbraco.Web.Routing
{
///
/// Provides an implementation of that handles page identifiers.
///
///
/// Handles /1234 where 1234 is the identified of a document.
///
//[ResolutionWeight(20)]
internal class LookupByIdPath : IPublishedContentLookup
{
///
/// Tries to find and assign an Umbraco document to a PublishedContentRequest.
///
/// The PublishedContentRequest.
/// A value indicating whether an Umbraco document was found and assigned.
public bool TrySetDocument(PublishedContentRequest docRequest)
{
IPublishedContent node = null;
var path = docRequest.Uri.GetAbsolutePathDecoded();
int nodeId = -1;
if (path != "/") // no id if "/"
{
string noSlashPath = path.Substring(1);
if (!Int32.TryParse(noSlashPath, out nodeId))
nodeId = -1;
if (nodeId > 0)
{
LogHelper.Debug("Id={0}", () => nodeId);
node = docRequest.RoutingContext.PublishedContentStore.GetDocumentById(
docRequest.RoutingContext.UmbracoContext,
nodeId);
if (node != null)
{
docRequest.PublishedContent = node;
LogHelper.Debug("Found node with id={0}", () => docRequest.DocumentId);
}
else
{
nodeId = -1; // trigger message below
}
}
}
if (nodeId == -1)
LogHelper.Debug("Not a node id");
return node != null;
}
}
}