private void CompileNxBrePolicySet(IPolicySetVersionCache version, IPolicySet policySet, string target)
        {
            if ((null == version) || (null == policySet) || (string.IsNullOrEmpty(target)))
                return;

            NxPolicyStore nxBreStore = new NxPolicyStore();
            nxBreStore.ResourceManager = ResourcesCache.GetResources();
            policySet.Export(nxBreStore);

            CompiledPolicySetExtractor cpse = new CompiledPolicySetExtractor(nxBreStore.XMLRepresentation);
            List<string> channels = cpse.GetChannelNames();

            foreach (string channel in channels)
            {
                string objects = cpse.GetObjects(channel);
                string rules = cpse.GetRules(channel);

                ICompiledPolicySetCache cps = version.GetCompiledPolicySet(channel, target);
                if (cps == null)
                {
                    cps = version.NewCompiledPolicySet(channel, target, rules, objects);
                }
                else
                {
                    cps.Content = rules;
                    cps.ObjectReferences = objects;
                }
                    
                SaveLanguages(cpse.GetLanguages(), cps);
            }
        }
        public void TestExportChannel()
        {
            string policyCatalogueFilename = m_testPath + "SamplePolicyCatalogueWithSingleChannel.xml";
            string policyLanguageFilename = m_testPath + "SampleLanguageBase.xml";
            string policyFilename = m_testPath + "SamplePolicyWithSingleChannel.xml";

            XmlPolicyLanguageStore languageStore = XmlPolicyLanguageStore.Instance;
            Guid languageId = languageStore.AddLanguage(System.IO.File.ReadAllText(policyLanguageFilename));
            XMLPolicyCatalogueStore catalogueStore = XMLPolicyCatalogueStore.Instance;
            catalogueStore.Reset();

            catalogueStore.AddPolicyCatalogue(System.IO.File.ReadAllText(policyCatalogueFilename));

            XmlStore store = new XmlStore(System.IO.File.ReadAllText(policyFilename));
            IPolicyObjectCollection<IPolicySet> policySet = store.Reader.PolicySets;
            IPolicyObjectCollection<IPolicy> policies = store.Reader.Policies(policySet[0]);
            IPolicy policy = policies[0];
            IPolicySet ps = policySet[0];

            NxPolicyStore policyStore = new NxPolicyStore();
            policyStore.ResourceManager = ResourcesCache.GetResources();
            ps.Export(policyStore);

            string nxstore = policyStore.XMLRepresentation;
            string emailrules = System.IO.Path.GetTempFileName();
            string emailobjects = System.IO.Path.GetTempFileName();

            CompiledPolicySetExtractor pse = new CompiledPolicySetExtractor(nxstore);
            System.IO.File.WriteAllText(emailrules, pse.GetRules("SMTP"));
            System.IO.File.WriteAllText(emailobjects, pse.GetObjects("SMTP"));

            pse.GetLanguages();
      
            XmlDocument rulesXml = new XmlDocument();
            rulesXml.Load(emailrules);

            string xpath = string.Format("/xBusinessRules/Set");
            XmlNodeList nodes = rulesXml.SelectNodes(xpath);

            Assert.AreEqual("Policies", nodes[0].Attributes["id"].Value);
            Assert.AreEqual("w-C1C0D5EA-5B82-4607-AE41-D52739AA6AB1", nodes[1].Attributes["id"].Value);
            Assert.AreEqual("w-c7d62e10-d889-4ef5-af3d-823c82c230ca", nodes[2].Attributes["id"].Value);
            Assert.AreEqual("w-af6e5d89-0c6f-4b10-9a6c-658d13cd3ea8", nodes[4].Attributes["id"].Value);

            System.IO.File.Delete(emailrules);
            System.IO.File.Delete(emailobjects);
        }
        public void TestExportMultiplePolicies()
        {
            string policyCatalogueFilename = m_testPath + "SamplePolicyCatalogue.xml";
            string policyLanguageFilename = m_testPath + "SampleLanguageBase.xml";
            string policyFilename = m_testPath + "SamplePolicy.xml";

            XmlPolicyLanguageStore languageStore = XmlPolicyLanguageStore.Instance;
            Guid languageId = languageStore.AddLanguage(System.IO.File.ReadAllText(policyLanguageFilename));
            XMLPolicyCatalogueStore catalogueStore = XMLPolicyCatalogueStore.Instance;
            catalogueStore.Reset();

            catalogueStore.AddPolicyCatalogue(System.IO.File.ReadAllText(policyCatalogueFilename));

            XmlStore store = new XmlStore(System.IO.File.ReadAllText(policyFilename));
            IPolicyObjectCollection<IPolicySet> policySet = store.Reader.PolicySets;
            IPolicyObjectCollection<IPolicy> policies = store.Reader.Policies(policySet[0]);
            IPolicy policy = policies[0];
            IPolicySet ps = policySet[0];

            NxPolicyStore policyStore = new NxPolicyStore();
            policyStore.ResourceManager = ResourcesCache.GetResources();
            List<Guid> policiesToExport = new List<Guid>();
            policiesToExport.Add(new Guid("{C7D62E10-D889-4EF5-AF3D-823C82C230CA}"));
            policiesToExport.Add(new Guid("{56C991B8-953D-46AD-B9E9-0CFB08F52C23}"));
            ps.Export(policyStore, policiesToExport);

            string nxstore = policyStore.XMLRepresentation;

            string emailrules = System.IO.Path.GetTempFileName();
            string emailobjects = System.IO.Path.GetTempFileName();
            string language = System.IO.Path.GetTempFileName();

            CompiledPolicySetExtractor pse = new CompiledPolicySetExtractor(nxstore);
            System.IO.File.WriteAllText(emailrules, pse.GetRules("SMTP"));
            System.IO.File.WriteAllText(emailobjects, pse.GetObjects("SMTP"));

            pse.GetLanguages();

   
            XmlDocument rulesXml = new XmlDocument();
            rulesXml.Load(emailrules);

            string xpath = string.Format("/xBusinessRules/Set[@id='Policies']/InvokeSet");
            XmlNodeList nodes = rulesXml.SelectNodes(xpath);

            Assert.AreEqual(1, nodes.Count);
            Assert.AreEqual("w-C1C0D5EA-5B82-4607-AE41-D52739AA6AB1", nodes[0].Attributes["id"].Value);

            xpath = string.Format("/xBusinessRules/Set[@id='w-C1C0D5EA-5B82-4607-AE41-D52739AA6AB1']");
            nodes = rulesXml.SelectNodes(xpath);

            Assert.AreEqual("w-C1C0D5EA-5B82-4607-AE41-D52739AA6AB1", nodes[0].Attributes["id"].Value);

            System.IO.File.Delete(emailrules);
            System.IO.File.Delete(emailobjects);
            System.IO.File.Delete(language);
        }