Пример #1
0
        /// <summary>
        /// Return a metadata as html file from a  datasetversion
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult GetMetadataAsHtmlFile(long id)
        {
            DatasetManager dm = new DatasetManager();

            try
            {
                using (var uow = this.GetUnitOfWork())
                {
                    long           dsId = dm.GetDatasetLatestVersion(id).Id;
                    DatasetVersion ds   = uow.GetUnitOfWork().GetReadOnlyRepository <DatasetVersion>().Get(dsId);

                    XmlDocument document = OutputMetadataManager.GetConvertedMetadata(id, TransmissionType.mappingFileExport,
                                                                                      ds.Dataset.MetadataStructure.Name);

                    string htmlPage = PartialView("SimpleMetadata", document).RenderToString();
                    byte[] content  = Encoding.ASCII.GetBytes(htmlPage);

                    return(File(content, "text/html", "metadata.html"));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dm.Dispose();
            }
        }
Пример #2
0
        public string Convert(long datasetVersionId)
        {
            MetadataStructureManager metadataStructureManager = new MetadataStructureManager();
            DatasetManager           datasetManager           = new DatasetManager();


            SubmissionManager  submissionManager  = new SubmissionManager();
            PublicationManager publicationManager = new PublicationManager();


            try
            {
                DatasetVersion datasetVersion = datasetManager.GetDatasetVersion(datasetVersionId);

                _broker   = publicationManager.GetBroker(_broker.Id);
                _dataRepo = publicationManager.GetRepository(_dataRepo.Id);


                long   datasetId = datasetVersion.Dataset.Id;
                string name      = datasetManager.GetDatasetVersion(datasetVersionId).Dataset.MetadataStructure.Name;

                XmlDocument metadata = OutputMetadataManager.GetConvertedMetadata(datasetId,
                                                                                  TransmissionType.mappingFileExport, name, false);

                // create links to the api calls of the primary data?



                //add all to a zip


                //save document  and return filepath for download


                string path     = submissionManager.GetDirectoryPath(datasetId, _broker.Name);
                string filename = submissionManager.GetFileNameForDataRepo(datasetId, datasetVersionId, _dataRepo.Name, "xml");

                string filepath = Path.Combine(path, filename);

                FileHelper.CreateDicrectoriesIfNotExist(path);

                metadata.Save(filepath);


                return(filepath);
            }
            finally
            {
                datasetManager.Dispose();
                metadataStructureManager.Dispose();
                publicationManager.Dispose();
            }
        }
Пример #3
0
        // GET: api/Metadata/5
        // HttpResponseMessage response = new HttpResponseMessage { Content = new StringContent(doc.innerXml, Encoding.UTF8,"application/xml") };
        public HttpResponseMessage Get(int id)
        {
            DatasetManager dm = new DatasetManager();

            try
            {
                string convertTo = "";
                try
                {
                    convertTo = this.Request.GetQueryNameValuePairs().FirstOrDefault(p => "format".Equals(p.Key, StringComparison.InvariantCultureIgnoreCase)).Value;
                }
                catch (Exception ex) { }

                DatasetVersion dsv    = dm.GetDatasetLatestVersion(id);
                XmlDocument    xmldoc = dsv.Metadata;

                if (string.IsNullOrEmpty(convertTo))
                {
                    //return xmldoc;
                    HttpResponseMessage response = new HttpResponseMessage {
                        Content = new StringContent(xmldoc.InnerXml, Encoding.UTF8, "application/xml")
                    };
                    return(response);
                }
                else
                {
                    try
                    {
                        XmlDocument newXmlDoc = OutputMetadataManager.GetConvertedMetadata(id, TransmissionType.mappingFileExport,
                                                                                           convertTo);

                        HttpResponseMessage response = new HttpResponseMessage {
                            Content = new StringContent(newXmlDoc.InnerXml, Encoding.UTF8, "application/xml")
                        };

                        return(response);
                    }
                    catch (Exception ex)
                    {
                        return(null);
                    }
                }
            }
            finally
            {
                dm.Dispose();
            }
        }
Пример #4
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("");
                        }
        }
Пример #5
0
        public HttpResponseMessage Get(int id, [FromUri] string format = null)
        {
            DatasetManager dm = new DatasetManager();

            try
            {
                if (id == 0)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.PreconditionFailed, "dataset id should be greater then 0."));
                }

                //entity permissions
                if (id > 0)
                {
                    Dataset d = dm.GetDataset(id);
                    if (d == null)
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.PreconditionFailed, "the dataset with the id (" + id + ") does not exist."));
                    }
                }

                string convertTo = "";

                try
                {
                    convertTo = this.Request.GetQueryNameValuePairs().FirstOrDefault(p => "format".Equals(p.Key, StringComparison.InvariantCultureIgnoreCase)).Value;
                }
                catch (Exception ex) { }

                DatasetVersion dsv    = dm.GetDatasetLatestVersion(id);
                XmlDocument    xmldoc = dsv.Metadata;

                if (string.IsNullOrEmpty(convertTo))
                {
                    //return xmldoc;
                    HttpResponseMessage response = new HttpResponseMessage {
                        Content = new StringContent(xmldoc.InnerXml, Encoding.UTF8, "application/xml")
                    };
                    return(response);
                }
                else
                {
                    try
                    {
                        XmlDocument newXmlDoc = OutputMetadataManager.GetConvertedMetadata(id, TransmissionType.mappingFileExport,
                                                                                           convertTo);

                        if (newXmlDoc == null)
                        {
                            return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, string.Format("No mapping found for this format : {0} .", format)));
                        }

                        HttpResponseMessage response = new HttpResponseMessage {
                            Content = new StringContent(newXmlDoc.InnerXml, Encoding.UTF8, "application/xml")
                        };

                        return(response);
                    }
                    catch (Exception ex)
                    {
                        return(null);
                    }
                }
            }
            finally
            {
                dm.Dispose();
            }
        }
Пример #6
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();
            }
        }