protected void Page_Load(object sender, EventArgs e)
    {
        string typeName = this.Request.QueryString["TypeName"];

        Type type = TypeManager.GetType(typeName);

        DataTypeDescriptor dataTypeDescriptor = DynamicTypeManager.GetDataTypeDescriptor(type);
        XElement           element            = dataTypeDescriptor.ToXml();

        this.DataTypeDescriptorHolder.Controls.Add(new LiteralControl(new XElement("h3", "Xml").ToString()));
        this.DataTypeDescriptorHolder.Controls.Add(new LiteralControl(new XElement("pre", element.ToString()).ToString()));

        this.DataTypeDescriptorHolder.Controls.Add(new LiteralControl(new XElement("h3", "Xml attribute encoded xml").ToString()));
        XElement dummyElement = new XElement("Dummy", new XAttribute("dummy", element.ToString()));
        string   dummyString  = dummyElement.ToString();

        dummyString = dummyString.Remove(0, 13);
        dummyString = dummyString.Remove(dummyString.Length - 3);
        this.DataTypeDescriptorHolder.Controls.Add(new LiteralControl(new XElement("pre", dummyString).ToString()));
    }
Exemple #2
0
        /// <exclude />
        public static void PersistMetaData(DataTypeDescriptor dataTypeDescriptor)
        {
            lock (_lock)
            {
                Initialize();

                string filepath = CreateFilename(dataTypeDescriptor);

                XElement  rootElement = dataTypeDescriptor.ToXml();
                XDocument doc         = new XDocument(rootElement);
                XDocumentUtils.Save(doc, filepath);

                _dataTypeDescriptorCache[dataTypeDescriptor.DataTypeId] = dataTypeDescriptor;

                if ((_dataTypeDescriptorFilesnamesCache.ContainsKey(dataTypeDescriptor.DataTypeId)) &&
                    (_dataTypeDescriptorFilesnamesCache[dataTypeDescriptor.DataTypeId] != filepath))
                {
                    FileUtils.Delete(_dataTypeDescriptorFilesnamesCache[dataTypeDescriptor.DataTypeId]);
                    _dataTypeDescriptorFilesnamesCache[dataTypeDescriptor.DataTypeId] = filepath;
                }
            }
        }
        public string CreatePackagePack()
        {
            using (new DataScope(DataScopeIdentifier.Public))
            {
                #region Init
                if (_packageName == string.Empty)
                {
                    return("");
                }

                _packageDirectoryPath = Path.Combine(_serviceDirectoryPath, _packageName);
                if (Directory.Exists(_packageDirectoryPath))
                {
                    Directory.Delete(_packageDirectoryPath, true);
                }

                var dataPackageDirectoryPath = Path.Combine(_serviceDirectoryPath, _packageName + "_Data");

                _zipDirectoryPath     = Path.Combine(_packageDirectoryPath, "Release");
                _packageDirectoryPath = Path.Combine(_packageDirectoryPath, "Package");

                Directory.CreateDirectory(_packageDirectoryPath);
                Directory.CreateDirectory(_zipDirectoryPath);

                if (Directory.Exists(dataPackageDirectoryPath))
                {
                    FileSystem.CopyDirectory(dataPackageDirectoryPath, _packageDirectoryPath);
                }

                string packageConfigFilePath = Path.Combine(_serviceDirectoryPath, _packageName + ".xml");

                XDocument config = XDocument.Load(packageConfigFilePath);

                var rootConfigElement = config.Element(pc + "PackageCreator");
                if (rootConfigElement == null)
                {
                    throw new InvalidOperationException("Root element of config PackageCreator 2.0 not found");
                }



                XElement blockElement;

                #endregion

                #region Init install.xml
                XDocument XPackage = new XDocument();
                XPackage.Declaration = new XDeclaration("1.0", "utf-8", "yes");

                XElement XPackageInstaller = new XElement(mi + "PackageInstaller");
                XPackageInstaller.Add(new XAttribute(XNamespace.Xmlns + "mi", mi));

                XElement XPackageRequirements = config.Descendants(mi + "PackageRequirements").FirstOrDefault();
                XPackageInstaller.Add(XPackageRequirements);


                XElement XPackageInformation = config.Descendants(mi + "PackageInformation").FirstOrDefault();
                AllowOverwriteDataOnInstall = XPackageInformation.AllowOverwriteAttributeValue();


                XPackageInstaller.Add(XPackageInformation);

                XElement XPackageFragmentInstallerBinaries = new XElement(mi + "PackageFragmentInstallerBinaries");
                try
                {
                    XPackageFragmentInstallerBinaries = config.Descendants(mi + "PackageFragmentInstallerBinaries").First();
                }
                catch
                {
                }

                XPackageFragmentInstallerBinaries.ReplaceNodes(XPackageFragmentInstallerBinaries.Elements().OrderBy(ReferencedAssemblies.AssemblyPosition));
                XPackageInstaller.Add(XPackageFragmentInstallerBinaries);


                XElement XPackageFragmentInstallers = new XElement(mi + "PackageFragmentInstallers");

                #endregion

                #region Init Categories
                foreach (var categoryType in PackageCreatorActionFacade.CategoryTypes)
                {
                    if (categoryType.Value.IsInitable())
                    {
                        foreach (var packageItem in PackageCreatorFacade.GetItems(categoryType.Value, config.Root))
                        {
                            (packageItem as IPackInit).Init(this);
                        }
                    }
                }
                #endregion

                #region XML PageTemplates
                XElement PageTemplates = config.Descendants(pc + "PageTemplates").FirstOrDefault();
                if (PageTemplates != null)
                {
                    foreach (XElement item in PageTemplates.Elements("Add"))
                    {
                        var pageTemplate = (from i in DataFacade.GetData <IXmlPageTemplate>()
                                            where i.Title == item.IndexAttributeValue()
                                            select i).FirstOrDefault();

                        if (pageTemplate != null)
                        {
                            var newPageTemplateFilePath = "\\" + pageTemplate.Title + ".xml";

                            AddFile("App_Data\\PageTemplates" + pageTemplate.PageTemplateFilePath, "App_Data\\PageTemplates" + newPageTemplateFilePath);
                            pageTemplate.PageTemplateFilePath = newPageTemplateFilePath;
                            AddData(pageTemplate);
                        }
                    }
                }

                #endregion

                #region Files
                XElement InstallFiles = config.Descendants(pc + "Files").FirstOrDefault();
                if (InstallFiles != null)
                {
                    foreach (XElement item in InstallFiles.Elements("Add"))
                    {
                        var filename = item.IndexAttributeValue();

                        if (string.IsNullOrEmpty(filename))
                        {
                            throw new InvalidOperationException("Files->Add attribute 'name' must be spesified");
                        }

                        AddFile(filename);
                    }
                }
                #endregion

                #region FilesInDirectory
                XElement InstallFilesInDirectory = config.Descendants(pc + "FilesInDirectories").FirstOrDefault();
                if (InstallFilesInDirectory != null)
                {
                    foreach (XElement item in InstallFilesInDirectory.Elements("Add"))
                    {
                        var path = item.IndexAttributeValue();
                        AddFilesInDirectory(path);
                    }
                }
                #endregion

                #region Directories
                XElement InstallDirectories = config.Descendants(pc + "Directories").FirstOrDefault();
                if (InstallDirectories != null)
                {
                    foreach (XElement item in InstallDirectories.Elements("Add"))
                    {
                        try
                        {
                            var filename = item.IndexAttributeValue();

                            AddDirectory(filename);
                        }
                        catch { }
                    }
                }
                #endregion

                #region Datas
                blockElement = rootConfigElement.Element(pc + "Datas");
                if (blockElement != null)
                {
                    foreach (XElement item in blockElement.Elements("Type"))
                    {
                        var dataTypeName        = item.Attribute("type").Value;
                        var dataScopeIdentifier = item.Element("Data").Attribute("dataScopeIdentifier").Value;
                        AddData(dataTypeName, dataScopeIdentifier);
                    }
                }
                #endregion

                #region DataItems
                blockElement = rootConfigElement.Element(pc + "DataItems");
                if (blockElement != null)
                {
                    foreach (XElement item in blockElement.Elements("Type"))
                    {
                        var dataTypeName = item.Attribute("type").Value;

                        Func <XElement, Func <IData, bool> > trueAttributes = e => e.Attributes().Where(x => x.Name.Namespace == XNamespace.None)
                                                                              .Aggregate(new Func <IData, bool>(d => true), (f, x) => new Func <IData, bool>(d => (d.GetProperty(x.Name.LocalName) == x.Value) && f(d)));
                        Func <IData, bool> where = item.Elements("Add")
                                                   .Aggregate(new Func <IData, bool>(d => false), (f, e) => new Func <IData, bool>(d => trueAttributes(e)(d) || f(d)));

                        AddData(dataTypeName, where);
                    }
                }
                #endregion

#warning TODO: Datatypes data format depends from datatype exists in package or not, thats why list of dtatype must created before adding data

                #region DynamicDataTypes
                XElement DynamicDataTypes = config.Descendants(pc + "DynamicDataTypes").FirstOrDefault();
                XElement DynamicDataTypePackageFragmentInstaller = null;
                if (DynamicDataTypes != null)
                {
                    List <XElement> dataTypes = new List <XElement>();
                    foreach (XElement item in DynamicDataTypes.Elements("Add").OrderBy(e => DataPosition(e.IndexAttributeValue())))
                    {
                        var dynamicDataType     = TypeManager.GetType(item.IndexAttributeValue());
                        var dynamicDataTypeName = TypeManager.SerializeType(dynamicDataType);

                        AddData <IGeneratedTypeWhiteList>(d => d.TypeManagerTypeName == dynamicDataTypeName);

                        installDataTypeNamesList.Add(dynamicDataTypeName);
                        var type = TypeManager.GetType(item.IndexAttributeValue());
                        DataTypeDescriptor dataTypeDescriptor = DynamicTypeManager.GetDataTypeDescriptor(type);
                        dataTypes.Add(new XElement("Type",
                                                   new XAttribute("providerName", "GeneratedDataTypesElementProvider"),
                                                   new XAttribute("dataTypeDescriptor", dataTypeDescriptor.ToXml().ToString()),
                                                   new XAttribute("allowOverwrite", AllowOverwriteDataOnInstall))
                                      );

                        AddFileIfExists("App_Data\\Composite\\DynamicTypeForms\\" + item.IndexAttributeValue().Replace('.', '\\') + ".xml");
                    }


                    DynamicDataTypePackageFragmentInstaller = new XElement(mi + "Add",
                                                                           new XAttribute("installerType", "Composite.Core.PackageSystem.PackageFragmentInstallers.DynamicDataTypePackageFragmentInstaller, Composite"),
                                                                           new XAttribute("uninstallerType", "Composite.Core.PackageSystem.PackageFragmentInstallers.DynamicDataTypePackageFragmentUninstaller, Composite"),
                                                                           new XElement("Types",
                                                                                        dataTypes
                                                                                        )
                                                                           );
                }

                #endregion


                //Add only one MetaTypeTab
                using (new GuidReplacer(from cd in DataFacade.GetData <ICompositionContainer>()
                                        select new KeyValuePair <Guid, Guid>(cd.GetProperty <Guid>("Id"), GuidReplacer.CompositionContainerGuid)
                                        ))
                {
                    foreach (var categoryType in PackageCreatorActionFacade.CategoryTypes)
                    {
                        if (categoryType.Value.IsPackagable())
                        {
                            foreach (var packageItem in PackageCreatorFacade.GetItems(categoryType.Value, config.Root))
                            {
                                (packageItem as IPack).Pack(this);
                            }
                        }
                    }

                    #region DynamicDataTypesData

                    XElement element = config.Descendants(pc + "DynamicDataTypesData").FirstOrDefault();

                    if (element != null)
                    {
                        foreach (XElement item in element.Elements("Add"))
                        {
                            var indexAttrValue = item.IndexAttributeValue();
                            AddDataTypeData(TypeManager.TryGetType(indexAttrValue));
                        }
                    }
                }
                #endregion

                #region TransformationPackageFragmentInstallers

                XElement ConfigurationTransformationPackageFragmentInstallers = null;

                var sources = new HashSet <string>();
                sources.UnionWith(ConfigurationXPaths.Keys);
                sources.UnionWith(ConfigurationTemplates.Keys);

                foreach (var source in sources)
                {
                    XDocument installXsl   = XDocument.Parse(blankXsl);
                    XDocument uninstallXsl = XDocument.Parse(blankXsl);


                    HashSet <string> xpaths;
                    if (ConfigurationXPaths.TryGetValue(source, out xpaths))
                    {
                        var nodes = new Dictionary <string, Dictionary <string, XElement> >();
                        foreach (string xpath in xpaths)
                        {
                            var   configuration = PackageCreatorFacade.GetConfigurationDocument(source);
                            var   element       = configuration.XPathSelectElement(xpath);
                            Regex re            = new Regex(@"^(.*?)/([^/]*(\[[^\]]*\])?)$");
                            Match match         = re.Match(xpath);
                            if (match.Success)
                            {
                                var itemPath = match.Groups[1].Value;
                                var itemKey  = match.Groups[2].Value;

                                nodes.Add(itemPath, itemKey, element);

                                uninstallXsl.Root.Add(
                                    new XElement(xsl + "template",
                                                 new XAttribute("match", xpath)
                                                 )
                                    );
                            }
                        }
                        foreach (var node in nodes)
                        {
                            installXsl.Root.Add(
                                new XElement(xsl + "template",
                                             new XAttribute("match", node.Key),
                                             new XElement(xsl + "copy",
                                                          new XElement(xsl + "apply-templates",
                                                                       new XAttribute("select", "@* | node()")),
                                                          from e in node.Value
                                                          select
                                                          new XElement(xsl + "if",
                                                                       new XAttribute("test", string.Format("count({0})=0", e.Key)),
                                                                       e.Value
                                                                       )
                                                          )
                                             )
                                );
                        }
                    }

                    List <XElement> templates;
                    if (ConfigurationTemplates.TryGetValue(source, out templates))
                    {
                        installXsl.Root.Add(templates);
                    }

                    var configDirectory = Path.Combine(_packageDirectoryPath, source);
                    if (!Directory.Exists(configDirectory))
                    {
                        Directory.CreateDirectory(configDirectory);
                    }
                    installXsl.SaveTabbed(Path.Combine(configDirectory, configInstallFileName));
                    uninstallXsl.SaveTabbed(Path.Combine(configDirectory, configUninstallFileName));

                    if (PCCompositeConfig.Source == source)
                    {
                        ConfigurationTransformationPackageFragmentInstallers =
                            new XElement(mi + "Add",
                                         new XAttribute("installerType", "Composite.Core.PackageSystem.PackageFragmentInstallers.ConfigurationTransformationPackageFragmentInstaller, Composite"),
                                         new XAttribute("uninstallerType", "Composite.Core.PackageSystem.PackageFragmentInstallers.ConfigurationTransformationPackageFragmentUninstaller, Composite"),
                                         new XElement("Install",
                                                      new XAttribute("xsltFilePath", string.Format(@"~\{0}\{1}", source, configInstallFileName))
                                                      ),
                                         new XElement("Uninstall",
                                                      new XAttribute("xsltFilePath", string.Format(@"~\{0}\{1}", source, configUninstallFileName))
                                                      )
                                         );
                    }
                    else
                    {
                        XslFiles.Add(
                            new XElement("XslFile",
                                         new XAttribute("pathXml", PackageCreatorFacade.GetConfigurationPath(source)),
                                         new XAttribute("installXsl", string.Format(@"~\{0}\{1}", source, configInstallFileName)),
                                         new XAttribute("uninstallXsl", string.Format(@"~\{0}\{1}", source, configUninstallFileName))
                                         )
                            );
                    }
                }
                #endregion



                XElement XPackageFragments = config.Descendants(pc + "PackageFragmentInstallers").FirstOrDefault();
                if (XPackageFragments != null)
                {
                    XPackageFragmentInstallers.Add(XPackageFragments.Elements());
                }


                XPackageFragmentInstallers.Add(ConfigurationTransformationPackageFragmentInstallers);

                #region FilePackageFragmentInstaller
                if (Files.Count + Directories.Count > 0)
                {
                    XElement FilePackageFragmentInstaller = new XElement(mi + "Add",
                                                                         new XAttribute("installerType", "Composite.Core.PackageSystem.PackageFragmentInstallers.FilePackageFragmentInstaller, Composite"),
                                                                         new XAttribute("uninstallerType", "Composite.Core.PackageSystem.PackageFragmentInstallers.FilePackageFragmentUninstaller, Composite"),
                                                                         Files.Count == 0 ? null : new XElement("Files"
                                                                                                                , Files.OrderBy(d => ReferencedAssemblies.AssemblyPosition(d))
                                                                                                                ),
                                                                         Directories.Count == 0 ? null : new XElement("Directories"
                                                                                                                      , Directories
                                                                                                                      )
                                                                         );
                    XPackageFragmentInstallers.Add(FilePackageFragmentInstaller);
                }
                #endregion


                #region FileXslTransformationPackageFragmentInstaller
                if (XslFiles.Count > 0)
                {
                    XPackageFragmentInstallers.Add(new XElement(mi + "Add",
                                                                new XAttribute("installerType", "Composite.Core.PackageSystem.PackageFragmentInstallers.FileXslTransformationPackageFragmentInstaller, Composite"),
                                                                new XAttribute("uninstallerType", "Composite.Core.PackageSystem.PackageFragmentInstallers.FileXslTransformationPackageFragmentUninstaller, Composite"),
                                                                new XElement("XslFiles"
                                                                             , XslFiles
                                                                             )
                                                                )
                                                   );
                }
                #endregion

                XPackageFragmentInstallers.Add(DynamicDataTypePackageFragmentInstaller);

                #region DataPackageFragmentInstaller
                if (Datas.Count > 0)
                {
                    var DataPackageFragmentInstaller = new XElement(mi + "Add",
                                                                    new XAttribute("installerType", "Composite.Core.PackageSystem.PackageFragmentInstallers.DataPackageFragmentInstaller, Composite"),
                                                                    new XAttribute("uninstallerType", "Composite.Core.PackageSystem.PackageFragmentInstallers.DataPackageFragmentUninstaller, Composite"),
                                                                    new XElement("Types",
                                                                                 from t in Datas
                                                                                 orderby t.Key
                                                                                 orderby DataPosition(t.Value.AttributeValue("type"))
                                                                                 select t.Value
                                                                                 )
                                                                    );
                    XPackageFragmentInstallers.Add(DataPackageFragmentInstaller);
                }
                #endregion

                DataFiles.Save();

                XPackageFragmentInstallers.ReplaceNodes(XPackageFragmentInstallers.Elements().OrderBy(FragmentPosition).Select(DeleteOrderingMark));
                XPackageInstaller.Add(XPackageFragmentInstallers);

                XPackage.Add(XPackageInstaller);

                XPackage.SaveTabbed(Path.Combine(_packageDirectoryPath, InstallFilename));

                var zipFilename = _packageName + ".zip";


                #region Zipping
                var filenames   = Directory.GetFiles(_packageDirectoryPath, "*", SearchOption.AllDirectories);
                var directories = Directory.GetDirectories(_packageDirectoryPath, "*", SearchOption.AllDirectories);


                using (var archiveFileStream = File.Create(Path.Combine(_zipDirectoryPath, zipFilename)))
                    using (var archive = new ZipArchive(archiveFileStream, ZipArchiveMode.Create))
                    {
                        foreach (string directory in directories)
                        {
                            string directoryEntryName = directory.Replace(_packageDirectoryPath + "\\", "").Replace("\\", "/") + "/";

                            var dirEntry = archive.CreateEntry(directoryEntryName);
                            dirEntry.LastWriteTime = Directory.GetLastWriteTime(directory);
                        }

                        foreach (string file in filenames)
                        {
                            string fileEntryName = file.Replace(_packageDirectoryPath + "\\", "").Replace("\\", "/");

                            var entry = archive.CreateEntry(fileEntryName, PackageCompressionLevel);

                            using (var fileStream = File.OpenRead(file))
                                using (var entryStream = entry.Open())
                                {
                                    fileStream.CopyTo(entryStream);
                                }
                        }
                    }
                #endregion

                var packageCreatorDirectory = Path.Combine(_packageDirectoryPath, "../_" + CREATOR_DRECTORY);
                try
                {
                    Directory.CreateDirectory(packageCreatorDirectory);

                    var packageCopyFilePath = packageCreatorDirectory + @"\" + _packageName + ".xml";
                    File.Copy(packageConfigFilePath, packageCopyFilePath);
                    SetFileReadAccess(packageCopyFilePath, false);
                }
                catch { }

                try
                {
                    if (Directory.Exists(dataPackageDirectoryPath))
                    {
                        FileSystem.CopyDirectory(dataPackageDirectoryPath, packageCreatorDirectory + @"\" + _packageName + "_Data");
                    }
                }
                catch { }

                return(Path.Combine(_packageName, "Release", zipFilename));
            }
        }