Search in sources :

Example 1 with GraphDestination

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

the class ImportJDBCIO method saveParameters.

public static void saveParameters(final Window parentWindow, final JDBCImportController importController) {
    final Preferences prefs = NbPreferences.forModule(ApplicationPreferenceKeys.class);
    final String userDir = ApplicationPreferenceKeys.getUserDir(prefs);
    final File delimIoDir = new File(userDir, IMPORT_JDBC_DIR);
    if (!delimIoDir.exists()) {
        delimIoDir.mkdir();
    }
    if (!delimIoDir.isDirectory()) {
        final String msg = String.format("Can't create directory '%s'.", delimIoDir);
        NotifyDisplayer.display(msg, NotifyDescriptor.ERROR_MESSAGE);
        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(SCHEMA_INIT, importController.isSchemaInitialised());
        // 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(false);
        final String[] columns = importController.getCurrentColumns();
        definitions.stream().forEach(impdef -> definitionCompute(definitionArray, columns, impdef));
        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) {
            LOGGER.log(Level.WARNING, ex.getMessage(), ex);
            final String msg = String.format("Can't save import definition: %s", ex.getMessage());
            final NotifyDescriptor nd = new NotifyDescriptor.Message(msg, NotifyDescriptor.ERROR_MESSAGE);
            DialogDisplayer.getDefault().notify(nd);
        }
    }
}
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) SchemaDestination(au.gov.asd.tac.constellation.plugins.importexport.SchemaDestination) IOException(java.io.IOException) ImportDefinition(au.gov.asd.tac.constellation.plugins.importexport.ImportDefinition) NotifyDescriptor(org.openide.NotifyDescriptor) 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 2 with GraphDestination

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

Aggregations

GraphDestination (au.gov.asd.tac.constellation.plugins.importexport.GraphDestination)2 ImportDefinition (au.gov.asd.tac.constellation.plugins.importexport.ImportDefinition)2 SchemaDestination (au.gov.asd.tac.constellation.plugins.importexport.SchemaDestination)2 TemplateListDialog (au.gov.asd.tac.constellation.plugins.importexport.TemplateListDialog)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 File (java.io.File)2 IOException (java.io.IOException)2 Preferences (java.util.prefs.Preferences)2 NbPreferences (org.openide.util.NbPreferences)2 AttributeType (au.gov.asd.tac.constellation.plugins.importexport.AttributeType)1 ImportAttributeDefinition (au.gov.asd.tac.constellation.plugins.importexport.ImportAttributeDefinition)1 NewAttribute (au.gov.asd.tac.constellation.plugins.importexport.NewAttribute)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Consumer (java.util.function.Consumer)1 NotifyDescriptor (org.openide.NotifyDescriptor)1