From 0e26f753a2d123c1f94faaebf676ead4b59d470d Mon Sep 17 00:00:00 2001 From: Reino Ruusu Date: Mon, 21 Sep 2020 18:16:07 +0300 Subject: [PATCH] Add an SCL interface for loading tech type files gitlab #93 Change-Id: Ie6d3fa4ccd71d967422c798bb7834c57cabd9416 --- .../scl/Simantics/District/TechType.scl | 2 ++ .../network/techtype/TechTypeUtils.java | 33 ++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/org.simantics.district.network/scl/Simantics/District/TechType.scl b/org.simantics.district.network/scl/Simantics/District/TechType.scl index 18663b85..e8ab4eec 100644 --- a/org.simantics.district.network/scl/Simantics/District/TechType.scl +++ b/org.simantics.district.network/scl/Simantics/District/TechType.scl @@ -7,3 +7,5 @@ importJava "org.simantics.district.network.techtype.TechTypeUtils" where resetComponents :: Resource -> () "Reset all map diagram elements to values in a tech type table: `resetMapElements table`" resetMapElements :: Resource -> () + "Load a tech type table for a given component type from a CSV file: `loadTechTypeTable componentType filePath`" + loadTechTypeTable :: Resource -> String -> () diff --git a/org.simantics.district.network/src/org/simantics/district/network/techtype/TechTypeUtils.java b/org.simantics.district.network/src/org/simantics/district/network/techtype/TechTypeUtils.java index 66280d12..92eef4fe 100644 --- a/org.simantics.district.network/src/org/simantics/district/network/techtype/TechTypeUtils.java +++ b/org.simantics.district.network/src/org/simantics/district/network/techtype/TechTypeUtils.java @@ -1,9 +1,14 @@ package org.simantics.district.network.techtype; +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.Objects; +import java.util.stream.Collectors; import org.simantics.Simantics; import org.simantics.databoard.Bindings; @@ -30,6 +35,7 @@ import org.simantics.district.network.techtype.requests.PossibleTechTypeItem; import org.simantics.district.network.techtype.requests.PossibleTechTypeTable; import org.simantics.district.network.techtype.requests.TechTypeTableData; import org.simantics.district.network.techtype.requests.TechTypeTableKeyName; +import org.simantics.district.network.techtype.requests.WriteTechTypeTable; import org.simantics.layer0.Layer0; import org.simantics.scl.runtime.SCLContext; import org.simantics.structural.stubs.StructuralResource2; @@ -42,6 +48,31 @@ public class TechTypeUtils { public static String DEFAULT_KEY_NAME = "pipeCode"; + /** + * Load a tech type table from a CSV file and write it in the active model. + * + * @param componentType The component type that the tech type table is associated with + * @param filePath A path to a CSV file + * @throws DatabaseException + * @throws IOException + */ + public static void loadTechTypeTable(Resource componentType, String filePath) throws DatabaseException, IOException { + String data; + try { + data = Files.lines(Paths.get(filePath), Charset.defaultCharset()).collect(Collectors.joining("\n")); + } catch (IOException e) { + LOGGER.error("Failed to read contents of file '{}' as {}", filePath, Charset.defaultCharset(), e); + return; + } + + Simantics.getSession().syncRequest(new WriteRequest() { + @Override + public void perform(WriteGraph graph) throws DatabaseException { + graph.syncRequest(new WriteTechTypeTable(componentType, data)); + } + }); + } + /** * Execute a cached query for a possible tech type table item in a table with a given item key. * @@ -100,7 +131,7 @@ public class TechTypeUtils { } /** - * Reset all components that address the given tech type table to the table values. + * Reset all diagram elements to tech type table values. * * @param table A tech type table * @throws DatabaseException -- 2.43.2