Example #1
0
        private bool unitInUse(Unit unit)
        {
            DataStructureManager dataStructureManager = null;
            DataContainerManager dataAttributeManager = null;

            try
            {
                dataAttributeManager = new DataContainerManager();
                dataStructureManager = new DataStructureManager();
                bool inUse = false;
                if (unit.Name.ToLower() == "none")
                {
                    inUse = true;
                }
                else if (dataAttributeManager.DataAttributeRepo.Query(d => d.Unit.Id.Equals(unit.Id)).Count() > 0)
                {
                    inUse = true;
                }
                else if (dataStructureManager.VariableRepo.Query(d => d.Unit.Id.Equals(unit.Id)).Count() > 0)
                {
                    inUse = true;
                }
                return(inUse);
            }
            finally
            {
                dataAttributeManager.Dispose();
                dataStructureManager.Dispose();
            }
        }
        public ActionResult deleteDataAttribute(long Id, string cssId = "")
        {
            MessageModel DataAttributeDeleteValidation = MessageModel.validateAttributeDelete(Id);

            if (DataAttributeDeleteValidation.hasMessage)
            {
                return(PartialView("_messageWindow", DataAttributeDeleteValidation));
            }
            else
            {
                DataContainerManager dataAttributeManager = null;
                try
                {
                    dataAttributeManager = new DataContainerManager();

                    DataAttribute dataAttribute = dataAttributeManager.DataAttributeRepo.Get(Id);

                    dataAttributeManager.DeleteDataAttribute(dataAttribute);

                    return(PartialView("_message", new MessageModel()
                    {
                        Message = "DataStructure" + Id + "deleted",
                        hasMessage = false,
                        CssId = "deleted"
                    }));
                }
                finally
                {
                    dataAttributeManager.Dispose();
                }
            }
        }
Example #3
0
        public ActionResult deletConstraint(long Id, long attributeId)
        {
            if (Id != 0 && attributeId != 0)
            {
                DataContainerManager dam = null;

                try
                {
                    dam = new DataContainerManager();
                    DataAttribute dataattribute = dam.DataAttributeRepo.Get(attributeId);
                    Constraint    constraint    = dam.DataAttributeRepo.Get(attributeId).Constraints.Where(c => c.Id == Id).FirstOrDefault();

                    foreach (Constraint c in dataattribute.Constraints.ToList())
                    {
                        if (c.Id == constraint.Id)
                        {
                            dataattribute.Constraints.Remove(c);
                            break;
                        }
                    }

                    dataattribute = dam.UpdateDataAttribute(dataattribute);
                }
                finally
                {
                    dam.Dispose();
                }
                //if (constraint is RangeConstraint)
                //    dam.RemoveConstraint((RangeConstraint)constraint);
            }
            return(RedirectToAction("openAttributeWindow", new { Id = attributeId, showConstraints = true }));
        }
Example #4
0
        /// <summary>
        ///
        /// </summary>
        /// <remarks></remarks>
        /// <seealso cref=""/>
        public void GetDataAttributeList()
        {
            DataContainerManager DataAttributeManager = null;

            try
            {
                DataAttributeManager   = new DataContainerManager();
                this.dataAttributeList = DataAttributeManager.DataAttributeRepo.Get().ToList();
            }
            finally
            {
                DataAttributeManager.Dispose();
            }
        }
Example #5
0
        public static MessageModel validateAttributeInUse(long Id)
        {
            DataContainerManager dataAttributeManager = null;

            try
            {
                dataAttributeManager = new DataContainerManager();
                DataAttribute dataAttribute = dataAttributeManager.DataAttributeRepo.Get(Id);
                return(validateAttributeInUse(Id, dataAttribute));
            }
            finally
            {
                dataAttributeManager.Dispose();
            }
        }
Example #6
0
        public AttributePreviewStruct fill(long attributeId, bool getConstraints)
        {
            DataContainerManager dataAttributeManager = null;

            try
            {
                dataAttributeManager = new DataContainerManager();
                DataAttribute DataAttribute = dataAttributeManager.DataAttributeRepo.Get(attributeId);

                return(this.fill(DataAttribute, getConstraints));
            }
            finally
            {
                dataAttributeManager.Dispose();
            }
        }
Example #7
0
 public DataAttributeManagerModel(DataAttributeModel dataAttributeModel)
 {
     try
     {
         dataContainerManager = new DataContainerManager();
         DataAttributeModel   = dataAttributeModel;
         dataContainerManager.DataAttributeRepo.Get().ToList().ForEach(da => DataAttributeStructs.Add(new DataAttributeStruct()
         {
             Id = da.Id, Name = da.Name, ShortName = da.ShortName, Description = da.Description, DataType = da.DataType.Name, Unit = da.Unit.Name, InUse = inUse(da), FormalDescriptions = getFormalDescriptions(da)
         }));
     }
     finally
     {
         dataContainerManager.Dispose();
     }
 }
        public DataAttribute storeDomainConstraint(DomainConstraintModel constraintModel)
        {
            DataContainerManager dcManager = null;

            try
            {
                dcManager = new DataContainerManager();

                DataAttribute     dataAttribute = dcManager.DataAttributeRepo.Get(constraintModel.AttributeId);
                List <DomainItem> items         = new List <DomainItem>();
                if (constraintModel.Terms != null && constraintModel.Terms.Trim() != "")
                {
                    items = createDomainItems(constraintModel.Terms.Trim());
                }

                if (items.Any())
                {
                    if (constraintModel.Id == 0)
                    {
                        DomainConstraint constraint = new DomainConstraint(ConstraintProviderSource.Internal, "", AppConfiguration.Culture.Name, constraintModel.Description, constraintModel.Negated, null, null, null, items);
                        dcManager.AddConstraint(constraint, dataAttribute);
                    }
                    else
                    {
                        DomainConstraint temp = new DomainConstraint();
                        for (int i = 0; i < dataAttribute.Constraints.Count; i++)
                        {
                            if (dataAttribute.Constraints.ElementAt(i).Id == constraintModel.Id)
                            {
                                temp = (DomainConstraint)dataAttribute.Constraints.ElementAt(i);
                                temp.Materialize();
                                temp.Description = constraintModel.Description;
                                temp.Negated     = constraintModel.Negated;
                                temp.Items       = items;
                                dcManager.AddConstraint(temp, dataAttribute);
                                break;
                            }
                        }
                    }
                }
                return(dataAttribute);
            }
            finally
            {
                dcManager.Dispose();
            }
        }
Example #9
0
        public List <VariableStruct> getOrderedVariableStructs(StructuredDataStructure structuredDataStructure)
        {
            List <VariableStruct> variableStructs      = new List <VariableStruct>();
            List <Variable>       variables            = getOrderedVariables(structuredDataStructure);
            DataContainerManager  dataAttributeManager = null;
            VariableStruct        temp = new VariableStruct();
            List <BExIS.Dlm.Entities.DataStructure.Constraint> tempconstraints;
            UnitDimenstionModel unitDimenstionModel = new UnitDimenstionModel();

            foreach (Variable v in variables)
            {
                try
                {
                    unitDimenstionModel     = new UnitDimenstionModel();
                    temp.variable           = v;
                    temp.unitStructs        = unitDimenstionModel.getUnitListByDimenstionAndDataType(v.DataAttribute.Unit.Dimension.Id, v.DataAttribute.DataType.Id);
                    dataAttributeManager    = new DataContainerManager();
                    tempconstraints         = dataAttributeManager.DataAttributeRepo.Get(v.DataAttribute.Id).Constraints.ToList();
                    temp.rangeConstraints   = new List <RangeConstraint>();
                    temp.domainConstraints  = new List <DomainConstraint>();
                    temp.patternConstraints = new List <PatternConstraint>();
                    foreach (BExIS.Dlm.Entities.DataStructure.Constraint c in tempconstraints)
                    {
                        if (c is DomainConstraint)
                        {
                            DomainConstraint tempDomainConstraint = (DomainConstraint)c;
                            tempDomainConstraint.Materialize();
                            temp.domainConstraints.Add(tempDomainConstraint);
                        }
                        if (c is PatternConstraint)
                        {
                            temp.patternConstraints.Add((PatternConstraint)c);
                        }
                        if (c is RangeConstraint)
                        {
                            temp.rangeConstraints.Add((RangeConstraint)c);
                        }
                    }
                    variableStructs.Add(temp);
                }
                finally
                {
                    dataAttributeManager.Dispose();
                }
            }
            return(variableStructs);
        }
Example #10
0
        public static MessageModel validateAttributeName(long Id, string Name, string cssId = "")
        {
            if (Name.Trim() == "" || string.IsNullOrEmpty(Name))
            {
                return(new MessageModel()
                {
                    hasMessage = true,
                    Message = "The name field is required.",
                    CssId = cssId
                });
            }
            else
            {
                DataContainerManager dataContainerManager = null;
                dataContainerManager = new DataContainerManager();
                try
                {
                    List <DataAttribute> dataAttributes = dataContainerManager.DataAttributeRepo.Get().ToList();

                    foreach (DataAttribute da in dataAttributes)
                    {
                        if (Id != da.Id)
                        {
                            if (da.Name.Trim().ToLower() == Name.Trim().ToLower())
                            {
                                return(new MessageModel()
                                {
                                    hasMessage = true,
                                    Message = "A variable template with same name already exists.",
                                    CssId = cssId
                                });
                            }
                        }
                    }
                }
                finally
                {
                    dataContainerManager.Dispose();
                }
            }
            return(new MessageModel()
            {
                CssId = cssId
            });
        }
Example #11
0
        public AttributePreviewModel fill(bool getConstraints)
        {
            this.AttributePreviews = new List <AttributePreviewStruct>();
            DataContainerManager dataAttributeManager = null;

            try
            {
                dataAttributeManager = new DataContainerManager();

                foreach (DataAttribute da in dataAttributeManager.DataAttributeRepo.Get().ToList())
                {
                    this.AttributePreviews.Add(new AttributePreviewStruct().fill(da, getConstraints));
                }
                return(this);
            }
            finally
            {
                dataAttributeManager.Dispose();
            }
        }
        public DataAttribute storeRangeConstraint(RangeConstraintModel constraintModel)
        {
            DataContainerManager dcManager = null;

            try
            {
                dcManager = new DataContainerManager();

                DataAttribute dataAttribute = dcManager.DataAttributeRepo.Get(constraintModel.AttributeId);

                if (constraintModel.Max != 0 || constraintModel.Min != 0)
                {
                    if (constraintModel.Id == 0)
                    {
                        RangeConstraint constraint = new RangeConstraint(ConstraintProviderSource.Internal, "", AppConfiguration.Culture.Name, constraintModel.Description, constraintModel.Negated, null, null, null, constraintModel.Min, constraintModel.MinInclude, constraintModel.Max, constraintModel.MaxInclude);
                        dcManager.AddConstraint(constraint, dataAttribute);
                    }
                    else
                    {
                        for (int i = 0; i < dataAttribute.Constraints.Count; i++)
                        {
                            if (dataAttribute.Constraints.ElementAt(i).Id == constraintModel.Id)
                            {
                                ((RangeConstraint)dataAttribute.Constraints.ElementAt(i)).Description        = constraintModel.Description;
                                ((RangeConstraint)dataAttribute.Constraints.ElementAt(i)).Negated            = constraintModel.Negated;
                                ((RangeConstraint)dataAttribute.Constraints.ElementAt(i)).Lowerbound         = constraintModel.Min;
                                ((RangeConstraint)dataAttribute.Constraints.ElementAt(i)).LowerboundIncluded = constraintModel.MinInclude;
                                ((RangeConstraint)dataAttribute.Constraints.ElementAt(i)).Upperbound         = constraintModel.Max;
                                ((RangeConstraint)dataAttribute.Constraints.ElementAt(i)).UpperboundIncluded = constraintModel.MaxInclude;
                                break;
                            }
                        }
                    }
                }
                return(dataAttribute);
            }
            finally
            {
                dcManager.Dispose();
            }
        }
Example #13
0
        public DataAttribute deletConstraint(long constraintId, DataAttribute attribute)
        {
            DataContainerManager dam = null;

            try
            {
                dam = new DataContainerManager();

                if (constraintId != 0 && attribute.Id != 0)
                {
                    foreach (Constraint c in attribute.Constraints.ToList())
                    {
                        if (c.Id == constraintId)
                        {
                            attribute.Constraints.Remove(c);
                            if (c is RangeConstraint)
                            {
                                dam.RemoveConstraint((RangeConstraint)c);
                            }
                            if (c is PatternConstraint)
                            {
                                dam.RemoveConstraint((PatternConstraint)c);
                            }
                            if (c is DomainConstraint)
                            {
                                dam.RemoveConstraint((DomainConstraint)c);
                            }
                            break;
                        }
                    }
                }
                return(attribute);
            }
            finally
            {
                dam.Dispose();
            }
        }
Example #14
0
        public new VariablePreviewStruct fill(long attributeId, bool getConstraints)
        {
            DataContainerManager dataAttributeManager = null;

            try
            {
                dataAttributeManager = new DataContainerManager();
                DataAttribute dataAttribute = dataAttributeManager.DataAttributeRepo.Get(attributeId);
                Variable      variable      = new Variable()
                {
                    Label         = dataAttribute.Name,
                    Description   = dataAttribute.Description,
                    Unit          = dataAttribute.Unit,
                    DataAttribute = dataAttribute
                };

                return(this.fill(variable, getConstraints));
            }
            finally
            {
                dataAttributeManager.Dispose();
            }
        }
        public DataAttribute storePatternConstraint(PatternConstraintModel constraintModel)
        {
            DataContainerManager dcManager = null;

            try
            {
                dcManager = new DataContainerManager();
                DataAttribute dataAttribute = dcManager.DataAttributeRepo.Get(constraintModel.AttributeId);

                if (constraintModel.MatchingPhrase != null && constraintModel.MatchingPhrase != "")
                {
                    if (constraintModel.Id == 0)
                    {
                        PatternConstraint constraint = new PatternConstraint(ConstraintProviderSource.Internal, "", AppConfiguration.Culture.Name, constraintModel.Description, constraintModel.Negated, null, null, null, constraintModel.MatchingPhrase, false);
                        dcManager.AddConstraint(constraint, dataAttribute);
                    }
                    else
                    {
                        for (int i = 0; i < dataAttribute.Constraints.Count; i++)
                        {
                            if (dataAttribute.Constraints.ElementAt(i).Id == constraintModel.Id)
                            {
                                ((PatternConstraint)dataAttribute.Constraints.ElementAt(i)).Description    = constraintModel.Description;
                                ((PatternConstraint)dataAttribute.Constraints.ElementAt(i)).Negated        = constraintModel.Negated;
                                ((PatternConstraint)dataAttribute.Constraints.ElementAt(i)).MatchingPhrase = constraintModel.MatchingPhrase;
                                break;
                            }
                        }
                    }
                }
                return(dataAttribute);
            }
            finally
            {
                dcManager.Dispose();
            }
        }
Example #16
0
 public ActionResult deletAttribute(long id, string name)
 {
     if (id != 0)
     {
         DataContainerManager DAM = null;
         try
         {
             DAM = new DataContainerManager();
             DataAttribute dataAttribute = DAM.DataAttributeRepo.Get(id);
             if (dataAttribute != null)
             {
                 if (!attributeInUse(dataAttribute))
                 {
                     DAM.DeleteDataAttribute(dataAttribute);
                 }
             }
         }
         finally
         {
             DAM.Dispose();
         }
     }
     return(RedirectToAction("AttributeManager"));
 }
Example #17
0
        /// <summary>
        ///
        /// </summary>
        /// <remarks></remarks>
        /// <seealso cref=""/>
        /// <param name="variables"></param>
        /// <param name="path"></param>
        /// <param name="filename"></param>
        /// <returns></returns>
        public SpreadsheetDocument CreateTemplate(List <long> variableIds, string path, string filename)
        {
            if (!Directory.Exists(Path.Combine(AppConfiguration.DataPath, path)))
            {
                Directory.CreateDirectory(Path.Combine(AppConfiguration.DataPath, path));
            }

            SpreadsheetDocument template          = SpreadsheetDocument.Open(Path.Combine(AppConfiguration.GetModuleWorkspacePath("RPM"), "Template", _fileName), true);
            SpreadsheetDocument dataStructureFile = SpreadsheetDocument.Create(Path.Combine(AppConfiguration.DataPath, path, filename), template.DocumentType);

            //dataStructureFile = SpreadsheetDocument.Open(Path.Combine(AppConfiguration.GetModuleWorkspacePath("RPM"), "Template", filename), true);



            foreach (OpenXmlPart part in template.GetPartsOfType <OpenXmlPart>())
            {
                OpenXmlPart newPart = dataStructureFile.AddPart <OpenXmlPart>(part);
            }

            template.Close();

            //uint iExcelIndex = 164;
            List <StyleIndexStruct> styleIndex = new List <StyleIndexStruct>();

            ExcelHelper.UpdateStylesheet(dataStructureFile.WorkbookPart.WorkbookStylesPart.Stylesheet, out styleIndex);

            Worksheet  worksheet = dataStructureFile.WorkbookPart.WorksheetParts.First().Worksheet;
            List <Row> rows      = GetRows(worksheet, 1, 11);

            List <Variable> variables = this.GetUnitOfWork().GetReadOnlyRepository <Variable>()
                                        .Query(p => variableIds.Contains(p.Id))
                                        .OrderBy(p => p.OrderNo)
                                        .ToList();

            foreach (Variable var in variables)
            {
                DataContainerManager CM = null;
                try
                {
                    CM = new DataContainerManager();
                    DataAttribute dataAttribute = CM.DataAttributeRepo.Get(var.DataAttribute.Id);

                    int    indexVar    = variables.ToList().IndexOf(var) + 1;
                    string columnIndex = GetClomunIndex(indexVar);

                    string cellRef = columnIndex + 1;
                    Cell   cell    = new Cell()
                    {
                        CellReference = cellRef,
                        StyleIndex    = (UInt32Value)4U,
                        DataType      = CellValues.String,
                        CellValue     = new CellValue(var.Label)
                    };
                    rows.ElementAt(0).AppendChild(cell);

                    cellRef = columnIndex + 2;
                    cell    = new Cell()
                    {
                        CellReference = cellRef,
                        DataType      = CellValues.String,
                        StyleIndex    = ExcelHelper.GetExcelStyleIndex(dataAttribute.DataType, styleIndex),
                        CellValue     = new CellValue("")
                    };
                    rows.ElementAt(1).AppendChild(cell);

                    cellRef = columnIndex + 3;
                    cell    = new Cell()
                    {
                        CellReference = cellRef,
                        StyleIndex    = (UInt32Value)4U,
                        DataType      = CellValues.String,
                        CellValue     = new CellValue(var.Id.ToString())
                    };
                    rows.ElementAt(2).AppendChild(cell);



                    cellRef = columnIndex + 4;
                    cell    = new Cell()
                    {
                        CellReference = cellRef,
                        StyleIndex    = (UInt32Value)4U,
                        DataType      = CellValues.String,
                        CellValue     = new CellValue(dataAttribute.ShortName)
                    };
                    rows.ElementAt(3).AppendChild(cell);

                    // description from variable
                    // if not then from attribute
                    string description = "";
                    description = String.IsNullOrEmpty(var.Description) ? dataAttribute.Description : var.Description;

                    cellRef = columnIndex + 5;
                    cell    = new Cell()
                    {
                        CellReference = cellRef,
                        StyleIndex    = (UInt32Value)4U,
                        DataType      = CellValues.String,
                        CellValue     = new CellValue(description)
                    };
                    rows.ElementAt(4).AppendChild(cell);

                    string classification = "";

                    if (dataAttribute.Classification != null)
                    {
                        classification = dataAttribute.Classification.Name;
                    }

                    cellRef = columnIndex + 6;
                    cell    = new Cell()
                    {
                        CellReference = cellRef,
                        StyleIndex    = (UInt32Value)4U,
                        DataType      = CellValues.String,
                        CellValue     = new CellValue(classification)
                    };
                    rows.ElementAt(5).AppendChild(cell);

                    string unit = "";

                    if (var.Unit != null)
                    {
                        unit = var.Unit.Name;
                    }

                    cellRef = columnIndex + 7;
                    cell    = new Cell()
                    {
                        CellReference = cellRef,
                        StyleIndex    = (UInt32Value)4U,
                        DataType      = CellValues.String,
                        CellValue     = new CellValue(unit)
                    };
                    rows.ElementAt(6).AppendChild(cell);

                    string dataType = "";

                    if (dataAttribute.DataType != null)
                    {
                        dataType = dataAttribute.DataType.Name;
                    }

                    cellRef = columnIndex + 8;
                    cell    = new Cell()
                    {
                        CellReference = cellRef,
                        StyleIndex    = (UInt32Value)4U,
                        DataType      = CellValues.String,
                        CellValue     = new CellValue(dataType)
                    };
                    rows.ElementAt(7).AppendChild(cell);

                    cellRef = columnIndex + 9;
                    cell    = new Cell()
                    {
                        CellReference = cellRef,
                        StyleIndex    = (UInt32Value)4U,
                        DataType      = CellValues.String,
                        CellValue     = new CellValue(var.IsValueOptional.ToString())
                    };
                    rows.ElementAt(8).AppendChild(cell);

                    cellRef = columnIndex + 10;
                    cell    = new Cell()
                    {
                        CellReference = cellRef,
                        StyleIndex    = (UInt32Value)4U,
                        DataType      = CellValues.String,
                        CellValue     = new CellValue(dataAttribute.IsMultiValue.ToString())
                    };
                    rows.ElementAt(9).AppendChild(cell);
                }
                finally
                {
                    CM.Dispose();
                }
            }

            foreach (DefinedName name in dataStructureFile.WorkbookPart.Workbook.GetFirstChild <DefinedNames>())
            {
                if (name.Name == "Data" || name.Name == "VariableIdentifiers")
                {
                    string[] tempArr = name.InnerText.Split('$');
                    string   temp    = "";
                    tempArr[tempArr.Count() - 2] = GetClomunIndex(variables.Count());
                    foreach (string t in tempArr)
                    {
                        if (t == tempArr.First())
                        {
                            temp = temp + t;
                        }
                        else
                        {
                            temp = temp + "$" + t;
                        }
                    }
                    name.Text = temp;
                }
            }

            //WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
            //WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();

            dataStructureFile.WorkbookPart.Workbook.Save();

            dataStructureFile.Close();

            return(dataStructureFile);
        }
Example #18
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();
            }
        }
Example #19
0
        public new VariablePreviewStruct fill(long attributeId, bool getConstraints)
        {
            DataContainerManager dataAttributeManager = null;

            XmlDocument settings = new XmlDocument();
            bool        optional = true;

            try
            {
                string filePath = Path.Combine(AppConfiguration.GetModuleWorkspacePath("RPM"), "Rpm.Settings.xml");
                settings.Load(filePath);
            }
            catch
            {
                settings = null;
            }

            if (settings != null)
            {
                try
                {
                    XmlNode optionalDefault = settings.GetElementsByTagName("optionalDefault")[0];
                    optional = Convert.ToBoolean(optionalDefault.InnerText);
                }
                catch
                {
                    optional = true;
                }

                try
                {
                    XmlNodeList missingValues = settings.GetElementsByTagName("missingValues")[0].ChildNodes;

                    foreach (XmlNode xn in missingValues)
                    {
                        this.MissingValues.Add(new MissingValueStruct()
                        {
                            Id          = 0,
                            DisplayName = xn["placeholder"].InnerText,
                            Description = xn["description"].InnerText
                        });
                    }
                }
                catch
                {
                    this.MissingValues = new List <MissingValueStruct>();
                }
            }

            try
            {
                dataAttributeManager = new DataContainerManager();
                DataAttribute dataAttribute = dataAttributeManager.DataAttributeRepo.Get(attributeId);
                Variable      variable      = new Variable()
                {
                    Label           = dataAttribute.Name,
                    Description     = "",
                    Unit            = dataAttribute.Unit,
                    DataAttribute   = dataAttribute,
                    IsValueOptional = optional
                };
                return(this.fill(variable, getConstraints));
            }
            finally
            {
                dataAttributeManager.Dispose();
            }
        }
Example #20
0
        // create read attributes in bpp
        public void CreateAttributes(ref DataTable mappedAttributes)
        {
            DataContainerManager attributeManager = null;
            DataTypeManager      dataTypeManager  = null;
            UnitManager          unitManager      = null;

            try
            {
                attributeManager = new DataContainerManager();
                dataTypeManager  = new DataTypeManager();
                unitManager      = new UnitManager();

                foreach (DataRow mapAttributesRow in mappedAttributes.Rows)
                {
                    DataAttribute attribute = new DataAttribute();

                    // values of the attribute
                    attribute.ShortName    = mapAttributesRow["ShortName"].ToString();
                    attribute.Name         = mapAttributesRow["Name"].ToString();
                    attribute.Description  = mapAttributesRow["Description"].ToString();
                    attribute.IsMultiValue = false;
                    attribute.IsBuiltIn    = false;
                    //attribute.Owner = "BMM";
                    attribute.Scope                    = "";
                    attribute.MeasurementScale         = MeasurementScale.Categorial; ////////!!!!!!!!fromMapping??????????????????
                    attribute.ContainerType            = DataContainerType.ReferenceType;
                    attribute.EntitySelectionPredicate = "";
                    attribute.DataType                 = dataTypeManager.Repo.Get(Convert.ToInt64(mapAttributesRow["DataTypeId"]));
                    attribute.Unit               = unitManager.Repo.Get(Convert.ToInt64(mapAttributesRow["UnitId"]));
                    attribute.Methodology        = null;
                    attribute.Classification     = null;
                    attribute.AggregateFunctions = null;
                    attribute.GlobalizationInfos = null;
                    attribute.Constraints        = null;
                    attribute.ExtendedProperties = null;

                    DataAttribute dataAttribute  = new DataAttribute();
                    DataAttribute existAttribute = attributeManager.DataAttributeRepo.Get(a =>
                                                                                          attribute.Name.Equals(a.Name) &&
                                                                                          attribute.ShortName.Equals(a.ShortName) &&
                                                                                          attribute.Unit.Id.Equals(a.Unit.Id) &&
                                                                                          attribute.DataType.Id.Equals(a.DataType.Id)
                                                                                          ).FirstOrDefault();

                    // if attribute not exists (name, shortName) then create
                    if (existAttribute == null)
                    {
                        dataAttribute = attributeManager.CreateDataAttribute(
                            attribute.ShortName,
                            attribute.Name,
                            attribute.Description,
                            attribute.IsMultiValue,
                            attribute.IsBuiltIn,
                            attribute.Scope,
                            attribute.MeasurementScale,
                            attribute.ContainerType,
                            attribute.EntitySelectionPredicate,
                            attribute.DataType,
                            attribute.Unit,
                            attribute.Methodology,
                            attribute.Classification,
                            attribute.AggregateFunctions,
                            attribute.GlobalizationInfos,
                            attribute.Constraints,
                            attribute.ExtendedProperties
                            );
                    }
                    else
                    {
                        dataAttribute = existAttribute;
                    }

                    // add attributeId to the mappedAttributes Table
                    mapAttributesRow["AttributeId"] = dataAttribute.Id;
                }
            }
            finally
            {
                attributeManager.Dispose();
                dataTypeManager.Dispose();
                unitManager.Dispose();
            }
        }
        public MessageModel storeAtttribute(long Id, string Name, long unitId, long dataTypeId, string Description = "", string cssId = "", bool inUse = false)
        {
            Name        = Server.UrlDecode(Name);
            Description = Server.UrlDecode(Description);
            MessageModel DataStructureValidation = MessageModel.validateAttributeInUse(Id);

            if (DataStructureValidation.hasMessage && inUse == false)
            {
                return(DataStructureValidation);
            }
            else
            {
                DataStructureValidation = MessageModel.validateAttributeName(Id, Name, cssId);
                if (DataStructureValidation.hasMessage)
                {
                    return(DataStructureValidation);
                }
                else
                {
                    DataContainerManager dataAttributeManager = null;
                    UnitManager          um = null;
                    DataTypeManager      dataTypeManager = null;

                    try
                    {
                        dataAttributeManager = new DataContainerManager();
                        DataAttribute dataAttribute;

                        if (Id == 0)
                        {
                            um = new UnitManager();

                            Unit unit = um.Repo.Get(unitId);

                            dataTypeManager = new DataTypeManager();

                            DataType dataType = dataTypeManager.Repo.Get(dataTypeId);
                            dataAttribute = dataAttributeManager.CreateDataAttribute(Name, Name, Description, false, false, "", MeasurementScale.Categorial, DataContainerType.ReferenceType, "", dataType, unit, null, null, null, null, null, null);
                            return(new MessageModel()
                            {
                                Message = dataAttribute.Id.ToString(),
                                hasMessage = false,
                                CssId = "refresh"
                            });
                        }
                        else
                        {
                            dataAttribute             = dataAttributeManager.DataAttributeRepo.Get(Id);
                            dataAttribute.Name        = Name;
                            dataAttribute.Description = Description;
                            dataAttribute             = dataAttributeManager.UpdateDataAttribute(dataAttribute);
                            return(new MessageModel()
                            {
                                Message = Id.ToString(),
                                hasMessage = false,
                                CssId = "refresh"
                            });
                        }
                    }
                    finally
                    {
                        dataAttributeManager.Dispose();
                        um.Dispose();
                        dataTypeManager.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();
            }
        }
        //[MeasurePerformance]
        //temporary solution: norman
        //For original solution, look into Aquadiva Code
        public List <Error> FinishUpload(EasyUploadTaskManager taskManager)
        {
            DataStructureManager dsm = new DataStructureManager();
            DatasetManager       dm  = new DatasetManager();
            DataContainerManager dam = new DataContainerManager();
            //SubjectManager sm = new SubjectManager();
            EntityPermissionManager entityPermissionManager = new EntityPermissionManager();

            List <Error> temp = new List <Error>();

            try
            {
                using (IUnitOfWork unitOfWork = this.GetUnitOfWork())
                {
                    // initialize all necessary manager

                    DataTuple[] rows = null;

                    //First, try to validate - if there are errors, return immediately
                    string       JsonArray        = TaskManager.Bus[EasyUploadTaskManager.SHEET_JSON_DATA].ToString();
                    List <Error> ValidationErrors = ValidateRows(JsonArray);
                    if (ValidationErrors.Count != 0)
                    {
                        temp.AddRange(ValidationErrors);
                        return(temp);
                    }

                    string timestamp = DateTime.UtcNow.ToString("r");
                    string title     = Convert.ToString(TaskManager.Bus[EasyUploadTaskManager.FILENAME]);

                    if (TaskManager.Bus.ContainsKey(EasyUploadTaskManager.DESCRIPTIONTITLE))
                    {
                        string tmp = Convert.ToString(TaskManager.Bus[EasyUploadTaskManager.DESCRIPTIONTITLE]);
                        if (!String.IsNullOrWhiteSpace(tmp))
                        {
                            title = Convert.ToString(TaskManager.Bus[EasyUploadTaskManager.DESCRIPTIONTITLE]);
                        }
                    }

                    StructuredDataStructure   sds         = null;
                    List <VariableIdentifier> identifiers = new List <VariableIdentifier>(); //Used in Excel reader
                    //Try to find an exact matching datastructure
                    Boolean foundReusableDataStructure = false;

                    List <RowModel> headers = (List <RowModel>)TaskManager.Bus[EasyUploadTaskManager.ROWS];

                    //For some reason, MappedHeaders might be in a different order in this list than what's indicated by its IDs - to prevent mismatching, sort the headers
                    headers.Sort((m1, m2) => m1.Index.CompareTo(m2.Index));

                    List <StructuredDataStructure> allDatastructures = dsm.StructuredDataStructureRepo.Get().ToList();
                    foreach (StructuredDataStructure existingStructure in allDatastructures)
                    {
                        if (!foundReusableDataStructure)
                        {
                            //For now a datastructure is considered an exact match if it contains variables with
                            //the same names (labels), datatypes and units in the correct order
                            List <Variable> variablesOfExistingStructure = existingStructure.Variables.ToList();
                            foundReusableDataStructure = true;
                            if (variablesOfExistingStructure.Count != headers.Count)
                            {
                                foundReusableDataStructure = false;
                            }
                            else
                            {
                                for (int i = 0; i < variablesOfExistingStructure.Count; i++)
                                {
                                    Variable exVar         = variablesOfExistingStructure.ElementAt(i);
                                    RowModel currentHeader = headers.ElementAt(i);
                                    if (exVar.Label != currentHeader.Name ||
                                        exVar.Unit.Id != currentHeader.SelectedUnit.UnitId ||
                                        exVar.DataAttribute.DataType.Id != currentHeader.SelectedDataType.DataTypeId)
                                    {
                                        foundReusableDataStructure = false;
                                    }
                                }
                            }
                            if (foundReusableDataStructure)
                            {
                                sds = existingStructure;
                                foreach (Variable exVar in variablesOfExistingStructure)
                                {
                                    VariableIdentifier vi = new VariableIdentifier
                                    {
                                        name = exVar.Label,
                                        id   = exVar.Id
                                    };
                                    identifiers.Add(vi);
                                }
                            }
                        }
                    }

                    if (!foundReusableDataStructure)
                    {
                        sds = dsm.CreateStructuredDataStructure(title, title + " " + timestamp, "", "", DataStructureCategory.Generic);
                    }

                    TaskManager.AddToBus(EasyUploadTaskManager.DATASTRUCTURE_ID, sds.Id);
                    TaskManager.AddToBus(EasyUploadTaskManager.DATASTRUCTURE_TITLE, title + " " + timestamp);

                    if (!TaskManager.Bus.ContainsKey(EasyUploadTaskManager.DATASET_TITLE))
                    {
                        TaskManager.AddToBus(EasyUploadTaskManager.DATASET_TITLE, title);
                        TaskManager.AddToBus(EasyUploadTaskManager.TITLE, title);
                    }

                    MetadataStructure metadataStructure = null;
                    if (TaskManager.Bus.ContainsKey(EasyUploadTaskManager.SCHEMA))
                    {
                        long metadataStructureId = Convert.ToInt64(TaskManager.Bus[EasyUploadTaskManager.SCHEMA]);
                        metadataStructure = unitOfWork.GetReadOnlyRepository <MetadataStructure>()
                                            .Get(m => m.Id == metadataStructureId).FirstOrDefault();
                    }
                    else
                    {
                        //Default option but shouldn't happen because previous steps can't be finished without selecting the metadata-structure
                        metadataStructure = unitOfWork.GetReadOnlyRepository <MetadataStructure>()
                                            .Get(m => m.Name.ToLower().Contains("eml")).FirstOrDefault();
                    }
                    ResearchPlan rp = unitOfWork.GetReadOnlyRepository <ResearchPlan>().Get().FirstOrDefault();
                    TaskManager.AddToBus(EasyUploadTaskManager.RESEARCHPLAN_ID, rp.Id);
                    TaskManager.AddToBus(EasyUploadTaskManager.RESEARCHPLAN_TITLE, rp.Title);

                    #region Progress Information

                    if (TaskManager.Bus.ContainsKey(EasyUploadTaskManager.CURRENTPACKAGESIZE))
                    {
                        TaskManager.Bus[EasyUploadTaskManager.CURRENTPACKAGESIZE] = 0;
                    }
                    else
                    {
                        TaskManager.Bus.Add(EasyUploadTaskManager.CURRENTPACKAGESIZE, 0);
                    }

                    if (TaskManager.Bus.ContainsKey(EasyUploadTaskManager.CURRENTPACKAGE))
                    {
                        TaskManager.Bus[EasyUploadTaskManager.CURRENTPACKAGE] = 0;
                    }
                    else
                    {
                        TaskManager.Bus.Add(EasyUploadTaskManager.CURRENTPACKAGE, 0);
                    }

                    #endregion Progress Information

                    if (!foundReusableDataStructure)
                    {
                        #region Set Variables and information for new DataStructure

                        XmlDocument xmldoc       = new XmlDocument();
                        XmlElement  extraElement = xmldoc.CreateElement("extra");
                        XmlElement  orderElement = xmldoc.CreateElement("order");

                        //Sorting necessary to prevent problems when inserting the tuples
                        headers.OrderBy(r => r.Index);

                        var dataTypeRepo      = unitOfWork.GetReadOnlyRepository <DataType>();
                        var unitRepo          = unitOfWork.GetReadOnlyRepository <Unit>();
                        var dataAttributeRepo = unitOfWork.GetReadOnlyRepository <DataAttribute>();

                        List <DataAttribute> allDataAttributes = dataAttributeRepo.Get().ToList();

                        foreach (RowModel header in headers)
                        {
                            int i = headers.IndexOf(header);

                            DataType dataType            = dataTypeRepo.Get(header.SelectedDataType.DataTypeId);
                            Unit     CurrentSelectedUnit = unitRepo.Get(header.SelectedUnit.UnitId);

                            DataAttribute CurrentDataAttribute = new DataAttribute();
                            //If possible, map the chosen variable name, unit and datatype to an existing DataAttribute (Exact match)
                            DataAttribute existingDataAttribute = allDataAttributes.Where(da => da.Name.ToLower().Equals(TrimAndLimitString(header.SelectedDataAttribute.Name).ToLower()) &&
                                                                                          da.Unit.Dimension == CurrentSelectedUnit.Dimension).FirstOrDefault();
                            if (existingDataAttribute != null)
                            {
                                CurrentDataAttribute = existingDataAttribute;
                            }
                            else
                            {
                                //No matching DataAttribute => Create a new one
                                CurrentDataAttribute = dam.CreateDataAttribute(TrimAndLimitString(header.Name), header.Name, header.SelectedDataAttribute.Description, false, false, "", MeasurementScale.Categorial, DataContainerType.ReferenceType, "", dataType, CurrentSelectedUnit, null, null, null, null, null, null);
                            }

                            Variable           newVariable = dsm.AddVariableUsage(sds, CurrentDataAttribute, true, header.Name, "", "", "", CurrentSelectedUnit);
                            VariableIdentifier vi          = new VariableIdentifier
                            {
                                name = newVariable.Label,
                                id   = newVariable.Id
                            };
                            identifiers.Add(vi);

                            XmlElement newVariableXml = xmldoc.CreateElement("variable");
                            newVariableXml.InnerText = Convert.ToString(newVariable.Id);

                            orderElement.AppendChild(newVariableXml);
                        }
                        extraElement.AppendChild(orderElement);
                        xmldoc.AppendChild(extraElement);

                        sds.Extra       = xmldoc;
                        sds.Name        = "generated import structure " + timestamp;
                        sds.Description = "automatically generated structured data structure by user " + GetUsernameOrDefault() + " for file " + title + " on " + timestamp;

                        #endregion Set Variables and information for new DataStructure
                    }

                    Dataset ds = null;
                    ds = dm.CreateEmptyDataset(sds, rp, metadataStructure);

                    long datasetId = ds.Id;
                    long sdsId     = sds.Id;

                    if (dm.IsDatasetCheckedOutFor(datasetId, GetUsernameOrDefault()) || dm.CheckOutDataset(datasetId, GetUsernameOrDefault()))
                    {
                        DatasetVersion    dsv = dm.GetDatasetWorkingCopy(datasetId);
                        long              METADATASTRUCTURE_ID = metadataStructure.Id;
                        XmlMetadataWriter xmlMetadatWriter     = new XmlMetadataWriter(XmlNodeMode.xPath);
                        XDocument         metadataX            = xmlMetadatWriter.CreateMetadataXml(METADATASTRUCTURE_ID);
                        XmlDocument       metadataXml          = XmlMetadataWriter.ToXmlDocument(metadataX);
                        dsv.Metadata = metadataXml;
                        try
                        {
                            dsv.Metadata = xmlDatasetHelper.SetInformation(dsv, metadataXml, NameAttributeValues.title, title);
                            dsv.Title    = title;
                        }
                        catch (NullReferenceException ex)
                        {
                            //Reference of the title node is missing
                            throw new NullReferenceException("The extra-field of this metadata-structure is missing the title-node-reference!");
                        }
                        dm.EditDatasetVersion(dsv, null, null, null);
                    }

                    #region security

                    // add security
                    if (GetUsernameOrDefault() != "DEFAULT")
                    {
                        foreach (RightType rightType in Enum.GetValues(typeof(RightType)).Cast <RightType>())
                        {
                            //The user gets full permissions
                            // add security
                            if (GetUsernameOrDefault() != "DEFAULT")
                            {
                                entityPermissionManager.Create <User>(GetUsernameOrDefault(), "Dataset", typeof(Dataset), ds.Id, Enum.GetValues(typeof(RightType)).Cast <RightType>().ToList());
                            }
                        }
                    }

                    #endregion security

                    #region excel reader

                    int packageSize  = 100000;
                    int numberOfRows = 0;
                    //HACK ?
                    TaskManager.Bus[EasyUploadTaskManager.CURRENTPACKAGESIZE] = packageSize;

                    int counter = 0;

                    dm.CheckOutDatasetIfNot(ds.Id, GetUsernameOrDefault()); // there are cases, the dataset does not get checked out!!
                    if (!dm.IsDatasetCheckedOutFor(ds.Id, GetUsernameOrDefault()))
                    {
                        throw new Exception(string.Format("Not able to checkout dataset '{0}' for  user '{1}'!", ds.Id, GetUsernameOrDefault()));
                    }

                    DatasetVersion workingCopy = dm.GetDatasetWorkingCopy(ds.Id);

                    counter++;
                    TaskManager.Bus[EasyUploadTaskManager.CURRENTPACKAGE] = counter;

                    //rows = reader.ReadFile(Stream, TaskManager.Bus[TaskManager.FILENAME].ToString(), oldSds, (int)id, packageSize).ToArray();

                    List <string> selectedDataAreaJsonArray   = (List <string>)TaskManager.Bus[EasyUploadTaskManager.SHEET_DATA_AREA];
                    string        selectedHeaderAreaJsonArray = TaskManager.Bus[EasyUploadTaskManager.SHEET_HEADER_AREA].ToString();
                    List <int[]>  areaDataValuesList          = new List <int[]>();
                    foreach (string area in selectedDataAreaJsonArray)
                    {
                        areaDataValuesList.Add(JsonConvert.DeserializeObject <int[]>(area));
                    }
                    int[] areaHeaderValues = JsonConvert.DeserializeObject <int[]>(selectedHeaderAreaJsonArray);

                    Orientation orientation = 0;

                    switch (TaskManager.Bus[EasyUploadTaskManager.SHEET_FORMAT].ToString())
                    {
                    case "LeftRight":
                        orientation = Orientation.rowwise;
                        break;

                    case "Matrix":
                        //orientation = Orientation.matrix;
                        break;

                    default:
                        orientation = Orientation.columnwise;
                        break;
                    }

                    String worksheetUri = null;
                    //Get the Uri to identify the correct worksheet
                    if (TaskManager.Bus.ContainsKey(EasyUploadTaskManager.ACTIVE_WOKSHEET_URI))
                    {
                        worksheetUri = TaskManager.Bus[EasyUploadTaskManager.ACTIVE_WOKSHEET_URI].ToString();
                    }

                    int batchSize = (new Object()).GetUnitOfWork().PersistenceManager.PreferredPushSize;
                    int batchnr   = 1;
                    foreach (int[] areaDataValues in areaDataValuesList)
                    {
                        //First batch starts at the start of the current data area
                        int currentBatchStartRow = areaDataValues[0] + 1;
                        while (currentBatchStartRow <= areaDataValues[2] + 1) //While the end of the current data area has not yet been reached
                        {
                            //End row is start row plus batch size
                            int currentBatchEndRow = currentBatchStartRow + batchSize;

                            //Set the indices for the reader
                            EasyUploadFileReaderInfo fri = new EasyUploadFileReaderInfo
                            {
                                DataStartRow = currentBatchStartRow,
                                //End row is either at the end of the batch or the end of the marked area
                                //DataEndRow = (currentBatchEndRow > areaDataValues[2] + 1) ? areaDataValues[2] + 1 : currentBatchEndRow,
                                DataEndRow = Math.Min(currentBatchEndRow, areaDataValues[2] + 1),
                                //Column indices as marked in a previous step
                                DataStartColumn = areaDataValues[1] + 1,
                                DataEndColumn   = areaDataValues[3] + 1,

                                //Header area as marked in a previous step
                                VariablesStartRow    = areaHeaderValues[0] + 1,
                                VariablesStartColumn = areaHeaderValues[1] + 1,
                                VariablesEndRow      = areaHeaderValues[2] + 1,
                                VariablesEndColumn   = areaHeaderValues[3] + 1,

                                Offset      = areaDataValues[1],
                                Orientation = orientation
                            };
                            //Create a new reader each time because the reader saves ALL tuples it read and therefore the batch processing wouldn't work
                            EasyUploadExcelReader reader = new EasyUploadExcelReader(sds, fri);
                            // open file
                            Stream = reader.Open(TaskManager.Bus[EasyUploadTaskManager.FILEPATH].ToString());

                            //Set variable identifiers because they might differ from the variable names in the file
                            reader.setSubmittedVariableIdentifiers(identifiers);

                            //Read the rows and convert them to DataTuples
                            rows = reader.ReadFile(Stream, TaskManager.Bus[EasyUploadTaskManager.FILENAME].ToString(), fri, (int)datasetId, worksheetUri);

                            //After reading the rows, add them to the dataset
                            if (rows != null)
                            {
                                dm.EditDatasetVersion(workingCopy, rows.ToList(), null, null);
                                numberOfRows += rows.Count();
                            }

                            //Close the Stream so the next ExcelReader can open it again
                            Stream.Close();

                            //Debug information
                            int lines   = (areaDataValues[2] + 1) - (areaDataValues[0] + 1);
                            int batches = lines / batchSize;
                            batchnr++;

                            //Next batch starts after the current one
                            currentBatchStartRow = currentBatchEndRow + 1;
                        }
                    }

                    #endregion excel reader

                    //set modification
                    workingCopy.ModificationInfo = new EntityAuditInfo()
                    {
                        Performer  = GetUsernameOrDefault(),
                        Comment    = "Data",
                        ActionType = AuditActionType.Create
                    };

                    dm.EditDatasetVersion(workingCopy, null, null, null);

                    dm.CheckInDataset(ds.Id, "Import " + numberOfRows + " rows", GetUsernameOrDefault());

                    //Reindex search
                    if (this.IsAccessible("DDM", "SearchIndex", "ReIndexSingle"))
                    {
                        this.Run("DDM", "SearchIndex", "ReIndexSingle", new RouteValueDictionary()
                        {
                            { "id", datasetId }
                        });
                    }

                    TaskManager.AddToBus(EasyUploadTaskManager.DATASET_ID, ds.Id);

                    return(temp);
                }
            }
            catch (Exception ex)
            {
                temp.Add(new Error(ErrorType.Other, "An error occured during the upload. " +
                                   "Please try again later. If this problem keeps occuring, please contact your administrator."));
                return(temp);
            }
            finally
            {
                dsm.Dispose();
                dm.Dispose();
                dam.Dispose();
                //sm.Dispose();
                entityPermissionManager.Dispose();
            }
        }
Example #24
0
        public StructuredDataStructure CreateADataStructure()
        {
            var unitManager      = new UnitManager();
            var dataTypeManager  = new DataTypeManager();
            var attributeManager = new DataContainerManager();
            var dsManager        = new DataStructureManager();

            try
            {
                var dim  = unitManager.Create("TestDimnesion", "For Unit Testing", "");
                var unit = unitManager.Create("None_UT", "NoneUT", "Use in unit tsting", dim, Dlm.Entities.DataStructure.MeasurementSystem.Metric);

                var intType      = dataTypeManager.Create("Integer", "Integer", TypeCode.Int32);
                var strType      = dataTypeManager.Create("String", "String", TypeCode.String);
                var doubleType   = dataTypeManager.Create("Double", "Double", TypeCode.Double);
                var boolType     = dataTypeManager.Create("Bool", "Bool", TypeCode.Boolean);
                var dateTimeType = dataTypeManager.Create("DateTime", "DateTime", TypeCode.DateTime);

                var dataAttribute1 = attributeManager.CreateDataAttribute(
                    "att1UT", "att1UT", "Attribute for Unit testing",
                    false, false, "", Dlm.Entities.DataStructure.MeasurementScale.Nominal, Dlm.Entities.DataStructure.DataContainerType.ValueType,
                    "", intType, unit,
                    null, null, null, null, null, null
                    );

                var dataAttribute2 = attributeManager.CreateDataAttribute(
                    "att2UT", "att1UT", "Attribute for Unit testing",
                    false, false, "", Dlm.Entities.DataStructure.MeasurementScale.Nominal, Dlm.Entities.DataStructure.DataContainerType.ValueType,
                    "", strType, unit,
                    null, null, null, null, null, null
                    );

                var dataAttribute3 = attributeManager.CreateDataAttribute(
                    "att3UT", "att3UT", "Attribute for Unit testing",
                    false, false, "", Dlm.Entities.DataStructure.MeasurementScale.Nominal, Dlm.Entities.DataStructure.DataContainerType.ValueType,
                    "", doubleType, unit,
                    null, null, null, null, null, null
                    );

                var dataAttribute4 = attributeManager.CreateDataAttribute(
                    "att4UT", "att4UT", "Attribute for Unit testing",
                    false, false, "", Dlm.Entities.DataStructure.MeasurementScale.Nominal, Dlm.Entities.DataStructure.DataContainerType.ValueType,
                    "", boolType, unit,
                    null, null, null, null, null, null
                    );

                var dataAttribute5 = attributeManager.CreateDataAttribute(
                    "att5UT", "att5UT", "Attribute for Unit testing",
                    false, false, "", Dlm.Entities.DataStructure.MeasurementScale.Nominal, Dlm.Entities.DataStructure.DataContainerType.ValueType,
                    "", dateTimeType, unit,
                    null, null, null, null, null, null
                    );

                StructuredDataStructure dataStructure = dsManager.CreateStructuredDataStructure("dsForTesting", "DS for unit testing", "", "", Dlm.Entities.DataStructure.DataStructureCategory.Generic);
                dsManager.AddVariableUsage(dataStructure, dataAttribute1, true, "var1UT", "", "", "Used for unit testing");
                dsManager.AddVariableUsage(dataStructure, dataAttribute2, true, "var2UT", "", "", "Used for unit testing");
                dsManager.AddVariableUsage(dataStructure, dataAttribute3, true, "var3UT", "", "", "Used for unit testing");
                dsManager.AddVariableUsage(dataStructure, dataAttribute4, true, "var4UT", "", "", "Used for unit testing");
                dsManager.AddVariableUsage(dataStructure, dataAttribute5, true, "var5UT", "", "", "Used for unit testing");
                return(dataStructure);
            }
            catch (Exception ex) { return(null); }
            finally
            {
                unitManager.Dispose();
                dataTypeManager.Dispose();
                attributeManager.Dispose();
                dsManager.Dispose();
            }
        }
Example #25
0
        private DataAttribute storeConstraint(ConstraintModel constraintModel, DataAttribute dataAttribute)
        {
            DataContainerManager dcManager = null;

            try
            {
                dcManager = new DataContainerManager();

                if (constraintModel is RangeConstraintModel)
                {
                    RangeConstraintModel rcm = (RangeConstraintModel)constraintModel;

                    if (rcm.Id == 0)
                    {
                        RangeConstraint constraint = new RangeConstraint(ConstraintProviderSource.Internal, "", AppConfiguration.Culture.Name, rcm.Description, rcm.Negated, null, null, null, rcm.Min, rcm.MinInclude, rcm.Max, rcm.MaxInclude);
                        dcManager.AddConstraint(constraint, dataAttribute);
                    }
                    else
                    {
                        for (int i = 0; i < dataAttribute.Constraints.Count; i++)
                        {
                            if (dataAttribute.Constraints.ElementAt(i).Id == rcm.Id)
                            {
                                ((RangeConstraint)dataAttribute.Constraints.ElementAt(i)).Description        = rcm.Description;
                                ((RangeConstraint)dataAttribute.Constraints.ElementAt(i)).Negated            = rcm.Negated;
                                ((RangeConstraint)dataAttribute.Constraints.ElementAt(i)).Lowerbound         = rcm.Min;
                                ((RangeConstraint)dataAttribute.Constraints.ElementAt(i)).LowerboundIncluded = rcm.MinInclude;
                                ((RangeConstraint)dataAttribute.Constraints.ElementAt(i)).Upperbound         = rcm.Max;
                                ((RangeConstraint)dataAttribute.Constraints.ElementAt(i)).UpperboundIncluded = rcm.MaxInclude;
                                break;
                            }
                        }
                    }
                }

                if (constraintModel is PatternConstraintModel)
                {
                    PatternConstraintModel pcm = (PatternConstraintModel)constraintModel;

                    if (pcm.Id == 0)
                    {
                        PatternConstraint constraint = new PatternConstraint(ConstraintProviderSource.Internal, "", AppConfiguration.Culture.Name, pcm.Description, pcm.Negated, null, null, null, pcm.MatchingPhrase, true);
                        dcManager.AddConstraint(constraint, dataAttribute);
                    }
                    else
                    {
                        for (int i = 0; i < dataAttribute.Constraints.Count; i++)
                        {
                            if (dataAttribute.Constraints.ElementAt(i).Id == pcm.Id)
                            {
                                ((PatternConstraint)dataAttribute.Constraints.ElementAt(i)).Description    = pcm.Description;
                                ((PatternConstraint)dataAttribute.Constraints.ElementAt(i)).Negated        = pcm.Negated;
                                ((PatternConstraint)dataAttribute.Constraints.ElementAt(i)).MatchingPhrase = pcm.MatchingPhrase;
                                break;
                            }
                        }
                    }
                }

                if (constraintModel is DomainConstraintModel)
                {
                    DomainConstraintModel dcm = (DomainConstraintModel)constraintModel;

                    List <DomainItem> items = createDomainItems(dcm.Terms);

                    dcm.Terms = cutSpaces(dcm.Terms);

                    if (items.Count > 0)
                    {
                        if (dcm.Id == 0)
                        {
                            DomainConstraint constraint = new DomainConstraint(ConstraintProviderSource.Internal, "", AppConfiguration.Culture.Name, dcm.Description, dcm.Negated, null, null, null, items);
                            dcManager.AddConstraint(constraint, dataAttribute);
                        }
                        else
                        {
                            DomainConstraint temp = new DomainConstraint();
                            for (int i = 0; i < dataAttribute.Constraints.Count; i++)
                            {
                                if (dataAttribute.Constraints.ElementAt(i).Id == dcm.Id)
                                {
                                    temp = (DomainConstraint)dataAttribute.Constraints.ElementAt(i);
                                    temp.Materialize();
                                    temp.Description = dcm.Description;
                                    temp.Negated     = dcm.Negated;
                                    temp.Items       = items;
                                    dcManager.AddConstraint(temp, dataAttribute);
                                    break;
                                }
                            }
                        }
                    }
                }

                return(dataAttribute);
            }
            finally
            {
                dcManager.Dispose();
            }
        }
        //[MeasurePerformance]
        //temporary solution: norman
        //For original solution, look into Aquadiva Code
        public List <Error> FinishUpload(EasyUploadTaskManager taskManager)
        {
            DataStructureManager dsm = new DataStructureManager();
            DatasetManager       dm  = new DatasetManager();
            DataContainerManager dam = new DataContainerManager();
            //SubjectManager sm = new SubjectManager();
            EntityPermissionManager entityPermissionManager = new EntityPermissionManager();

            List <Error> temp = new List <Error>();

            try
            {
                using (IUnitOfWork unitOfWork = this.GetUnitOfWork())
                {
                    // initialize all necessary manager

                    DataTuple[] rows = null;

                    //First, try to validate - if there are errors, return immediately
                    string       JsonArray        = TaskManager.Bus[EasyUploadTaskManager.SHEET_JSON_DATA].ToString();
                    List <Error> ValidationErrors = ValidateRows(JsonArray);
                    if (ValidationErrors.Count != 0)
                    {
                        temp.AddRange(ValidationErrors);
                        return(temp);
                    }

                    string timestamp = DateTime.UtcNow.ToString("r");
                    string title     = Convert.ToString(TaskManager.Bus[EasyUploadTaskManager.FILENAME]);

                    if (TaskManager.Bus.ContainsKey(EasyUploadTaskManager.DESCRIPTIONTITLE))
                    {
                        string tmp = Convert.ToString(TaskManager.Bus[EasyUploadTaskManager.DESCRIPTIONTITLE]);
                        if (!String.IsNullOrWhiteSpace(tmp))
                        {
                            title = Convert.ToString(TaskManager.Bus[EasyUploadTaskManager.DESCRIPTIONTITLE]);
                        }
                    }

                    StructuredDataStructure sds = dsm.CreateStructuredDataStructure(title, title + " " + timestamp, "", "", DataStructureCategory.Generic);

                    TaskManager.AddToBus(EasyUploadTaskManager.DATASTRUCTURE_ID, sds.Id);
                    TaskManager.AddToBus(EasyUploadTaskManager.DATASTRUCTURE_TITLE, title + " " + timestamp);

                    if (!TaskManager.Bus.ContainsKey(EasyUploadTaskManager.DATASET_TITLE))
                    {
                        TaskManager.AddToBus(EasyUploadTaskManager.DATASET_TITLE, title);
                        TaskManager.AddToBus(EasyUploadTaskManager.TITLE, title);
                    }

                    MetadataStructure metadataStructure = null;
                    if (TaskManager.Bus.ContainsKey(EasyUploadTaskManager.SCHEMA))
                    {
                        long metadataStructureId = Convert.ToInt64(TaskManager.Bus[EasyUploadTaskManager.SCHEMA]);
                        metadataStructure = unitOfWork.GetReadOnlyRepository <MetadataStructure>()
                                            .Get(m => m.Id == metadataStructureId).FirstOrDefault();
                    }
                    else
                    {
                        //Default option but shouldn't happen because previous steps can't be finished without selecting the metadata-structure
                        metadataStructure = unitOfWork.GetReadOnlyRepository <MetadataStructure>()
                                            .Get(m => m.Name.ToLower().Contains("eml")).FirstOrDefault();
                    }
                    ResearchPlan rp = unitOfWork.GetReadOnlyRepository <ResearchPlan>().Get().FirstOrDefault();
                    TaskManager.AddToBus(EasyUploadTaskManager.RESEARCHPLAN_ID, rp.Id);
                    TaskManager.AddToBus(EasyUploadTaskManager.RESEARCHPLAN_TITLE, rp.Title);

                    #region Progress Information

                    if (TaskManager.Bus.ContainsKey(EasyUploadTaskManager.CURRENTPACKAGESIZE))
                    {
                        TaskManager.Bus[EasyUploadTaskManager.CURRENTPACKAGESIZE] = 0;
                    }
                    else
                    {
                        TaskManager.Bus.Add(EasyUploadTaskManager.CURRENTPACKAGESIZE, 0);
                    }

                    if (TaskManager.Bus.ContainsKey(EasyUploadTaskManager.CURRENTPACKAGE))
                    {
                        TaskManager.Bus[EasyUploadTaskManager.CURRENTPACKAGE] = 0;
                    }
                    else
                    {
                        TaskManager.Bus.Add(EasyUploadTaskManager.CURRENTPACKAGE, 0);
                    }

                    #endregion

                    #region DataStructure
                    XmlDocument xmldoc       = new XmlDocument();
                    XmlElement  extraElement = xmldoc.CreateElement("extra");
                    XmlElement  orderElement = xmldoc.CreateElement("order");


                    List <Tuple <int, string, UnitInfo> > MappedHeaders = (List <Tuple <int, string, UnitInfo> >)TaskManager.Bus[EasyUploadTaskManager.VERIFICATION_MAPPEDHEADERUNITS];
                    //Sorting necessary to prevent problems when inserting the tuples
                    MappedHeaders.Sort((head1, head2) => head1.Item1.CompareTo(head2.Item1));
                    List <VariableIdentifier> identifiers = new List <VariableIdentifier>();

                    var dataTypeRepo      = unitOfWork.GetReadOnlyRepository <DataType>();
                    var unitRepo          = unitOfWork.GetReadOnlyRepository <Unit>();
                    var dataAttributeRepo = unitOfWork.GetReadOnlyRepository <DataAttribute>();

                    List <DataAttribute> allDataAttributes = dataAttributeRepo.Get().ToList();

                    foreach (Tuple <int, string, UnitInfo> Entry in MappedHeaders)
                    {
                        int i = MappedHeaders.IndexOf(Entry);

                        DataType dataType            = dataTypeRepo.Get(Entry.Item3.SelectedDataTypeId);
                        Unit     CurrentSelectedUnit = unitRepo.Get(Entry.Item3.UnitId);

                        DataAttribute CurrentDataAttribute = new DataAttribute();
                        //If possible, map the chosen variable name, unit and datatype to an existing DataAttribute (Exact match)
                        DataAttribute existingDataAttribute = allDataAttributes.Where(da => da.Name.ToLower().Equals(TrimAndLimitString(Entry.Item2).ToLower()) &&
                                                                                      da.DataType.Id == dataType.Id &&
                                                                                      da.Unit.Id == CurrentSelectedUnit.Id).FirstOrDefault();
                        if (existingDataAttribute != null)
                        {
                            CurrentDataAttribute = existingDataAttribute;
                        }
                        else
                        {
                            //No matching DataAttribute => Create a new one
                            CurrentDataAttribute = dam.CreateDataAttribute(TrimAndLimitString(Entry.Item2), Entry.Item2, "", false, false, "", MeasurementScale.Categorial, DataContainerType.ReferenceType, "", dataType, CurrentSelectedUnit, null, null, null, null, null, null);
                        }

                        Variable           newVariable = dsm.AddVariableUsage(sds, CurrentDataAttribute, true, Entry.Item2, "", "", "");
                        VariableIdentifier vi          = new VariableIdentifier
                        {
                            name = newVariable.Label,
                            id   = newVariable.Id
                        };
                        identifiers.Add(vi);

                        XmlElement newVariableXml = xmldoc.CreateElement("variable");
                        newVariableXml.InnerText = Convert.ToString(newVariable.Id);

                        orderElement.AppendChild(newVariableXml);
                    }
                    extraElement.AppendChild(orderElement);
                    xmldoc.AppendChild(extraElement);

                    sds.Extra       = xmldoc;
                    sds.Name        = "generated import structure " + timestamp;
                    sds.Description = "automatically generated structured data structure by user " + GetUsernameOrDefault() + " for file " + title + " on " + timestamp;

                    #endregion

                    Dataset ds = null;
                    ds = dm.CreateEmptyDataset(sds, rp, metadataStructure);

                    //TODO Should a template be created?

                    /*ExcelTemplateProvider etp = new ExcelTemplateProvider();
                     * etp.CreateTemplate(sds);*/

                    long datasetId = ds.Id;
                    long sdsId     = sds.Id;


                    if (dm.IsDatasetCheckedOutFor(datasetId, GetUsernameOrDefault()) || dm.CheckOutDataset(datasetId, GetUsernameOrDefault()))
                    {
                        DatasetVersion    dsv = dm.GetDatasetWorkingCopy(datasetId);
                        long              METADATASTRUCTURE_ID = metadataStructure.Id;
                        XmlMetadataWriter xmlMetadatWriter     = new XmlMetadataWriter(XmlNodeMode.xPath);
                        XDocument         metadataX            = xmlMetadatWriter.CreateMetadataXml(METADATASTRUCTURE_ID);
                        XmlDocument       metadataXml          = XmlMetadataWriter.ToXmlDocument(metadataX);
                        dsv.Metadata = metadataXml;
                        try
                        {
                            dsv.Metadata = xmlDatasetHelper.SetInformation(dsv, metadataXml, NameAttributeValues.title, title);
                        }
                        catch (NullReferenceException ex)
                        {
                            //Reference of the title node is missing
                            throw new NullReferenceException("The extra-field of this metadata-structure is missing the title-node-reference!");
                        }
                        dm.EditDatasetVersion(dsv, null, null, null);
                    }


                    #region security
                    // add security
                    if (GetUsernameOrDefault() != "DEFAULT")
                    {
                        //PermissionManager pm = new PermissionManager();

                        //User user = sm.GetUserByName(GetUsernameOrDefault());

                        //Rights-Management

                        /*
                         * TODO: Use the BExIS Party API for that
                         *
                         * */
                        /*
                         * UserPiManager upm = new UserPiManager();
                         * List<long> piList = (new UserSelectListModel(GetUsernameOrDefault())).UserList.Select(x => x.Id).ToList();
                         */


                        foreach (RightType rightType in Enum.GetValues(typeof(RightType)).Cast <RightType>())
                        {
                            //The user gets full permissions
                            // add security
                            if (GetUsernameOrDefault() != "DEFAULT")
                            {
                                entityPermissionManager.Create <User>(GetUsernameOrDefault(), "Dataset", typeof(Dataset), ds.Id, Enum.GetValues(typeof(RightType)).Cast <RightType>().ToList());
                            }

                            // adding the rights for the pis

                            /*foreach (long piId in piList)
                             * {
                             *  //Each pi gets full permissions
                             *  pm.CreateDataPermission(piId, 1, ds.Id, rightType);
                             *
                             *  // get all pi members
                             *  List<UserPi> currentMembers = upm.GetAllPiMember(piId).ToList();
                             *
                             *  foreach (UserPi currentMapping in currentMembers)
                             *  {
                             *      switch (rightType)
                             *      {
                             *          //Each member of each of the pis gets view-rights
                             *          case RightType.View:
                             *              pm.CreateDataPermission(currentMapping.UserId, 1, ds.Id, rightType);
                             *              break;
                             *          //Each member of each of the pis gets download-rights
                             *          case RightType.Download:
                             *              pm.CreateDataPermission(currentMapping.UserId, 1, ds.Id, rightType);
                             *              break;
                             *          default:
                             *              //No other permissions - is this call necessary?
                             *              pm.CreateDataPermission(currentMapping.UserId, 0, ds.Id, rightType);
                             *              break;
                             *      }
                             *  }
                             * }*/
                        }
                    }
                    #endregion security


                    #region excel reader

                    int packageSize = 10000;
                    //HACK ?
                    TaskManager.Bus[EasyUploadTaskManager.CURRENTPACKAGESIZE] = packageSize;

                    int counter = 0;

                    dm.CheckOutDatasetIfNot(ds.Id, GetUsernameOrDefault()); // there are cases, the dataset does not get checked out!!
                    if (!dm.IsDatasetCheckedOutFor(ds.Id, GetUsernameOrDefault()))
                    {
                        throw new Exception(string.Format("Not able to checkout dataset '{0}' for  user '{1}'!", ds.Id, GetUsernameOrDefault()));
                    }

                    DatasetVersion workingCopy = dm.GetDatasetWorkingCopy(ds.Id);

                    counter++;
                    TaskManager.Bus[EasyUploadTaskManager.CURRENTPACKAGE] = counter;

                    //rows = reader.ReadFile(Stream, TaskManager.Bus[TaskManager.FILENAME].ToString(), oldSds, (int)id, packageSize).ToArray();

                    List <string> selectedDataAreaJsonArray   = (List <string>)TaskManager.Bus[EasyUploadTaskManager.SHEET_DATA_AREA];
                    string        selectedHeaderAreaJsonArray = TaskManager.Bus[EasyUploadTaskManager.SHEET_HEADER_AREA].ToString();
                    List <int[]>  areaDataValuesList          = new List <int[]>();
                    foreach (string area in selectedDataAreaJsonArray)
                    {
                        areaDataValuesList.Add(JsonConvert.DeserializeObject <int[]>(area));
                    }
                    int[] areaHeaderValues = JsonConvert.DeserializeObject <int[]>(selectedHeaderAreaJsonArray);

                    Orientation orientation = 0;

                    switch (TaskManager.Bus[EasyUploadTaskManager.SHEET_FORMAT].ToString())
                    {
                    case "LeftRight":
                        orientation = Orientation.rowwise;
                        break;

                    case "Matrix":
                        //orientation = Orientation.matrix;
                        break;

                    default:
                        orientation = Orientation.columnwise;
                        break;
                    }

                    String worksheetUri = null;
                    //Get the Uri to identify the correct worksheet
                    if (TaskManager.Bus.ContainsKey(EasyUploadTaskManager.ACTIVE_WOKSHEET_URI))
                    {
                        worksheetUri = TaskManager.Bus[EasyUploadTaskManager.ACTIVE_WOKSHEET_URI].ToString();
                    }

                    int batchSize = (new Object()).GetUnitOfWork().PersistenceManager.PreferredPushSize;
                    int batchnr   = 1;
                    foreach (int[] areaDataValues in areaDataValuesList)
                    {
                        //First batch starts at the start of the current data area
                        int currentBatchStartRow = areaDataValues[0] + 1;
                        while (currentBatchStartRow <= areaDataValues[2] + 1) //While the end of the current data area has not yet been reached
                        {
                            //Create a new reader each time because the reader saves ALL tuples it read and therefore the batch processing wouldn't work
                            EasyUploadExcelReader reader = new EasyUploadExcelReader();
                            // open file
                            Stream = reader.Open(TaskManager.Bus[EasyUploadTaskManager.FILEPATH].ToString());

                            //End row is start row plus batch size
                            int currentBatchEndRow = currentBatchStartRow + batchSize;

                            //Set the indices for the reader
                            EasyUploadFileReaderInfo fri = new EasyUploadFileReaderInfo
                            {
                                DataStartRow = currentBatchStartRow,
                                //End row is either at the end of the batch or the end of the marked area
                                //DataEndRow = (currentBatchEndRow > areaDataValues[2] + 1) ? areaDataValues[2] + 1 : currentBatchEndRow,
                                DataEndRow = Math.Min(currentBatchEndRow, areaDataValues[2] + 1),
                                //Column indices as marked in a previous step
                                DataStartColumn = areaDataValues[1] + 1,
                                DataEndColumn   = areaDataValues[3] + 1,

                                //Header area as marked in a previous step
                                VariablesStartRow    = areaHeaderValues[0] + 1,
                                VariablesStartColumn = areaHeaderValues[1] + 1,
                                VariablesEndRow      = areaHeaderValues[2] + 1,
                                VariablesEndColumn   = areaHeaderValues[3] + 1,

                                Offset      = areaDataValues[1],
                                Orientation = orientation
                            };

                            //Set variable identifiers because they might differ from the variable names in the file
                            reader.setSubmittedVariableIdentifiers(identifiers);

                            //Read the rows and convert them to DataTuples
                            rows = reader.ReadFile(Stream, TaskManager.Bus[EasyUploadTaskManager.FILENAME].ToString(), fri, sds, (int)datasetId, worksheetUri);

                            //After reading the rows, add them to the dataset
                            if (rows != null)
                            {
                                dm.EditDatasetVersion(workingCopy, rows.ToList(), null, null);
                            }

                            //Close the Stream so the next ExcelReader can open it again
                            Stream.Close();

                            //Debug information
                            int lines   = (areaDataValues[2] + 1) - (areaDataValues[0] + 1);
                            int batches = lines / batchSize;
                            batchnr++;

                            //Next batch starts after the current one
                            currentBatchStartRow = currentBatchEndRow + 1;
                        }
                    }

                    #endregion


                    dm.CheckInDataset(ds.Id, "upload data from upload wizard", GetUsernameOrDefault());

                    //Reindex search
                    if (this.IsAccessibale("DDM", "SearchIndex", "ReIndexSingle"))
                    {
                        this.Run("DDM", "SearchIndex", "ReIndexSingle", new RouteValueDictionary()
                        {
                            { "id", datasetId }
                        });
                    }

                    TaskManager.AddToBus(EasyUploadTaskManager.DATASET_ID, ds.Id);

                    return(temp);
                }
            }
            catch (Exception ex)
            {
                temp.Add(new Error(ErrorType.Other, "An error occured during the upload. " +
                                   "Please try again later. If this problem keeps occuring, please contact your administrator."));
                return(temp);
            }
            finally
            {
                dsm.Dispose();
                dm.Dispose();
                dam.Dispose();
                //sm.Dispose();
                entityPermissionManager.Dispose();
            }
        }