public Parameter AddParameterUsage(Variable variableUsage, DataAttribute dataAttribute, bool isValueOptional, string label, string defaultValue, string missingValue, string description)
{
Contract.Requires(variableUsage != null && variableUsage.DataAttribute.Id >= 0);
Contract.Requires(dataAttribute != null && dataAttribute.Id >= 0);
Contract.Ensures(Contract.Result<Parameter>() != null && Contract.Result<Parameter>().Id >= 0);
VariableRepo.Reload(variableUsage);
VariableRepo.LoadIfNot(variableUsage.Parameters);
int count = (from pu in variableUsage.Parameters
where pu.DataAttribute.Id.Equals(dataAttribute.Id)
select pu
)
.Count();
// support multiple use of a data attribute as a parameter in a variable context
//if (count > 0)
// throw new Exception(string.Format("Data attribute {0} is already used as a parameter in conjunction with variable {1} in data structure {2}", dataAttribute.Id, variableUsage.DataAttribute.Id, variableUsage.DataStructure.Id));
Parameter usage = new Parameter()
{
DataAttribute = dataAttribute,
Variable = variableUsage,
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 usage
Label = !string.IsNullOrWhiteSpace(label) ? label : (count <= 0 ? dataAttribute.Name : string.Format("{0} ({1})", dataAttribute.Name, count)),
DefaultValue = defaultValue,
MissingValue = missingValue,
Description = description
};
dataAttribute.UsagesAsParameter.Add(usage);
variableUsage.Parameters.Add(usage);
using (IUnitOfWork uow = this.GetUnitOfWork())
{
IRepository<Parameter> repo = uow.GetRepository<Parameter>();
repo.Put(usage);
uow.Commit();
}
return (usage);
}