public void Convert(PhysicalInstance pi) { if (pi == null) { throw new ArgumentNullException(nameof(pi)); } if (pi.RecordLayouts.Count == 0) { return; } // Create a single DataRelationship for the PhysicalInstance. // Within this, we will add one LogicalRecord per old RecordLayout. var dataRelationship = new DataRelationship() { AgencyId = pi.AgencyId }; pi.DataRelationships.Add(dataRelationship); // For each record layout, add the variables to a new DataRelationship. foreach (var recordLayout in pi.RecordLayouts) { // Create a LogicalRecord with the same descriptive information // as the RecordLayout we are migrating. var logicalRecord = new LogicalRecord { AgencyId = pi.AgencyId }; dataRelationship.LogicalRecords.Add(logicalRecord); logicalRecord.ItemName.Copy(recordLayout.ItemName); logicalRecord.Label.Copy(recordLayout.Label); logicalRecord.Description.Copy(recordLayout.Description); // For each DataItem in the RecordLayout, add a VariablesInRecord entry // to the LogicalRecord. foreach (var dataItem in recordLayout.DataItems) { logicalRecord.VariablesInRecord.Add(dataItem.Variable); } } // Remove the old RecordLayouts. pi.RecordLayouts.Clear(); }
public ResourcePackage Import(string path, string agencyId) { this.harmonizingCache = new HarmonizingCache(MultilingualString.CurrentCulture); var resourcePackage = new ResourcePackage(); resourcePackage.AgencyId = agencyId; logger.Debug("Importing RData"); if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException("fileName"); } if (!File.Exists(path)) { throw new ArgumentException("The specified file must exist"); } string fileNameWithExtension = Path.GetFileName(path); string fileNameOnly = Path.GetFileNameWithoutExtension(path); logger.Debug("RData import file: " + fileNameOnly); resourcePackage.DublinCoreMetadata.Title.Current = fileNameOnly; // Create the PhysicalInstance. var physicalInstance = new PhysicalInstance() { AgencyId = agencyId }; resourcePackage.PhysicalInstances.Add(physicalInstance); physicalInstance.DublinCoreMetadata.Title.Current = fileNameOnly; // File location if (path != null) { DataFileIdentification fileID = new DataFileIdentification(); Uri uri; if (Uri.TryCreate(path, UriKind.RelativeOrAbsolute, out uri)) { fileID.Uri = uri; } fileID.Path = path; physicalInstance.FileIdentifications.Add(fileID); } // Create the DataRelationship. var dataRelationship = new DataRelationship(); physicalInstance.DataRelationships.Add(dataRelationship); dataRelationship.AgencyId = agencyId; dataRelationship.Label.Current = fileNameOnly; // Load the file into R. string pathForR = path.Replace("\\", "/"); engine.Evaluate(string.Format("load('{0}')", pathForR)); // Find all the data frames. var dataFrames = GetDataFrames(); // For each data frame in the RData file, create a LogicalRecord. foreach (var pair in dataFrames) { string name = pair.Key; var dataFrame = pair.Value; // TODO This should be tracked per record, not PhysicalInstance. physicalInstance.FileStructure.CaseQuantity = dataFrame.RowCount; var logicalRecord = new LogicalRecord() { AgencyId = agencyId }; dataRelationship.LogicalRecords.Add(logicalRecord); logicalRecord.Label.Current = name; List <string> variableLabels = null; var variableLabelsExpr = dataFrame.GetAttribute("var.labels"); if (variableLabelsExpr != null) { var labelVector = variableLabelsExpr.AsVector(); variableLabels = new List <string>(labelVector.Select(x => (string)x)); } for (int i = 0; i < dataFrame.ColumnCount; i++) { string columnName = dataFrame.ColumnNames[i]; var column = dataFrame[i]; var variable = new Variable() { AgencyId = agencyId }; logicalRecord.VariablesInRecord.Add(variable); // Name variable.ItemName.Current = columnName; // Label if (variableLabels != null) { variable.Label.Current = variableLabels[i]; } // Type if (column.Type == RDotNet.Internals.SymbolicExpressionType.NumericVector) { variable.RepresentationType = RepresentationType.Numeric; variable.Additivity = AdditivityType.Stock; } else if (column.Type == RDotNet.Internals.SymbolicExpressionType.IntegerVector) { if (column.IsFactor()) { variable.RepresentationType = RepresentationType.Code; string[] factors = column.AsFactor().GetLevels(); variable.CodeRepresentation.Codes = GetCodeList(factors, agencyId, resourcePackage); } else { variable.RepresentationType = RepresentationType.Numeric; variable.NumericRepresentation.NumericType = NumericType.Integer; variable.Additivity = AdditivityType.Stock; } } else if (column.Type == RDotNet.Internals.SymbolicExpressionType.CharacterVector) { variable.RepresentationType = RepresentationType.Text; } } } return(resourcePackage); }