/// <summary>
 /// Saves a MySettings file to disk.
 /// </summary>
 /// <param name="filePath">The file path.</param>
 /// <param name="mySettings">The MySettings object.</param>
 private static void SaveFileToDisk(string filePath, MySettings mySettings)
 {
     EnsureFolder(filePath);
     using (var writer = new FileStream(filePath, FileMode.Create))
     {
         DataContractSerializer ser = new DataContractSerializer(typeof(MySettings));
         ser.WriteObject(writer, mySettings);
     }
 }
        /// <summary>
        /// Saves a MySettings file to disk in GZip format.
        /// </summary>
        /// <param name="filePath">The file path.</param>
        /// <param name="mySettings">The MySettings object.</param>
        private static void SaveGZipFileToDisk(string filePath, MySettings mySettings)
        {
            EnsureFolder(filePath);

            using (MemoryStream objectStream = new MemoryStream())
            {
                DataContractSerializer ser = new DataContractSerializer(typeof(MySettings));
                ser.WriteObject(objectStream, mySettings);
                using (FileStream compressedFileStream = File.Create(filePath))
                {
                    using (GZipStream compressionStream = new GZipStream(compressedFileStream, CompressionMode.Compress))
                    {
                        compressionStream.Write(objectStream.ToArray(), 0, (int)objectStream.Length);
                    }
                }
            }
        }
        /// <summary>
        /// Saves a MySettings file to disk.
        /// The file will be stored in separate folder unique for the user.
        /// </summary>
        /// <param name="userContext">The user context.</param>
        /// <param name="settingsName">The settings name.</param>
        /// <param name="mySettings">The MySettings object.</param>
        public static void SaveToDisk(IUserContext userContext, string settingsName, MySettings mySettings)
        {
            if (string.IsNullOrEmpty(settingsName))
            {
                throw new Exception("Name is empty");
            }

            if (mySettings.IsNull())
            {
                throw new Exception("MySettings object is null");
            }

            if (userContext.IsNull() || userContext.User.IsNull() || string.IsNullOrEmpty(userContext.User.UserName))
            {
                throw new Exception("User object is not initialized");
            }

            string fileName = GetFilePath(settingsName, userContext.User.UserName);

            SaveGZipFileToDisk(fileName, mySettings);
            //SaveFileToDisk(fileName, mySettings);
        }
        public void Test_ResultCacheNeedsRefresh()
        {
            AnalysisPortal.MySettings.MySettings mySettings = new ArtDatabanken.WebApplication.AnalysisPortal.MySettings.MySettings();
            mySettings.ResultCacheNeedsRefresh = true;
            Assert.IsTrue(mySettings.ResultCacheNeedsRefresh);
            Assert.IsTrue(mySettings.Filter.ResultCacheNeedsRefresh);
            Assert.IsTrue(mySettings.Filter.Taxa.ResultCacheNeedsRefresh);

            mySettings.ResultCacheNeedsRefresh = false;
            Assert.IsFalse(mySettings.ResultCacheNeedsRefresh);
            Assert.IsFalse(mySettings.Filter.ResultCacheNeedsRefresh);
            Assert.IsFalse(mySettings.Filter.Taxa.ResultCacheNeedsRefresh);

            mySettings.Filter.Taxa.TaxonIds.Add(25);
            Assert.IsTrue(mySettings.ResultCacheNeedsRefresh);
            Assert.IsTrue(mySettings.Filter.ResultCacheNeedsRefresh);
            Assert.IsTrue(mySettings.Filter.Taxa.ResultCacheNeedsRefresh);

            mySettings.ResultCacheNeedsRefresh = false;
            Assert.IsFalse(mySettings.ResultCacheNeedsRefresh);
            Assert.IsFalse(mySettings.Filter.ResultCacheNeedsRefresh);
            Assert.IsFalse(mySettings.Filter.Taxa.ResultCacheNeedsRefresh);
        }
        /// <summary>
        /// Saves a MySettings file to disk.
        /// </summary>
        /// <param name="settingsName">This will be the name of the settings file.</param>
        /// <param name="mySettings">MySettings object.</param>
        public static void SaveToDisk(string settingsName, MySettings mySettings)
        {
            if (string.IsNullOrEmpty(settingsName))
            {
                throw new Exception("name is empty");
            }

            if (mySettings.IsNull())
            {
                throw new Exception("MySettings object is null");
            }

            try
            {
                string fileName = GetFileName(settingsName);
                //SaveFileToDisk(fileName, mySettings);
                SaveGZipFileToDisk(fileName, mySettings);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 6
0
        public void SerializeAndDeserializeProtectedSettings()
        {
            // When this test is run a file called test.dat will be saved to
            // c:\InetPub\wwwRoot\AnalysisPortalTestRun\temp\settings
            using (ShimsContext.Create())
            {
                string fullPath = @"C:\InetPub\wwwRoot\AnalysisPortalTestRun\temp\settings\myTempFile.txt";
                ShimHostingEnvironment.MapPathString = (path) => { return(fullPath); };


                var mySettings = new ArtDatabanken.WebApplication.AnalysisPortal.MySettings.MySettings();
                mySettings.Filter.Taxa.TaxonIds.Add(1);
                mySettings.Filter.Taxa.TaxonIds.Add(2);
                mySettings.Filter.Taxa.TaxonIds.Add(5);

                int wfsLayersCount = mySettings.DataProvider.MapLayers.WfsLayers.Count;
                var layer          = mySettings.DataProvider.MapLayers.AddWfsLayer(new WfsLayerSetting()
                {
                    Name         = "Test",
                    GeometryType = GeometryType.Polygon,
                    Color        = "#336699",
                    Filter       = "",
                    ServerUrl    = "http://slwgeo.artdata.slu.se:8080/geoserver/SLW/wfs",
                    TypeName     = "SLW:MapOfSwedishCounties"
                });


                MySettingsManager.SaveToDisk("test", mySettings);
                var mySettings2 = MySettingsManager.LoadFromDisk("test");

                Assert.IsNotNull(mySettings2);
                Assert.IsTrue(mySettings2.Filter.Taxa.TaxonIds.Contains(1));
                Assert.IsTrue(mySettings2.Filter.Taxa.TaxonIds.Contains(2));
                Assert.IsTrue(mySettings2.Filter.Taxa.TaxonIds.Contains(5));
                Assert.AreEqual(wfsLayersCount + 1, mySettings.DataProvider.MapLayers.WfsLayers.Count);
            }
        }
        //public static MemoryStream Serialize(MySettings mySettings)
        //{
        //    var memoryStream = new MemoryStream();
        //    var ser = new DataContractSerializer(typeof(MySettings));
        //    ser.WriteObject(memoryStream, mySettings);
        //    return memoryStream;
        //}

        public static void SaveLastSettings(IUserContext userContext, MySettings mySettings)
        {
            SaveToDisk(userContext, LastSettingsFileName, mySettings);
        }