BExISMigration.MetadataCreator.getPublicationsMetadataXml C# (CSharp) Method

getPublicationsMetadataXml() public method

public getPublicationsMetadataXml ( string DataBase ) : BexisPublication[]
DataBase string
return BexisPublication[]
        public BexisPublication[] getPublicationsMetadataXml(string DataBase)
        {
            var bexisPublications = new List<BexisPublication>();
            var docs = new List<XmlDocument>();
            var doc = new XmlDocument();
            string mySelectQuery = "SELECT ID,DATA  FROM \"EXPLORER\".\"PUBLICATIONLIST\"";
            DB2Connection connect = new DB2Connection(DataBase);
            DB2Command myCommand = new DB2Command(mySelectQuery, connect);
            connect.Open();
            DB2DataReader myReader = myCommand.ExecuteReader();
            while (myReader.Read())
            {
                doc.LoadXml(myReader.GetString(1));
                var bp = new BexisPublication();
                bp.Id = (int)(myReader.GetValue(0));
                bp.MetaDataXml = doc;
                var fileCommand = new DB2Command("SELECT FILENAME, MIMETYPE ,FILE  FROM \"EXPLORER\".\"PUBLICATIONFILES\" where PUBID=" + bp.Id, connect);
                var fileReader = fileCommand.ExecuteReader();
                string dataPath = AppConfiguration.DataPath;
                string storepath = Path.Combine(dataPath, "Temp", "Administrator");
                // if folder not exist
                if (!Directory.Exists(storepath))
                    Directory.CreateDirectory(storepath);
                var index = 1;
                if (fileReader.Read())
                {
                    PublicationContentDescriptor pubContent = new PublicationContentDescriptor()
                    {
                        OrderNo = index++,
                        Name = fileReader.GetString(0),
                        MimeType = fileReader.GetString(1)
                    };
                    var filePath = Path.Combine(storepath, Guid.NewGuid().ToString() + Path.GetExtension(pubContent.Name));
                    File.WriteAllBytes(filePath, ((Byte[])fileReader.GetValue(2)));
                    pubContent.URI = filePath;
                    bp.PublicationContentDescriptors.Add(pubContent);
                }
                fileReader.Close();
                bexisPublications.Add(bp);
            }
            myReader.Close();
            connect.Close();
            return bexisPublications.ToArray();
        }

Usage Example

Example #1
0
        public string TransferPublications()
        {
            string xsdPath = Path.Combine(AppConfiguration.DataPath, "Temp", "Administrator");
            PublicationManager pms = new PublicationManager();
            MetadataCreator metadataCreator = new MetadataCreator();
            //Import MetaDataStructure
            string schemaFile = xsdPath + @"\publication.xsd";
            //open schema
            XmlSchemaManager xmlSchemaManager = new XmlSchemaManager();
            string userName = "******";
            xmlSchemaManager.Load(schemaFile, userName);
            long metadataStructureId = 0;
            try
            {
                metadataStructureId = metadataCreator.importMetadataStructure(schemaFile, userName, schemaFile, "Publication", "Metadata/publicationDetails/publicationDetails/title/title", "Metadata/publicationDetails/publicationDetails/information/information");// xmlSchemaManager.GenerateMetadataStructure("publication", "publication");
            }
            catch (Exception ex)
            {
                xmlSchemaManager.Delete("publication");
                return "Couldn't create publication metadatastructure!";
            }
            //Create an empty publication
            MetadataStructureManager msm = new MetadataStructureManager();
            MetadataStructure metadataStructure = msm.Repo.Get(metadataStructureId);
            XmlDocument metadataXmlTemplate = BExIS.Xml.Helpers.XmlMetadataWriter.ToXmlDocument(metadataCreator.createXmlTemplate(metadataStructureId));
            var publicationsMetaData = metadataCreator.getPublicationsMetadataXml(DataBase);
            var xmlMapperManager = new XmlMapperManager();
            string filePath = AppConfiguration.GetModuleWorkspacePath("BMM");
            string path_mappingFile = filePath + @"\bexis_publication_metadata_mapping.xml";
            foreach (var publicationMetaData in publicationsMetaData)
            {
                var pc = pms.CreateEmptyPublication(metadataStructure);
                var publicationId = pc.Id;
                if (pms.IsPublicationCheckedOutFor(publicationId, userName) || pms.CheckOutPublication(publicationId, userName))
                {

                    PublicationVersion workingCopy = pms.GetPublicationWorkingCopy(publicationId);
                    // XML mapper + mapping file
                    xmlMapperManager.Load(path_mappingFile, "Publication");
                    XmlDocument metadataBpp = xmlMapperManager.Generate(publicationMetaData.MetaDataXml, 99);
                    metadataBpp = metadataCreator.fillInXmlAttributes(metadataBpp, metadataXmlTemplate);
                    workingCopy.Metadata = metadataBpp;

                    foreach (var pbContent in publicationMetaData.PublicationContentDescriptors)
                    {
                        string storePath = Path.Combine(AppConfiguration.DataPath, "Publications", publicationId.ToString());
                        storePath = Path.Combine(storePath, publicationId.ToString() + "_" + workingCopy.VersionNo.ToString() + "_" + pbContent.Name);
                        File.Move(pbContent.URI, storePath);
                        pbContent.URI = storePath;
                        pbContent.PublicationVersion = workingCopy;
                        workingCopy.PublicationContentDescriptors.Add(pbContent);
                    }
                    pms.CheckInPublication(publicationId, "Metadata was submited.", userName);
                }
            }
            return "";
        }