public MetadataAttributeUsage AddMetadataAtributeUsage(MetadataPackage package, MetadataAttribute attribute, string label, string description, int minCardinality, int maxCardinality) { Contract.Requires(package != null && package.Id >= 0); Contract.Requires(attribute != null && attribute.Id >= 0); Contract.Ensures(Contract.Result<MetadataAttributeUsage>() != null && Contract.Result<MetadataAttributeUsage>().Id >= 0); MetadataPackageRepo.Reload(package); MetadataPackageRepo.LoadIfNot(package.MetadataAttributeUsages); int count = 0; try { count = (from v in package.MetadataAttributeUsages where v.MetadataAttribute.Id.Equals(attribute.Id) select v ) .Count(); } catch { } MetadataAttributeUsage usage = new MetadataAttributeUsage() { MetadataPackage = package, MetadataAttribute = attribute, // if there is no label provided, use the attribute name and a sequence number calculated by the number of occurrences of that attribute in the current structure Label = !string.IsNullOrWhiteSpace(label) ? label : (count <= 0 ? attribute.Name : string.Format("{0} ({1})", attribute.Name, count)), Description = description, MinCardinality = minCardinality, MaxCardinality = maxCardinality, }; package.MetadataAttributeUsages.Add(usage); attribute.UsedIn.Add(usage); using (IUnitOfWork uow = this.GetUnitOfWork()) { IRepository<MetadataAttributeUsage> repo = uow.GetRepository<MetadataAttributeUsage>(); repo.Put(usage); uow.Commit(); } return (usage); }
public MetadataAttribute Update(MetadataAttribute 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<MetadataAttribute>() != null && Contract.Result<MetadataAttribute>().Id >= 0, "No entity is persisted!"); using (IUnitOfWork uow = this.GetUnitOfWork()) { IRepository<MetadataAttribute> repo = uow.GetRepository<MetadataAttribute>(); repo.Put(entity); // Merge is required here!!!! uow.Commit(); } return (entity); }
private RangeConstraint GetRangeConstraint(double min, double max,string description, bool negated, bool lowerBoundIncluded, bool upperBoundIncluded, MetadataAttribute attr) { RangeConstraint constraint = new RangeConstraint(ConstraintProviderSource.Internal, "", "en-US", description, negated, null, null, null, min, lowerBoundIncluded, max, upperBoundIncluded); dataContainerManager.AddConstraint(constraint, attr); return constraint; }
/// <summary> /// Deletes the saved <paramref name="entity"/> from the database. The entity should not be a built-in one. /// </summary> /// <param name="entity"></param> /// <returns>true if successful, false otherwise.</returns> /// <remarks>If by deleting the <paramref name="entity"/> any other compound attribute will have less than 2 members, the deletion fails.</remarks> public bool Delete(MetadataAttribute entity) { Contract.Requires(entity != null); Contract.Requires(entity.Id >= 0); Contract.Requires(entity.IsBuiltIn == false); // check whether this attribute is used in any compound and if yes, those compounds have enough members after the deletion! var q = from compound in MetadataCompoundAttributeRepo.Query() from usage in compound.MetadataNestedAttributeUsages // whether the compound is linked to entity and whether deleting entity causes the compound to have less than two members where usage.Member == entity && compound.MetadataNestedAttributeUsages.Count() <= 2 select compound; int cnt = 0; if ((cnt = q.Count()) > 0) { if(cnt ==1 ) throw new Exception(string.Format("Deletion failed! Attribute {0} is used in {1} compound attribute that has less than 3 members. Compound attributes should have at least 2 members, invariantly.", entity.Id, cnt)); else throw new Exception(string.Format("Deletion failed! Attribute {0} is used in {1} compound attributes that have less than 3 members. Compound attributes should have at least 2 members, invariantly.", entity.Id, cnt)); } using (IUnitOfWork uow = this.GetUnitOfWork()) { IRepository<MetadataAttribute> repo = uow.GetRepository<MetadataAttribute>(); entity = repo.Reload(entity); repo.LoadIfNot(entity.ExtendedProperties); entity.ExtendedProperties.Clear(); repo.Delete(entity); uow.Commit(); } // if any problem was detected during the commit, an exception will be thrown! return (true); }
private PatternConstraint GetPatternConstraint(string patternString, string description,bool negated, MetadataAttribute attr) { PatternConstraint constraint = new PatternConstraint(ConstraintProviderSource.Internal, "", "en-US", description, negated, null, null, null, patternString, false); dataContainerManager.AddConstraint(constraint, attr); return constraint; }
private DomainConstraint GetDomainConstraint(XmlSchemaObject restriction, MetadataAttribute attr, string restrictionDescription) { XmlSchemaObjectCollection facets = new XmlSchemaObjectCollection(); if (restriction is XmlSchemaSimpleTypeRestriction) facets = ((XmlSchemaSimpleTypeRestriction)restriction).Facets; if (restriction is XmlSchemaSimpleContentRestriction) facets = ((XmlSchemaSimpleContentRestriction)restriction).Facets; List<DomainItem> items = new List<DomainItem>(); foreach (XmlSchemaEnumerationFacet facet in facets) { if (facet != null) items.Add(new DomainItem(){Key = facet.Value, Value = facet.Value }); } DomainConstraint domainConstraint = new DomainConstraint(ConstraintProviderSource.Internal, "", "en-US", restrictionDescription, false, null, null, null, items); domainConstraint.Materialize(); dataContainerManager.AddConstraint(domainConstraint,attr); return domainConstraint; }
private List<Constraint> ConvertToConstraints(XmlSchemaObject restriction, MetadataAttribute attr) { List<Constraint> constraints = new List<Constraint>(); /// XmlSchemaSimpleTypeRestriction if (restriction is XmlSchemaSimpleTypeRestriction) { XmlSchemaSimpleTypeRestriction simpleTypeRestriction = (XmlSchemaSimpleTypeRestriction)restriction; // if content of simpletype is a restriction if (restriction != null) { // if the simpletype is a domin constraint if (XmlSchemaUtility.IsEnumerationType(simpleTypeRestriction)) { constraints.Add(GetDomainConstraint(simpleTypeRestriction, attr, GetDescription(simpleTypeRestriction.Annotation))); } else { foreach (XmlSchemaObject facet in simpleTypeRestriction.Facets) { Constraint c = ConvertFacetToConstraint(facet, attr, constraints); if (c != null) constraints.Add(c); } } } } /// XmlSchemaSimpleContentRestriction if (restriction is XmlSchemaSimpleContentRestriction) { XmlSchemaSimpleContentRestriction simpleContentRestriction = (XmlSchemaSimpleContentRestriction)restriction; // if content of simpletype is a restriction if (restriction != null) { // if the simpletype is a domin constraint if (XmlSchemaUtility.IsEnumerationType(simpleContentRestriction)) { constraints.Add(GetDomainConstraint(simpleContentRestriction, attr, GetDescription(simpleContentRestriction.Annotation))); } else { foreach (XmlSchemaObject facet in simpleContentRestriction.Facets) { Constraint c = ConvertFacetToConstraint(facet, attr, constraints); if (c != null) constraints.Add(c); } } } } return constraints; }
/// <summary> /// length, minLength,maxLength,pattern,enumeration,whiteSpace,maxInclusive,maxExclusive,minExclusive,minInclusive,totalDigits,fractionDigits /// </summary> /// <param name="facet"></param> /// <returns></returns> private Constraint ConvertFacetToConstraint(XmlSchemaObject facet, MetadataAttribute attr, List<Constraint> constraints) { #region pattern constraints //if (facet is XmlSchemaWhiteSpaceFacet) //{ // XmlSchemaWhiteSpaceFacet defFacet = (XmlSchemaWhiteSpaceFacet)facet; // return GetPatternConstraint(" ", GetDescription(defFacet.Annotation), true, attr); //} if (facet is XmlSchemaPatternFacet) { XmlSchemaPatternFacet defFacet = (XmlSchemaPatternFacet)facet; return GetPatternConstraint(defFacet.Value, GetDescription(defFacet.Annotation), false ,attr); } #endregion #region range constraints if (facet is XmlSchemaLengthFacet) { XmlSchemaLengthFacet defFacet = (XmlSchemaLengthFacet)facet; if (!IsRangeExist(constraints)) { return GetRangeConstraint(Convert.ToDouble(defFacet.Value), Convert.ToDouble(defFacet.Value), GetDescription(defFacet.Annotation), false, true, true, attr); } else { double value = Convert.ToDouble(defFacet.Value); RangeConstraint rc = GetRangeConstraint(constraints); dataContainerManager.RemoveConstraint(rc); // if value is bigger then Lowerbound us value if (rc.Lowerbound < value) rc.Lowerbound = value; // if value is smaller then Upperbound us value if (rc.Upperbound > value) rc.Upperbound = value; rc.LowerboundIncluded = true; rc.UpperboundIncluded = true; constraints.Remove(rc); return GetRangeConstraint(rc.Lowerbound, rc.Upperbound, GetDescription(defFacet.Annotation), false, rc.LowerboundIncluded, rc.UpperboundIncluded, attr); } } if (facet is XmlSchemaMinLengthFacet) { XmlSchemaMinLengthFacet defFacet = (XmlSchemaMinLengthFacet)facet; if (!IsRangeExist(constraints)) { return GetRangeConstraint(Convert.ToDouble(defFacet.Value), double.MaxValue, GetDescription(defFacet.Annotation), false, true, true, attr); } else { double value = Convert.ToDouble(defFacet.Value); RangeConstraint rc = GetRangeConstraint(constraints); dataContainerManager.RemoveConstraint(rc); // if value is bigger then Lowerbound us value if (rc.Lowerbound < value) rc.Lowerbound = value; rc.LowerboundIncluded = true; constraints.Remove(rc); return GetRangeConstraint(rc.Lowerbound, rc.Upperbound, GetDescription(defFacet.Annotation), false, rc.LowerboundIncluded, rc.UpperboundIncluded, attr); } } if (facet is XmlSchemaMaxLengthFacet) { XmlSchemaMaxLengthFacet defFacet = (XmlSchemaMaxLengthFacet)facet; if (!IsRangeExist(constraints)) { return GetRangeConstraint(double.MinValue, Convert.ToDouble(defFacet.Value), GetDescription(defFacet.Annotation), false, true, true, attr); } else { double value = Convert.ToDouble(defFacet.Value); RangeConstraint rc = GetRangeConstraint(constraints); dataContainerManager.RemoveConstraint(rc); // if value is smaller then Upperbound us value if (rc.Upperbound > value) rc.Upperbound = value; rc.UpperboundIncluded = true; constraints.Remove(rc); return GetRangeConstraint(rc.Lowerbound, rc.Upperbound, GetDescription(defFacet.Annotation), false, rc.LowerboundIncluded, rc.UpperboundIncluded, attr); } } if (facet is XmlSchemaMaxInclusiveFacet) { XmlSchemaMaxInclusiveFacet defFacet = (XmlSchemaMaxInclusiveFacet)facet; if (!IsRangeExist(constraints)) { return GetRangeConstraint(0, Convert.ToDouble(defFacet.Value), GetDescription(defFacet.Annotation), false, true, true, attr); } else { double value = Convert.ToDouble(defFacet.Value); RangeConstraint rc = GetRangeConstraint(constraints); dataContainerManager.RemoveConstraint(rc); // if value is smaller then Upperbound us value if (rc.Upperbound > value) rc.Upperbound = value; rc.UpperboundIncluded = true; constraints.Remove(rc); return GetRangeConstraint(rc.Lowerbound, rc.Upperbound, GetDescription(defFacet.Annotation), false, rc.LowerboundIncluded, rc.UpperboundIncluded, attr); } } if (facet is XmlSchemaMinInclusiveFacet) { XmlSchemaMinInclusiveFacet defFacet = (XmlSchemaMinInclusiveFacet)facet; if (!IsRangeExist(constraints)) { return GetRangeConstraint(Convert.ToDouble(defFacet.Value), int.MaxValue, GetDescription(defFacet.Annotation), false, true, true, attr); } else { double value = Convert.ToDouble(defFacet.Value); RangeConstraint rc = GetRangeConstraint(constraints); dataContainerManager.RemoveConstraint(rc); // if value is bigger then Lowerbound us value if (rc.Lowerbound < value) rc.Lowerbound = value; rc.LowerboundIncluded = true; constraints.Remove(rc); return GetRangeConstraint(rc.Lowerbound, rc.Upperbound, GetDescription(defFacet.Annotation), false, rc.LowerboundIncluded, rc.UpperboundIncluded, attr); } } if (facet is XmlSchemaMaxExclusiveFacet) { XmlSchemaMaxExclusiveFacet defFacet = (XmlSchemaMaxExclusiveFacet)facet; if (!IsRangeExist(constraints)) { return GetRangeConstraint(0, Convert.ToDouble(defFacet.Value), GetDescription(defFacet.Annotation), false, true, false, attr); } else { double value = Convert.ToDouble(defFacet.Value); RangeConstraint rc = GetRangeConstraint(constraints); dataContainerManager.RemoveConstraint(rc); // if value is smaller then Upperbound us value if (rc.Upperbound > value) rc.Upperbound = value; rc.UpperboundIncluded = false; constraints.Remove(rc); return GetRangeConstraint(rc.Lowerbound, rc.Upperbound, GetDescription(defFacet.Annotation), false, rc.LowerboundIncluded, rc.UpperboundIncluded, attr); } } if (facet is XmlSchemaMinExclusiveFacet) { XmlSchemaMinExclusiveFacet defFacet = (XmlSchemaMinExclusiveFacet)facet; if (!IsRangeExist(constraints)) { return GetRangeConstraint(Convert.ToDouble(defFacet.Value), int.MaxValue, GetDescription(defFacet.Annotation), false, false, true, attr); } else { double value = Convert.ToDouble(defFacet.Value); RangeConstraint rc = GetRangeConstraint(constraints); dataContainerManager.RemoveConstraint(rc); // if value is bigger then Lowerbound us value if (rc.Lowerbound < value) rc.Lowerbound = value; rc.LowerboundIncluded = false; constraints.Remove(rc); return GetRangeConstraint(rc.Lowerbound, rc.Upperbound, GetDescription(defFacet.Annotation), false, rc.LowerboundIncluded, rc.UpperboundIncluded, attr); } } #endregion if (facet is XmlSchemaTotalDigitsFacet) { } if (facet is XmlSchemaFractionDigitsFacet) { } /* special case if (facet is XmlSchemaEnumerationFacet) { return GetDomainConstraint((XmlSchemaEnumerationFacet)facet); } * */ return null; }
private static List<object> createDomainContraintList(MetadataAttribute attribute) { List<object> list = new List<object>(); foreach (Constraint constraint in attribute.Constraints) { if (constraint is DomainConstraint) { DomainConstraint domainConstraint = (DomainConstraint)constraint; domainConstraint.Materialize(); domainConstraint.Items.ForEach(i => list.Add(i.Value)); } } return list; }