Beispiel #1
0
        public static DataSet LoadObjects(SiteImportSettings settings, string objectType, bool siteObjects, bool selectionOnly = false, bool forceXMLStructure = false)
        {
            DataSet ds;

            var e = new ImportGetDataEventArgs
            {
                Settings      = settings,
                ObjectType    = objectType,
                SiteObjects   = siteObjects,
                SelectionOnly = selectionOnly
            };

            // Get empty data set
            GeneralizedInfo infoObj;

            ds = GetEmptyDataSet(settings, objectType, siteObjects, selectionOnly, out infoObj, forceXMLStructure);

            // Turn off constrains check
            ds.EnforceConstraints = false;

            // Raise prepare data event
            var eData = new ImportGetDataEventArgs
            {
                Data          = ds,
                Settings      = settings,
                ObjectType    = objectType,
                SiteObjects   = siteObjects,
                SelectionOnly = selectionOnly
            };

            // Handle the event
            SpecialActionsEvents.PrepareDataStructure.StartEvent(eData);

            // Lowercase table names for compatibility
            DataHelper.LowerCaseTableNames(ds);


            Stream    reader = null;
            XmlReader xml    = null;

            string safeObjectType = ImportExportHelper.GetSafeObjectTypeName(objectType);

            // Prepare the path
            string filePath = DirectoryHelper.CombinePath(settings.TemporaryFilesPath, ImportExportHelper.DATA_FOLDER) + "\\";

            // Get object type subfolder
            filePath += ImportExportHelper.GetObjectTypeSubFolder(settings, objectType, siteObjects);
            filePath += safeObjectType + ".xml";
            string rootElement = safeObjectType;

            filePath = ImportExportHelper.GetExportFilePath(filePath);

            // Import only if file exists
            if (System.IO.File.Exists(filePath))
            {
                // Reader setting
                XmlReaderSettings rs = new XmlReaderSettings();
                rs.CloseInput      = true;
                rs.CheckCharacters = false;

                // Open reader
                reader = FileStream.New(filePath, CMS.IO.FileMode.Open, CMS.IO.FileAccess.Read, CMS.IO.FileShare.Read, 8192);
                xml    = XmlReader.Create(reader, rs);

                // Read main document element
                do
                {
                    xml.Read();
                } while ((xml.NodeType != XmlNodeType.Element) && !xml.EOF);

                if (xml.Name.ToLowerInvariant() != rootElement.ToLowerInvariant())
                {
                    throw new Exception("[ImportProvider.LoadObjects]: The required page element is '" + safeObjectType + "', element found is '" + xml.Name + "'.");
                }

                // Get version
                if (xml.HasAttributes)
                {
                    xml.MoveToAttribute("version");
                }

                if ((xml.NodeType != XmlNodeType.Attribute) || (xml.Name.ToLowerInvariant() != "version"))
                {
                    throw new Exception("[ImportProvider.LoadObjects]: Cannot find version attribute.");
                }


                // Get the dataset
                do
                {
                    xml.Read();
                } while (((xml.NodeType != XmlNodeType.Element) || (xml.Name != "NewDataSet")) && !xml.EOF);

                // Read data
                if (xml.Name == "NewDataSet")
                {
                    ds.ReadXml(xml);
                }

                // Filter data by object type condition for selection, some object types may overlap
                if (selectionOnly && !DataHelper.DataSourceIsEmpty(ds))
                {
                    // Remove unwanted rows from data collection with dependence on type condition
                    if (infoObj.TypeInfo.TypeCondition != null)
                    {
                        var dt = ObjectHelper.GetTable(ds, infoObj);
                        var where = infoObj.TypeInfo.WhereCondition;
                        where.Replace(" N'", " '").Replace("(N'", "('");
                        DataHelper.KeepOnlyRows(dt, where);

                        dt.AcceptChanges();
                    }
                }
            }


            // Safely close readers
            if (xml != null)
            {
                xml.Close();
            }

            if (reader != null)
            {
                reader.Close();
            }

            return(ds);
        }