Exemplo n.º 1
0
 public static MessageModel validateDataStructureInUse(long Id, DataStructure dataStructure)
 {
     if (dataStructure != null && dataStructure.Id != 0)
     {
         DatasetManager datasetManager = null;
         bool           inUse          = false;
         try
         {
             datasetManager = new DatasetManager();
             foreach (Dataset d in dataStructure.Datasets)
             {
                 if (datasetManager.RowCount(d.Id, null) > 0)
                 {
                     inUse = true;
                     break;
                 }
             }
         }
         finally
         {
             datasetManager.Dispose();
         }
         if (inUse)
         {
             try
             {
                 return(new MessageModel()
                 {
                     hasMessage = true,
                     Message = "Can't save data structure " + dataStructure.Name + " (" + Id + "), it's uesed by a Dataset.",
                     CssId = "inUse"
                 });
             }
             catch
             {
                 return(new MessageModel()
                 {
                     hasMessage = true,
                     Message = "Something is wrong with data structure " + Id,
                     CssId = "0"
                 });
             }
         }
         else
         {
             return(new MessageModel());
         }
     }
     else
     {
         if (Id == 0)
         {
             return(new MessageModel());
         }
         else
         {
             return(new MessageModel()
             {
                 hasMessage = true,
                 Message = "Can't store variable for the data structure " + Id + ", it's uesed by a dataset.",
                 CssId = "0"
             });
         }
     }
 }
Exemplo n.º 2
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();
            }
        }