Beispiel #1
0
        //ToDO -> David <- do not store the files on the server
        public string Convert(long datasetVersionId)
        {
            string            datarepo          = _dataRepo.Name;
            SubmissionManager publishingManager = new SubmissionManager();

            using (DatasetManager datasetManager = new DatasetManager())
                using (DataStructureManager dataStructureManager = new DataStructureManager())
                    using (PublicationManager publicationManager = new PublicationManager())
                        using (ZipFile zip = new ZipFile())
                        {
                            DatasetVersion datasetVersion = datasetManager.GetDatasetVersion(datasetVersionId);
                            long           datasetId      = datasetVersion.Dataset.Id;

                            Publication publication =
                                publicationManager.GetPublication()
                                .Where(
                                    p =>
                                    p.DatasetVersion.Id.Equals(datasetVersion.Id) &&
                                    p.Broker.Name.ToLower().Equals(_broker.Name))
                                .FirstOrDefault();

                            // if(broker exist)
                            if (publication == null && _broker != null)
                            {
                                Broker broker = _broker;

                                if (broker != null)
                                {
                                    OutputMetadataManager.GetConvertedMetadata(datasetId, TransmissionType.mappingFileExport,
                                                                               broker.MetadataFormat);

                                    // get primary data
                                    // check the data sturcture type ...
                                    if (datasetVersion.Dataset.DataStructure.Self is StructuredDataStructure)
                                    {
                                        OutputDataManager odm = new OutputDataManager();
                                        // apply selection and projection

                                        odm.GenerateAsciiFile(datasetId, datasetVersion.Id, broker.PrimaryDataFormat, false);
                                    }

                                    int versionNr = datasetManager.GetDatasetVersionNr(datasetVersion);

                                    string zipName         = publishingManager.GetZipFileName(datasetId, versionNr);
                                    string zipPath         = publishingManager.GetDirectoryPath(datasetId, broker.Name);
                                    string dynamicZipPath  = publishingManager.GetDynamicDirectoryPath(datasetId, broker.Name);
                                    string zipFilePath     = Path.Combine(zipPath, zipName);
                                    string dynamicFilePath = Path.Combine(dynamicZipPath, zipName);

                                    FileHelper.CreateDicrectoriesIfNotExist(Path.GetDirectoryName(zipFilePath));

                                    if (FileHelper.FileExist(zipFilePath))
                                    {
                                        if (FileHelper.WaitForFile(zipFilePath))
                                        {
                                            FileHelper.Delete(zipFilePath);
                                        }
                                    }

                                    // add datastructure
                                    //ToDo put that functiom to the outputDatatructureManager

                                    #region datatructure

                                    long          dataStructureId = datasetVersion.Dataset.DataStructure.Id;
                                    DataStructure dataStructure   = dataStructureManager.StructuredDataStructureRepo.Get(dataStructureId);

                                    if (dataStructure != null)
                                    {
                                        try
                                        {
                                            string dynamicPathOfDS = "";
                                            dynamicPathOfDS = storeGeneratedFilePathToContentDiscriptor(datasetId, datasetVersion,
                                                                                                        "datastructure", ".txt");
                                            string datastructureFilePath = AsciiWriter.CreateFile(dynamicPathOfDS);

                                            string json = OutputDataStructureManager.GetVariableListAsJson(dataStructureId);

                                            AsciiWriter.AllTextToFile(datastructureFilePath, json);
                                        }
                                        catch (Exception ex)
                                        {
                                            throw ex;
                                        }
                                    }

                                    #endregion datatructure

                                    foreach (ContentDescriptor cd in datasetVersion.ContentDescriptors)
                                    {
                                        string path = Path.Combine(AppConfiguration.DataPath, cd.URI);
                                        string name = cd.URI.Split('\\').Last();

                                        if (FileHelper.FileExist(path))
                                        {
                                            zip.AddFile(path, "");
                                        }
                                    }

                                    // add xsd of the metadata schema
                                    string xsdDirectoryPath = OutputMetadataManager.GetSchemaDirectoryPath(datasetId);
                                    if (Directory.Exists(xsdDirectoryPath))
                                    {
                                        zip.AddDirectory(xsdDirectoryPath, "Schema");
                                    }

                                    XmlDocument manifest = OutputDatasetManager.GenerateManifest(datasetId, datasetVersion.Id);

                                    if (manifest != null)
                                    {
                                        string dynamicManifestFilePath = OutputDatasetManager.GetDynamicDatasetStorePath(datasetId,
                                                                                                                         versionNr, "manifest", ".xml");
                                        string fullFilePath = Path.Combine(AppConfiguration.DataPath, dynamicManifestFilePath);

                                        manifest.Save(fullFilePath);
                                        zip.AddFile(fullFilePath, "");
                                    }

                                    string message = string.Format("dataset {0} version {1} was published for repository {2}", datasetId,
                                                                   datasetVersion.Id, broker.Name);
                                    LoggerFactory.LogCustom(message);

                                    //Session["ZipFilePath"] = dynamicFilePath;

                                    zip.Save(zipFilePath);

                                    return(zipFilePath);
                                }
                            }

                            return("");
                        }
        }
Beispiel #2
0
        public ActionResult GenerateZip(long id, long versionid, string format)
        {
            XmlDatasetHelper     xmlDatasetHelper = new XmlDatasetHelper();
            DatasetManager       dm = new DatasetManager();
            DataStructureManager dataStructureManager = new DataStructureManager();
            PublicationManager   publicationManager   = new PublicationManager();
            SubmissionManager    publishingManager    = new SubmissionManager();

            string brokerName = "generic";

            try
            {
                using (var uow = this.GetUnitOfWork())
                {
                    LoggerFactory.LogCustom("Generate Zip Start");
                    long dsvId = versionid;
                    if (dsvId <= 0)
                    {
                        dsvId = dm.GetDatasetLatestVersion(id).Id;
                    }
                    DatasetVersion datasetVersion = uow.GetUnitOfWork().GetReadOnlyRepository <DatasetVersion>().Get(dsvId);
                    int            versionNr      = dm.GetDatasetVersionNr(datasetVersion);

                    #region metadata

                    LoggerFactory.LogCustom("Metadata Start");

                    //metadata as xml output
                    XmlDocument document = OutputMetadataManager.GetConvertedMetadata(id, TransmissionType.mappingFileExport,
                                                                                      datasetVersion.Dataset.MetadataStructure.Name);

                    //metadata as html
                    generateMetadataHtml(datasetVersion);

                    #endregion metadata

                    #region primary data

                    LoggerFactory.LogCustom("Primary Data Start");

                    // check the data sturcture type ...
                    if (format != null && datasetVersion.Dataset.DataStructure.Self is StructuredDataStructure)
                    {
                        OutputDataManager odm = new OutputDataManager();
                        // apply selection and projection

                        //check wheter title is empty or not
                        string title = String.IsNullOrEmpty(datasetVersion.Title) ? "no title available" : datasetVersion.Title;

                        switch (format)
                        {
                        case "application/xlsx":
                            odm.GenerateExcelFile(id, datasetVersion.Id, false);
                            break;

                        case "application/xlsm":
                            odm.GenerateExcelFile(id, datasetVersion.Id, true);
                            break;

                        default:
                            odm.GenerateAsciiFile(id, datasetVersion.Id, format, false);
                            break;
                        }
                    }

                    #endregion primary data

                    LoggerFactory.LogCustom("check zip on server Start");

                    string zipName         = publishingManager.GetZipFileName(id, versionNr);
                    string zipPath         = publishingManager.GetDirectoryPath(id, brokerName);
                    string dynamicZipPath  = publishingManager.GetDynamicDirectoryPath(id, brokerName);
                    string zipFilePath     = Path.Combine(zipPath, zipName);
                    string dynamicFilePath = Path.Combine(dynamicZipPath, zipName);

                    FileHelper.CreateDicrectoriesIfNotExist(Path.GetDirectoryName(zipFilePath));

                    if (FileHelper.FileExist(zipFilePath))
                    {
                        if (FileHelper.WaitForFile(zipFilePath))
                        {
                            FileHelper.Delete(zipFilePath);
                        }
                    }

                    // add datastructure
                    //ToDo put that functiom to the outputDatatructureManager

                    #region datatructure

                    LoggerFactory.LogCustom("Datastructure Start");

                    long          dataStructureId = datasetVersion.Dataset.DataStructure.Id;
                    DataStructure dataStructure   = dataStructureManager.StructuredDataStructureRepo.Get(dataStructureId);

                    if (dataStructure != null)
                    {
                        try
                        {
                            string dynamicPathOfDS = "";
                            dynamicPathOfDS = storeGeneratedFilePathToContentDiscriptor(id, datasetVersion,
                                                                                        "datastructure", ".txt");
                            string datastructureFilePath = AsciiWriter.CreateFile(dynamicPathOfDS);

                            string json = OutputDataStructureManager.GetVariableListAsJson(dataStructureId);

                            AsciiWriter.AllTextToFile(datastructureFilePath, json);


                            //generate datastructure as html
                            DatasetVersion ds = uow.GetUnitOfWork().GetReadOnlyRepository <DatasetVersion>().Get(dsvId);
                            generateDataStructureHtml(ds);
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    }

                    #endregion datatructure

                    LoggerFactory.LogCustom("Zip Start");

                    using (ZipFile zip = new ZipFile())
                    {
                        foreach (ContentDescriptor cd in datasetVersion.ContentDescriptors)
                        {
                            bool addFile = true;

                            if (cd.Name.ToLower().Contains("generated"))
                            {
                                if (!cd.MimeType.ToLower().Equals(format))
                                {
                                    addFile = false;
                                }
                            }

                            if (addFile)
                            {
                                string path = Path.Combine(AppConfiguration.DataPath, cd.URI);
                                string name = cd.URI.Split('\\').Last();

                                if (FileHelper.FileExist(path))
                                {
                                    zip.AddFile(path, "");
                                }
                            }
                        }

                        // add xsd of the metadata schema
                        LoggerFactory.LogCustom("Schema Start");

                        string xsdDirectoryPath = OutputMetadataManager.GetSchemaDirectoryPath(id);
                        if (Directory.Exists(xsdDirectoryPath))
                        {
                            zip.AddDirectory(xsdDirectoryPath, "Schema");
                        }

                        LoggerFactory.LogCustom("Manifest Start");

                        XmlDocument manifest = OutputDatasetManager.GenerateManifest(id, datasetVersion.Id);

                        if (manifest != null)
                        {
                            string dynamicManifestFilePath = OutputDatasetManager.GetDynamicDatasetStorePath(id,
                                                                                                             versionNr, "manifest", ".xml");
                            string fullFilePath = Path.Combine(AppConfiguration.DataPath, dynamicManifestFilePath);

                            manifest.Save(fullFilePath);
                            zip.AddFile(fullFilePath, "");
                        }

                        LoggerFactory.LogCustom("Save zip Start");

                        zip.Save(zipFilePath);

                        LoggerFactory.LogCustom("Return");

                        string title = datasetVersion.Title;
                        title = String.IsNullOrEmpty(title) ? "unknown" : title;

                        string message = string.Format("dataset {0} version {1} was downloaded as zip - {2}.", id,
                                                       versionNr, format);
                        LoggerFactory.LogCustom(message);

                        var es = new EmailService();
                        es.Send(MessageHelper.GetDownloadDatasetHeader(id, versionNr),
                                MessageHelper.GetDownloadDatasetMessage(id, title, getPartyNameOrDefault(), "zip - " + format, versionNr),
                                ConfigurationManager.AppSettings["SystemEmail"]
                                );

                        return(File(zipFilePath, "application/zip", Path.GetFileName(zipFilePath)));
                    }
                }
            }
            catch (Exception ex)
            {
                LoggerFactory.LogCustom("Error: " + ex.Message);
                return(null);
            }
            finally
            {
                dm.Dispose();
                dataStructureManager.Dispose();
                publicationManager.Dispose();
            }
        }