private void generateMetadataHtml(DatasetVersion dsv) { XmlDatasetHelper xmlDatasetHelper = new XmlDatasetHelper(); long datasetId = dsv.Dataset.Id; long metadatastructureId = dsv.Dataset.MetadataStructure.Id; long datastructureId = dsv.Dataset.DataStructure.Id; long researchplanId = dsv.Dataset.ResearchPlan.Id; string title = dsv.Title; Session["ShowDataMetadata"] = dsv.Metadata; var view = this.Render("DCM", "Form", "LoadMetadataOfflineVersion", new RouteValueDictionary() { { "entityId", datasetId }, { "title", title }, { "metadatastructureId", metadatastructureId }, { "datastructureId", datastructureId }, { "researchplanId", researchplanId }, { "sessionKeyForMetadata", "ShowDataMetadata" }, { "resetTaskManager", false } }); byte[] content = Encoding.ASCII.GetBytes(view.ToString()); string dynamicPathOfMD = ""; dynamicPathOfMD = storeGeneratedFilePathToContentDiscriptor(datasetId, dsv, "metadata", ".html"); string metadataFilePath = AsciiWriter.CreateFile(dynamicPathOfMD); AsciiWriter.AllTextToFile(metadataFilePath, view.ToString()); }
public async Task <bool> Store() { try { Debug.WriteLine("start storing data"); asciiWriter = new AsciiWriter(IO.TextSeperator.tab); //create a file in a user tempfolder _filepath = Path.Combine(AppConfiguration.DataPath, _user.UserName, "temp" + DateTime.Now.Millisecond + ".tsv"); FileHelper.CreateDicrectoriesIfNotExist(Path.GetDirectoryName(_filepath)); asciiWriter.CreateFile(Path.GetDirectoryName(_filepath), Path.GetFileName(_filepath)); //store data into file without units asciiWriter.AddData(_data.Data, _data.Columns, _filepath, _dataset.DataStructure.Id); //todo send email to user var es = new EmailService(); es.Send(MessageHelper.GetPushApiStoreHeader(_dataset.Id, _title), MessageHelper.GetPushApiStoreMessage(_dataset.Id, _user.UserName), new List <string>() { _user.Email }, new List <string>() { ConfigurationManager.AppSettings["SystemEmail"] } ); } catch (Exception ex) { // var es = new EmailService(); es.Send(MessageHelper.GetPushApiStoreHeader(_data.DatasetId, _title), MessageHelper.GetPushApiStoreMessage(_dataset.Id, _user.UserName, new string[] { ex.Message }), new List <string>() { _user.Email }, new List <string>() { ConfigurationManager.AppSettings["SystemEmail"] } ); return(false); } Debug.WriteLine("end storing data"); if (_uploadMethod.Equals(UploadMethod.Update)) { return(await PKCheck()); } return(await Validate()); }
private void generateDataStructureHtml(DatasetVersion dsv) { var view = this.Render("DIM", "Export", "SimpleDataStructure", new RouteValueDictionary() { { "id", dsv.Dataset.Id } }); byte[] content = Encoding.ASCII.GetBytes(view.ToString()); string dynamicPathOfMD = ""; dynamicPathOfMD = storeGeneratedFilePathToContentDiscriptor(dsv.Dataset.Id, dsv, "datastructure", ".html"); string metadataFilePath = AsciiWriter.CreateFile(dynamicPathOfMD); AsciiWriter.AllTextToFile(metadataFilePath, view.ToString()); }
//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(""); } }
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(); } }