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);
}