public void Given_head_site_with_one_child_site_When_build_then_should_only_have_correct_result()
        {
            // Arrange
            var childSite = new CompositeSiteViewModel()
                                             {
                                                 Name = "Liverpool",
                                                 SiteId = 2,
                                                 Id = 93,
                                                 SiteType = CompositeSiteType.SiteAddress
                                             };

            var masterSite = new CompositeSiteViewModel()
                                             {
                                                 Name = "Boston",
                                                 SiteId = 1,
                                                 Id = 87,
                                                 SiteType = CompositeSiteType.SiteAddress,
                                                 Children = new List<CompositeSiteViewModel>()
                                                                                             {
                                                                                                 childSite,
                                                                                             }
                                             };


            // Act
            var result = new SiteTreeViewBuilder(masterSite).Build();

            // Assert
            Assert.That(result, Is.EqualTo(string.Format("<li><div class='linked-site' data-type='siteaddress' data-id='{0}'>{1}</div><ul><li><div class='linked-site' data-type='siteaddress' data-id='{2}'>{3}</div></li></ul></li>", masterSite.Id, masterSite.Name, childSite.Id, childSite.Name)));
        }
        private static string GetSiteType(CompositeSiteViewModel site)
        {
            if (site.SiteType == CompositeSiteType.SiteAddress)
                return "siteaddress";

            if (site.SiteType == CompositeSiteType.SiteGroup)
                return "sitegroup";

            throw new ArgumentException("Site Type not defined. Neither SiteAddress or SiteGroup");

        }
        public void Given_invalid_site_type_When_build_then_should_throw_invalid_argument_exception()
        {
            // Arrange
            var siteOrganisationalUnitDto = new CompositeSiteViewModel();
            {
            };

            // Act
            // Assert
            Assert.Throws<ArgumentException>(() => new SiteTreeViewBuilder(siteOrganisationalUnitDto).Build());
        }
        public void Given_only_head_site_When_build_then_should_only_have_correct_result()
        {
            // Arrange
            const string expectedHeadSiteName = "Boston";
            var siteOrganisationalUnitDto = new CompositeSiteViewModel()
                                                {
                                                    Name = expectedHeadSiteName,
                                                    SiteId = 1,
                                                    Id = 88,
                                                    SiteType = CompositeSiteType.SiteAddress
                                                };

            // Act
            var result = new SiteTreeViewBuilder(siteOrganisationalUnitDto).Build();

            // Assert
            Assert.That(result, Is.EqualTo(string.Format("<li><div class='linked-site' data-type='siteaddress' data-id='88'>{0}</div></li>", expectedHeadSiteName)));
        }
        private StringBuilder ProcessChildrenSites(CompositeSiteViewModel master)
        {
            var result = new StringBuilder();
            result.Append(CreateStartUnorderedList());

            foreach (var site in master.Children)
            {
                result.Append(CreateStartListItem(site));

                if (site.Children.Any())
                {
                    var childrenSitesHtml = ProcessChildrenSites(site);
                    result.Append(childrenSitesHtml.ToString());
                }

                result.Append(CreateEndListItem());
            }

            result.Append(CreateEndUnorderedList());
            return result;
        }
        public void Given_head_site_with_two_child_sites_one_child_site_has_one_child_site__which_has_one_child_site_When_build_then_should_only_have_correct_result()
        {
            // Arrange
            var thirdLevelChildSite = new CompositeSiteViewModel()
            {
                Name = "A Shed by a house",
                SiteId = 5,
                Id = 93,
                SiteType = CompositeSiteType.SiteAddress,
            };

            var secondLevelChildSite = new CompositeSiteViewModel()
            {
                Name = "Ottley",
                SiteId = 4,
                Id = 92,
                SiteType = CompositeSiteType.SiteAddress,
                Children = new List<CompositeSiteViewModel>() { thirdLevelChildSite }
            };

            var childSite1 = new CompositeSiteViewModel()
            {
                Name = "Liverpool",
                SiteId = 3,
                Id = 91,
                SiteType = CompositeSiteType.SiteAddress,
                Children = new List<CompositeSiteViewModel>()
                               {
                                   secondLevelChildSite
                               }
            };
            var childSite2 = new CompositeSiteViewModel()
            {
                Name = "Manchester",
                SiteId = 2,
                Id = 90,
                SiteType = CompositeSiteType.SiteAddress,
            };
            var masterSite = new CompositeSiteViewModel()
            {
                Name = "Boston",
                SiteId = 1,
                Id = 89,
                SiteType = CompositeSiteType.SiteAddress,
                Children = new List<CompositeSiteViewModel>()
                                                                                             {
                                                                                                 childSite1,
                                                                                                 childSite2
                                                                                             }
            };


            // Act
            var result = new SiteTreeViewBuilder(masterSite).Build();

            // Assert
            Assert.That(result, Is.EqualTo(string.Format("<li><div class='linked-site' data-type='siteaddress' data-id='{0}'>{1}</div><ul><li><div class='linked-site' data-type='siteaddress' data-id='{2}'>{3}</div><ul><li><div class='linked-site' data-type='siteaddress' data-id='{4}'>{5}</div><ul><li><div class='linked-site' data-type='siteaddress' data-id='{6}'>{7}</div></li></ul></li></ul></li><li><div class='linked-site' data-type='siteaddress' data-id='{8}'>{9}</div></li></ul></li>", masterSite.Id, masterSite.Name, childSite1.Id, childSite1.Name, secondLevelChildSite.Id, secondLevelChildSite.Name, thirdLevelChildSite.Id, thirdLevelChildSite.Name, childSite2.Id, childSite2.Name)));
        }
        private static string GetDataIdToLoad(CompositeSiteViewModel site)
        {
            if (site.SiteType == CompositeSiteType.SiteAddress)
                return site.Id.ToString();

            if (site.SiteType == CompositeSiteType.SiteGroup)
                return site.Id.ToString();

            throw new ArgumentException("Site Type not defined. Neither SiteAddress or SiteGroup");
        }
 private string CreateCompleteListItem(CompositeSiteViewModel site)
 {
     return string.Format("<li><div class='linked-site' data-type='{0}' data-id='{1}'>{2}</div></li>", GetSiteType(site), GetDataIdToLoad(site), site.Name);
 }
 public SiteTreeViewBuilder(CompositeSiteViewModel site)
 {
     _site = site;
 }
        private static CompositeSiteViewModel GetCompositeSiteViewModel(SiteStructureElementDto siteStructureElement, bool showClosedSites)
        {
            var compositeSiteViewModel = new CompositeSiteViewModel();
            compositeSiteViewModel.Id = siteStructureElement.Id;
            compositeSiteViewModel.Name = siteStructureElement.Name;
            
            var site = siteStructureElement as SiteDto;

            if (site != null)
            {
                compositeSiteViewModel.SiteType = CompositeSiteType.SiteAddress;
                compositeSiteViewModel.SiteId = site.SiteId;
            }

            var siteGroup = siteStructureElement as SiteGroupDto;

            if (siteGroup != null)
            {
                compositeSiteViewModel.SiteType = CompositeSiteType.SiteGroup;
            }

            ////todo: do this nicer.
            //if(site == null && siteGroup == null)
            //{
            //    if(siteStructureElement.SiteStructureElementType == "Site")
            //        compositeSiteViewModel.SiteType = CompositeSiteType.SiteAddress;
            //    else
            //        compositeSiteViewModel.SiteType = CompositeSiteType.SiteGroup;
            //}

            if (siteStructureElement.Children != null)
            {
                compositeSiteViewModel.Children = new List<CompositeSiteViewModel>();

                foreach (var child in siteStructureElement.Children)
                {
                    var areClosedSitesToBeShown = showClosedSites || !child.SiteClosed;
                    
                    if (!child.Deleted && areClosedSitesToBeShown)
                    {
                        compositeSiteViewModel.Children.Add(GetCompositeSiteViewModel(child, showClosedSites));
                    }
                }
            }

            return compositeSiteViewModel;
        }