Search in sources :

Example 71 with NodeInterface

use of org.structr.core.graph.NodeInterface in project structr by structr.

the class RelationshipEndNodeTypeConverter method revert.

@Override
public Object revert(String source) {
    if (currentObject instanceof AbstractRelationship) {
        final AbstractRelationship rel = (AbstractRelationship) currentObject;
        final NodeInterface endNode = rel.getTargetNode();
        if (endNode != null) {
            return endNode.getType();
        }
    }
    return null;
}
Also used : AbstractRelationship(org.structr.core.entity.AbstractRelationship) NodeInterface(org.structr.core.graph.NodeInterface)

Example 72 with NodeInterface

use of org.structr.core.graph.NodeInterface in project structr by structr.

the class RelationshipStartNodeTypeConverter method revert.

@Override
public Object revert(Object source) {
    if (currentObject instanceof AbstractRelationship) {
        final AbstractRelationship rel = (AbstractRelationship) currentObject;
        final NodeInterface startNode = rel.getSourceNode();
        if (startNode != null) {
            return startNode.getType();
        }
    }
    return null;
}
Also used : AbstractRelationship(org.structr.core.entity.AbstractRelationship) NodeInterface(org.structr.core.graph.NodeInterface)

Example 73 with NodeInterface

use of org.structr.core.graph.NodeInterface in project structr by structr.

the class CsvImportTest method testCsvFileImportSingleQuotes.

@Test
public void testCsvFileImportSingleQuotes() {
    String newFileId = null;
    // test setup
    try (final Tx tx = app.tx()) {
        final String csvData = "'id';'type';'name';'Test header with whitespace';'ümläüt header'\n" + "'0';'One';'name: one';'11';'22'\n" + "'1';'Two';'name: two';'22';'33'\n" + "'2';'Three';'name: three';'33';'44'";
        final byte[] fileData = csvData.getBytes("utf-8");
        final File file = FileHelper.createFile(securityContext, fileData, "text/csv", File.class, "test.csv");
        // extract UUID for later use
        newFileId = file.getUuid();
        // create new type
        final JsonSchema schema = StructrSchema.createEmptySchema();
        final JsonType newType = schema.addType("Item");
        newType.addStringProperty("name");
        newType.addIntegerProperty("originId").isIndexed();
        newType.addStringProperty("typeName");
        newType.addIntegerProperty("test1");
        newType.addIntegerProperty("test2");
        StructrSchema.extendDatabaseSchema(app, schema);
        // create test user
        app.create(User.class, new NodeAttribute<>(StructrApp.key(User.class, "name"), "admin"), new NodeAttribute<>(StructrApp.key(User.class, "password"), "admin"), new NodeAttribute<>(StructrApp.key(User.class, "isAdmin"), true));
        tx.success();
    } catch (Throwable t) {
        t.printStackTrace();
        fail("Unexpected exception.");
    }
    final Gson gson = new GsonBuilder().setPrettyPrinting().create();
    final Map<String, Object> params = new LinkedHashMap<>();
    final Map<String, Object> mappings = new LinkedHashMap<>();
    // import parameters
    params.put("targetType", "Item");
    params.put("quoteChar", "'");
    params.put("delimiter", ";");
    params.put("mappings", mappings);
    // property mapping
    mappings.put("originId", "id");
    mappings.put("typeName", "type");
    mappings.put("name", "name");
    mappings.put("test1", "Test header with whitespace");
    mappings.put("test2", "ümläüt header");
    RestAssured.given().contentType("application/json; charset=UTF-8").header("X-User", "admin").header("X-Password", "admin").filter(RequestLoggingFilter.logRequestTo(System.out)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(401)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(403)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).body(gson.toJson(params)).expect().statusCode(200).when().post("/File/" + newFileId + "/doCSVImport");
    // wait for result (import is async.)
    try {
        Thread.sleep(2000);
    } catch (Throwable t) {
    }
    // check imported data for correct import
    try (final Tx tx = app.tx()) {
        final ConfigurationProvider conf = StructrApp.getConfiguration();
        final Class type = conf.getNodeEntityClass("Item");
        final List<NodeInterface> items = app.nodeQuery(type).sort(conf.getPropertyKeyForJSONName(type, "originId")).getAsList();
        assertEquals("Invalid CSV import result, expected 3 items to be created from CSV import. ", 3, items.size());
        final NodeInterface one = items.get(0);
        final NodeInterface two = items.get(1);
        final NodeInterface three = items.get(2);
        assertEquals("Invalid CSV mapping result", 0, one.getProperty(conf.getPropertyKeyForJSONName(type, "originId")));
        assertEquals("Invalid CSV mapping result", 1, two.getProperty(conf.getPropertyKeyForJSONName(type, "originId")));
        assertEquals("Invalid CSV mapping result", 2, three.getProperty(conf.getPropertyKeyForJSONName(type, "originId")));
        assertEquals("Invalid CSV mapping result", "One", one.getProperty(conf.getPropertyKeyForJSONName(type, "typeName")));
        assertEquals("Invalid CSV mapping result", "Two", two.getProperty(conf.getPropertyKeyForJSONName(type, "typeName")));
        assertEquals("Invalid CSV mapping result", "Three", three.getProperty(conf.getPropertyKeyForJSONName(type, "typeName")));
        assertEquals("Invalid CSV mapping result", "name: one", one.getProperty(conf.getPropertyKeyForJSONName(type, "name")));
        assertEquals("Invalid CSV mapping result", "name: two", two.getProperty(conf.getPropertyKeyForJSONName(type, "name")));
        assertEquals("Invalid CSV mapping result", "name: three", three.getProperty(conf.getPropertyKeyForJSONName(type, "name")));
        assertEquals("Invalid CSV mapping result", 11, one.getProperty(conf.getPropertyKeyForJSONName(type, "test1")));
        assertEquals("Invalid CSV mapping result", 22, two.getProperty(conf.getPropertyKeyForJSONName(type, "test1")));
        assertEquals("Invalid CSV mapping result", 33, three.getProperty(conf.getPropertyKeyForJSONName(type, "test1")));
        assertEquals("Invalid CSV mapping result", 22, one.getProperty(conf.getPropertyKeyForJSONName(type, "test2")));
        assertEquals("Invalid CSV mapping result", 33, two.getProperty(conf.getPropertyKeyForJSONName(type, "test2")));
        assertEquals("Invalid CSV mapping result", 44, three.getProperty(conf.getPropertyKeyForJSONName(type, "test2")));
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception.");
    }
}
Also used : JsonType(org.structr.schema.json.JsonType) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) GsonBuilder(com.google.gson.GsonBuilder) ConfigurationProvider(org.structr.schema.ConfigurationProvider) JsonSchema(org.structr.schema.json.JsonSchema) Gson(com.google.gson.Gson) LinkedHashMap(java.util.LinkedHashMap) File(org.structr.web.entity.File) NodeInterface(org.structr.core.graph.NodeInterface) Test(org.junit.Test)

Example 74 with NodeInterface

use of org.structr.core.graph.NodeInterface in project structr by structr.

the class CsvImportTest method testCsvFileImportNoQuotes.

@Test
public void testCsvFileImportNoQuotes() {
    String newFileId = null;
    // test setup
    try (final Tx tx = app.tx()) {
        final String csvData = "id;type;name;Test header with whitespace;ümläüt header\n" + "0;One;name: one;11;22\n" + "1;Two;name: two;22;33\n" + "2;Three;name: three;33;44";
        final byte[] fileData = csvData.getBytes("utf-8");
        final File file = FileHelper.createFile(securityContext, fileData, "text/csv", File.class, "test.csv");
        // extract UUID for later use
        newFileId = file.getUuid();
        // create new type
        final JsonSchema schema = StructrSchema.createEmptySchema();
        final JsonType newType = schema.addType("Item");
        newType.addStringProperty("name");
        newType.addIntegerProperty("originId").isIndexed();
        newType.addStringProperty("typeName");
        newType.addIntegerProperty("test1");
        newType.addIntegerProperty("test2");
        StructrSchema.extendDatabaseSchema(app, schema);
        // create test user
        app.create(User.class, new NodeAttribute<>(StructrApp.key(User.class, "name"), "admin"), new NodeAttribute<>(StructrApp.key(User.class, "password"), "admin"), new NodeAttribute<>(StructrApp.key(User.class, "isAdmin"), true));
        tx.success();
    } catch (Throwable t) {
        t.printStackTrace();
        fail("Unexpected exception.");
    }
    final Gson gson = new GsonBuilder().setPrettyPrinting().create();
    final Map<String, Object> params = new LinkedHashMap<>();
    final Map<String, Object> mappings = new LinkedHashMap<>();
    // import parameters
    params.put("targetType", "Item");
    params.put("quoteChar", "");
    params.put("delimiter", ";");
    params.put("mappings", mappings);
    // property mapping
    mappings.put("originId", "id");
    mappings.put("typeName", "type");
    mappings.put("name", "name");
    mappings.put("test1", "Test header with whitespace");
    mappings.put("test2", "ümläüt header");
    RestAssured.given().contentType("application/json; charset=UTF-8").header("X-User", "admin").header("X-Password", "admin").filter(RequestLoggingFilter.logRequestTo(System.out)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(401)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(403)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).body(gson.toJson(params)).expect().statusCode(200).when().post("/File/" + newFileId + "/doCSVImport");
    // wait for result (import is async.)
    try {
        Thread.sleep(2000);
    } catch (Throwable t) {
    }
    // check imported data for correct import
    try (final Tx tx = app.tx()) {
        final ConfigurationProvider conf = StructrApp.getConfiguration();
        final Class type = conf.getNodeEntityClass("Item");
        final List<NodeInterface> items = app.nodeQuery(type).sort(conf.getPropertyKeyForJSONName(type, "originId")).getAsList();
        assertEquals("Invalid CSV import result, expected 3 items to be created from CSV import. ", 3, items.size());
        final NodeInterface one = items.get(0);
        final NodeInterface two = items.get(1);
        final NodeInterface three = items.get(2);
        assertEquals("Invalid CSV mapping result", 0, one.getProperty(conf.getPropertyKeyForJSONName(type, "originId")));
        assertEquals("Invalid CSV mapping result", 1, two.getProperty(conf.getPropertyKeyForJSONName(type, "originId")));
        assertEquals("Invalid CSV mapping result", 2, three.getProperty(conf.getPropertyKeyForJSONName(type, "originId")));
        assertEquals("Invalid CSV mapping result", "One", one.getProperty(conf.getPropertyKeyForJSONName(type, "typeName")));
        assertEquals("Invalid CSV mapping result", "Two", two.getProperty(conf.getPropertyKeyForJSONName(type, "typeName")));
        assertEquals("Invalid CSV mapping result", "Three", three.getProperty(conf.getPropertyKeyForJSONName(type, "typeName")));
        assertEquals("Invalid CSV mapping result", "name: one", one.getProperty(conf.getPropertyKeyForJSONName(type, "name")));
        assertEquals("Invalid CSV mapping result", "name: two", two.getProperty(conf.getPropertyKeyForJSONName(type, "name")));
        assertEquals("Invalid CSV mapping result", "name: three", three.getProperty(conf.getPropertyKeyForJSONName(type, "name")));
        assertEquals("Invalid CSV mapping result", 11, one.getProperty(conf.getPropertyKeyForJSONName(type, "test1")));
        assertEquals("Invalid CSV mapping result", 22, two.getProperty(conf.getPropertyKeyForJSONName(type, "test1")));
        assertEquals("Invalid CSV mapping result", 33, three.getProperty(conf.getPropertyKeyForJSONName(type, "test1")));
        assertEquals("Invalid CSV mapping result", 22, one.getProperty(conf.getPropertyKeyForJSONName(type, "test2")));
        assertEquals("Invalid CSV mapping result", 33, two.getProperty(conf.getPropertyKeyForJSONName(type, "test2")));
        assertEquals("Invalid CSV mapping result", 44, three.getProperty(conf.getPropertyKeyForJSONName(type, "test2")));
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception.");
    }
}
Also used : JsonType(org.structr.schema.json.JsonType) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) GsonBuilder(com.google.gson.GsonBuilder) ConfigurationProvider(org.structr.schema.ConfigurationProvider) JsonSchema(org.structr.schema.json.JsonSchema) Gson(com.google.gson.Gson) LinkedHashMap(java.util.LinkedHashMap) File(org.structr.web.entity.File) NodeInterface(org.structr.core.graph.NodeInterface) Test(org.junit.Test)

Example 75 with NodeInterface

use of org.structr.core.graph.NodeInterface in project structr by structr.

the class CsvImportTest method testCsvFileImportSingleQuotesLineBreakInCell.

@Test
public void testCsvFileImportSingleQuotesLineBreakInCell() {
    String newFileId = null;
    // test setup
    try (final Tx tx = app.tx()) {
        final String csvData = "'id';'type';'name';'Test header with whitespace';'ümläüt header'\n" + "'0';'One';'name:\none';'11';'22'\n" + "'1';'Two';'name: two';'22';'33'\n" + "'2';'Three';'name: three';'33';'44'";
        final byte[] fileData = csvData.getBytes("utf-8");
        final File file = FileHelper.createFile(securityContext, fileData, "text/csv", File.class, "test.csv");
        // extract UUID for later use
        newFileId = file.getUuid();
        // create new type
        final JsonSchema schema = StructrSchema.createEmptySchema();
        final JsonType newType = schema.addType("Item");
        newType.addStringProperty("name");
        newType.addIntegerProperty("originId").isIndexed();
        newType.addStringProperty("typeName");
        newType.addIntegerProperty("test1");
        newType.addIntegerProperty("test2");
        StructrSchema.extendDatabaseSchema(app, schema);
        // create test user
        app.create(User.class, new NodeAttribute<>(StructrApp.key(User.class, "name"), "admin"), new NodeAttribute<>(StructrApp.key(User.class, "password"), "admin"), new NodeAttribute<>(StructrApp.key(User.class, "isAdmin"), true));
        tx.success();
    } catch (Throwable t) {
        t.printStackTrace();
        fail("Unexpected exception.");
    }
    final Gson gson = new GsonBuilder().setPrettyPrinting().create();
    final Map<String, Object> params = new LinkedHashMap<>();
    final Map<String, Object> mappings = new LinkedHashMap<>();
    // import parameters
    params.put("targetType", "Item");
    params.put("quoteChar", "'");
    params.put("delimiter", ";");
    params.put("mappings", mappings);
    // property mapping
    mappings.put("originId", "id");
    mappings.put("typeName", "type");
    mappings.put("name", "name");
    mappings.put("test1", "Test header with whitespace");
    mappings.put("test2", "ümläüt header");
    RestAssured.given().contentType("application/json; charset=UTF-8").header("X-User", "admin").header("X-Password", "admin").filter(RequestLoggingFilter.logRequestTo(System.out)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(401)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(403)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).body(gson.toJson(params)).expect().statusCode(200).when().post("/File/" + newFileId + "/doCSVImport");
    // wait for result (import is async.)
    try {
        Thread.sleep(2000);
    } catch (Throwable t) {
    }
    // check imported data for correct import
    try (final Tx tx = app.tx()) {
        final ConfigurationProvider conf = StructrApp.getConfiguration();
        final Class type = conf.getNodeEntityClass("Item");
        final List<NodeInterface> items = app.nodeQuery(type).sort(conf.getPropertyKeyForJSONName(type, "originId")).getAsList();
        assertEquals("Invalid CSV import result, expected 3 items to be created from CSV import. ", 3, items.size());
        final NodeInterface one = items.get(0);
        final NodeInterface two = items.get(1);
        final NodeInterface three = items.get(2);
        assertEquals("Invalid CSV mapping result", 0, one.getProperty(conf.getPropertyKeyForJSONName(type, "originId")));
        assertEquals("Invalid CSV mapping result", 1, two.getProperty(conf.getPropertyKeyForJSONName(type, "originId")));
        assertEquals("Invalid CSV mapping result", 2, three.getProperty(conf.getPropertyKeyForJSONName(type, "originId")));
        assertEquals("Invalid CSV mapping result", "One", one.getProperty(conf.getPropertyKeyForJSONName(type, "typeName")));
        assertEquals("Invalid CSV mapping result", "Two", two.getProperty(conf.getPropertyKeyForJSONName(type, "typeName")));
        assertEquals("Invalid CSV mapping result", "Three", three.getProperty(conf.getPropertyKeyForJSONName(type, "typeName")));
        assertEquals("Invalid CSV mapping result", "name:\none", one.getProperty(conf.getPropertyKeyForJSONName(type, "name")));
        assertEquals("Invalid CSV mapping result", "name: two", two.getProperty(conf.getPropertyKeyForJSONName(type, "name")));
        assertEquals("Invalid CSV mapping result", "name: three", three.getProperty(conf.getPropertyKeyForJSONName(type, "name")));
        assertEquals("Invalid CSV mapping result", 11, one.getProperty(conf.getPropertyKeyForJSONName(type, "test1")));
        assertEquals("Invalid CSV mapping result", 22, two.getProperty(conf.getPropertyKeyForJSONName(type, "test1")));
        assertEquals("Invalid CSV mapping result", 33, three.getProperty(conf.getPropertyKeyForJSONName(type, "test1")));
        assertEquals("Invalid CSV mapping result", 22, one.getProperty(conf.getPropertyKeyForJSONName(type, "test2")));
        assertEquals("Invalid CSV mapping result", 33, two.getProperty(conf.getPropertyKeyForJSONName(type, "test2")));
        assertEquals("Invalid CSV mapping result", 44, three.getProperty(conf.getPropertyKeyForJSONName(type, "test2")));
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception.");
    }
}
Also used : JsonType(org.structr.schema.json.JsonType) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) GsonBuilder(com.google.gson.GsonBuilder) ConfigurationProvider(org.structr.schema.ConfigurationProvider) JsonSchema(org.structr.schema.json.JsonSchema) Gson(com.google.gson.Gson) LinkedHashMap(java.util.LinkedHashMap) File(org.structr.web.entity.File) NodeInterface(org.structr.core.graph.NodeInterface) Test(org.junit.Test)

Aggregations

NodeInterface (org.structr.core.graph.NodeInterface)186 FrameworkException (org.structr.common.error.FrameworkException)120 Tx (org.structr.core.graph.Tx)116 Test (org.junit.Test)81 PropertyKey (org.structr.core.property.PropertyKey)61 LinkedList (java.util.LinkedList)36 StructrTest (org.structr.common.StructrTest)29 PropertyMap (org.structr.core.property.PropertyMap)26 TestOne (org.structr.core.entity.TestOne)24 List (java.util.List)23 GraphObject (org.structr.core.GraphObject)22 App (org.structr.core.app.App)21 StructrApp (org.structr.core.app.StructrApp)21 GenericNode (org.structr.core.entity.GenericNode)21 Before (org.junit.Before)18 Result (org.structr.core.Result)18 AbstractRelationship (org.structr.core.entity.AbstractRelationship)16 Random (java.util.Random)15 RelationshipInterface (org.structr.core.graph.RelationshipInterface)14 ErrorToken (org.structr.common.error.ErrorToken)12