Ejemplo n.º 1
0
        private string generatePrimaryData(long datasetVersionId)
        {
            try
            {
                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;

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

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

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

            return("");
        }
Ejemplo n.º 2
0
        public JsonResult PrepareTxtData(long id)
        {
            if (hasUserRights(id, RightType.Read))
            {
                string         ext            = ".txt";
                DatasetManager datasetManager = new DatasetManager();

                try
                {
                    DatasetVersion    datasetVersion      = datasetManager.GetDatasetLatestVersion(id);
                    AsciiWriter       writer              = new AsciiWriter(TextSeperator.comma);
                    OutputDataManager ioOutputDataManager = new OutputDataManager();
                    string            title = getTitle(writer.GetTitle(id));
                    string            path  = "";

                    string message = string.Format("dataset {0} version {1} was downloaded as txt.", id,
                                                   datasetVersion.Id);

                    // if filter selected
                    if (filterInUse())
                    {
                        #region generate a subset of a dataset

                        String[] visibleColumns = null;

                        if (Session["Columns"] != null)
                        {
                            visibleColumns = (String[])Session["Columns"];
                        }

                        path = ioOutputDataManager.GenerateAsciiFile(id, title, "text/plain", visibleColumns);

                        LoggerFactory.LogCustom(message);


                        #endregion generate a subset of a dataset
                    }
                    else
                    {
                        path = ioOutputDataManager.GenerateAsciiFile(id, title, "text/plain");

                        LoggerFactory.LogCustom(message);
                    }

                    return(Json(true, JsonRequestBehavior.AllowGet));
                }
                catch (Exception ex)
                {
                    return(Json(ex.Message, JsonRequestBehavior.AllowGet));
                }
                finally
                {
                    datasetManager.Dispose();
                }
            }
            else
            {
                return(Json("User has no rights.", JsonRequestBehavior.AllowGet));
            }
        }
Ejemplo n.º 3
0
        public JsonResult PrepareExcelData(long id)
        {
            if (hasUserRights(id, RightType.Read))
            {
                string ext = ".xlsm";

                DatasetManager datasetManager = new DatasetManager();

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

                    string title = getTitle(writer.GetTitle(id));

                    string path = "";

                    string message = string.Format("dataset {0} version {1} was downloaded as excel.", id,
                                                   datasetVersion.Id);

                    // if filter selected
                    if (filterInUse())
                    {
                        #region generate a subset of a dataset

                        //ToDo filter datatuples

                        LoggerFactory.LogCustom(message);


                        #endregion generate a subset of a dataset
                    }

                    //filter not in use
                    else
                    {
                        OutputDataManager outputDataManager = new OutputDataManager();
                        path = outputDataManager.GenerateExcelFile(id, title);
                        LoggerFactory.LogCustom(message);
                    }


                    return(Json(true, JsonRequestBehavior.AllowGet));
                }
                catch (Exception ex)
                {
                    return(Json(ex.Message, JsonRequestBehavior.AllowGet));
                }
                finally
                {
                    datasetManager.Dispose();
                }
            }
            else
            {
                return(Json("User has no rights.", JsonRequestBehavior.AllowGet));
            }
        }
Ejemplo n.º 4
0
        // GET: api/data/5
        /// <summary>
        /// In addition to the id, it is possible to have projection and selection criteria passed to the action via query string parameters
        /// </summary>
        /// <param name="id">Dataset Id</param>
        /// <returns></returns>
        /// <remarks> The action accepts the following additional parameters via the query string
        /// 1: projection: is a comman separated list of ids that determines which variables of the dataset version tuples should take part in the result set
        /// 2: selection: is a logical expression that filters the tuples of the chosen dataset. The expression should have been written against the variables of the dataset only.
        /// logical operators, nesting, precedence, and SOME functions should be supported.
        /// /api/data/6?header=TimeUTC,D8CO1_1&filter=TimeUTC<5706000
        /// </remarks>
        public HttpResponseMessage Get(int id)
        {
            string projection = this.Request.GetQueryNameValuePairs().FirstOrDefault(p => "header".Equals(p.Key, StringComparison.InvariantCultureIgnoreCase)).Value;
            string selection  = this.Request.GetQueryNameValuePairs().FirstOrDefault(p => "filter".Equals(p.Key, StringComparison.InvariantCultureIgnoreCase)).Value;

            DatasetManager datasetManager = new DatasetManager();

            try
            {
                OutputDataManager ioOutputDataManager = new OutputDataManager();

                DatasetVersion version = datasetManager.GetDatasetLatestVersion(id);

                string title = xmlDatasetHelper.GetInformationFromVersion(version.Id, NameAttributeValues.title);

                // check the data sturcture type ...
                if (version.Dataset.DataStructure.Self is StructuredDataStructure)
                {
                    // apply selection and projection
                    //var tuples = dm.GetDatasetVersionEffectiveTuples(version);
                    DataTable dt = OutputDataManager.ConvertPrimaryDataToDatatable(datasetManager, version, title, true);

                    if (!string.IsNullOrEmpty(selection))
                    {
                        dt = OutputDataManager.SelectionOnDataTable(dt, selection);
                    }

                    if (!string.IsNullOrEmpty(projection))
                    {
                        // make the header names upper case to make them case insensitive
                        dt = OutputDataManager.ProjectionOnDataTable(dt, projection.ToUpper().Split(','));
                    }

                    DatasetModel model = new DatasetModel();
                    model.DataTable = dt;

                    var response = Request.CreateResponse();
                    response.Content = new ObjectContent(typeof(DatasetModel), model, new DatasetModelCsvFormatter(model.DataTable.TableName));


                    //set headers on the "response"
                    return(response);

                    //return model;
                }
                else
                {
                    return(Request.CreateResponse());
                }
            }
            finally
            {
                datasetManager.Dispose();
            }
        }
Ejemplo n.º 5
0
        // GET: api/data/5
        /// <summary>
        /// In addition to the id, it is possible to have projection and selection criteria passed to the action via query string parameters
        /// </summary>
        /// <param name="id">Dataset Id</param>
        /// <returns></returns>
        /// <remarks> The action accepts the following additional parameters via the query string
        /// 1: projection: is a comman separated list of ids that determines which variables of the dataset version tuples should take part in the result set
        /// 2: selection: is a logical expression that filters the tuples of the chosen dataset. The expression should have been written against the variables of the dataset only.
        /// logical operators, nesting, precedence, and SOME functions should be supported.
        /// </remarks>
        public HttpResponseMessage Get(int id)
        {
            string projection = this.Request.GetQueryNameValuePairs().FirstOrDefault(p => "header".Equals(p.Key, StringComparison.InvariantCultureIgnoreCase)).Value;
            string selection  = this.Request.GetQueryNameValuePairs().FirstOrDefault(p => "filter" .Equals(p.Key, StringComparison.InvariantCultureIgnoreCase)).Value;

            OutputDataManager ioOutputDataManager = new OutputDataManager();

            DatasetManager dm = new DatasetManager();
            DatasetVersion version = dm.GetDatasetLatestVersion(id);

            string title = XmlDatasetHelper.GetInformation(version, NameAttributeValues.title);

            // check the data sturcture type ...
            if (version.Dataset.DataStructure.Self is StructuredDataStructure)
            {
                // apply selection and projection
                //var tuples = dm.GetDatasetVersionEffectiveTuples(version);
                DataTable dt = OutputDataManager.ConvertPrimaryDataToDatatable(dm, version, title, true);

                if (!string.IsNullOrEmpty(selection))
                {
                    dt = OutputDataManager.SelectionOnDataTable(dt, selection);
                }

                if (!string.IsNullOrEmpty(projection))
                {
                    // make the header names upper case to make them case insensitive
                    dt = OutputDataManager.ProjectionOnDataTable(dt, projection.ToUpper().Split(','));
                }

                DatasetModel model = new DatasetModel();
                model.DataTable = dt;

                var response = Request.CreateResponse();
                response.Content = new ObjectContent(typeof(DatasetModel), model, new DatasetModelCsvFormatter(model.DataTable.TableName));

                //set headers on the "response"
                return response;

                //return model;

            } else
            {
                return Request.CreateResponse();
            }
        }
Ejemplo n.º 6
0
        private HttpResponseMessage getData(long id, int variableId, string token)
        {
            DatasetManager          datasetManager          = new DatasetManager();
            UserManager             userManager             = new UserManager();
            EntityPermissionManager entityPermissionManager = new EntityPermissionManager();
            EntityManager           entityManager           = new EntityManager();
            DataStructureManager    dataStructureManager    = null;

            bool isPublic = false;

            try
            {
                // if a dataset is public, then the api should also return data if there is no token for a user

                #region is public
                dataStructureManager = new DataStructureManager();

                long?entityTypeId = entityManager.FindByName(typeof(Dataset).Name)?.Id;
                entityTypeId = entityTypeId.HasValue ? entityTypeId.Value : -1;

                isPublic = entityPermissionManager.Exists(null, entityTypeId.Value, id);

                #endregion is public

                if (!isPublic && String.IsNullOrEmpty(token))

                {
                    var request = Request.CreateResponse();
                    request.Content = new StringContent("Bearer token not exist.");

                    return(request);
                }

                User user = userManager.Users.Where(u => u.Token.Equals(token)).FirstOrDefault();

                if (isPublic || user != null)
                {
                    if (isPublic || entityPermissionManager.HasEffectiveRight(user.Name, typeof(Dataset), id, RightType.Read))
                    {
                        XmlDatasetHelper  xmlDatasetHelper    = new XmlDatasetHelper();
                        OutputDataManager ioOutputDataManager = new OutputDataManager();

                        Dataset        dataset        = datasetManager.GetDataset(id);
                        DatasetVersion datasetVersion = datasetManager.GetDatasetLatestVersion(id);

                        string title = datasetVersion.Title;

                        // check the data sturcture type ...
                        if (datasetVersion.Dataset.DataStructure.Self is StructuredDataStructure)
                        {
                            object stats = new object();

                            DataTable dt = new DataTable("Varibales");

                            List <ApiDataStatisticModel> dataStatisticModels = new List <ApiDataStatisticModel>();
                            if (variableId == -1)
                            {
                                StructuredDataStructure structuredDataStructure = dataStructureManager.StructuredDataStructureRepo.Get(datasetVersion.Dataset.DataStructure.Id);
                                List <string>           varIds = new List <string>();
                                foreach (Variable vs in structuredDataStructure.Variables)
                                {
                                    varIds.Add("var" + vs.Id);
                                }
                                dt = GetDuplicates(id, varIds);
                            }
                            else
                            {
                            }
                            //dt.Strip();


                            dt.TableName = id + "_data";

                            DatasetModel model = new DatasetModel();
                            model.DataTable = dt;

                            var    response = Request.CreateResponse(HttpStatusCode.OK);
                            string resp     = JsonConvert.SerializeObject(model);

                            response.Content = new StringContent(resp, System.Text.Encoding.UTF8, "application/json");
                            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                            return(response);
                        }
                        else
                        {
                            return(Request.CreateResponse());
                        }
                    }
                    else // has rights?
                    {
                        var request = Request.CreateResponse();
                        request.Content = new StringContent("User has no read right.");

                        return(request);
                    }
                }
                else
                {
                    var request = Request.CreateResponse();
                    request.Content = new StringContent("User is not available.");

                    return(request);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                datasetManager.Dispose();
                userManager.Dispose();
                entityPermissionManager.Dispose();
                entityManager.Dispose();
                dataStructureManager.Dispose();
            }
        }
Ejemplo n.º 7
0
        private HttpResponseMessage getData(long id, int variableId, string token)
        {
            DatasetManager          datasetManager          = new DatasetManager();
            UserManager             userManager             = new UserManager();
            EntityPermissionManager entityPermissionManager = new EntityPermissionManager();
            EntityManager           entityManager           = new EntityManager();
            DataStructureManager    dataStructureManager    = null;

            bool isPublic = false;

            try
            {
                // if a dataset is public, then the api should also return data if there is no token for a user

                #region is public
                dataStructureManager = new DataStructureManager();

                long?entityTypeId = entityManager.FindByName(typeof(Dataset).Name)?.Id;
                entityTypeId = entityTypeId.HasValue ? entityTypeId.Value : -1;

                isPublic = entityPermissionManager.Exists(null, entityTypeId.Value, id);

                #endregion is public

                if (!isPublic && String.IsNullOrEmpty(token))

                {
                    var request = Request.CreateResponse();
                    request.Content = new StringContent("Bearer token not exist.");

                    return(request);
                }

                User user = userManager.Users.Where(u => u.Token.Equals(token)).FirstOrDefault();

                if (isPublic || user != null)
                {
                    if (isPublic || entityPermissionManager.HasEffectiveRight(user.Name, typeof(Dataset), id, RightType.Read))
                    {
                        XmlDatasetHelper  xmlDatasetHelper    = new XmlDatasetHelper();
                        OutputDataManager ioOutputDataManager = new OutputDataManager();

                        Dataset        dataset        = datasetManager.GetDataset(id);
                        DatasetVersion datasetVersion = datasetManager.GetDatasetLatestVersion(id);

                        string title = datasetVersion.Title;

                        // check the data sturcture type ...
                        if (datasetVersion.Dataset.DataStructure.Self is StructuredDataStructure)
                        {
                            object stats = new object();

                            DataTable dt = new DataTable("Varibales");
                            DataTable dtMissingValues = new DataTable("MissingValues");
                            dtMissingValues.Columns.Add("placeholder", typeof(String));
                            dtMissingValues.Columns.Add("displayName", typeof(String));

                            List <ApiDataStatisticModel> dataStatisticModels     = new List <ApiDataStatisticModel>();
                            StructuredDataStructure      structuredDataStructure = dataStructureManager.StructuredDataStructureRepo.Get(datasetVersion.Dataset.DataStructure.Id);
                            if (variableId == -1)
                            {
                                foreach (Variable vs in structuredDataStructure.Variables)
                                {
                                    ApiDataStatisticModel dataStatisticModel = new ApiDataStatisticModel();
                                    dt = GetUniqueValues(id, vs.Id);
                                    dataStatisticModel.VariableId   = vs.Id;
                                    dataStatisticModel.uniqueValues = dt;
                                    dataStatisticModel.minLength    = dt.Compute("Min(length)", string.Empty).ToString();
                                    dataStatisticModel.maxLength    = dt.Compute("Max(length)", string.Empty).ToString();
                                    dataStatisticModel.count        = dt.Compute("Sum(count)", string.Empty).ToString();
                                    dtMissingValues.Clear();
                                    foreach (var missingValue in vs.MissingValues)
                                    {
                                        DataRow workRow = dtMissingValues.NewRow();
                                        workRow["placeholder"] = missingValue.Placeholder;
                                        workRow["displayName"] = missingValue.DisplayName;
                                        dtMissingValues.Rows.Add(workRow);
                                    }
                                    dataStatisticModel.min           = GetMin(dtMissingValues, dt);
                                    dataStatisticModel.max           = GetMax(dtMissingValues, dt);
                                    dataStatisticModel.missingValues = dtMissingValues;
                                    dataStatisticModels.Add(dataStatisticModel);
                                }
                            }
                            else
                            {
                                Variable variable = new Variable();

                                foreach (Variable vs in structuredDataStructure.Variables)
                                {
                                    if (vs.Id == variableId)
                                    {
                                        variable = vs;
                                    }
                                }

                                ApiDataStatisticModel dataStatisticModel = new ApiDataStatisticModel();
                                dt = GetUniqueValues(id, variableId);
                                dataStatisticModel.VariableId   = variableId;
                                dataStatisticModel.uniqueValues = dt;

                                dataStatisticModel.minLength = dt.Compute("Min(length)", string.Empty).ToString();
                                dataStatisticModel.maxLength = dt.Compute("Max(length)", string.Empty).ToString();
                                dataStatisticModel.count     = dt.Compute("Sum(count)", string.Empty).ToString();
                                foreach (var missingValue in variable.MissingValues)
                                {
                                    DataRow workRow = dtMissingValues.NewRow();
                                    workRow["placeholder"] = missingValue.Placeholder;
                                    workRow["displayName"] = missingValue.DisplayName;
                                    dtMissingValues.Rows.Add(workRow);
                                }
                                dataStatisticModel.min           = GetMin(dtMissingValues, dt);
                                dataStatisticModel.max           = GetMax(dtMissingValues, dt);
                                dataStatisticModel.missingValues = dtMissingValues;
                                dataStatisticModels.Add(dataStatisticModel);
                            }
                            dt.Strip();


                            dt.TableName = id + "_data";

                            DatasetModel model = new DatasetModel();
                            model.DataTable = dt;

                            var    response = Request.CreateResponse(HttpStatusCode.OK);
                            string resp     = JsonConvert.SerializeObject(dataStatisticModels);

                            response.Content = new StringContent(resp, System.Text.Encoding.UTF8, "application/json");
                            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                            return(response);
                        }
                        else
                        {
                            return(Request.CreateResponse());
                        }
                    }
                    else // has rights?
                    {
                        var request = Request.CreateResponse();
                        request.Content = new StringContent("User has no read right.");

                        return(request);
                    }
                }
                else
                {
                    var request = Request.CreateResponse();
                    request.Content = new StringContent("User is not available.");

                    return(request);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                datasetManager.Dispose();
                userManager.Dispose();
                entityPermissionManager.Dispose();
                entityManager.Dispose();
                dataStructureManager.Dispose();
            }
        }
Ejemplo 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("");
                        }
        }
Ejemplo n.º 9
0
        public ActionResult DownloadAsTxtData(long id)
        {
            if (hasUserRights(id, RightType.Read))
            {
                string         ext            = ".txt";
                DatasetManager datasetManager = new DatasetManager();

                try
                {
                    DatasetVersion    datasetVersion      = datasetManager.GetDatasetLatestVersion(id);
                    AsciiWriter       writer              = new AsciiWriter(TextSeperator.comma);
                    OutputDataManager ioOutputDataManager = new OutputDataManager();
                    string            title = getTitle(writer.GetTitle(id));
                    string            path  = "";

                    string message = string.Format("dataset {0} version {1} was downloaded as txt.", id,
                                                   datasetVersion.Id);

                    // if filter selected
                    if (filterInUse())
                    {
                        #region generate a subset of a dataset

                        String[] visibleColumns = null;

                        if (Session["Columns"] != null)
                        {
                            visibleColumns = (String[])Session["Columns"];
                        }

                        path = ioOutputDataManager.GenerateAsciiFile(id, title, "text/plain", visibleColumns);

                        LoggerFactory.LogCustom(message);

                        return(File(path, "text/csv", title + ext));

                        #endregion generate a subset of a dataset
                    }
                    else
                    {
                        path = ioOutputDataManager.GenerateAsciiFile(id, title, "text/plain");

                        LoggerFactory.LogCustom(message);

                        var es = new EmailService();
                        es.Send(MessageHelper.GetDownloadDatasetHeader(),
                                MessageHelper.GetDownloadDatasetMessage(id, title, GetUsernameOrDefault()),
                                ConfigurationManager.AppSettings["SystemEmail"]
                                );


                        return(File(path, "text/plain", title + ".txt"));
                    }
                }
                catch (Exception ex)
                {
                    var es = new EmailService();
                    es.Send(MessageHelper.GetUpdateDatasetHeader(),
                            ex.Message,
                            ConfigurationManager.AppSettings["SystemEmail"]
                            );

                    throw ex;
                }
                finally
                {
                    datasetManager.Dispose();
                }
            }
            else
            {
                return(Content("User has no rights."));
            }
        }
Ejemplo n.º 10
0
        public ActionResult DownloadAsExcelData(long id)
        {
            if (hasUserRights(id, RightType.Read))
            {
                string ext = ".xlsm";

                DatasetManager datasetManager = new DatasetManager();

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

                    string title = getTitle(writer.GetTitle(id));

                    string path = "";

                    string message = string.Format("dataset {0} version {1} was downloaded as excel.", id,
                                                   datasetVersion.Id);

                    // if filter selected
                    if (filterInUse())
                    {
                        #region generate a subset of a dataset

                        //ToDo filter datatuples

                        OutputDataManager ioOutputDataManager = new OutputDataManager();
                        path = ioOutputDataManager.GenerateExcelFile(id, title);
                        LoggerFactory.LogCustom(message);

                        return(File(path, "application/xlsm", title + ext));

                        #endregion generate a subset of a dataset
                    }

                    //filter not in use
                    else
                    {
                        OutputDataManager outputDataManager = new OutputDataManager();
                        path = outputDataManager.GenerateExcelFile(id, title);
                        LoggerFactory.LogCustom(message);

                        var es = new EmailService();
                        es.Send(MessageHelper.GetDownloadDatasetHeader(),
                                MessageHelper.GetDownloadDatasetMessage(id, title, GetUsernameOrDefault()),
                                ConfigurationManager.AppSettings["SystemEmail"]
                                );


                        return(File(Path.Combine(AppConfiguration.DataPath, path), "application/xlsm", title + ext));
                    }
                }
                catch (Exception ex)
                {
                    var es = new EmailService();
                    es.Send(MessageHelper.GetUpdateDatasetHeader(),
                            ex.Message,
                            ConfigurationManager.AppSettings["SystemEmail"]
                            );

                    throw ex;
                }
                finally
                {
                    datasetManager.Dispose();
                }
            }
            else
            {
                return(Content("User has no rights."));
            }
        }
Ejemplo n.º 11
0
        private HttpResponseMessage getData(long id, int version, string token, string projection = null, string selection = null)
        {
            DatasetManager          datasetManager          = new DatasetManager();
            UserManager             userManager             = new UserManager();
            EntityPermissionManager entityPermissionManager = new EntityPermissionManager();
            EntityManager           entityManager           = new EntityManager();

            bool isPublic = false;

            try
            {
                // if a dataset is public, then the api should also return data if there is no token for a user

                #region is public

                long?entityTypeId = entityManager.FindByName(typeof(Dataset).Name)?.Id;
                entityTypeId = entityTypeId.HasValue ? entityTypeId.Value : -1;

                isPublic = entityPermissionManager.Exists(null, entityTypeId.Value, id);

                #endregion is public

                if (!isPublic && String.IsNullOrEmpty(token))

                {
                    var request = Request.CreateResponse();
                    request.Content = new StringContent("Bearer token not exist.");

                    return(request);
                }

                User user = userManager.Users.Where(u => u.Token.Equals(token)).FirstOrDefault();

                if (isPublic || user != null)
                {
                    if (isPublic || entityPermissionManager.HasEffectiveRight(user.Name, typeof(Dataset), id, RightType.Read))
                    {
                        XmlDatasetHelper  xmlDatasetHelper    = new XmlDatasetHelper();
                        OutputDataManager ioOutputDataManager = new OutputDataManager();

                        Dataset dataset = datasetManager.GetDataset(id);

                        // If the requested version is -1 or the last version of the dataset, then the data will be loaded in a
                        // different way than when loading the data from an older version
                        bool isLatestVersion = false;
                        if (version == -1 || dataset.Versions.Count == version)
                        {
                            isLatestVersion = true;
                        }

                        if (isLatestVersion)
                        {
                            #region get data from the latest version of a dataset

                            DatasetVersion datasetVersion = datasetManager.GetDatasetLatestVersion(id);

                            string title = datasetVersion.Title;

                            // check the data sturcture type ...
                            if (datasetVersion.Dataset.DataStructure.Self is StructuredDataStructure)
                            {
                                //FilterExpression filter = null;
                                //OrderByExpression orderBy = null;
                                //ProjectionExpression projectionExpression = GetProjectionExpression(projection);

                                // apply selection and projection
                                long count = datasetManager.RowCount(id);

                                DataTable dt = datasetManager.GetLatestDatasetVersionTuples(id, null, null, null, 0, (int)count);
                                dt.Strip();

                                if (!string.IsNullOrEmpty(selection))
                                {
                                    dt = OutputDataManager.SelectionOnDataTable(dt, selection, true);
                                }

                                if (!string.IsNullOrEmpty(projection))
                                {
                                    // make the header names upper case to make them case insensitive
                                    dt = OutputDataManager.ProjectionOnDataTable(dt, projection.ToUpper().Split(','));
                                }

                                dt.TableName = id + "_data";

                                DatasetModel model = new DatasetModel();
                                model.DataTable = dt;

                                var response = Request.CreateResponse();
                                response.Content = new ObjectContent(typeof(DatasetModel), model, new DatasetModelCsvFormatter(model.DataTable.TableName));
                                response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/csv");

                                //set headers on the "response"
                                return(response);

                                #endregion get data from the latest version of a dataset

                                //return model;
                            }
                            else
                            {
                                return(Request.CreateResponse());
                            }
                        }
                        else
                        {
                            #region load data of a older version of a dataset

                            int index = version - 1;
                            if (version >= dataset.Versions.Count)
                            {
                                return(Request.CreateResponse(HttpStatusCode.PreconditionFailed, String.Format("This version ({0}) is not available for the dataset", version)));
                            }

                            DatasetVersion datasetVersion = dataset.Versions.OrderBy(d => d.Timestamp).ElementAt(version - 1);

                            string title = datasetVersion.Title;

                            // check the data sturcture type ...
                            if (datasetVersion.Dataset.DataStructure.Self is StructuredDataStructure)
                            {
                                //FilterExpression filter = null;
                                //OrderByExpression orderBy = null;

                                // apply selection and projection
                                int       count = datasetManager.GetDatasetVersionEffectiveTuples(datasetVersion).Count;
                                DataTable dt    = datasetManager.GetDatasetVersionTuples(datasetVersion.Id, 0, count);

                                dt.Strip();

                                if (!string.IsNullOrEmpty(selection))
                                {
                                    dt = OutputDataManager.SelectionOnDataTable(dt, selection);
                                }

                                if (!string.IsNullOrEmpty(projection))
                                {
                                    // make the header names upper case to make them case insensitive
                                    dt = OutputDataManager.ProjectionOnDataTable(dt, projection.ToUpper().Split(','));
                                }

                                dt.TableName = id + "_data";

                                DatasetModel model = new DatasetModel();
                                model.DataTable = dt;

                                var response = Request.CreateResponse();
                                response.Content = new ObjectContent(typeof(DatasetModel), model, new DatasetModelCsvFormatter(model.DataTable.TableName));
                                response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/csv");

                                //set headers on the "response"
                                return(response);
                            }
                            else // return files of the unstructure dataset
                            {
                                return(Request.CreateResponse());
                            }

                            #endregion load data of a older version of a dataset
                        }
                    }
                    else // has rights?
                    {
                        var request = Request.CreateResponse();
                        request.Content = new StringContent("User has no read right.");

                        return(request);
                    }
                }
                else
                {
                    var request = Request.CreateResponse();
                    request.Content = new StringContent("User is not available.");

                    return(request);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                datasetManager.Dispose();
                userManager.Dispose();
                entityPermissionManager.Dispose();
                entityManager.Dispose();
            }
        }
Ejemplo 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();
            }
        }
Ejemplo n.º 13
0
        public ActionResult Test()
        {
            UiTestModel model = new UiTestModel();

            model = DynamicListToDataTable();

            SubmissionManager pm = new SubmissionManager();
            DatasetManager dm = new DatasetManager();

            pm.Load();

            DataRepository gfbio = pm.DataRepositories.Where(d => d.Name.ToLower().Equals("gfbio")).FirstOrDefault();

            // get metadata
            long testdatasetId = 1;
            string formatname = "";
            XmlDocument newXmlDoc;
            DatasetVersion dsv = dm.GetDatasetLatestVersion(testdatasetId);
            string title = XmlDatasetHelper.GetInformation(dsv, NameAttributeValues.title);


            if (gfbio != null)
            {
                formatname =
                    XmlDatasetHelper.GetAllTransmissionInformation(1, TransmissionType.mappingFileExport, AttributeNames.name)
                        .First();
                OutputMetadataManager.GetConvertedMetadata(testdatasetId, TransmissionType.mappingFileExport,
                    formatname);


                // get primary data
                // check the data sturcture type ...
                if (dsv.Dataset.DataStructure.Self is StructuredDataStructure)
                {
                    OutputDataManager odm = new OutputDataManager();
                    // apply selection and projection
                    
                    odm.GenerateAsciiFile(testdatasetId, title, gfbio.PrimaryDataFormat);
                }

                string zipName = pm.GetZipFileName(testdatasetId, dsv.Id);
                string zipPath = pm.GetDirectoryPath(testdatasetId, gfbio);
                string zipFilePath = Path.Combine(zipPath, zipName);

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

                

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

                ZipFile zip = new ZipFile();

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

                    if (FileHelper.FileExist(path))
                    {
                        zip.AddFile(path, "");
                    }
                }
                zip.Save(zipFilePath);
            }
            else
            {
                newXmlDoc = dsv.Metadata;
            }


            

            return View("Index", model);
        }