Search in sources :

Example 6 with SchemaDestination

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

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