Наследование: BaseEntity
Пример #1
0
        public bool AddAssociatedDataType(Unit end1, IEnumerable<DataType> end2)
        {
            Contract.Requires(end1 != null && end1.Id >= 0);
            Contract.Requires(Contract.ForAll(end2, (DataType e) => e != null));
            Contract.Requires(Contract.ForAll(end2, (DataType e) => e.Id >= 0));

            bool result = false;
            using (IUnitOfWork uow = this.GetUnitOfWork())
            {
                IRepository<Unit> repo = uow.GetRepository<Unit>();

                end1 = repo.Reload(end1);
                repo.LoadIfNot(end1.AssociatedDataTypes);
                foreach (var e2 in end2)
                {
                    if (!end1.AssociatedDataTypes.Contains(e2))
                    {
                        end1.AssociatedDataTypes.Add(e2);
                        e2.ApplicableUnits.Add(end1);
                    }
                }
                uow.Commit();
                result = true;
            }
            return (result);
        }
Пример #2
0
        public bool AddAssociatedDataType(Unit end1, DataType end2)
        {
            Contract.Requires(end1 != null && end1.Id >= 0);
            Contract.Requires(end2 != null && end2.Id >= 0);

            bool result = false;
            using (IUnitOfWork uow = this.GetUnitOfWork())
            {
                IRepository<Unit> repo = uow.GetRepository<Unit>();

                end1 = repo.Reload(end1);
                repo.LoadIfNot(end1.AssociatedDataTypes);
                if (!end1.AssociatedDataTypes.Contains(end2))
                {
                    end1.AssociatedDataTypes.Add(end2);
                    end2.ApplicableUnits.Add(end1);
                    uow.Commit();
                    result = true;
                }
            }
            return (result);
        }
Пример #3
0
        public DataAttribute CreateDataAttribute(string shortName, string name, string description, bool isMultiValue, bool isBuiltIn, string scope, MeasurementScale measurementScale, DataContainerType containerType, string entitySelectionPredicate,
            DataType dataType, Unit unit, Methodology methodology, Classifier classifier,
            ICollection<AggregateFunction> functions, ICollection<GlobalizationInfo> globalizationInfos, ICollection<Constraint> constraints,
            ICollection<ExtendedProperty> extendedProperies
            )
        {
            Contract.Requires(!string.IsNullOrWhiteSpace(shortName));
            Contract.Requires(dataType != null && dataType.Id >= 0);
            Contract.Requires(unit != null && unit.Id >= 0);

            Contract.Ensures(Contract.Result<DataAttribute>() != null && Contract.Result<DataAttribute>().Id >= 0);
            DataAttribute e = new DataAttribute()
            {
                ShortName = shortName,
                Name = name,
                Description = description,
                IsMultiValue = isMultiValue,
                IsBuiltIn = isBuiltIn,
                Scope = scope,
                MeasurementScale = measurementScale,
                ContainerType = containerType,
                EntitySelectionPredicate = entitySelectionPredicate,
                DataType = dataType,
                Unit = unit,
                Methodology = methodology,
                AggregateFunctions = functions,
                GlobalizationInfos = globalizationInfos,
                Constraints = constraints,
                ExtendedProperties = extendedProperies,
            };
            if (classifier != null && classifier.Id > 0)
                e.Classification = classifier;
            using (IUnitOfWork uow = this.GetUnitOfWork())
            {
                IRepository<DataAttribute> repo = uow.GetRepository<DataAttribute>();
                repo.Put(e);
                uow.Commit();
            }
            return (e);
        }
Пример #4
0
        public ActionResult editAttribute(DataAttributeModel Model)
        {
            ViewBag.Title = PresentationModel.GetViewTitleForTenant( "Manage Data Attributes", this.Session.GetTenant());
            DataContainerManager dataAttributeManager = new DataContainerManager();
            IList<DataAttribute> DataAttributeList = dataAttributeManager.DataAttributeRepo.Get();
            long tempUnitId = Convert.ToInt64(Model.Unit.Id);
            long tempDataTypeId = Convert.ToInt64(Model.DataType.Id);

            Model.Id = Model.Id;
            Model.ShortName = cutSpaces(Model.ShortName);
            Model.Name = cutSpaces(Model.Name);
            Model.Description = cutSpaces(Model.Description);

            //if (Model.DomainConstraints.Count > 0)
            //{
            //    if (Model.DomainConstraints. != null && Model.DomainItems.Count > 0)
            //    {
            //        Model.DomainConstraints.FirstOrDefault().DomainItems = clearEmptyItems(Model.DomainItems);
            //    }
            //}

            if (Model.Name == "" | Model.Name == null)
            {
                Session["nameMsg"] = "invalid Name";
                Session["Window"] = true;
                return View("AttributeManager", new DataAttributeManagerModel(Model));
            }
            else
            {
                bool nameNotExist = DataAttributeList.Where(p => p.Name.ToLower().Equals(Model.Name.ToLower())).Count().Equals(0);

                if (Model.Id == 0)
                {
                    if (nameNotExist)
                    {
                        UnitManager UM = new UnitManager();
                        Unit unit = new Unit();
                        DataTypeManager DTM = new DataTypeManager();
                        DataType dataType = new DataType();
                        DataContainerManager DAM = new DataContainerManager();

                        DataAttribute temp = new DataAttribute();

                        if(UM.Repo.Get(tempUnitId)!= null)
                            unit = UM.Repo.Get(tempUnitId);
                        else
                            unit = UM.Repo.Get().Where(u => u.Name.ToLower() == "none").FirstOrDefault();

                        if (DTM.Repo.Get(tempDataTypeId) != null)
                            dataType = DTM.Repo.Get(tempDataTypeId);
                        else
                            dataType = DTM.Repo.Get().ToList().FirstOrDefault();

                        temp = DAM.CreateDataAttribute(Model.ShortName, Model.Name, Model.Description, false, false, "", MeasurementScale.Categorial, DataContainerType.ReferenceType, "", dataType, unit, null, null, null, null, null, null);

                        #region store constraint

                        if (Model.RangeConstraints.Count > 0 && (Model.RangeConstraints.FirstOrDefault().Min !=null || Model.RangeConstraints.FirstOrDefault().Max !=null)
                            && (Model.RangeConstraints.FirstOrDefault().Min != 0.0 || Model.RangeConstraints.FirstOrDefault().Max != 0.0 ))
                            temp = storeConstraint(Model.RangeConstraints.First(), temp);

                        if (Model.PatternConstraints.Count > 0 && !String.IsNullOrEmpty(Model.PatternConstraints.FirstOrDefault().MatchingPhrase))
                            temp = storeConstraint(Model.PatternConstraints.First(), temp);

                        if (Model.DomainConstraints.Count > 0)
                        {
                            foreach (DomainConstraintModel d in Model.DomainConstraints)
                            {
                                temp = storeConstraint(d, temp);
                            }
                        }

                        #endregion

                        temp = DAM.UpdateDataAttribute(temp);
                    }
                    else
                    {
                        Session["nameMsg"] = "Name already exist";
                        Session["Window"] = true;
                        return View("AttributeManager", new DataAttributeManagerModel(Model));
                    }
                }
                else
                {
                    if (nameNotExist || DataAttributeList.Where(p => p.Name.Equals(Model.Name)).ToList().First().Id == Model.Id)
                    {
                        DataAttribute dataAttribute = DataAttributeList.Where(p => p.Id.Equals(Model.Id)).ToList().First();
                        if (!attributeInUse(dataAttribute))
                        {
                            DataContainerManager DAM = new DataContainerManager();
                            dataAttribute.Name = cutSpaces(Model.Name);
                            dataAttribute.ShortName = Model.ShortName;
                            dataAttribute.Description = Model.Description;
                            UnitManager UM = new UnitManager();

                            if (UM.Repo.Get(tempUnitId) != null)
                                dataAttribute.Unit = UM.Repo.Get(tempUnitId);
                            else
                                dataAttribute.Unit = UM.Repo.Get().Where(u => u.Name.ToLower() == "none").FirstOrDefault();

                            DataTypeManager DTM = new DataTypeManager();

                            if (DTM.Repo.Get(tempDataTypeId) != null)
                                dataAttribute.DataType = DTM.Repo.Get(tempDataTypeId);
                            else
                                dataAttribute.DataType = DTM.Repo.Get().ToList().FirstOrDefault();

                            #region store constraint

                            if (Model.RangeConstraints.Count > 0 && (Model.RangeConstraints.FirstOrDefault().Min != null || Model.RangeConstraints.FirstOrDefault().Max != null)
                                && (Model.RangeConstraints.FirstOrDefault().Min != 0.0 || Model.RangeConstraints.FirstOrDefault().Max != 0.0))
                                dataAttribute = storeConstraint(Model.RangeConstraints.First(), dataAttribute);
                            else
                                dataAttribute = deletConstraint(Model.RangeConstraints.First().Id, dataAttribute);

                            if (Model.PatternConstraints.Count > 0 && !String.IsNullOrEmpty(Model.PatternConstraints.FirstOrDefault().MatchingPhrase))
                                dataAttribute = storeConstraint(Model.PatternConstraints.First(), dataAttribute);
                            else
                                dataAttribute = deletConstraint(Model.PatternConstraints.First().Id, dataAttribute);

                            if (Model.PatternConstraints.Count > 0 && !String.IsNullOrEmpty(Model.DomainConstraints.FirstOrDefault().Terms))
                                dataAttribute = storeConstraint(Model.DomainConstraints.First(), dataAttribute);
                            else
                                dataAttribute = deletConstraint(Model.DomainConstraints.First().Id, dataAttribute);

                            #endregion
                            DAM.UpdateDataAttribute(dataAttribute);
                        }
                    }
                    else
                    {
                        Session["nameMsg"] = "Name already exist";
                        Session["Window"] = true;
                        return View("AttributeManager", new DataAttributeManagerModel(Model));
                    }
                }
            }

            Session["Window"] = false;
            return RedirectToAction("AttributeManager");
        }
Пример #5
0
        /// <summary>
        /// Creates a link between a <see cref="StructuredDataStructure"/> and <see cref="DataAttribute"/>. This link is known as <see cref="Variable"/>.
        /// In addition to what a variable inherits from the associated data attribute, it can have its own label, default and missing values, and optionality of its value.
        /// </summary>
        /// <param name="dataStructure">The structured data structure to be linked to the data attribute</param>
        /// <param name="dataAttribute">The data attribute to be used in a data structure as a variable</param>
        /// <param name="isValueOptional">Indicates whether the <see cref="VariableValue"/> associated to the variable is optional or not. This allows dataset to not provide data values for optional variables.</param>
        /// <param name="label">The display name of the variable. It may differ from the associated data attribute name. The variable label usually indicates the role of the data attribute in the structure. 
        /// Its possible for a data structure to use a data attribute more than once by creating more than one variables, hence having different labels.</param>
        /// <param name="defaultValue">The default value of the associated variable values. Mainly considered for user interface purposes.</param>
        /// <param name="missingValue">A specific sentinel value that when is put into the variable values, means those values are missing and should not be considered data.</param>
        /// <param name="variableUnit">A specific unit for the variable. If not provided the unit of the <paramref name="dataAttibute"/> is used.
        /// If provided, its dimension must be equal to the dimension of the <paramref name="dataAttribute"/>'s unit.</param>
        /// <returns>A created and persisted variable object.</returns>
        public Variable AddVariableUsage(StructuredDataStructure dataStructure, DataAttribute dataAttribute, bool isValueOptional, string label, string defaultValue, string missingValue, string description, Unit variableUnit = null)
        {
            Contract.Requires(dataStructure != null && dataStructure.Id >= 0);
            Contract.Requires(dataAttribute != null && dataAttribute.Id >= 0);
            Contract.Requires((variableUnit == null && dataAttribute.Unit == null) || (variableUnit == null) || (variableUnit.Dimension == dataAttribute.Unit.Dimension));
            Contract.Ensures(Contract.Result<Variable>() != null && Contract.Result<Variable>().Id >= 0);

            //StructuredDataStructureRepo.Reload(dataStructure);
            StructuredDataStructureRepo.LoadIfNot(dataStructure.Variables);
            int count = (from v in dataStructure.Variables
                         where v.DataAttribute.Id.Equals(dataAttribute.Id)
                         select v
                        )
                        .Count();

            //if (count > 0)
            //    throw new Exception(string.Format("Data attribute {0} is already used as a variable in data structure {0}", dataAttribute.Id, dataStructure.Id));

            Variable usage = new Variable()
            {
                DataStructure = dataStructure,
                DataAttribute = dataAttribute,
                MinCardinality = isValueOptional ? 0 : 1,
                // if there is no label provided, use the data attribute name and a sequence number calculated by the number of occurrences of that data attribute in the current structure
                Label = !string.IsNullOrWhiteSpace(label) ? label : (count <= 0 ? dataAttribute.Name : string.Format("{0} ({1})", dataAttribute.Name, count)),
                DefaultValue = defaultValue,
                MissingValue = missingValue,
                Description = description,
                Unit = (variableUnit != null ? variableUnit : dataAttribute.Unit),
            };
            dataAttribute.UsagesAsVariable.Add(usage);
            dataStructure.Variables.Add(usage);
            using (IUnitOfWork uow = this.GetUnitOfWork())
            {
                IRepository<Variable> repo = uow.GetRepository<Variable>();
                repo.Put(usage);
                uow.Commit();
            }
            return (usage);
        }
Пример #6
0
 public UnitItemModel(Unit unit)
 {
     Id = unit.Id;
     Name = unit.Name;
 }
Пример #7
0
        public bool Delete(Unit entity)
        {
            Contract.Requires(entity != null);
            Contract.Requires(entity.Id >= 0);

            using (IUnitOfWork uow = this.GetUnitOfWork())
            {
                IRepository<Unit> repo = uow.GetRepository<Unit>();
                IRepository<ConversionMethod> repoCM = uow.GetRepository<ConversionMethod>();

                entity = repo.Reload(entity);

                // delete all conversions that are somehow connected to current unit
                repoCM.Delete(entity.ConversionsIamTheSource);
                repoCM.Delete(entity.ConversionsIamTheTarget);

                // remove all associations between current unit and the conversions
                entity.ConversionsIamTheSource.ToList().ForEach(a => a.Source = a.Target = null);
                entity.ConversionsIamTheTarget.ToList().ForEach(a => a.Source = a.Target = null);
                entity.ConversionsIamTheSource.Clear();
                entity.ConversionsIamTheTarget.Clear();

                //delete the unit
                repo.Delete(entity);

                // commit changes
                uow.Commit();
            }
            // if any problem was detected during the commit, an exception will be thrown!
            return (true);
        }
Пример #8
0
        public Unit Update(Unit entity)
        {
            Contract.Requires(entity != null, "provided entity can not be null");
            Contract.Requires(entity.Id >= 0, "provided entity must have a permanent ID");

            Contract.Ensures(Contract.Result<Unit>() != null && Contract.Result<Unit>().Id >= 0, "No entity is persisted!");

            using (IUnitOfWork uow = this.GetUnitOfWork())
            {
                IRepository<Unit> repo = uow.GetRepository<Unit>();
                repo.Put(entity); // Merge is required here!!!!
                uow.Commit();
            }
            return (entity);
        }
Пример #9
0
        /// <summary>
        /// if there is no conversion method between source and target units, creates one otherwise fails
        /// </summary>
        /// <param name="description"></param>
        /// <param name="formula"></param>
        /// <param name="source"></param>
        /// <param name="target"></param>
        /// <returns></returns>
        public ConversionMethod CreateConversionMethod(string formula, string description, Unit source, Unit target)
        {
            Contract.Requires(!string.IsNullOrWhiteSpace(formula), "formula can not be empty");
            Contract.Requires(formula.Contains("s"), "Formula must use \'s\' as the representative for the source unit");
            Contract.Requires(source != null, "source unit can not be null");
            Contract.Requires(source.Id >= 0, "source unit must be persisted before this call");
            Contract.Requires(target != null, "target unit can not be null");
            Contract.Requires(target.Id >= 0, "target unit must be persisted before this call");

            Contract.Ensures(Contract.Result<ConversionMethod>() != null && Contract.Result<ConversionMethod>().Id >= 0, "No Conversion Method persisted!");

            ConversionMethod cm = new ConversionMethod()
            {
                Formula = formula,
                Description = description,
                Source = source,
                Target = target,
            };

            source.ConversionsIamTheSource.Add(cm);
            target.ConversionsIamTheTarget.Add(cm);

            using (IUnitOfWork uow = this.GetUnitOfWork())
            {
                IRepository<ConversionMethod> repoCM = uow.GetRepository<ConversionMethod>();
                ConversionMethod temp = repoCM.Get(c => c.Source.Id == source.Id && c.Target.Id == target.Id).FirstOrDefault(); // change it to Count instead of Get
                if (temp != null)
                    throw new Exception(string.Format("There is already a conversion method between {0} and {1} having [{2}] formula", cm.Source.Name, cm.Target.Name, cm.Formula));
                repoCM.Put(cm);
                uow.Commit();
            }
            return (cm);
        }
Пример #10
0
        public Unit Create(string name, string abbreviation, string description, Dimension dimension, MeasurementSystem measurementSystem)
        {
            Contract.Requires(!string.IsNullOrWhiteSpace(name));
            Contract.Requires(!string.IsNullOrWhiteSpace(abbreviation));
            Contract.Requires(dimension != null);

            Contract.Ensures(Contract.Result<Unit>() != null && Contract.Result<Unit>().Id >= 0);

            Unit u = new Unit()
            {
                Name = name,
                Abbreviation = abbreviation,
                Description = description,
                Dimension = dimension,
                MeasurementSystem = measurementSystem,
            };

            using (IUnitOfWork uow = this.GetUnitOfWork())
            {
                IRepository<Unit> repo = uow.GetRepository<Unit>();
                repo.Put(u);
                uow.Commit();
            }
            return (u);
        }
Пример #11
0
        // create read units in bpp
        public void CreateUnits(ref DataTable mappedUnits)
        {
            UnitManager unitManager = new UnitManager();
            DataTypeManager dataTypeManger = new DataTypeManager();
            Unit unit = new Unit();

            foreach (DataRow mapUnitsRow in mappedUnits.Rows)
            {
                // values of the unit
                unit.Name = mapUnitsRow["Name"].ToString();
                unit.Abbreviation = mapUnitsRow["Abbreviation"].ToString();
                unit.Description = mapUnitsRow["Description"].ToString();

                if (unit.Description.Length > 255)
                    unit.Description = unit.Description.Substring(0, 255);

                unit.Dimension = unitManager.DimensionRepo.Get(Convert.ToInt64(mapUnitsRow["DimensionId"]));

                // find measurement system
                foreach (MeasurementSystem msCheck in Enum.GetValues(typeof(MeasurementSystem)))
                {
                    if (msCheck.ToString().Equals(mapUnitsRow["MeasurementSystem"].ToString()))
                    {
                        unit.MeasurementSystem = msCheck;
                    }
                }

                // set data type to created unit or add data type to existing unit
                List<string> Types = mapUnitsRow["DataTypes"].ToString().Split(' ').Distinct().ToList();

                // get existing unit or create
                Unit existU = unitManager.Repo.Get(u => u.Abbreviation.Equals(unit.Abbreviation)).FirstOrDefault();
                if (existU == null)
                {
                    unit = unitManager.Create(unit.Name, unit.Abbreviation, unit.Description, unit.Dimension, unit.MeasurementSystem);
                    addDataTypes(unit.Id, Types);
                }
                else
                {
                    addDataTypes(existU.Id, Types);
                }
                // add unit-ID to the mappedUnits Table
                mapUnitsRow["UnitId"] = unit.Id;
            }
        }
Пример #12
0
        private List<DataType> updataAssociatedDataType(Unit unit, long[] newDataTypeIds)
        {
            if (unit != null)
            {
                DataTypeManager dataTypeManger = new DataTypeManager();

                UnitManager unitManager = new UnitManager();

                unit = unitManager.Repo.Get(unit.Id);
                List<DataType> existingDataTypes = unit.AssociatedDataTypes.ToList();
                List<DataType> newDataTypes = newDataTypeIds == null ? new List<DataType>() : dataTypeManger.Repo.Query().Where(p => newDataTypeIds.Contains(p.Id)).ToList();
                List<DataType> tobeAddedDataTypes = newDataTypes.Except(existingDataTypes).ToList();

                if (tobeAddedDataTypes != null && tobeAddedDataTypes.Count > 0)
                    unitManager.AddAssociatedDataType(unit, tobeAddedDataTypes);

                unit = unitManager.Repo.Get(unit.Id);
                existingDataTypes = unit.AssociatedDataTypes.ToList();
                List<DataType> toBeRemoved = existingDataTypes.Except(newDataTypes).ToList();
                if (toBeRemoved != null && toBeRemoved.Count() > 0)
                    unitManager.RemoveAssociatedDataType(unit, toBeRemoved);

                unit = unitManager.Repo.Get(unit.Id);
                return unit.AssociatedDataTypes.ToList();
            }
            return null;
        }
Пример #13
0
        private bool unitValidation(Unit unit, long[] checkedRecords)
        {
            bool check = true;
            UnitManager unitManager = new UnitManager();
            List<Unit> unitList = unitManager.Repo.Get().ToList(); ;

            if (unit.Name == null || unit.Name == "")
            {
                Session["nameMsg"] = "invalid Name";
                check = false;
            }
            else
            {
                bool nameExist = !(unitList.Where(p => p.Name.ToLower().Equals(unit.Name.ToLower())).Count().Equals(0));
                if (nameExist)
                {
                    Unit tempUnit = unitList.Where(p => p.Name.ToLower().Equals(unit.Name.ToLower())).ToList().First();
                    if (unit.Id != tempUnit.Id)
                    {
                        Session["nameMsg"] = "Name already exist";
                        check = false;
                    }
                    else
                    {
                        Session["nameMsg"] = null;
                    }
                }
                else
                {
                    Session["nameMsg"] = null;
                }
            }

            if (unit.Abbreviation == null || unit.Abbreviation == "")
            {
                Session["abbrMsg"] = "invalid Abbreviation";
                check = false;
            }
            else
            {
                bool abbreviationExist = !(unitList.Where(p => p.Abbreviation.ToLower().Equals(unit.Abbreviation.ToLower())).Count().Equals(0));
                if (abbreviationExist)
                {
                    Unit tempUnit = unitList.Where(p => p.Abbreviation.ToLower().Equals(unit.Abbreviation.ToLower())).ToList().First();
                    if (unit.Id != tempUnit.Id)
                    {
                        Session["abbrMsg"] = "Abbreviation already exist";
                        check = false;
                    }
                    else
                    {
                        Session["abbrMsg"] = null;
                    }
                }
                else
                {
                    Session["abbrMsg"] = null;
                }
            }

            if (checkedRecords != null)
            {
                Session["dataTypeMsg"] = null;
            }
            else
            {
                Session["dataTypeMsg"] = "Choose at least one Data Type.";
                check = false;
            }

            if (!String.IsNullOrEmpty(unit.Dimension.Name) && unit.Dimension.Name != "Select or Enter")
            {
                Session["dimensionMsg"] = null;
            }
            else
            {
                Session["dimensionMsg"] = "Select or create an Dimension.";
                check = false;
            }

            return check;
        }