Пример #1
0
        //Todo: Check the code twice
        //Todo: test the code (by iterating 100.000 times)
        //Todo: why calling the GetFilePath() several times?
        public WSReturnValue SaveSetAnalysisDefinition(object key, object session, object value)
        {
            value = Server.UrlDecode(value.ToString());
            string sessionToSave = string.Empty;
            string keyToSave     = string.Empty;
            bool   useSpecialDir = key.ToString().StartsWith("_");

            bool   checkProceed = false; // Check if we can proceed to write the file
            string fileContent  = string.Empty;

            // If we are dealing with an existing/passed key and a passed session
            // we have to check the session id, which is saved in the first line
            // of the storage file
            if (key != null && !string.IsNullOrEmpty((string)key) && !string.IsNullOrEmpty((string)session))
            {
                string passedKey        = (string)key;
                string existingFilePath = Path.Combine(GetFilePath(useSpecialDir), passedKey + ".txt");
                if (File.Exists(existingFilePath))
                {
                    // Open the file, get the content and check if the session from the
                    // file matches with the passed session key
                    fileContent = ReadFile(existingFilePath);
                    if (fileContent != null)
                    {
                        string savedSession = GetLineFromString(fileContent, 1);
                        if (savedSession == (string)session)
                        {
                            keyToSave     = (string)key;     // Use the existing key
                            sessionToSave = (string)session; // Use the existing session key
                            checkProceed  = true;
                        }
                    }
                }
            }

            // No existing key is passed, so we have a new item
            if (key == null || string.IsNullOrEmpty((string)key))
            {
                sessionToSave = System.Guid.NewGuid().ToString();
                keyToSave     = GetUniqueId(GetFilePath(useSpecialDir));
                checkProceed  = true;
            }



            if (checkProceed)
            {
                string uniqueFileName = keyToSave + ".txt";
                string uniqueFilePath = GetFilePath(useSpecialDir) + uniqueFileName;



                string valueToSave = sessionToSave + "\r\n" + value;

                //Todo: To prevent unnecessary writing we could check the existing content
                // (which is alreay loaded above) with the content which should be saved to the file
                if (fileContent != valueToSave)
                {
                    // Delete the existing file (check have be made before)
                    System.IO.File.Delete(uniqueFilePath);

                    // Write the new file
                    WriteToFile(uniqueFilePath, (string)valueToSave);
                }
            } // (checkProceed)

            WSReturnValue retVal = new WSReturnValue();

            retVal.Session = sessionToSave;
            retVal.Key     = keyToSave;

            return(retVal);
        } // (SaveSetAnalysisDefinition)
        public WSReturnValue SaveSetAnalysisDefinition(object key, object session, object value)
        {
            value = Server.UrlDecode(value.ToString());
            string sessionToSave = string.Empty;
            string keyToSave = string.Empty;
            bool useSpecialDir = key.ToString().StartsWith("_");

            bool checkProceed = false; // Check if we can proceed to write the file
            string fileContent = string.Empty;

            // If we are dealing with an existing/passed key and a passed session
            // we have to check the session id, which is saved in the first line
            // of the storage file
            if (key != null && !string.IsNullOrEmpty((string)key) && !string.IsNullOrEmpty((string)session))
            {
                string passedKey = (string)key;
                string existingFilePath = Path.Combine(GetFilePath(useSpecialDir), passedKey + ".txt");
                if (File.Exists(existingFilePath))
                {
                    // Open the file, get the content and check if the session from the
                    // file matches with the passed session key
                    fileContent = ReadFile(existingFilePath);
                    if (fileContent != null)
                    {
                        string savedSession = GetLineFromString(fileContent, 1);
                        if (savedSession == (string)session)
                        {
                            keyToSave = (string)key; // Use the existing key
                            sessionToSave = (string)session; // Use the existing session key
                            checkProceed = true;
                        }
                    }
                }
            }

            // No existing key is passed, so we have a new item
            if (key == null || string.IsNullOrEmpty((string)key))
            {
                sessionToSave = System.Guid.NewGuid().ToString();
                keyToSave = GetUniqueId(GetFilePath(useSpecialDir));
                checkProceed = true;
            }

            if (checkProceed)
            {
                string uniqueFileName = keyToSave + ".txt";
                string uniqueFilePath = GetFilePath(useSpecialDir) + uniqueFileName;

                string valueToSave = sessionToSave + "\r\n" + value;

                //Todo: To prevent unnecessary writing we could check the existing content
                // (which is alreay loaded above) with the content which should be saved to the file
                if (fileContent != valueToSave)
                {
                    // Delete the existing file (check have be made before)
                    System.IO.File.Delete(uniqueFilePath);

                    // Write the new file
                    WriteToFile(uniqueFilePath, (string)valueToSave);
                }

            } // (checkProceed)

            WSReturnValue retVal = new WSReturnValue();
            retVal.Session = sessionToSave;
            retVal.Key = keyToSave;

            return retVal;
        }