Exemple #1
0
        public ActionResult copyDataStructure(long Id, bool isStructured, string Name = "", string Description = "", string cssId = "")
        {
            Name        = Server.UrlDecode(Name);
            Description = Server.UrlDecode(Description);
            DataStructureManager dataStructureManager = null;

            try
            {
                dataStructureManager = new DataStructureManager();


                if (!isStructured)
                {
                    UnStructuredDataStructure dataStructure = dataStructureManager.GetUnitOfWork().GetReadOnlyRepository <UnStructuredDataStructure>().Get(Id);
                    if (dataStructure != null)
                    {
                        if (Name == "")
                        {
                            Name = dataStructure.Name + " - Copy";
                        }

                        if (Description == "" && dataStructure.Description != null)
                        {
                            Description = dataStructure.Description;
                        }
                        LoggerFactory.LogCustom("Copy Data Structure" + Id);
                        return(createDataStructure(0, Name.Trim(), isStructured, Description.Trim(), cssId));
                    }
                }
                else
                {
                    StructuredDataStructure dataStructure = dataStructureManager.GetUnitOfWork().GetReadOnlyRepository <StructuredDataStructure>().Get(Id);
                    if (dataStructure != null)
                    {
                        if (Name == "")
                        {
                            Name = dataStructure.Name + " - Copy";
                        }

                        if (Description == "" && dataStructure.Description != null)
                        {
                            Description = dataStructure.Description;
                        }

                        MessageModel messageModel = storeDataStructure(0, Name.Trim(), isStructured, Description.Trim(), cssId);
                        List <long>  order        = new List <long>();
                        Variable     variable     = new Variable();

                        if (!messageModel.hasMessage)
                        {
                            StructuredDataStructure dataStructureCopy = dataStructureManager.GetUnitOfWork().GetReadOnlyRepository <StructuredDataStructure>().Get(Convert.ToInt64(messageModel.Message));
                            dataStructureManager.GetUnitOfWork().GetReadOnlyRepository <StructuredDataStructure>().LoadIfNot(dataStructureCopy.Variables);
                            foreach (Variable v in DataStructureIO.getOrderedVariables(dataStructure))
                            {
                                variable = dataStructureManager.AddVariableUsage(dataStructureCopy, v.DataAttribute, v.IsValueOptional, v.Label.Trim(), v.DefaultValue, v.MissingValue, v.Description.Trim(), v.Unit);
                                order.Add(variable.Id);
                            }
                            DataStructureIO.setVariableOrder(dataStructureCopy, order);
                        }
                        LoggerFactory.LogCustom("Copy Data Structure" + Id);
                        return(PartialView("_message", messageModel));
                    }
                }
                return(PartialView("_message", new MessageModel()));
            }
            finally
            {
                dataStructureManager.Dispose();
            }
        }
        public ActionResult storeVariables(long Id, storeVariableStruct[] variables)
        {
            DataStructureManager dataStructureManager = null;
            DataContainerManager dataContainerManager = null;
            MissingValueManager  missingValueManager  = null;
            UnitManager          um = null;

            try
            {
                dataStructureManager = new DataStructureManager();
                missingValueManager  = new MissingValueManager();
                dataContainerManager = new DataContainerManager();
                um = new UnitManager();
                var structureRepo = dataStructureManager.GetUnitOfWork().GetReadOnlyRepository <StructuredDataStructure>();

                StructuredDataStructure dataStructure = structureRepo.Get(Id);
                MessageModel            returnObject  = new MessageModel();
                MessageModel            messageModel  = MessageModel.validateDataStructureInUse(dataStructure.Id, dataStructure);
                if (messageModel.hasMessage)
                {
                    foreach (Variable v in dataStructure.Variables)
                    {
                        if (variables.Select(svs => svs.Id).ToList().Contains(v.Id))
                        {
                            v.Description = variables.Where(svs => svs.Id == v.Id).FirstOrDefault().Description;
                            dataStructure = dataStructureManager.UpdateStructuredDataStructure(dataStructure);
                        }
                    }
                    return(PartialView("_messageWindow", messageModel));
                }

                if (variables != null && variables.Any())
                {
                    Variable    variable = new Variable();
                    List <long> order    = new List <long>();

                    foreach (Variable v in dataStructure.Variables)
                    {
                        if (!variables.Select(svs => svs.Id).ToList().Contains(v.Id))
                        {
                            List <MissingValue> missingValues = missingValueManager.Repo.Query(mv => mv.Variable.Id.Equals(v.Id)).ToList();
                            foreach (MissingValue mv in missingValues)
                            {
                                missingValueManager.Delete(mv);
                            }
                            dataStructureManager.RemoveVariableUsage(v);
                        }
                    }

                    foreach (storeVariableStruct svs in variables.Where(svs => svs.Id == 0).ToList())
                    {
                        if (svs.Lable == null)
                        {
                            svs.Lable = "";
                        }
                        if (svs.Description == null)
                        {
                            svs.Description = "";
                        }

                        DataAttribute dataAttribute = dataContainerManager.DataAttributeRepo.Get(svs.AttributeId);
                        if (dataAttribute != null)
                        {
                            variable = dataStructureManager.AddVariableUsage(dataStructure, dataAttribute, svs.isOptional, svs.Lable.Trim(), null, null, svs.Description.Trim(), um.Repo.Get(svs.UnitId));
                            svs.Id   = variable.Id;
                            foreach (MissingValueStruct mvs in svs.MissingValues)
                            {
                                if (mvs.Id == 0)
                                {
                                    if (String.IsNullOrEmpty(mvs.Placeholder))
                                    {
                                        missingValueManager.Create(mvs.DisplayName, mvs.Description, variable);
                                    }
                                    else
                                    {
                                        missingValueManager.Create(mvs.DisplayName, mvs.Description, variable, mvs.Placeholder);
                                    }
                                }
                            }
                        }
                        else
                        {
                            returnObject = new MessageModel()
                            {
                                hasMessage = true,
                                Message    = "Not all Variables are stored.",
                                CssId      = "0"
                            };
                        }
                    }

                    //dataStructure = structureRepo.Get(Id); // Javad: why it is needed?

                    variables = variables.Where(v => v.Id != 0).ToArray();
                    MissingValue missingValue = new MissingValue();
                    foreach (storeVariableStruct svs in variables.Where(svs => svs.Id != 0).ToList())
                    {
                        if (svs.Lable == null)
                        {
                            svs.Lable = "";
                        }
                        if (svs.Description == null)
                        {
                            svs.Description = "";
                        }

                        variable = dataStructure.Variables.Where(v => v.Id == svs.Id).FirstOrDefault();
                        if (variable != null)
                        {
                            variable.Label           = svs.Lable.Trim();
                            variable.Description     = svs.Description.Trim();
                            variable.Unit            = um.Repo.Get(svs.UnitId);
                            variable.DataAttribute   = dataContainerManager.DataAttributeRepo.Get(svs.AttributeId);
                            variable.IsValueOptional = svs.isOptional;


                            List <MissingValue> missingValues = missingValueManager.Repo.Query(mv => mv.Variable.Id.Equals(svs.Id)).ToList();
                            foreach (MissingValue mv in missingValues)
                            {
                                if (!svs.MissingValues.Select(mvs => mvs.Id).Contains(mv.Id))
                                {
                                    missingValueManager.Delete(mv);
                                }
                            }

                            foreach (MissingValueStruct mvs in svs.MissingValues)
                            {
                                if (mvs.Id == 0)
                                {
                                    if (String.IsNullOrEmpty(mvs.Placeholder))
                                    {
                                        missingValueManager.Create(mvs.DisplayName, mvs.Description, variable);
                                    }
                                    else
                                    {
                                        missingValueManager.Create(mvs.DisplayName, mvs.Description, variable, mvs.Placeholder);
                                    }
                                }
                                else if (mvs.Id > 0)
                                {
                                    missingValue = missingValues.Where(mv => mv.Id.Equals(mvs.Id)).FirstOrDefault();
                                    if (missingValue != null)
                                    {
                                        missingValue.DisplayName = mvs.DisplayName;
                                        missingValue.Description = mvs.Description;
                                        missingValue.Placeholder = mvs.Placeholder;
                                        missingValueManager.Update(missingValue);
                                    }
                                }
                            }
                        }
                    }

                    dataStructure = dataStructureManager.UpdateStructuredDataStructure(dataStructure);
                    DataStructureIO.setVariableOrder(dataStructure, variables.Select(svs => svs.Id).ToList());
                }
                else
                {
                    foreach (Variable v in dataStructure.Variables)
                    {
                        List <MissingValue> missingValues = missingValueManager.Repo.Query(mv => mv.Variable.Id.Equals(v.Id)).ToList();
                        foreach (MissingValue mv in missingValues)
                        {
                            missingValueManager.Delete(mv);
                        }
                        dataStructureManager.RemoveVariableUsage(v);
                    }
                }
                LoggerFactory.LogCustom("Variables for Data Structure " + Id + " stored.");
                return(Json(returnObject, JsonRequestBehavior.AllowGet));
            }
            finally
            {
                dataStructureManager.Dispose();
                dataContainerManager.Dispose();
                um.Dispose();
                missingValueManager.Dispose();
            }
        }
Exemple #3
0
        public ActionResult storeVariables(long Id, storeVariableStruct[] variables)
        {
            DataStructureManager dataStructureManager = null;
            DataContainerManager dataContainerManager = null;
            UnitManager          um = null;

            try
            {
                dataStructureManager = new DataStructureManager();
                var structureRepo = dataStructureManager.GetUnitOfWork().GetReadOnlyRepository <StructuredDataStructure>();

                StructuredDataStructure dataStructure = structureRepo.Get(Id);
                MessageModel            returnObject  = new MessageModel();
                MessageModel            messageModel  = MessageModel.validateDataStructureInUse(dataStructure.Id, dataStructure);
                if (messageModel.hasMessage)
                {
                    foreach (Variable v in dataStructure.Variables)
                    {
                        if (variables.Select(svs => svs.Id).ToList().Contains(v.Id))
                        {
                            v.Description = variables.Where(svs => svs.Id == v.Id).FirstOrDefault().Description;
                            dataStructure = dataStructureManager.UpdateStructuredDataStructure(dataStructure);
                        }
                    }
                    return(PartialView("_messageWindow", messageModel));
                }

                if (variables != null && variables.Count() > 0)
                {
                    Variable    variable = new Variable();
                    List <long> order    = new List <long>();

                    foreach (Variable v in dataStructure.Variables)
                    {
                        if (!variables.Select(svs => svs.Id).ToList().Contains(v.Id))
                        {
                            dataStructureManager.RemoveVariableUsage(v);
                        }
                    }

                    foreach (storeVariableStruct svs in variables.Where(svs => svs.Id == 0).ToList())
                    {
                        if (svs.Lable == null)
                        {
                            svs.Lable = "";
                        }
                        if (svs.Description == null)
                        {
                            svs.Description = "";
                        }
                        try
                        {
                            dataContainerManager = new DataContainerManager();
                            DataAttribute dataAttribute = dataContainerManager.DataAttributeRepo.Get(svs.AttributeId);
                            if (dataAttribute != null)
                            {
                                try
                                {
                                    um       = new UnitManager();
                                    variable = dataStructureManager.AddVariableUsage(dataStructure, dataAttribute, svs.isOptional, svs.Lable.Trim(), null, null, svs.Description.Trim(), um.Repo.Get(svs.UnitId));
                                    svs.Id   = variable.Id;
                                }
                                finally
                                {
                                    um.Dispose();
                                }
                            }
                            else
                            {
                                returnObject = new MessageModel()
                                {
                                    hasMessage = true,
                                    Message    = "Not all Variables are stored.",
                                    CssId      = "0"
                                };
                            }
                        }
                        finally
                        {
                            // Javad: would be better to conctruct and dispose this object outside of the loop
                            dataContainerManager.Dispose();
                        }
                    }
                    dataStructure = structureRepo.Get(Id); // Javad: why it is needed?

                    variables = variables.Where(v => v.Id != 0).ToArray();

                    foreach (storeVariableStruct svs in variables.Where(svs => svs.Id != 0).ToList())
                    {
                        if (svs.Lable == null)
                        {
                            svs.Lable = "";
                        }
                        if (svs.Description == null)
                        {
                            svs.Description = "";
                        }

                        variable = dataStructure.Variables.Where(v => v.Id == svs.Id).FirstOrDefault();
                        if (variable != null)
                        {
                            variable.Label       = svs.Lable.Trim();
                            variable.Description = svs.Description.Trim();

                            try
                            {
                                um = new UnitManager();
                                dataContainerManager = new DataContainerManager();

                                variable.Unit            = um.Repo.Get(svs.UnitId);
                                variable.DataAttribute   = dataContainerManager.DataAttributeRepo.Get(svs.AttributeId);
                                variable.IsValueOptional = svs.isOptional;
                            }
                            finally
                            {
                                um.Dispose(); // Javad: would be better to conctruct and dipose these objects outside of the loop
                                dataContainerManager.Dispose();
                            }
                        }
                    }

                    dataStructure = dataStructureManager.UpdateStructuredDataStructure(dataStructure);
                    DataStructureIO.setVariableOrder(dataStructure, variables.Select(svs => svs.Id).ToList());
                }
                else
                {
                    foreach (Variable v in dataStructure.Variables)
                    {
                        dataStructureManager.RemoveVariableUsage(v);
                    }
                }
                LoggerFactory.LogCustom("Variables for Data Structure " + Id + " stored.");
                return(Json(returnObject, JsonRequestBehavior.AllowGet));
            }
            finally
            {
                dataStructureManager.Dispose();
                dataContainerManager.Dispose();
                um.Dispose();
            }
        }