public ActionResult deleteDataStructure(long Id, string cssId = "")
        {
            MessageModel DataStructureDeleteValidation = MessageModel.validateDataStructureDelete(Id);

            if (DataStructureDeleteValidation.hasMessage)
            {
                return(PartialView("_messageWindow", DataStructureDeleteValidation));
            }
            else
            {
                DataStructureManager dataStructureManager = null;
                MissingValueManager  missingValueManager  = null;

                try
                {
                    dataStructureManager = new DataStructureManager();
                    missingValueManager  = new MissingValueManager();
                    var structureRepo = dataStructureManager.GetUnitOfWork().GetReadOnlyRepository <StructuredDataStructure>();
                    StructuredDataStructure structuredDataStructure = structureRepo.Get(Id);

                    if (structuredDataStructure != null) // Javad: This one retrieves the entity withough using it, and then the next line agian fetches the same!
                    {
                        DataStructureIO.deleteTemplate(structuredDataStructure.Id);
                        foreach (Variable v in structuredDataStructure.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);
                        }
                        dataStructureManager.DeleteStructuredDataStructure(structuredDataStructure);
                        LoggerFactory.LogData(structuredDataStructure.Id.ToString(), typeof(DataStructure).Name, Vaiona.Entities.Logging.CrudState.Deleted);
                    }
                    else
                    {
                        var unStructureRepo = dataStructureManager.GetUnitOfWork().GetReadOnlyRepository <UnStructuredDataStructure>();
                        UnStructuredDataStructure unStructuredDataStructure = unStructureRepo.Get(Id);
                        dataStructureManager.DeleteUnStructuredDataStructure(unStructuredDataStructure);
                        LoggerFactory.LogData(unStructuredDataStructure.Id.ToString(), typeof(DataStructure).Name, Vaiona.Entities.Logging.CrudState.Deleted);
                    }
                    return(PartialView("_message", new MessageModel()
                    {
                        Message = "DataStructure" + Id + "deleted",
                        hasMessage = false,
                        CssId = "deleted"
                    }));
                }
                finally
                {
                    dataStructureManager.Dispose();
                    missingValueManager.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();
            }
        }
Beispiel #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();
            }
        }