/// <summary> /// Creates a simple extent containing three elements with four properties /// </summary> /// <returns>The created uri extent</returns> private static IUriExtent CreateSimpleCsvExtent() { var csvFile = "eins 1 one\r\nzwei 2 two\r\ndrei 3 three\r\nvier 4 four\r\n"; File.WriteAllText("data.txt", csvFile); var mapper = new ManualConfigurationToExtentStorageMapper(); mapper.AddMapping(typeof (CSVStorageConfiguration), scope => new CSVStorage(null, null)); DataLayers dataLayers; var dataLayerLogic = DataLayerLogic.InitDefault(out dataLayers); var data = new ExtentStorageData(); var logic = new ExtentStorageLoader(data, mapper, dataLayerLogic); var configuration = new CSVStorageConfiguration() { Path = "data.txt", ExtentUri = "dm:///local/", Settings = { HasHeader = false, Separator = ' ' } }; var csvExtent = logic.LoadExtent(configuration); return csvExtent; }
public void AddZipExample([FromBody] WorkspaceReferenceModel workspace) { // Finds the file and copies the file to the given location var appBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; string filename; var tries = 0; int randomNumber; do // while File.Exists { randomNumber = Random.Next(int.MaxValue); filename = Path.Combine(appBase, "App_Data/Database", $"plz_{randomNumber}.csv"); tries++; if (tries == 10000) { throw new InvalidOperationException("Did not find a unique name for zip extent"); } } while (File.Exists(filename)); var originalFilename = Path.Combine( appBase, "App_Data/Example", "plz.csv"); File.Copy(originalFilename, filename); var defaultConfiguration = new CSVStorageConfiguration { ExtentUri = $"datenmeister:///zipcodes/{randomNumber}", Path = filename, Workspace = workspace.ws, Settings = { HasHeader = false, Separator = '\t', Encoding = "UTF-8", Columns = new [] { "Id", "Zip", "PositionLong", "PositionLat", "CityName" }.ToList(), // Columns = new object[] { idProperty, zipProperty, positionLongProperty, positionLatProperty, citynameProperty }.ToList(), MetaclassUri = "dm:///types#DatenMeister.Apps.ZipCode.Model.ZipCode" } }; _loader.LoadExtent(defaultConfiguration, false); Debug.WriteLine("Zip codes loaded"); }
public void TestStorage() { var csvFile = "eins 1 one\r\nzwei 2 two\r\ndrei 3 three\r\nvier 4 four\r\n"; var csvOtherFile = "eens 1 one\r\nzwei 2 two\r\ndrei 3 three\r\nvier 4 four\r\n"; File.WriteAllText("data.txt", csvFile); var storageConfiguration = new CSVStorageConfiguration { Path = "data.txt", ExtentUri = "dm:///test", Settings = { HasHeader = false, Separator = ' ' } }; var storage = new CSVStorage(null, null); var extent = storage.LoadExtent(storageConfiguration, false); Assert.That(storageConfiguration.Settings.Columns.Count, Is.EqualTo(3)); Assert.That(extent.elements().Count(), Is.EqualTo(4)); // Stores the csv file storage.StoreExtent(extent, storageConfiguration); var readCsvFile = File.ReadAllText("data.txt"); Assert.That(readCsvFile, Is.EqualTo(csvFile)); var firstElement = extent.elements().ElementAt(0) as IObject; Assert.That(firstElement, Is.Not.Null); firstElement.set(storageConfiguration.Settings.Columns[0], "eens"); storage.StoreExtent(extent, storageConfiguration); readCsvFile = File.ReadAllText("data.txt"); Assert.That(readCsvFile, Is.EqualTo(csvOtherFile)); File.Delete("data.txt"); }
public void TestExtentStorageLogic() { var csvFile = "eins 1 one\r\nzwei 2 two\r\ndrei 3 three\r\nvier 4 four\r\n"; File.WriteAllText("data.txt", csvFile); var mapper = new ManualConfigurationToExtentStorageMapper(); mapper.AddMapping(typeof (CSVStorageConfiguration), scope => new CSVStorage(null, null)); DataLayers dataLayers; var dataLayerLogic = DataLayerLogic.InitDefault(out dataLayers); var data = new ExtentStorageData(); var logic = new ExtentStorageLoader(data, mapper, dataLayerLogic); var configuration = new CSVStorageConfiguration() { Path = "data.txt", ExtentUri = "dm:///local/", Settings = { HasHeader = false, Separator = ' ' } }; var csvExtent = logic.LoadExtent(configuration); Assert.That(csvExtent, Is.Not.Null); Assert.That(csvExtent.elements().Count(), Is.EqualTo(4)); logic.StoreExtent(csvExtent); // Changes content, store it and check, if stored (csvExtent.elements().ElementAt(0) as IObject).set(configuration.Settings.Columns[0], "eens"); logic.StoreAll(); var read = File.ReadAllText("data.txt"); Assert.That(read.Contains("eens"), Is.True); }
/// <summary> /// Gets the configuration by using the given model and their filename /// </summary> /// <param name="model">Model to be used to retrieve the information</param> /// <param name="filename">Filename to be used</param> /// <returns></returns> private static ExtentStorageConfiguration GetStorageConfiguration(ExtentAddModel model, string filename) { ExtentStorageConfiguration configuration; switch (model.type) { case "xmi": configuration = new XmiStorageConfiguration { ExtentUri = model.contextUri, Path = filename, Workspace = model.workspace }; break; case "csv": var csvExtentData = new CSVStorageConfiguration { ExtentUri = model.contextUri, Path = filename, Workspace = model.workspace, Settings = new CSVSettings() }; var modelAsCreateModel = model as ExtentCreateModel; if (modelAsCreateModel != null) { foreach (var c in modelAsCreateModel.ColumnsAsEnumerable) { csvExtentData.Settings.Columns.Add(c); } } configuration = csvExtentData; break; default: throw new InvalidOperationException($"Unknown extent type: {model.type}"); } return configuration; }
private static void LoadZipCodes(ILifetimeScope scope) { ////////////////////// // Loads the workspace var file = Path.Combine( Path.Combine( AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "App_Data/Database"), "plz.csv"); var defaultConfiguration = new CSVStorageConfiguration { ExtentUri = "datenmeister:///zipcodes", Path = file, Workspace = "Data", Settings = { HasHeader = false, Separator = '\t', Encoding = "UTF-8" } }; var extentStorageLogic = scope.Resolve<IExtentStorageLoader>(); extentStorageLogic.LoadExtent(defaultConfiguration, false); Debug.WriteLine("Zip codes loaded"); }