/// <summary>
        /// Convert a list of WebFactorTreeNode instances
        /// to a list of IFactorTreeNode instances.
        /// </summary>
        /// <param name="userContext">
        /// Information about the user that makes this method call.
        /// </param>
        /// <param name="webFactorTrees">A list of WebFactorTreeNode instances.</param>
        /// <returns>A list of IFactorTreeNode instances.</returns>
        private FactorTreeNodeList GetFactorTrees(IUserContext userContext,
                                                  List <WebFactorTreeNode> webFactorTrees)
        {
            FactorDataTypeList   factorDataTypes;
            FactorOriginList     factorOrigins;
            FactorTreeNodeList   factorTrees;
            FactorUpdateModeList factorUpdateModes;
            Hashtable            factorTreeNodes;

            factorDataTypes   = CoreData.FactorManager.GetFactorDataTypes(userContext);
            factorOrigins     = CoreData.FactorManager.GetFactorOrigins(userContext);
            factorUpdateModes = CoreData.FactorManager.GetFactorUpdateModes(userContext);
            factorTrees       = new FactorTreeNodeList();
            factorTreeNodes   = new Hashtable();
            if (webFactorTrees.IsNotEmpty())
            {
                foreach (WebFactorTreeNode webFactorTree in webFactorTrees)
                {
                    factorTrees.Add(GetFactorTree(userContext, webFactorTree, factorTreeNodes, factorDataTypes, factorOrigins, factorUpdateModes));
                }
            }

            if (factorTrees.IsNotEmpty())
            {
                factorTrees.Sort();
            }

            return(factorTrees);
        }
        /// <summary>
        /// Save UserDataSet to xml file.
        /// </summary>
        /// <param name='fileName'>File name.</param>
        /// <param name='userDataSet'>UserDataSet to save.</param>
        /// <param name='dictionaryURI'>Path.</param>
        /// <param name='renewCommonDefinitions'>Indicates if common definitions should be updated.</param>
        public static void Serialize(String fileName, UserDataSet userDataSet, String dictionaryURI, Boolean renewCommonDefinitions)
        {
            BinaryFormatter formatter = new BinaryFormatter();

            Stream stream = File.OpenWrite(fileName);

            formatter.Serialize(stream, userDataSet);
            stream.Close();

            if (renewCommonDefinitions)
            {
                String factorFileName = dictionaryURI + @"\Factors.bin";
                stream = File.OpenWrite(factorFileName);
                FactorList allFactors = FactorManager.GetFactors();
                formatter.Serialize(stream, allFactors);
                stream.Close();

                FactorTreeSearchCriteria searchCriteria = new FactorTreeSearchCriteria();
                FactorTreeNodeList       factorTrees    = new FactorTreeNodeList();

                foreach (Factor factor in allFactors)
                {
                    List <Int32> factorIds = new List <Int32>();
                    factorIds.Add(factor.Id);
                    searchCriteria.RestrictSearchToFactorIds = factorIds;
                }


                String factorTreeFileName = dictionaryURI + @"\FactorTrees.bin";
                stream = File.OpenWrite(factorTreeFileName);
                formatter.Serialize(stream, factorTrees);
                stream.Close();

                String speciesFactQualityFileName = dictionaryURI + @"\SpeciesFactQualities.bin";
                stream = File.OpenWrite(speciesFactQualityFileName);
                formatter.Serialize(stream, SpeciesFactManager.GetSpeciesFactQualities());
                stream.Close();

                String periodsFileName = dictionaryURI + @"\Periods.bin";
                stream = File.OpenWrite(periodsFileName);
                formatter.Serialize(stream, PeriodManager.GetPeriods());
                stream.Close();
            }
        }
        /// <summary>
        /// Read UserDataSet from xml file.
        /// </summary>
        /// <param name='fileName'>File name.</param>
        /// <param name='dictionaryURI'>Path.</param>
        /// <returns>The read UserDataSet.</returns>
        public static UserDataSet Deserialize(String fileName, String dictionaryURI)
        {
            Stream          stream      = File.OpenRead(fileName);
            BinaryFormatter formatter   = new BinaryFormatter();
            UserDataSet     userDataSet = (UserDataSet)formatter.Deserialize(stream);

            stream.Close();


            stream = File.OpenRead(dictionaryURI + @"\Factors.bin");
            FactorList allFactors = ((FactorList)formatter.Deserialize(stream));

            stream.Close();

            stream = File.OpenRead(dictionaryURI + @"\FactorTrees.bin");
            FactorTreeNodeList factorTrees = ((FactorTreeNodeList)formatter.Deserialize(stream));

            stream.Close();

            stream = File.OpenRead(dictionaryURI + @"\SpeciesFactQualities.bin");
            SpeciesFactQualityList speciesFactQualities = ((SpeciesFactQualityList)formatter.Deserialize(stream));

            stream.Close();

            stream = File.OpenRead(dictionaryURI + @"\Periods.bin");
            PeriodList periods = ((PeriodList)formatter.Deserialize(stream));

            stream.Close();

            PeriodManager.InitialisePeriods(periods);
            SpeciesFactManager.InitialiseSpeciesFactQualities(speciesFactQualities);
            FactorManager.InitialiseAllFactors(allFactors, factorTrees);
            SpeciesFactManager.InitAutomatedCalculations(userDataSet.SpeciesFacts);

            return(userDataSet);
        }