/// <summary> /// Update versions and references to a Variable along this path: /// PhysicalInstance /// DataRelationship /// Variable /// VariableStatistic /// Variable /// </summary> /// <param name="variable"></param> public static void UpdateVersionsAndSave(Variable variable, PhysicalInstance physicalInstance, DataRelationship dataRelationship, VariableStatistic variableStatistic) { var client = RepositoryHelper.GetClient(); var now = DateTime.UtcNow; var itemsToRegister = new List <IVersionable>(); itemsToRegister.Add(variable); itemsToRegister.Add(physicalInstance); itemsToRegister.Add(dataRelationship); // Increase version of the variable. variable.Version++; variable.VersionDate = now; // Increase the version and reference of the PhysicalInstance that // references the DataRelationship and VariableStatistic. physicalInstance.Version++; physicalInstance.VersionDate = now; // Increase the version and reference of the DataRelationship that references the variable. dataRelationship.Version++; dataRelationship.VersionDate = now; // Update the reference to the variable. var variableInDataRelationship = dataRelationship.LogicalRecords .SelectMany(x => x.VariablesInRecord) .Where(x => x.Identifier == variable.Identifier) .FirstOrDefault(); variableInDataRelationship.Version = variable.Version; variableInDataRelationship.VersionDate = now; // Increase the version and reference of hte VariableStatistic that references the Variable. if (variableStatistic != null) { variableStatistic.Version++; variableStatistic.VersionDate = now; variableStatistic.VariableReference.Version = variable.Version; itemsToRegister.Add(variableStatistic); } // Register all the changes. var options = new CommitOptions(); options.NamedOptions.Add("RegisterOrReplace"); client.RegisterItems(itemsToRegister, options); }
static void CreateCodeList(Variable variable, PhysicalInstance physicalInstance, DataRelationship dataRelationship, VariableStatistic variableStatistic) { // Create and save a code list with all the unique values. var codeList = CreateCodeList(physicalInstance, dataRelationship, variable, variableStatistic); variable.CodeRepresentation.Codes = codeList; if (codeList != null) { var gatherer = new ItemGathererVisitor(); codeList.Accept(gatherer); var client = RepositoryHelper.GetClient(); var repoOptions = new CommitOptions(); repoOptions.NamedOptions.Add("RegisterOrReplace"); client.RegisterItems(gatherer.FoundItems, repoOptions); } }
static CodeList CreateCodeList(PhysicalInstance physicalInstance, DataRelationship dataRelationship, Variable variable, VariableStatistic stats) { using (IDataReader reader = GetReaderForPhysicalInstance(physicalInstance)) { if (reader == null) { return(null); } int columnIdx = GetColumnIndex(physicalInstance, dataRelationship, variable); if (columnIdx == -1) { return(null); } var gatherer = new DataValueFrequencyGatherer(reader); var values = gatherer.CountValueFrequencies(columnIdx); var codeList = new CodeList() { AgencyId = variable.AgencyId }; codeList.ItemName.Copy(variable.ItemName); codeList.Label.Copy(variable.Label); // Clear any existing category statistics. stats.UnfilteredCategoryStatistics.Clear(); foreach (var pair in values.OrderBy(x => x.Key)) { string value = pair.Key; int count = pair.Value; // Create the code and category. var category = new Category() { AgencyId = variable.AgencyId }; var code = new Code() { AgencyId = variable.AgencyId }; code.Value = value; code.Category = category; codeList.Codes.Add(code); // Update the statistics with category frequency. var catStats = new CategoryStatistics(); catStats.CategoryValue = value.ToString(); catStats.Frequency = count; stats.UnfilteredCategoryStatistics.Add(catStats); } return(codeList); } }
public static void UpdateVariableProperty(Variable variable, PhysicalInstance physicalInstance, DataRelationship dataRelationship, VariableStatistic variableStatistic, string propertyName, string value) { if (propertyName == "ItemName") { variable.ItemName.Current = value; } else if (propertyName == "Label") { variable.Label.Current = value; } else if (propertyName == "Description") { variable.Description.Current = value; } else if (propertyName == "AnalysisUnit") { variable.AnalysisUnit = value; } else if (propertyName == "ResponseUnit") { variable.ResponseUnit = value; } else if (propertyName == "RepresentationType") { if (value == "Text" && variable.RepresentationType != RepresentationType.Text) { // Clear any existing category statistics. variableStatistic.UnfilteredCategoryStatistics.Clear(); variable.RepresentationType = RepresentationType.Text; } else if (value == "Numeric" && variable.RepresentationType != RepresentationType.Numeric) { // Clear any existing category statistics. variableStatistic.UnfilteredCategoryStatistics.Clear(); variable.RepresentationType = RepresentationType.Numeric; } else if (value == "Code" && variable.RepresentationType != RepresentationType.Code) { variable.RepresentationType = RepresentationType.Code; CreateCodeList(variable, physicalInstance, dataRelationship, variableStatistic); } } }
void GetReferencingItems(RepositoryClientBase client, IdentifierTriple variableId, out PhysicalInstance physicalInstance, out DataRelationship dataRelationship, out VariableStatistic variableStatistic) { dataRelationship = null; variableStatistic = null; physicalInstance = VariableMapper.GetPhysicalInstanceWithVariable(variableId, client); foreach (var dr in physicalInstance.DataRelationships) { client.PopulateItem(dr, false, ChildReferenceProcessing.Populate); if (dr.LogicalRecords .SelectMany(x => x.VariablesInRecord) .Any(x => x.CompositeId == variableId)) { dataRelationship = dr; } } foreach (var stats in physicalInstance.Statistics) { client.PopulateItem(stats); if (stats.AgencyId == variableId.AgencyId & stats.VariableReference.Identifier == variableId.Identifier) { variableStatistic = stats; break; } } }