Search in sources :

Example 1 with AttributeTranslator

use of au.gov.asd.tac.constellation.plugins.importexport.translator.AttributeTranslator in project constellation by constellation-app.

the class ImportJDBCIO method loadParameterFile.

private static void loadParameterFile(final JDBCImportController importController, final File delimIoDir, final String templName) {
    try {
        final ObjectMapper mapper = new ObjectMapper();
        final JsonNode root = mapper.readTree(new File(delimIoDir, FilenameEncoder.encode(templName) + FileExtensionConstants.JSON));
        final JsonNode source = root.get(SOURCE);
        final boolean schemaInit = source.get(SCHEMA_INIT).booleanValue();
        importController.setSchemaInitialised(schemaInit);
        final String destination = source.get(DESTINATION).textValue();
        final SchemaFactory schemaFactory = SchemaFactoryUtilities.getSchemaFactory(destination);
        if (schemaFactory != null) {
            importController.setDestination(new SchemaDestination(schemaFactory));
            final List<ImportDefinition> definitions = new ArrayList<>();
            final ArrayNode definitionsArray = (ArrayNode) root.withArray(DEFINITIONS);
            for (final JsonNode definitionNode : definitionsArray) {
                final int firstRow = definitionNode.get(FIRST_ROW).intValue();
                final RowFilter filter = new RowFilter();
                if (definitionNode.has(FILTER)) {
                    final JsonNode filterNode = definitionNode.get(FILTER);
                    final String script = filterNode.get(SCRIPT).textValue();
                    final JsonNode columnsArray = filterNode.withArray(COLUMNS);
                    final List<String> columns = new ArrayList<>();
                    for (final JsonNode column : columnsArray) {
                        columns.add(column.textValue());
                    }
                    filter.setScript(script);
                    filter.setColumns(columns.toArray(new String[columns.size()]));
                }
                final ImportDefinition impdef = new ImportDefinition("", firstRow, filter);
                final JsonNode attributesNode = definitionNode.get(ATTRIBUTES);
                for (final AttributeType attrType : AttributeType.values()) {
                    final ArrayNode columnArray = (ArrayNode) attributesNode.withArray(attrType.toString());
                    for (final JsonNode column : columnArray) {
                        final String columnLabel = column.get(COLUMN_LABEL).textValue();
                        final String label = column.get(ATTRIBUTE_LABEL).textValue();
                        if (!importController.hasAttribute(attrType.getElementType(), label)) {
                            // Manually created attribute.
                            final String type = column.get(ATTRIBUTE_TYPE).textValue();
                            final String descr = column.get(ATTRIBUTE_DESCRIPTION).textValue();
                            final NewAttribute a = new NewAttribute(attrType.getElementType(), type, label, descr);
                            importController.createManualAttribute(a);
                        }
                        final Attribute attribute = importController.getAttribute(attrType.getElementType(), label);
                        final AttributeTranslator translator = AttributeTranslator.getTranslator(column.get(TRANSLATOR).textValue());
                        final String args = column.get(TRANSLATOR_ARGS).textValue();
                        final String defaultValue = column.get(DEFAULT_VALUE).textValue();
                        final PluginParameters params = translator.createParameters();
                        translator.setParameterValues(params, args);
                        final ImportAttributeDefinition iad = new ImportAttributeDefinition(columnLabel, defaultValue, attribute, translator, params);
                        impdef.addDefinition(attrType, iad);
                    }
                }
                definitions.add(impdef);
            }
            importController.setClearManuallyAdded(false);
            try {
                ((JDBCImportPane) importController.getStage()).update(importController, definitions);
            } finally {
                importController.setClearManuallyAdded(true);
            }
        } else {
            final String msg = String.format("Can't find schema factory '%s'", destination);
            final NotifyDescriptor nd = new NotifyDescriptor.Message(msg, NotifyDescriptor.ERROR_MESSAGE);
            DialogDisplayer.getDefault().notify(nd);
        }
    } catch (final IOException ex) {
        LOGGER.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
    }
}
Also used : SchemaFactory(au.gov.asd.tac.constellation.graph.schema.SchemaFactory) NewAttribute(au.gov.asd.tac.constellation.plugins.importexport.NewAttribute) SchemaDestination(au.gov.asd.tac.constellation.plugins.importexport.SchemaDestination) NewAttribute(au.gov.asd.tac.constellation.plugins.importexport.NewAttribute) Attribute(au.gov.asd.tac.constellation.graph.Attribute) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) ImportDefinition(au.gov.asd.tac.constellation.plugins.importexport.ImportDefinition) AttributeTranslator(au.gov.asd.tac.constellation.plugins.importexport.translator.AttributeTranslator) NotifyDescriptor(org.openide.NotifyDescriptor) ImportAttributeDefinition(au.gov.asd.tac.constellation.plugins.importexport.ImportAttributeDefinition) RowFilter(au.gov.asd.tac.constellation.plugins.importexport.RowFilter) AttributeType(au.gov.asd.tac.constellation.plugins.importexport.AttributeType) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with AttributeTranslator

use of au.gov.asd.tac.constellation.plugins.importexport.translator.AttributeTranslator in project constellation by constellation-app.

the class ImportDelimitedIO method loadParameterFile.

private static void loadParameterFile(final DelimitedImportController importController, final File delimIoDir, final String templName) {
    try {
        final ObjectMapper mapper = new ObjectMapper();
        final JsonNode root = mapper.readTree(new File(delimIoDir, FilenameEncoder.encode(templName) + FileExtensionConstants.JSON));
        final JsonNode source = root.get(SOURCE);
        final String parser = source.get(PARSER).textValue();
        final ImportFileParser ifp = ImportFileParser.getParser(parser);
        if (!importController.getImportFileParser().getLabel().equals(parser)) {
            final String message = String.format("Template is for a different file Parser '%s'.", parser);
            NotifyDisplayer.displayAlert(LOAD_TEMPLATE, "File Parser Mismatch", message, Alert.AlertType.ERROR);
            return;
        }
        importController.setImportFileParser(ifp);
        final boolean schemaInit = source.get(SCHEMA_INIT).booleanValue();
        importController.setSchemaInitialised(schemaInit);
        final boolean filesIncludeHeaders = source.get(FILES_INCLUDE_HEADERS).booleanValue();
        importController.setfilesIncludeHeaders(filesIncludeHeaders);
        final boolean showAllSchemaAttributes = source.get(SHOW_ALL_SCHEMA_ATTRIBUTES) != null && source.get(SHOW_ALL_SCHEMA_ATTRIBUTES).booleanValue();
        importController.setShowAllSchemaAttributes(showAllSchemaAttributes);
        final String destination = source.get(DESTINATION).textValue();
        final SchemaFactory schemaFactory = SchemaFactoryUtilities.getSchemaFactory(destination);
        if (schemaFactory != null) {
            importController.setDestination(new SchemaDestination(schemaFactory));
            final List<ImportDefinition> definitions = new ArrayList<>();
            final ArrayNode definitionsArray = (ArrayNode) root.withArray(DEFINITIONS);
            for (final JsonNode definitionNode : definitionsArray) {
                final int firstRow = definitionNode.get(FIRST_ROW).intValue();
                final RowFilter filter = new RowFilter();
                if (definitionNode.has(FILTER)) {
                    final JsonNode filterNode = definitionNode.get(FILTER);
                    final String script = filterNode.get(SCRIPT).textValue();
                    final JsonNode columnsArray = filterNode.withArray(COLUMNS);
                    final ArrayList<String> columns = new ArrayList<>();
                    for (final JsonNode column : columnsArray) {
                        columns.add(column.textValue());
                    }
                    filter.setScript(script);
                    filter.setColumns(columns.toArray(new String[columns.size()]));
                }
                final ImportDefinition impdef = new ImportDefinition("", firstRow, filter);
                final JsonNode attributesNode = definitionNode.get(ATTRIBUTES);
                for (final AttributeType attrType : AttributeType.values()) {
                    final ArrayNode columnArray = (ArrayNode) attributesNode.withArray(attrType.toString());
                    for (final JsonNode column : columnArray) {
                        final String columnLabel = column.get(COLUMN_LABEL).textValue();
                        final String label = column.get(ATTRIBUTE_LABEL).textValue();
                        if (!importController.hasAttribute(attrType.getElementType(), label)) {
                            // Manually created attribute.
                            final String type = column.get(ATTRIBUTE_TYPE).textValue();
                            final String descr = column.get(ATTRIBUTE_DESCRIPTION).textValue();
                            final NewAttribute a = new NewAttribute(attrType.getElementType(), type, label, descr);
                            importController.createManualAttribute(a);
                        }
                        final Attribute attribute = importController.getAttribute(attrType.getElementType(), label);
                        final AttributeTranslator translator = AttributeTranslator.getTranslator(column.get(TRANSLATOR).textValue());
                        final String args = column.get(TRANSLATOR_ARGS).textValue();
                        final String defaultValue = column.get(DEFAULT_VALUE).textValue();
                        final PluginParameters params = translator.createParameters();
                        translator.setParameterValues(params, args);
                        final ImportAttributeDefinition iad = new ImportAttributeDefinition(columnLabel, defaultValue, attribute, translator, params);
                        impdef.addDefinition(attrType, iad);
                    }
                }
                definitions.add(impdef);
            }
            importController.setClearManuallyAdded(false);
            try {
                ((DelimitedImportPane) importController.getStage()).update(importController, definitions);
            } finally {
                importController.setClearManuallyAdded(true);
            }
        } else {
            final String message = String.format("Can't find schema factory '%s'", destination);
            NotifyDisplayer.displayAlert(LOAD_TEMPLATE, "Destination Schema Error", message, Alert.AlertType.ERROR);
        }
    } catch (final IOException ex) {
        LOGGER.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
    }
}
Also used : SchemaFactory(au.gov.asd.tac.constellation.graph.schema.SchemaFactory) ImportFileParser(au.gov.asd.tac.constellation.plugins.importexport.delimited.parser.ImportFileParser) NewAttribute(au.gov.asd.tac.constellation.plugins.importexport.NewAttribute) SchemaDestination(au.gov.asd.tac.constellation.plugins.importexport.SchemaDestination) NewAttribute(au.gov.asd.tac.constellation.plugins.importexport.NewAttribute) Attribute(au.gov.asd.tac.constellation.graph.Attribute) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) ImportDefinition(au.gov.asd.tac.constellation.plugins.importexport.ImportDefinition) AttributeTranslator(au.gov.asd.tac.constellation.plugins.importexport.translator.AttributeTranslator) ImportAttributeDefinition(au.gov.asd.tac.constellation.plugins.importexport.ImportAttributeDefinition) RowFilter(au.gov.asd.tac.constellation.plugins.importexport.RowFilter) AttributeType(au.gov.asd.tac.constellation.plugins.importexport.AttributeType) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with AttributeTranslator

use of au.gov.asd.tac.constellation.plugins.importexport.translator.AttributeTranslator in project constellation by constellation-app.

the class ImportTableColumn method validate.

public boolean validate(final List<TableRow> data) {
    final AttributeNode attributeNode = getAttributeNode();
    boolean columnFailed = false;
    if (attributeNode != null) {
        final AttributeTranslator parser = attributeNode.getTranslator();
        final PluginParameters parserParameters = attributeNode.getTranslatorParameters();
        final String defaultValue = attributeNode.getDefaultValue();
        Class<? extends AttributeDescription> attributeDescriptionClass = AttributeRegistry.getDefault().getAttributes().get(attributeNode.getAttribute().getAttributeType());
        // Handle pseudo-attributes
        if (attributeDescriptionClass == null && attributeNode.getAttribute().getName().equals(ImportController.DIRECTED)) {
            attributeDescriptionClass = BooleanAttributeDescription.class;
        }
        try {
            final AttributeDescription attributeDescription = attributeDescriptionClass != null ? attributeDescriptionClass.getDeclaredConstructor().newInstance() : BooleanAttributeDescription.class.getDeclaredConstructor().newInstance();
            for (final TableRow row : data) {
                columnFailed = processRow(row, attributeDescription, parser, parserParameters, defaultValue);
            }
        } catch (final IllegalAccessException | IllegalArgumentException | InstantiationException | NoSuchMethodException | SecurityException | InvocationTargetException ex) {
            LOGGER.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
        }
    } else {
        data.stream().map(row -> row.getProperty(columnIndex)).map(property -> {
            property.setText(property.get().getOriginalText());
            return property;
        }).forEachOrdered(property -> property.setMessage(null, false));
    }
    getGraphic().setStyle(columnFailed ? "-fx-background-color: rgba(255, 0, 0, 0.3);" : "-fx-background-color: transparent;");
    return !columnFailed;
}
Also used : Pos(javafx.geometry.Pos) AttributeRegistry(au.gov.asd.tac.constellation.graph.attribute.AttributeRegistry) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) CellValue(au.gov.asd.tac.constellation.plugins.importexport.model.CellValue) Label(javafx.scene.control.Label) Node(javafx.scene.Node) AttributeTranslator(au.gov.asd.tac.constellation.plugins.importexport.translator.AttributeTranslator) Logger(java.util.logging.Logger) InvocationTargetException(java.lang.reflect.InvocationTargetException) Level(java.util.logging.Level) TableColumn(javafx.scene.control.TableColumn) List(java.util.List) Insets(javafx.geometry.Insets) CellValueProperty(au.gov.asd.tac.constellation.plugins.importexport.model.CellValueProperty) BooleanAttributeDescription(au.gov.asd.tac.constellation.graph.attribute.BooleanAttributeDescription) AttributeDescription(au.gov.asd.tac.constellation.graph.attribute.AttributeDescription) BorderPane(javafx.scene.layout.BorderPane) TableRow(au.gov.asd.tac.constellation.plugins.importexport.model.TableRow) AttributeTranslator(au.gov.asd.tac.constellation.plugins.importexport.translator.AttributeTranslator) InvocationTargetException(java.lang.reflect.InvocationTargetException) BooleanAttributeDescription(au.gov.asd.tac.constellation.graph.attribute.BooleanAttributeDescription) AttributeDescription(au.gov.asd.tac.constellation.graph.attribute.AttributeDescription) TableRow(au.gov.asd.tac.constellation.plugins.importexport.model.TableRow) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters)

Example 4 with AttributeTranslator

use of au.gov.asd.tac.constellation.plugins.importexport.translator.AttributeTranslator in project constellation by constellation-app.

the class AttributeList method createDefinition.

/**
 * Create an {@code ImportDefinition} from attributes modified on the
 * attribute list
 *
 * @param importDefinition the {@link ImportDefinition} that will hold the
 * new {@link ImportAttributeDefinition}s.
 */
public void createDefinition(final ImportDefinition importDefinition) {
    attributeNodes.values().stream().filter(attributeNode -> (attributeNode.getColumn() == null)).forEachOrdered(attributeNode -> {
        final String defaultValue = attributeNode.getDefaultValue();
        final AttributeTranslator translator = attributeNode.getTranslator();
        if (defaultValue != null || (translator != null && !(translator instanceof DefaultAttributeTranslator))) {
            final ImportAttributeDefinition attributeDefinition = new ImportAttributeDefinition(defaultValue, attributeNode.getAttribute(), attributeNode.getTranslator(), attributeNode.getTranslatorParameters());
            importDefinition.addDefinition(attributeType, attributeDefinition);
        }
    });
}
Also used : Pos(javafx.geometry.Pos) DefaultAttributeTranslator(au.gov.asd.tac.constellation.plugins.importexport.translator.DefaultAttributeTranslator) Iterator(java.util.Iterator) Node(javafx.scene.Node) MouseEvent(javafx.scene.input.MouseEvent) Set(java.util.Set) AttributeTranslator(au.gov.asd.tac.constellation.plugins.importexport.translator.AttributeTranslator) HashMap(java.util.HashMap) VBox(javafx.scene.layout.VBox) HashSet(java.util.HashSet) Insets(javafx.geometry.Insets) Map(java.util.Map) Entry(java.util.Map.Entry) Point2D(javafx.geometry.Point2D) Attribute(au.gov.asd.tac.constellation.graph.Attribute) ObservableList(javafx.collections.ObservableList) DefaultAttributeTranslator(au.gov.asd.tac.constellation.plugins.importexport.translator.DefaultAttributeTranslator) AttributeTranslator(au.gov.asd.tac.constellation.plugins.importexport.translator.AttributeTranslator) DefaultAttributeTranslator(au.gov.asd.tac.constellation.plugins.importexport.translator.DefaultAttributeTranslator)

Aggregations

AttributeTranslator (au.gov.asd.tac.constellation.plugins.importexport.translator.AttributeTranslator)4 Attribute (au.gov.asd.tac.constellation.graph.Attribute)3 PluginParameters (au.gov.asd.tac.constellation.plugins.parameters.PluginParameters)3 SchemaFactory (au.gov.asd.tac.constellation.graph.schema.SchemaFactory)2 AttributeType (au.gov.asd.tac.constellation.plugins.importexport.AttributeType)2 ImportAttributeDefinition (au.gov.asd.tac.constellation.plugins.importexport.ImportAttributeDefinition)2 ImportDefinition (au.gov.asd.tac.constellation.plugins.importexport.ImportDefinition)2 NewAttribute (au.gov.asd.tac.constellation.plugins.importexport.NewAttribute)2 RowFilter (au.gov.asd.tac.constellation.plugins.importexport.RowFilter)2 SchemaDestination (au.gov.asd.tac.constellation.plugins.importexport.SchemaDestination)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 File (java.io.File)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Insets (javafx.geometry.Insets)2 Pos (javafx.geometry.Pos)2 Node (javafx.scene.Node)2 AttributeDescription (au.gov.asd.tac.constellation.graph.attribute.AttributeDescription)1