Esempio n. 1
0
        private string getStorePath(long datasetVersionId, string exportTo)
        {
            using (DatasetManager datasetManager = new DatasetManager())
                using (MetadataStructureManager metadataStructureManager = new MetadataStructureManager())
                {
                    DatasetVersion datasetVersion = datasetManager.GetDatasetVersion(datasetVersionId);

                    Dataset dataset = datasetManager.GetDataset(datasetVersionId);

                    string md_title = metadataStructureManager.Repo.Get(datasetVersion.Dataset.MetadataStructure.Id).Name;

                    string path;

                    int versionNr = datasetManager.GetDatasetVersionNr(datasetVersion);

                    if (string.IsNullOrEmpty(exportTo) || exportTo.ToLower().Equals("generic"))
                    {
                        path = IOHelper.GetDynamicStorePath(datasetVersion.Dataset.Id, versionNr, "metadata", ".xml");
                    }
                    else
                    {
                        path = IOHelper.GetDynamicStorePath(datasetVersion.Dataset.Id, versionNr, "metadata_" + exportTo, ".xml");
                    }

                    return(path);
                }
        }
Esempio n. 2
0
        private string storeGeneratedFilePathToContentDiscriptor(long datasetId, DatasetVersion datasetVersion,
                                                                 string title, string ext)
        {
            string name     = "";
            string mimeType = "";

            if (ext.Contains("csv"))
            {
                name     = "datastructure";
                mimeType = "text/comma-separated-values";
            }

            if (ext.Contains("html"))
            {
                name     = title;
                mimeType = "application/html";
            }

            using (DatasetManager dm = new DatasetManager())
            {
                int versionNr = dm.GetDatasetVersionNr(datasetVersion);

                // create the generated FileStream and determine its location
                string dynamicPath = OutputDatasetManager.GetDynamicDatasetStorePath(datasetId, versionNr, title,
                                                                                     ext);
                //Register the generated data FileStream as a resource of the current dataset version
                //ContentDescriptor generatedDescriptor = new ContentDescriptor()
                //{
                //    OrderNo = 1,
                //    Name = name,
                //    MimeType = mimeType,
                //    URI = dynamicPath,
                //    DatasetVersion = datasetVersion,
                //};

                if (datasetVersion.ContentDescriptors.Count(p => p.Name.Equals(name)) > 0)
                {
                    // remove the one contentdesciptor
                    foreach (ContentDescriptor cd in datasetVersion.ContentDescriptors)
                    {
                        if (cd.Name == name)
                        {
                            cd.URI = dynamicPath;
                            dm.UpdateContentDescriptor(cd);
                        }
                    }
                }
                else
                {
                    // add current contentdesciptor to list
                    //datasetVersion.ContentDescriptors.Add(generatedDescriptor);
                    dm.CreateContentDescriptor(name, mimeType, dynamicPath, 1, datasetVersion);
                }

                //dm.EditDatasetVersion(datasetVersion, null, null, null);
                return(dynamicPath);
            }
        }
Esempio n. 3
0
        private static void storeGeneratedFilePathToContentDiscriptor(long datasetId, DatasetVersion datasetVersion, string title, string ext)
        {
            string name     = "";
            string mimeType = "";

            if (ext.Contains("xml"))
            {
                name     = "metadata";
                mimeType = "text/xml";
            }

            DatasetManager dm        = new DatasetManager();
            int            versionNr = dm.GetDatasetVersionNr(datasetVersion);

            // create the generated FileStream and determine its location
            string dynamicPath = OutputDatasetManager.GetDynamicDatasetStorePath(datasetId, versionNr, title, ext);

            //Register the generated data FileStream as a resource of the current dataset version
            //ContentDescriptor generatedDescriptor = new ContentDescriptor()
            //{
            //    OrderNo = 1,
            //    Name = name,
            //    MimeType = mimeType,
            //    URI = dynamicPath,
            //    DatasetVersion = datasetVersion,
            //};

            try
            {
                datasetVersion = dm.GetDatasetVersion(datasetVersion.Id);

                if (datasetVersion.ContentDescriptors.Count(p => p.Name.Equals(name)) > 0)
                {   // remove the one contentdesciptor
                    foreach (ContentDescriptor cd in datasetVersion.ContentDescriptors)
                    {
                        if (cd.Name == name)
                        {
                            cd.URI = dynamicPath;
                            dm.UpdateContentDescriptor(cd);
                        }
                    }
                }
                else
                {
                    // add current contentdesciptor to list
                    //datasetVersion.ContentDescriptors.Add(generatedDescriptor);
                    dm.CreateContentDescriptor(name, mimeType, dynamicPath, 1, datasetVersion);
                }

                //dm.EditDatasetVersion(datasetVersion, null, null, null);
            }
            finally
            {
                dm.Dispose();
            }
        }
Esempio n. 4
0
        public static string CreateConvertedMetadata(long datasetId, TransmissionType type)
        {
            XmlDocument              newXml;
            DatasetManager           datasetManager = new DatasetManager();
            MetadataStructureManager metadataMetadataStructureManager = new MetadataStructureManager();

            try
            {
                DatasetVersion    datasetVersion    = datasetManager.GetDatasetLatestVersion(datasetId);
                MetadataStructure metadataStructure = metadataMetadataStructureManager.Repo.Get(datasetVersion.Dataset.MetadataStructure.Id);
                XmlDatasetHelper  xmlDatasetHelper  = new XmlDatasetHelper();

                int    versionNr   = datasetManager.GetDatasetVersionNr(datasetVersion);
                string mappingName = metadataStructure.Name;

                string mappingFileName = xmlDatasetHelper.GetTransmissionInformation(datasetVersion.Id, type, mappingName);
                //if mapping file not exist
                if (string.IsNullOrEmpty(mappingFileName))
                {
                    return("");
                }
                string pathMappingFile = Path.Combine(AppConfiguration.GetModuleWorkspacePath("DIM"), mappingFileName);

                XmlMapperManager xmlMapperManager = new XmlMapperManager(TransactionDirection.InternToExtern);
                xmlMapperManager.Load(pathMappingFile, "exporttest");

                newXml = xmlMapperManager.Export(datasetVersion.Metadata, datasetVersion.Id, mappingName, true);

                string title = datasetVersion.Title;

                // store in content descriptor
                string filename = "metadata";
                if (String.IsNullOrEmpty(mappingName) || mappingName.ToLower() == "generic")
                {
                    storeGeneratedFilePathToContentDiscriptor(datasetId, datasetVersion, filename, ".xml");
                }
                else
                {
                    filename = "metadata_" + mappingName;
                    storeGeneratedFilePathToContentDiscriptor(datasetId, datasetVersion, filename,
                                                              ".xml");
                }

                return(OutputDatasetManager.GetDynamicDatasetStorePath(datasetId, versionNr, filename, ".xml"));
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                datasetManager.Dispose();
                metadataMetadataStructureManager.Dispose();
            }
        }
        public FileResult getFile(string path)
        {
            path = Server.UrlDecode(path);
            if (FileHelper.FileExist(Path.Combine(AppConfiguration.DataPath, path)))
            {
                EntityPermissionManager entityPermissionManager = null;
                DatasetManager          datasetManager          = null;
                try
                {
                    entityPermissionManager = new EntityPermissionManager();
                    datasetManager          = new DatasetManager();

                    DatasetInfo datasetInfo = (DatasetInfo)Session["DatasetInfo"];
                    string      entityType  = (string)Session["EntityType"];
                    long        datasetID   = datasetInfo.DatasetId;
                    bool        access      = entityPermissionManager.HasEffectiveRight(HttpContext.User.Identity.Name, typeof(Dataset), datasetID, RightType.Read);
                    if (access)
                    {
                        path = Path.Combine(AppConfiguration.DataPath, path);
                        FileInfo fileInfo = new FileInfo(path);
                        Session["DatasetInfo"] = datasetInfo;
                        Session["EntityType"]  = entityType;

                        // after 2.14.1 files are stored in original names
                        // by download only the files, the user need to know th edataset id and the version number
                        int    versionNr = datasetManager.GetDatasetVersionNr(datasetInfo.DatasetVersionId);
                        string filename  = datasetInfo.DatasetId + "_" + versionNr + "_" + fileInfo.Name;

                        return(File(path, MimeMapping.GetMimeMapping(fileInfo.Name), filename));
                    }
                    else
                    {
                        Session["DatasetInfo"] = datasetInfo;
                        return(null);
                    }
                }
                catch (Exception ex)
                {
                    return(null);
                }
                finally
                {
                    entityPermissionManager.Dispose();
                    datasetManager.Dispose();
                }
            }
            else
            {
                WebRequest      request  = WebRequest.Create(path);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                return(File(response.GetResponseStream(), MimeMapping.GetMimeMapping(response.ResponseUri.Segments.LastOrDefault()), response.ResponseUri.Segments.LastOrDefault()));
            }
        }
        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);
                int    versionNr = datasetManager.GetDatasetVersionNr(datasetVersion);
                string filename  = submissionManager.GetFileNameForDataRepo(datasetId, versionNr, _dataRepo.Name, "xml");

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

                FileHelper.CreateDicrectoriesIfNotExist(path);

                metadata.Save(filepath);

                return(filepath);
            }
            finally
            {
                datasetManager.Dispose();
                metadataStructureManager.Dispose();
                publicationManager.Dispose();
            }
        }
        private string generatePrimaryData(long datasetVersionId)
        {
            try
            {
                using (DatasetManager datasetManager = new DatasetManager())
                {
                    DatasetVersion datasetVersion = datasetManager.GetDatasetVersion(datasetVersionId);

                    if (datasetVersion.Dataset.DataStructure.Self is StructuredDataStructure)
                    {
                        OutputDataManager outputDataManager = new OutputDataManager();
                        SubmissionManager submissionManager = new SubmissionManager();

                        long datasetId = datasetVersion.Dataset.Id;
                        int  versionNr = datasetManager.GetDatasetVersionNr(datasetVersion);

                        string fileName = submissionManager.GetFileNameForDataRepo(datasetId, versionNr,
                                                                                   _dataRepo.Name,
                                                                                   "csv");

                        string filepath = outputDataManager.GenerateAsciiFile(
                            datasetId,
                            datasetVersionId,
                            "text/txt",
                            false);

                        return(filepath);
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }

            return("");
        }
Esempio n. 8
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("");
                        }
        }
Esempio n. 9
0
        private void storeGeneratedFilePathToContentDiscriptor(long datasetId, DatasetVersion datasetVersion, string ext, bool withUnits)
        {
            DatasetManager dm      = new DatasetManager();
            string         nameExt = "";

            if (withUnits)
            {
                nameExt = "_withunits";
            }

            try
            {
                string name     = "";
                string mimeType = "";

                if (ext.Contains("csv"))
                {
                    name     = "generatedCSV" + nameExt;
                    mimeType = "text/csv";
                }

                if (ext.Contains("txt"))
                {
                    name     = "generatedTXT" + nameExt;
                    mimeType = "text/plain";
                }

                if (ext.Contains("tsv"))
                {
                    name     = "generatedTSV" + nameExt;
                    mimeType = "text/tsv";
                }

                if (ext.Contains("xlsm"))
                {
                    name     = "generated";
                    mimeType = "application/xlsm";
                }

                if (ext.Contains("xlsx"))
                {
                    name     = "generatedExcel" + nameExt;
                    mimeType = "application/xlsx";
                }

                int versionNr = dm.GetDatasetVersionNr(datasetVersion);

                // create the generated FileStream and determine its location
                string dynamicPath = IOHelper.GetDynamicStorePath(datasetId, versionNr, "data" + nameExt, ext);
                //Register the generated data FileStream as a resource of the current dataset version
                //ContentDescriptor generatedDescriptor = new ContentDescriptor()
                //{
                //    OrderNo = 1,
                //    Name = name,
                //    MimeType = mimeType,
                //    URI = dynamicPath,
                //    DatasetVersion = datasetVersion,
                //};

                if (datasetVersion.ContentDescriptors.Count(p => p.Name.Equals(name)) > 0)
                {   // remove the one contentdesciptor
                    foreach (ContentDescriptor cd in datasetVersion.ContentDescriptors)
                    {
                        if (cd.Name == name)
                        {
                            cd.URI = dynamicPath;
                            dm.UpdateContentDescriptor(cd);
                        }
                    }
                }
                else
                {
                    // add current contentdesciptor to list
                    //datasetVersion.ContentDescriptors.Add(generatedDescriptor);
                    dm.CreateContentDescriptor(name, mimeType, dynamicPath, 1, datasetVersion);
                }

                //dm.EditDatasetVersion(datasetVersion, null, null, null);
            }
            finally
            {
                dm.Dispose();
            }
        }
Esempio n. 10
0
        public string GenerateAsciiFile(long id, long versionId, string mimeType, bool withUnits)
        {
            DatasetManager       datasetManager          = new DatasetManager();
            DataStructureManager datasetStructureManager = new DataStructureManager();

            try
            {
                DatasetVersion datasetVersion = datasetManager.GetDatasetVersion(versionId);
                int            versionNr      = datasetManager.GetDatasetVersionNr(datasetVersion);

                string        contentDescriptorTitle = "";
                string        ext           = "";
                string        nameExt       = "";
                TextSeperator textSeperator = TextSeperator.semicolon;

                if (withUnits)
                {
                    nameExt = "_withunits";
                }

                switch (mimeType)
                {
                case "text/csv":
                case "text/comma-separated-values":
                case "application/octet-stream":
                    /* of course this is a wrong  mimetype for csv.
                     * but the c# class MimeMapping.GetMimeMapping(ext) currently returns this as a result for .csv.
                     * since we don't use the datatype at the moment,
                     * it will be rebuilt into the case here*/
                {
                    contentDescriptorTitle = "generatedCSV" + nameExt;
                    ext           = ".csv";
                    textSeperator = TextSeperator.semicolon;
                    break;
                }

                case "text/tsv":
                case "text/tab-separated-values":
                {
                    contentDescriptorTitle = "generatedTSV" + nameExt;
                    ext           = ".tsv";
                    textSeperator = TextSeperator.tab;
                    break;
                }

                default:
                {
                    contentDescriptorTitle = "generatedTXT" + nameExt;
                    ext           = ".txt";
                    textSeperator = TextSeperator.tab;
                    break;
                }
                }

                AsciiWriter writer = new AsciiWriter(textSeperator);

                string path = "";

                //ascii allready exist
                if (datasetVersion.ContentDescriptors.Count(p => p.Name.Equals(contentDescriptorTitle) &&
                                                            p.URI.Contains(datasetVersion.Id.ToString())) > 0 &&
                    !withUnits)
                {
                    #region FileStream exist

                    ContentDescriptor contentdescriptor = datasetVersion.ContentDescriptors.Where(p => p.Name.Equals(contentDescriptorTitle)).FirstOrDefault();
                    path = Path.Combine(AppConfiguration.DataPath, contentdescriptor.URI);

                    if (FileHelper.FileExist(path))
                    {
                        return(path);
                    }

                    #endregion FileStream exist
                }

                // not exist, needs to generated - get data first as datatable
                DataTable data = getData(id, versionId);

                long datastuctureId = datasetVersion.Dataset.DataStructure.Id;

                path = createDownloadFile(id, versionNr, datastuctureId, "data", ext, writer, null, withUnits);

                storeGeneratedFilePathToContentDiscriptor(id, datasetVersion, ext, withUnits);

                //add units if want
                string[] units = null;
                if (withUnits)
                {
                    units = getUnits(datastuctureId, null);
                }

                writer.AddData(data, path, datastuctureId, units);

                return(path);
            }
            finally
            {
                datasetManager.Dispose();
                datasetStructureManager.Dispose();
            }
        }
Esempio n. 11
0
        //public string GenerateExcelFile(long id, string title, bool createAsTemplate, DataTable data = null, bool withUnits = false)
        //{
        //    string mimeType = "";
        //    string ext = ".xlsx";
        //    string contentDescriptorTitle = "";

        //    if (createAsTemplate)
        //    {
        //        ext = ".xlsm";
        //        contentDescriptorTitle = "generated";
        //    }
        //    else
        //    {
        //        ext = ".xlsx";
        //        if (withUnits) contentDescriptorTitle = "generatedExcelWithUnits";
        //        else contentDescriptorTitle = "generatedExcel";
        //    }

        //    mimeType = MimeMapping.GetMimeMapping(ext);

        //    DatasetManager datasetManager = new DatasetManager();

        //    try
        //    {
        //        DatasetVersion datasetVersion = datasetManager.GetDatasetLatestVersion(id);
        //        ExcelWriter writer = new ExcelWriter(createAsTemplate);

        //        string path = "";

        //        //excel allready exist
        //        if (datasetVersion.ContentDescriptors.Count(p => p.Name.Equals(contentDescriptorTitle) && p.URI.Contains(datasetVersion.Id.ToString())) > 0 &&
        //            data == null)
        //        {
        //            #region FileStream exist

        //            ContentDescriptor contentdescriptor =
        //                datasetVersion.ContentDescriptors.Where(p => p.Name.Equals(contentDescriptorTitle))
        //                    .FirstOrDefault();
        //            path = Path.Combine(AppConfiguration.DataPath, contentdescriptor.URI);

        //            long version = datasetVersion.Id;
        //            long versionNrGeneratedFile =
        //                Convert.ToInt64(contentdescriptor.URI.Split('\\').Last().Split('_')[1]);

        //            // check if FileStream exist
        //            if (FileHelper.FileExist(path) && version == versionNrGeneratedFile)
        //            {
        //                return path;
        //            }

        //            #endregion FileStream exist
        //        }

        //        // not exist needs to generated

        //        #region FileStream not exist

        //        if (data == null)
        //        {
        //            DatasetManager dm = new DatasetManager();
        //            data = dm.GetLatestDatasetVersionTuples(id);
        //            data.Strip();
        //        }

        //        long datastuctureId = datasetVersion.Dataset.DataStructure.Id;
        //        int versionNr = datasetManager.GetDatasetVersionNr(datasetVersion);
        //        if (createAsTemplate)
        //        {
        //            string[] columnNames = (from dc in data.Columns.Cast<DataColumn>()
        //                                    select dc.Caption).ToArray();

        //            path = createDownloadFile(id, versionNr, datastuctureId, "data", ext, writer, columnNames);
        //            storeGeneratedFilePathToContentDiscriptor(id, datasetVersion, ext, false);
        //            writer.AddData(data.Rows, path, datastuctureId);
        //        }
        //        else
        //        {
        //            path = createDownloadFile(id, versionNr, datastuctureId, "data", ext, writer, null, withUnits);

        //            // the default data is without units, so store the path of the file if it was generated
        //            storeGeneratedFilePathToContentDiscriptor(id, datasetVersion, ext, withUnits);

        //            string[] units = null;
        //            if (withUnits) units = getUnits(datastuctureId, null);

        //            writer.AddData(data, path, datastuctureId, units);
        //        }

        //        return path;

        //        #endregion FileStream not exist
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //    finally
        //    {
        //        datasetManager.Dispose();
        //    }
        //}

        /// <summary>
        /// version id = 0 == latest version
        /// </summary>
        /// <param name="id"></param>
        /// <param name="versionId"></param>
        /// <param name="createAsTemplate"></param>
        /// <param name="data"></param>
        /// <param name="withUnits"></param>
        /// <returns></returns>
        public string GenerateExcelFile(long id, long versionId, bool createAsTemplate, DataTable data = null, bool withUnits = false)
        {
            string mimeType = "";
            string ext      = ".xlsx";
            string contentDescriptorTitle = "";

            if (createAsTemplate)
            {
                ext = ".xlsm";
                contentDescriptorTitle = "generated";
            }
            else
            {
                ext = ".xlsx";
                if (withUnits)
                {
                    contentDescriptorTitle = "generatedExcelWithUnits";
                }
                else
                {
                    contentDescriptorTitle = "generatedExcel";
                }
            }

            mimeType = MimeMapping.GetMimeMapping(ext);

            DatasetManager datasetManager = new DatasetManager();

            try
            {
                DatasetVersion datasetVersion = datasetManager.GetDatasetLatestVersion(id);
                ExcelWriter    writer         = new ExcelWriter(createAsTemplate);

                string path = "";

                //excel allready exist
                if (datasetVersion.ContentDescriptors.Count(p => p.Name.Equals(contentDescriptorTitle) && p.URI.Contains(datasetVersion.Id.ToString())) > 0 &&
                    data == null)
                {
                    #region FileStream exist

                    ContentDescriptor contentdescriptor =
                        datasetVersion.ContentDescriptors.Where(p => p.Name.Equals(contentDescriptorTitle))
                        .FirstOrDefault();
                    path = Path.Combine(AppConfiguration.DataPath, contentdescriptor.URI);

                    long version = datasetVersion.Id;
                    long versionNrGeneratedFile =
                        Convert.ToInt64(contentdescriptor.URI.Split('\\').Last().Split('_')[1]);

                    // check if FileStream exist
                    if (FileHelper.FileExist(path) && version == versionNrGeneratedFile)
                    {
                        return(path);
                    }

                    #endregion FileStream exist
                }

                // not exist needs to generated

                #region FileStream not exist

                if (data == null)
                {
                    data = getData(id, versionId);
                }

                long datastuctureId = datasetVersion.Dataset.DataStructure.Id;
                int  versionNr      = datasetManager.GetDatasetVersionNr(datasetVersion);
                if (createAsTemplate)
                {
                    string[] columnNames = (from dc in data.Columns.Cast <DataColumn>()
                                            select dc.Caption).ToArray();

                    path = createDownloadFile(id, versionNr, datastuctureId, "data", ext, writer, columnNames);
                    storeGeneratedFilePathToContentDiscriptor(id, datasetVersion, ext, false);
                    writer.AddData(data.Rows, path, datastuctureId);
                }
                else
                {
                    path = createDownloadFile(id, versionNr, datastuctureId, "data", ext, writer, null, withUnits);

                    // the default data is without units, so store the path of the file if it was generated
                    storeGeneratedFilePathToContentDiscriptor(id, datasetVersion, ext, withUnits);

                    string[] units = null;
                    if (withUnits)
                    {
                        units = getUnits(datastuctureId, null);
                    }

                    writer.AddData(data, path, datastuctureId, units);
                }

                return(path);

                #endregion FileStream not exist
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                datasetManager.Dispose();
            }
        }
Esempio n. 12
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();
            }
        }
Esempio n. 13
0
        private ShowPublishDataModel getShowPublishDataModel(long datasetId, long datasetVersionId = -1)
        {
            PublicationManager      publicationManager      = new PublicationManager();
            DatasetManager          datasetManager          = new DatasetManager();
            EntityPermissionManager entityPermissionManager = new EntityPermissionManager();
            ShowPublishDataModel    model = new ShowPublishDataModel();

            int versionNr = 1;

            try
            {
                Dataset dataset = datasetManager.GetDataset(datasetId);

                List <Broker> Brokers = GetBrokers(dataset.MetadataStructure.Id, publicationManager);

                model.Brokers   = Brokers.Select(b => b.Name).ToList();
                model.DatasetId = datasetId;

                //Todo Download Rigths -> currently set read rigths for this case
                model.DownloadRights = entityPermissionManager.HasEffectiveRight(HttpContext.User.Identity.Name,
                                                                                 typeof(Dataset), datasetId, RightType.Read);
                model.EditRights = entityPermissionManager.HasEffectiveRight(HttpContext.User.Identity.Name,
                                                                             typeof(Dataset), datasetId, RightType.Write);

                List <long> versions = new List <long>();
                if (datasetVersionId == -1)
                {
                    DatasetVersion datasetVersion = datasetManager.GetDatasetLatestVersion(datasetId);
                    datasetVersionId = datasetVersion.Id;
                    versions         = datasetManager.GetDatasetVersions(datasetId).Select(d => d.Id).ToList();

                    if (datasetVersion.StateInfo != null)
                    {
                        model.MetadataIsValid = DatasetStateInfo.Valid.ToString().Equals(datasetVersion.StateInfo.State) ? true : false;
                    }
                }

                //todo check if datasetversion id is correct
                List <Publication> publications =
                    publicationManager.PublicationRepo.Query().Where(p => versions.Contains(p.DatasetVersion.Id)).ToList();

                //get versionNr
                versionNr = datasetManager.GetDatasetVersionNr(datasetVersionId);

                foreach (var pub in publications)
                {
                    Broker     broker = publicationManager.BrokerRepo.Get(pub.Broker.Id);
                    Repository repo   = null;

                    if (pub.Repository != null)
                    {
                        repo = publicationManager.RepositoryRepo.Get(pub.Repository.Id);
                    }
                    string dataRepoName = repo == null ? "" : repo.Name;

                    List <string> repos =
                        GetRepos(dataset.MetadataStructure.Id, broker.Id, publicationManager).Select(r => r.Name).ToList();

                    model.Publications.Add(new PublicationModel()
                    {
                        Broker           = new BrokerModel(broker.Name, repos, broker.Link),
                        DataRepo         = dataRepoName,
                        DatasetVersionId = pub.DatasetVersion.Id,
                        CreationDate     = pub.Timestamp,
                        ExternalLink     = pub.ExternalLink,
                        FilePath         = pub.FilePath,
                        Status           = pub.Status,
                        DatasetVersionNr = versionNr
                    });
                }

                return(model);
            }
            finally
            {
                publicationManager.Dispose();
                datasetManager.Dispose();
                entityPermissionManager.Dispose();
            }
        }
Esempio n. 14
0
        //ToDO -> David <- do not store the files on the server
        public string Convert(long datasetVersionId)
        {
            DatasetManager datasetManager = new DatasetManager();

            PublicationManager publicationManager = new PublicationManager();

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

                /***
                 *  Generate a txt file for pangaea
                 *  1. json metadata
                 *  2. tabseperated primary Data
                 *
                 *
                 * if data only unstructred, then only metadata
                 */

                string primaryDataFilePath = "";

                #region create primary Data

                primaryDataFilePath = generatePrimaryData(datasetVersionId);

                #endregion create primary Data

                DatasetVersion datasetVersion      = datasetManager.GetDatasetVersion(datasetVersionId);
                long           datasetId           = datasetVersion.Dataset.Id;
                long           metadataStructureId = datasetVersion.Dataset.MetadataStructure.Id;

                DataStructureDataList datastructureDataList = OutputDataStructureManager.GetVariableList(datasetVersion.Dataset.DataStructure.Id);

                PanageaMetadata metadata = getMetadata(datasetVersion.Metadata, metadataStructureId);
                metadata.ParameterIDs = datastructureDataList.Variables;

                string json = JsonConvert.SerializeObject(metadata, Formatting.Indented);

                SubmissionManager submissionManager = new SubmissionManager();

                string path = submissionManager.GetDirectoryPath(datasetId, _broker.Name);

                int verionNr = datasetManager.GetDatasetVersionNr(datasetVersion);

                string filename = submissionManager.GetFileNameForDataRepo(datasetId, verionNr, _dataRepo.Name, "txt");

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

                try
                {
                    if (File.Exists(filepath))
                    {
                        File.Delete(filepath);
                    }

                    FileHelper.Create(filepath).Close();
                    File.WriteAllText(filepath, json + Environment.NewLine + Environment.NewLine);

                    if (!string.IsNullOrEmpty(primaryDataFilePath))
                    {
                        File.AppendAllText(filepath, File.ReadAllText(primaryDataFilePath));
                    }

                    return(filepath);
                }
                catch (Exception exception)
                {
                    throw exception;
                }

                return("");
            }
            finally
            {
                datasetManager.Dispose();
                publicationManager.Dispose();
            }
        }