+package org.simantics.district.network.techtype;
+
+import java.util.Collection;
+import java.util.Map;
+
+import org.simantics.Simantics;
+import org.simantics.databoard.Bindings;
+import org.simantics.databoard.binding.NumberBinding;
+import org.simantics.databoard.binding.error.BindingException;
+import org.simantics.databoard.type.Datatype;
+import org.simantics.databoard.type.NumberType;
+import org.simantics.databoard.type.StringType;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.RequestProcessor;
+import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.procedure.adapter.TransientCacheListener;
+import org.simantics.db.common.request.PossibleIndexRoot;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.QueryIndexUtils;
+import org.simantics.db.layer0.request.PossibleVariable;
+import org.simantics.db.layer0.variable.Variable;
+import org.simantics.district.network.ontology.DistrictNetworkResource;
+import org.simantics.district.network.techtype.requests.PossibleTechTypeItem;
+import org.simantics.district.network.techtype.requests.PossibleTechTypeTable;
+import org.simantics.district.network.techtype.requests.TechTypeTableKeyName;
+import org.simantics.scl.runtime.SCLContext;
+import org.simantics.structural.stubs.StructuralResource2;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class TechTypeUtils {
+
+ final static Logger LOGGER = LoggerFactory.getLogger(TechTypeUtils.class);
+
+ public static String DEFAULT_KEY_NAME = "pipeCode";
+
+ /**
+ * Execute a cached query for a possible tech type table item in a table with a given item key.
+ *
+ * Result is null, if no such item was found.
+ *
+ * @param session A request processor on which to run the query
+ * @param itemCode A key value, such as a pipe code
+ * @throws DatabaseException
+ */
+ public static Map<String, String> getTableItem(RequestProcessor session, Resource table, String itemCode) throws DatabaseException {
+ return session.syncRequest(new PossibleTechTypeItem(table, itemCode), TransientCacheListener.instance());
+ }
+
+ /**
+ * Get a single row in a tech type table for a given item code
+ *
+ * Result is null, if no such item was found.
+ *
+ * @param table A TechTypeTable resource
+ * @param itemCode A key value, such as a pipe code
+ * @return A map from property name to value
+ * @throws DatabaseException
+ */
+ public static Map<String, String> getTableItem(Resource table, String itemCode) throws DatabaseException {
+ Object graph = SCLContext.getCurrent().get("graph");
+ if (graph != null && graph instanceof ReadGraph)
+ return getTableItem((ReadGraph) graph, table, itemCode);
+ else
+ return getTableItem(Simantics.getSession(), table, itemCode);
+ }
+
+ /**
+ * Reset all components that address the given tech type table to the table values.
+ *
+ * @param table A tech type table
+ * @throws DatabaseException
+ */
+ public static void resetComponents(Resource table) throws DatabaseException {
+ Simantics.getSession().syncRequest(new WriteRequest() {
+ @Override
+ public void perform(WriteGraph graph) throws DatabaseException {
+ Resource model = graph.syncRequest(new PossibleIndexRoot(table), TransientCacheListener.instance());
+ if (model == null)
+ return;
+
+ Resource type = graph.getPossibleObject(table, DistrictNetworkResource.getInstance(graph).TechType_TechTypeTable_HasComponentType);
+ if (type == null)
+ return;
+
+ Collection<Resource> components = QueryIndexUtils.searchByType(graph, model, type);
+ for (Resource component : components) {
+ updateComponent(graph, component, table);
+ }
+ }
+ });
+ }
+
+ /**
+ * Update property values of a component based on the values in an associated tech type table, if any
+ *
+ * @param graph A write access interface to the graph database
+ * @param component A structural component
+ * @throws DatabaseException
+ */
+ public static void updateComponent(WriteGraph graph, Resource component) throws DatabaseException {
+ StructuralResource2 STR = StructuralResource2.getInstance(graph);
+
+ Resource type = graph.getSingleType(component, STR.Component);
+ Resource model = graph.syncRequest(new PossibleIndexRoot(component));
+ if (model == null) {
+ LOGGER.info("updateComponent: No model for {}", component);
+ return;
+ }
+
+ Resource table = graph.syncRequest(new PossibleTechTypeTable(model, type), TransientCacheListener.instance());
+ if (table == null) {
+ LOGGER.info("updateComponent: No tech type table for {} in {}", type, model);
+ return;
+ }
+
+ updateComponent(graph, component, table);
+ }
+
+ /**
+ * Update property values of a component based on the values in an associated tech type table, if any
+ *
+ * @param component A structural component
+ * @throws DatabaseException
+ */
+ public static void updateComponent(Resource component) throws DatabaseException {
+ if (LOGGER.isTraceEnabled())
+ LOGGER.trace("updateComponent({})", component);
+
+ Simantics.getSession().syncRequest(new WriteRequest() {
+ @Override
+ public void perform(WriteGraph graph) throws DatabaseException {
+ updateComponent(graph, component);
+ }
+ });
+ }
+
+ /**
+ * Update a single component's property values to those selected from a tech
+ * type table
+ *
+ * @param graph A write interface to the db
+ * @param component The component to be updated
+ * @param table A tech type table
+ * @throws DatabaseException
+ */
+ public static void updateComponent(WriteGraph graph, Resource component, Resource table)
+ throws DatabaseException {
+ if (LOGGER.isTraceEnabled())
+ LOGGER.trace("updateComponent(graph, {}, {}), component, table)");
+
+ Variable v = graph.syncRequest(new PossibleVariable(component));
+ if (v == null) {
+ LOGGER.info("No variable found for {}", component);
+ return;
+ }
+
+ String keyProp = getKeyPropertyName(graph, table);
+ String itemCode = v.getPropertyValue(graph, keyProp);
+
+ Map<String, String> map = graph.syncRequest(new PossibleTechTypeItem(table, itemCode), TransientCacheListener.instance());
+ if (map == null) {
+ LOGGER.info("No entry found for \"{}\" in tech type table {}", itemCode, table);
+ return;
+ }
+
+ for (String key : map.keySet()) {
+ // Don't write the key property
+ if (key.equals(DEFAULT_KEY_NAME))
+ continue;
+
+ Variable prop = v.getPossibleProperty(graph, key);
+ if (prop == null) continue;
+
+ Datatype dt = prop.getPossibleDatatype(graph);
+ if (dt == null) continue;
+
+ String value = map.get(key);
+ if (dt instanceof NumberType) {
+ try {
+ Double num = Double.valueOf(value.replace(",", "."));
+ NumberBinding binding = Bindings.getBinding(dt);
+ prop.setValue(graph, binding.create(num));
+ } catch (NumberFormatException e) {
+ // Revert to asserted value
+ Resource pred = prop.getPossiblePredicateResource(graph);
+ if (pred != null)
+ graph.deny(component, pred);
+ } catch (BindingException e) {
+ LOGGER.error("Failed to get binding for datatype {}", dt, e);
+ }
+ } else if (dt instanceof StringType) {
+ prop.setValue(graph, value);
+ } else {
+ LOGGER.warn("updateComponent: Unsupported property type {}", dt);
+ }
+ }
+ }
+
+ public static String getKeyPropertyName(ReadGraph graph, Resource table) throws DatabaseException {
+ String name = graph.syncRequest(new TechTypeTableKeyName(table), TransientCacheListener.instance());
+ if (name == null) name = "_" + DEFAULT_KEY_NAME;
+ return name;
+ }
+}