Search in sources :

Example 1 with NewAttribute

use of au.gov.asd.tac.constellation.plugins.importexport.NewAttribute 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 NewAttribute

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

the class ImportJDBCIO method definitionCompute.

private static void definitionCompute(final ArrayNode definitionArray, final String[] columns, final ImportDefinition impdef) {
    final ObjectNode def = definitionArray.addObject();
    def.put(FIRST_ROW, impdef.getFirstRow());
    if (impdef.getRowFilter() != null) {
        final ObjectNode filter = def.putObject(FILTER);
        final ArrayNode columnsArray = filter.putArray(COLUMNS);
        for (final String column : impdef.getRowFilter().getColumns()) {
            columnsArray.add(column);
        }
        final String script = impdef.getRowFilter().getScript();
        if (script != null) {
            filter.put(SCRIPT, script);
        } else {
            filter.putNull(SCRIPT);
        }
    }
    final ObjectNode attrDefs = def.putObject(ATTRIBUTES);
    for (final AttributeType attrType : AttributeType.values()) {
        final ArrayNode typeArray = attrDefs.putArray(attrType.name());
        final List<ImportAttributeDefinition> iadefs = impdef.getDefinitions(attrType);
        final Consumer<ImportAttributeDefinition> importConsumer = (ImportAttributeDefinition iadef) -> {
            if (hasSavableAttribute(iadef)) {
                final ObjectNode type = typeArray.addObject();
                // that the settings still get applied in the attribute list on load
                if (iadef.getColumnIndex() == ImportConstants.ATTRIBUTE_NOT_ASSIGNED_TO_COLUMN) {
                    type.putNull(COLUMN_LABEL);
                } else {
                    type.put(COLUMN_LABEL, columns[iadef.getColumnIndex() + 1]);
                }
                type.put(ATTRIBUTE_LABEL, iadef.getAttribute().getName());
                if (iadef.getAttribute() instanceof NewAttribute) {
                    type.put(ATTRIBUTE_TYPE, iadef.getAttribute().getAttributeType());
                    type.put(ATTRIBUTE_DESCRIPTION, iadef.getAttribute().getDescription());
                }
                type.put(TRANSLATOR, iadef.getTranslator().getClass().getName());
                type.put(TRANSLATOR_ARGS, iadef.getTranslator().getParameterValues(iadef.getParameters()));
                if (iadef.getDefaultValue() != null) {
                    type.put(DEFAULT_VALUE, iadef.getDefaultValue());
                } else {
                    type.putNull(DEFAULT_VALUE);
                }
            }
        };
        iadefs.stream().forEach(importConsumer);
    }
}
Also used : ImportAttributeDefinition(au.gov.asd.tac.constellation.plugins.importexport.ImportAttributeDefinition) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) NewAttribute(au.gov.asd.tac.constellation.plugins.importexport.NewAttribute) AttributeType(au.gov.asd.tac.constellation.plugins.importexport.AttributeType) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 3 with NewAttribute

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

the class ImportDelimitedIO method saveParameters.

public static void saveParameters(final Window parentWindow, final DelimitedImportController importController) {
    final Preferences prefs = NbPreferences.forModule(ApplicationPreferenceKeys.class);
    final String userDir = ApplicationPreferenceKeys.getUserDir(prefs);
    final File delimIoDir = new File(userDir, IMPORT_DELIMITED_DIR);
    if (!delimIoDir.exists()) {
        delimIoDir.mkdir();
    }
    if (!delimIoDir.isDirectory()) {
        final String message = String.format("Can't create directory '%s'.", delimIoDir);
        NotifyDisplayer.displayAlert(SAVE_TEMPLATE, "Templates Directory Error", message, Alert.AlertType.ERROR);
        return;
    }
    final String templName = new TemplateListDialog(parentWindow, false).getName(delimIoDir);
    if (templName != null) {
        // A JSON document to store everything in.
        // Two objects; the source data + the configuration data.
        final ObjectMapper mapper = new ObjectMapper();
        final ObjectNode rootNode = mapper.createObjectNode();
        final ObjectNode source = rootNode.putObject(SOURCE);
        source.put(PARSER, importController.getImportFileParser().getLabel());
        source.put(SCHEMA_INIT, importController.isSchemaInitialised());
        source.put(SHOW_ALL_SCHEMA_ATTRIBUTES, importController.isShowAllSchemaAttributesEnabled());
        source.put(FILES_INCLUDE_HEADERS, importController.isFilesIncludeHeadersEnabled());
        // We don't want to rely on a particular kind of graph being current when we load this definition.
        // Therefore, we only save a schema factory as the destination.
        final ImportDestination<?> importd = importController.getDestination();
        final String destination;
        if (importd instanceof SchemaDestination) {
            destination = ((SchemaDestination) importd).getDestination().getName();
        } else if (importd instanceof GraphDestination) {
            destination = ((GraphDestination) importd).getGraph().getSchema().getFactory().getName();
        } else {
            throw new IllegalArgumentException(String.format("Unrecognised destination '%s'", importd));
        }
        source.put(DESTINATION, destination);
        // One ImportDefinition per tab.
        final ArrayNode definitionArray = rootNode.putArray(DEFINITIONS);
        final List<ImportDefinition> definitions = importController.getDefinitions(importController.isFilesIncludeHeadersEnabled());
        final String[] columns = importController.getCurrentColumns();
        final Consumer<ImportDefinition> definitionConsumer = (ImportDefinition impdef) -> {
            final ObjectNode def = definitionArray.addObject();
            def.put(FIRST_ROW, impdef.getFirstRow());
            if (impdef.getRowFilter() != null) {
                final ObjectNode filter = def.putObject(FILTER);
                final ArrayNode columnsArray = filter.putArray(COLUMNS);
                for (final String column : impdef.getRowFilter().getColumns()) {
                    columnsArray.add(column);
                }
                final String script = impdef.getRowFilter().getScript();
                if (script != null) {
                    filter.put(SCRIPT, script);
                } else {
                    filter.putNull(SCRIPT);
                }
            }
            final ObjectNode attrDefs = def.putObject(ATTRIBUTES);
            for (final AttributeType attrType : AttributeType.values()) {
                final ArrayNode typeArray = attrDefs.putArray(attrType.name());
                final List<ImportAttributeDefinition> iadefs = impdef.getDefinitions(attrType);
                final Consumer<ImportAttributeDefinition> attributeDefConsumer = (ImportAttributeDefinition iadef) -> {
                    if (hasSavableAttribute(iadef)) {
                        final ObjectNode type = typeArray.addObject();
                        // that the settings still get applied in the attribute list on load
                        if (iadef.getColumnIndex() == ImportConstants.ATTRIBUTE_NOT_ASSIGNED_TO_COLUMN) {
                            type.putNull(COLUMN_LABEL);
                        } else {
                            type.put(COLUMN_LABEL, columns[iadef.getColumnIndex() + 1]);
                        }
                        type.put(ATTRIBUTE_LABEL, iadef.getAttribute().getName());
                        if (iadef.getAttribute() instanceof NewAttribute) {
                            type.put(ATTRIBUTE_TYPE, iadef.getAttribute().getAttributeType());
                            type.put(ATTRIBUTE_DESCRIPTION, iadef.getAttribute().getDescription());
                        }
                        type.put(TRANSLATOR, iadef.getTranslator().getClass().getName());
                        type.put(TRANSLATOR_ARGS, iadef.getTranslator().getParameterValues(iadef.getParameters()));
                        if (iadef.getDefaultValue() != null) {
                            type.put(DEFAULT_VALUE, iadef.getDefaultValue());
                        } else {
                            type.putNull(DEFAULT_VALUE);
                        }
                    }
                };
                iadefs.stream().forEach(attributeDefConsumer);
            }
        };
        definitions.stream().forEach(definitionConsumer);
        mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
        mapper.configure(SerializationFeature.CLOSE_CLOSEABLE, true);
        final File f = new File(delimIoDir, FilenameEncoder.encode(templName + FileExtensionConstants.JSON));
        try {
            mapper.writeValue(f, rootNode);
            StatusDisplayer.getDefault().setStatusText(String.format("Import definition saved to %s.", f.getPath()));
        } catch (final IOException ex) {
            NotifyDisplayer.display(String.format("Can't save import definition: %s", ex.getMessage()), NotifyDescriptor.ERROR_MESSAGE);
            LOGGER.log(Level.WARNING, ex.getMessage(), ex);
        }
    }
}
Also used : GraphDestination(au.gov.asd.tac.constellation.plugins.importexport.GraphDestination) TemplateListDialog(au.gov.asd.tac.constellation.plugins.importexport.TemplateListDialog) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) NewAttribute(au.gov.asd.tac.constellation.plugins.importexport.NewAttribute) SchemaDestination(au.gov.asd.tac.constellation.plugins.importexport.SchemaDestination) IOException(java.io.IOException) ImportDefinition(au.gov.asd.tac.constellation.plugins.importexport.ImportDefinition) ImportAttributeDefinition(au.gov.asd.tac.constellation.plugins.importexport.ImportAttributeDefinition) Consumer(java.util.function.Consumer) AttributeType(au.gov.asd.tac.constellation.plugins.importexport.AttributeType) ArrayList(java.util.ArrayList) List(java.util.List) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Preferences(java.util.prefs.Preferences) NbPreferences(org.openide.util.NbPreferences) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 4 with NewAttribute

use of au.gov.asd.tac.constellation.plugins.importexport.NewAttribute 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)

Aggregations

AttributeType (au.gov.asd.tac.constellation.plugins.importexport.AttributeType)4 ImportAttributeDefinition (au.gov.asd.tac.constellation.plugins.importexport.ImportAttributeDefinition)4 NewAttribute (au.gov.asd.tac.constellation.plugins.importexport.NewAttribute)4 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)4 ImportDefinition (au.gov.asd.tac.constellation.plugins.importexport.ImportDefinition)3 SchemaDestination (au.gov.asd.tac.constellation.plugins.importexport.SchemaDestination)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 File (java.io.File)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Attribute (au.gov.asd.tac.constellation.graph.Attribute)2 SchemaFactory (au.gov.asd.tac.constellation.graph.schema.SchemaFactory)2 RowFilter (au.gov.asd.tac.constellation.plugins.importexport.RowFilter)2 AttributeTranslator (au.gov.asd.tac.constellation.plugins.importexport.translator.AttributeTranslator)2 PluginParameters (au.gov.asd.tac.constellation.plugins.parameters.PluginParameters)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 GraphDestination (au.gov.asd.tac.constellation.plugins.importexport.GraphDestination)1 TemplateListDialog (au.gov.asd.tac.constellation.plugins.importexport.TemplateListDialog)1 ImportFileParser (au.gov.asd.tac.constellation.plugins.importexport.delimited.parser.ImportFileParser)1