public void Can_Reverse_TermGroups()
        {
            var siteTermStore = new TaxonomyTermStoreDefinition
            {
                Id = null,
                Name = string.Empty,
                UseDefaultSiteCollectionTermStore = true
            };

            var termGroup1 = new TaxonomyTermGroupDefinition
            {
                Name = Rnd.String(),
                Id = Rnd.Guid()
            };

            var termGroup2 = new TaxonomyTermGroupDefinition
            {
                Name = Rnd.String(),
                Id = Rnd.Guid()
            };


            
            var model = SPMeta2Model.NewSiteModel(site =>
            {
                site.AddTaxonomyTermStore(siteTermStore, store =>
                {
                    store.AddTaxonomyTermGroup(termGroup1);
                    store.AddTaxonomyTermGroup(termGroup2);
                });

            });

            DeployReverseAndTestModel(model);
        }
        protected Group FindGroup(TermStoreModelHost termStoreHost, TaxonomyTermGroupDefinition groupModel)
        {
            Group currentGroup = null;

            var termStore = termStoreHost.HostTermStore;

            if (groupModel.IsSiteCollectionGroup)
            {
                currentGroup = FindSiteCollectionGroup(termStoreHost, groupModel);
                return currentGroup;
            }

            //if (groupModel.IsSystemGroup)
            //{
            //    currentGroup = FindSystemGroup(termStoreHost, groupModel);
            //    return currentGroup;
            //}

            if (groupModel.Id.HasValue)
                currentGroup = termStore.GetGroup(groupModel.Id.Value);
            else if (!string.IsNullOrEmpty(groupModel.Name))
                currentGroup = termStore.Groups.FirstOrDefault(g => g.Name.ToUpper() == groupModel.Name.ToUpper());

            return currentGroup;
        }
        protected Group FindSiteCollectionGroup(TermStoreModelHost termStoreHost, TaxonomyTermGroupDefinition groupModel)
        {
            var termStore = termStoreHost.HostTermStore;

#if NET35
            return termStore.GetSiteCollectionGroup(termStoreHost.HostSite);
#endif

#if !NET35
            return termStore.GetSiteCollectionGroup(termStoreHost.HostSite, true);
#endif
        }
        public void Can_Reverse_TermSets()
        {
            var siteTermStore = new TaxonomyTermStoreDefinition
            {
                Id = null,
                Name = string.Empty,
                UseDefaultSiteCollectionTermStore = true
            };

            var termGroup1 = new TaxonomyTermGroupDefinition
            {
                Name = Rnd.String(),
                Id = Rnd.Guid()
            };

            var termSet1 = new TaxonomyTermSetDefinition
            {
                Name = Rnd.String(),
                Id = Rnd.Guid(),
                Contact = Rnd.String(),
                Description = Rnd.String()
            };

            var termSet2 = new TaxonomyTermSetDefinition
            {
                Name = Rnd.String(),
                Id = Rnd.Guid(),
                Contact = Rnd.String(),
                Description = Rnd.String()
            };

            // only witin a tes term group
            // performance boost
            var groupName = termGroup1.Name;
            var options = ReverseOptions.Default
                .AddFilterOption<TaxonomyTermGroupDefinition>(g => g.Name == groupName);

            var model = SPMeta2Model.NewSiteModel(site =>
            {
                site.AddTaxonomyTermStore(siteTermStore, store =>
                {
                    store.AddTaxonomyTermGroup(termGroup1, group =>
                    {
                        group.AddTaxonomyTermSet(termSet1);
                        group.AddTaxonomyTermSet(termSet2);
                    });
                });

            });

            DeployReverseAndTestModel(model, options);
        }
        protected TermGroup FindSiteCollectionGroup(TermStoreModelHost storeModelHost, TaxonomyTermGroupDefinition groupModel)
        {
            var termStore = storeModelHost.HostTermStore;
            var site = storeModelHost.HostSite;

            var context = termStore.Context;

            TermGroup currentGroup = termStore.GetSiteCollectionGroup(site, true);

            context.Load(currentGroup);
            context.ExecuteQueryWithTrace();
            return currentGroup;
        }
        public override ModelNode ReverseSingleHost(object reverseHost, ReverseOptions options)
        {
            var typedHost = (reverseHost as TaxonomyTermGroupReverseHost);
            var item = typedHost.HostTermGroup;

            var def = new TaxonomyTermGroupDefinition();

            def.Id = item.Id;
            def.Name = item.Name;

            // TODO, M2
            // https://github.com/SubPointSolutions/spmeta2/issues/827
            //def.Descrption = item.Description;

            return new TaxonomyTermGroupModelNode
            {
                Options = { RequireSelfProcessing = true },
                Value = def
            };
        }
        protected TermGroup FindGroup(TermStoreModelHost storeModelHost, TaxonomyTermGroupDefinition groupModel)
        {
            var termStore = storeModelHost.HostTermStore;
            var context = termStore.Context;

            TermGroup currentGroup = null;

            if (groupModel.IsSiteCollectionGroup)
            {
                currentGroup = FindSiteCollectionGroup(storeModelHost, groupModel);
                return currentGroup;
            }

            if (groupModel.Id.HasValue)
            {
                var scope = new ExceptionHandlingScope(context);
                using (scope.StartScope())
                {
                    using (scope.StartTry())
                    {
                        currentGroup = termStore.Groups.GetById(groupModel.Id.Value);
                        context.Load(currentGroup);
                    }

                    using (scope.StartCatch())
                    {

                    }
                }
            }
            else if (!string.IsNullOrEmpty(groupModel.Name))
            {
                var scope = new ExceptionHandlingScope(context);
                using (scope.StartScope())
                {
                    using (scope.StartTry())
                    {
                        currentGroup = termStore.Groups.GetByName(groupModel.Name);
                        context.Load(currentGroup);
                    }

                    using (scope.StartCatch())
                    {

                    }
                }
            }

            context.ExecuteQueryWithTrace();

            if (currentGroup != null && currentGroup.ServerObjectIsNull == false)
            {
                context.Load(currentGroup, g => g.Id);
                context.Load(currentGroup, g => g.Name);

                context.ExecuteQueryWithTrace();

                return currentGroup;
            }

            return null;
        }
        private void DeployTaxonomyGroup(object modelHost, TermStoreModelHost siteModelHost, TaxonomyTermGroupDefinition groupModel)
        {
            var termStore = siteModelHost.HostTermStore;
            var currentGroup = FindGroup(siteModelHost, groupModel);

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model = null,
                EventType = ModelEventType.OnProvisioning,
                Object = currentGroup,
                ObjectType = typeof(TermGroup),
                ObjectDefinition = groupModel,
                ModelHost = modelHost
            });

            if (currentGroup == null)
            {
                TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject, "Processing new Term Group");

                currentGroup = groupModel.Id.HasValue
                                        ? termStore.CreateGroup(groupModel.Name, groupModel.Id.Value)
                                        : termStore.CreateGroup(groupModel.Name, Guid.NewGuid());

                if (!string.IsNullOrEmpty(groupModel.Description))
                    currentGroup.Description = groupModel.Description;

                InvokeOnModelEvent(this, new ModelEventArgs
                {
                    CurrentModelNode = null,
                    Model = null,
                    EventType = ModelEventType.OnProvisioned,
                    Object = currentGroup,
                    ObjectType = typeof(TermGroup),
                    ObjectDefinition = groupModel,
                    ModelHost = modelHost
                });

            }
            else
            {
                TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Processing existing Term Group");

                if (!string.IsNullOrEmpty(groupModel.Description))
                    currentGroup.Description = groupModel.Description;

                InvokeOnModelEvent(this, new ModelEventArgs
                {
                    CurrentModelNode = null,
                    Model = null,
                    EventType = ModelEventType.OnProvisioned,
                    Object = currentGroup,
                    ObjectType = typeof(TermGroup),
                    ObjectDefinition = groupModel,
                    ModelHost = modelHost
                });
            }

            termStore.CommitAll();
            termStore.Context.ExecuteQueryWithTrace();
        }
        public void CanDeploy_WebNavigationSettings_As_TaxonomyProvider()
        {
            var globalTerm = Rnd.String();
            var currentlTerm = Rnd.String();

            var currentNavigationGroup = new TaxonomyTermGroupDefinition
            {
                Name = string.Format("{0}", globalTerm)
            };

            var currentNavigationTermSet = new TaxonomyTermSetDefinition
            {
                Name = string.Format("{0}", globalTerm)
            };

            var globalNavigationGroup = new TaxonomyTermGroupDefinition
            {
                Name = string.Format("{0}", currentlTerm)
            };

            var globalNavigationTermSet = new TaxonomyTermSetDefinition
            {
                Name = string.Format("{0}", currentlTerm)
            };

            var taxWeb = ModelGeneratorService.GetRandomDefinition<WebDefinition>(def =>
            {

            });

            var siteModel = SPMeta2Model.NewSiteModel(site =>
            {
                site.AddSiteFeature(BuiltInSiteFeatures.SharePointServerPublishingInfrastructure.Inherit(def =>
                {
                    def.Enable = true;
                }));

                site.AddTaxonomyTermStore(new TaxonomyTermStoreDefinition
                {
                    UseDefaultSiteCollectionTermStore = true
                },
                store =>
                {
                    store.AddTaxonomyTermGroup(currentNavigationGroup, group =>
                    {
                        group.AddTaxonomyTermSet(currentNavigationTermSet, termSet =>
                        {
                            termSet.AddRandomTerm();
                            termSet.AddRandomTerm();
                            termSet.AddRandomTerm();
                        });
                    });

                    store.AddTaxonomyTermGroup(globalNavigationGroup, group =>
                    {
                        group.AddTaxonomyTermSet(globalNavigationTermSet, termSet =>
                        {
                            termSet.AddRandomTerm();
                            termSet.AddRandomTerm();
                            termSet.AddRandomTerm();
                        });
                    });
                });
            });

            var webModel = SPMeta2Model.NewWebModel(web =>
            {
                web.AddWeb(taxWeb, subWeb =>
                {
                    subWeb.AddWebFeature(BuiltInWebFeatures.SharePointServerPublishing.Inherit(def =>
                    {
                        def.Enable = true;
                    }));

                    subWeb.AddWebNavigationSettings(new WebNavigationSettingsDefinition()
                    {
                        CurrentNavigationSource = BuiltInStandardNavigationSources.TaxonomyProvider,

                        CurrentNavigationUseDefaultSiteCollectionTermStore = true,
                        CurrentNavigationTermSetName = currentNavigationGroup.Name,

                        GlobalNavigationSource = BuiltInStandardNavigationSources.TaxonomyProvider,

                        GlobalNavigationUseDefaultSiteCollectionTermStore = true,
                        GlobalNavigationTermSetName = globalNavigationGroup.Name,

                        DisplayShowHideRibbonAction = true
                    });
                });
            });

            TestModel(siteModel, webModel);
        }
        private void DeployTaxonomyGroup(object modelHost, TermStoreModelHost siteModelHost, TaxonomyTermGroupDefinition groupModel)
        {
            var termStore = siteModelHost.HostTermStore;
            var currentGroup = FindGroup(siteModelHost, groupModel);
            
            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model = null,
                EventType = ModelEventType.OnProvisioning,
                Object = currentGroup,
                ObjectType = typeof(Group),
                ObjectDefinition = groupModel,
                ModelHost = modelHost
            });

            if (currentGroup == null)
            {
                TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject, "Processing new Term Group");

#if !NET35

                currentGroup = groupModel.Id.HasValue
                                        ? termStore.CreateGroup(groupModel.Name, groupModel.Id.Value)
                                        : termStore.CreateGroup(groupModel.Name);

#endif

#if NET35
                // SP2010 API does not support creating groups with particular ID
                currentGroup = termStore.CreateGroup(groupModel.Name);

#endif

                InvokeOnModelEvent(this, new ModelEventArgs
                {
                    CurrentModelNode = null,
                    Model = null,
                    EventType = ModelEventType.OnProvisioned,
                    Object = currentGroup,
                    ObjectType = typeof(Group),
                    ObjectDefinition = groupModel,
                    ModelHost = modelHost
                });

            }
            else
            {
                TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Processing existing Term Group");

                InvokeOnModelEvent(this, new ModelEventArgs
                {
                    CurrentModelNode = null,
                    Model = null,
                    EventType = ModelEventType.OnProvisioned,
                    Object = currentGroup,
                    ObjectType = typeof(Group),
                    ObjectDefinition = groupModel,
                    ModelHost = modelHost
                });
            }

            siteModelHost.HostTermStore.CommitAll();
        }
 protected Group FindSystemGroup(TermStoreModelHost termStoreHost, TaxonomyTermGroupDefinition groupModel)
 {
     return termStoreHost.HostTermStore.SystemGroup;
 }
 protected Group FindSiteCollectionGroup(TermStoreModelHost termStoreHost, TaxonomyTermGroupDefinition groupModel)
 {
     var termStore = termStoreHost.HostTermStore;
     return termStore.GetSiteCollectionGroup(termStoreHost.HostSite);
 }
 public static ModelNode AddHostTaxonomyTermGroup(this ModelNode model, TaxonomyTermGroupDefinition definition, Action<ModelNode> action)
 {
     return model.AddDefinitionNodeWithOptions(definition, action, ModelNodeOptions.New().NoSelfProcessing());
 }
 public static ModelNode AddHostTaxonomyTermGroup(this ModelNode model, TaxonomyTermGroupDefinition definition)
 {
     return AddHostTaxonomyTermGroup(model, definition, null);
 }
 public static ModelNode AddTaxonomyTermGroup(this ModelNode model, TaxonomyTermGroupDefinition definition, Action<ModelNode> action)
 {
     return model.AddDefinitionNode(definition, action);
 }