Search in sources :

Example 46 with ConfigurationProvider

use of org.structr.schema.ConfigurationProvider in project structr by structr.

the class OWLInstance method createDatabaseNode.

public void createDatabaseNode(final App app) throws FrameworkException {
    final ConfigurationProvider config = StructrApp.getConfiguration();
    final String className = type.getStructrName(true);
    nodeType = config.getNodeEntityClass(className);
    if (nodeType != null) {
        originIdKey = StructrApp.key(nodeType, "originId");
        if (originIdKey != null) {
            instance = app.create(nodeType, new NodeAttribute(originIdKey, getId().toString()));
            if (instance != null) {
                instances.put(getId(), instance);
            }
        } else {
            OWLParserv2.logger.println("NOT creating instance for " + getId() + ", no originId property key found.");
        }
    } else {
        OWLParserv2.logger.println("NOT creating instance for " + getId() + ", no node type found.");
    }
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) ConfigurationProvider(org.structr.schema.ConfigurationProvider)

Example 47 with ConfigurationProvider

use of org.structr.schema.ConfigurationProvider in project structr by structr.

the class RDFDescription method resolveProperties.

public void resolveProperties(final NodeInterface nodeInterface, final Map<String, OWLClass> owlClassesByName, final Map<String, OWLProperty> propertiesByName) {
    final ConfigurationProvider config = StructrApp.getConfiguration();
    final NodeList children = getElement().getChildNodes();
    final int length = children.getLength();
    for (int i = 0; i < length; i++) {
        final Node node = children.item(i);
        if (node instanceof Element) {
            final Element childElement = (Element) node;
            final String tagName = childElement.getTagName();
            final OWLProperty prop = propertiesByName.get(tagName);
            if (prop != null) {
                final Class type = nodeInterface.getClass();
                final String keyName = prop.getStructrName(false);
                Object value = getValue(childElement);
                try {
                    PropertyKey key = StructrApp.key(type, keyName);
                    if (key == null) {
                        // property key not found, try inverse
                        final OWLClass owlClass = owlClassesByName.get(type.getSimpleName());
                        if (owlClass != null) {
                            final OWLClass inverse = owlClass.getInverse();
                            if (inverse != null) {
                                final Class inverseType = config.getNodeEntityClass(inverse.getStructrName(true));
                                if (inverseType != null) {
                                    key = StructrApp.key(inverseType, keyName);
                                }
                            }
                        }
                    }
                    if (key != null) {
                        final PropertyConverter converter = key.inputConverter(SecurityContext.getSuperUserInstance());
                        if (converter != null) {
                            value = converter.convert(value);
                        }
                        nodeInterface.setProperty(key, value);
                    } else {
                        System.out.println("Description: no property key found for " + keyName + " of " + type.getSimpleName());
                    }
                } catch (Throwable t) {
                    t.printStackTrace();
                }
            } else {
                System.out.println("Property for " + tagName + " not found!");
            }
        }
    }
}
Also used : ConfigurationProvider(org.structr.schema.ConfigurationProvider) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) PropertyConverter(org.structr.core.converter.PropertyConverter) PropertyKey(org.structr.core.property.PropertyKey)

Example 48 with ConfigurationProvider

use of org.structr.schema.ConfigurationProvider in project structr by structr.

the class PeerToPeerService method onCreate.

@Override
public void onCreate(final RepositoryObject object, final Map<String, Object> data) {
    logger.info("New object with UUID {} created in repository", object.getUuid());
    final ConfigurationProvider config = StructrApp.getConfiguration();
    final Class type = config.getNodeEntityClass(object.getType());
    if (type != null) {
        final App app = StructrApp.getInstance(getUserContext(object));
        try (final Tx tx = app.tx()) {
            // create new node
            final SharedNodeInterface newNode = app.create(type, new NodeAttribute<>(GraphObject.id, object.getUuid()), new NodeAttribute<>(SharedNodeInterface.lastModifiedPseudoTime, object.getLastModificationTime().toString()), new NodeAttribute<>(SharedNodeInterface.createdPseudoTime, object.getCreationTime().toString()));
            // store data
            for (final Entry<String, Object> entry : data.entrySet()) {
                final PropertyKey key = StructrApp.key(type, entry.getKey());
                if (key != null) {
                    newNode.setProperty(app, key, entry.getValue());
                }
            }
            // add listener to store future updates
            object.addListener(new ObjectUpdater(object.getUuid()));
            tx.success();
        } catch (FrameworkException fex) {
            logger.warn("", fex);
        }
    } else {
        System.out.println("Type " + object.getType() + " not found, NOT creating object with ID " + object.getUuid());
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) ConfigurationProvider(org.structr.schema.ConfigurationProvider) RepositoryObject(org.structr.net.repository.RepositoryObject) GraphObject(org.structr.core.GraphObject) PropertyKey(org.structr.core.property.PropertyKey)

Example 49 with ConfigurationProvider

use of org.structr.schema.ConfigurationProvider in project structr by structr.

the class XmlImportTest method testXmlFileImport.

@Test
public void testXmlFileImport() {
    final String htmlPropertyData = "<!DOCTYPE html><html><head><title>bad idea</title></head><body><h1>ORLY?</h1></body></html>";
    String newFileId = null;
    // test setup
    try (final Tx tx = app.tx()) {
        final String xmlData = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" + "<items>\n" + "	<item id=\"0\" type=\"One\" name=\"name: one\">\n" + "		<test1><![CDATA[11]]></test1>\n" + "		<test2>22</test2>\n" + "	</item>\n" + "	<item id=\"1\" type=\"Two\" name=\"name: two\" >\n" + "		<test1>22</test1>\n" + "		<test2>33</test2>\n" + "	</item>\n" + "	<item id=\"2\" type=\"Three\" name=\"name: three\" >\n" + "		<test1>33</test1>\n" + "		<test2>44</test2>\n" + "		<test3><![CDATA[" + htmlPropertyData + "]]></test3>\n" + "	</item>\n" + "</items>\n";
        final byte[] fileData = xmlData.getBytes("utf-8");
        final File file = FileHelper.createFile(securityContext, fileData, "application/xml", File.class, "test.xml");
        // 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");
        newType.addStringProperty("test3");
        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> itemConfig = new LinkedHashMap<>();
    final Map<String, Object> itemProperties = new LinkedHashMap<>();
    final Map<String, Object> test1Config = new LinkedHashMap<>();
    final Map<String, Object> test2Config = new LinkedHashMap<>();
    final Map<String, Object> test3Config = new LinkedHashMap<>();
    params.put("/items/item", itemConfig);
    params.put("/items/item/test1", test1Config);
    params.put("/items/item/test2", test2Config);
    params.put("/items/item/test3", test3Config);
    // import parameters
    itemConfig.put("action", "createNode");
    itemConfig.put("isRoot", true);
    itemConfig.put("type", "Item");
    itemConfig.put("properties", itemProperties);
    // property mapping
    itemProperties.put("id", "originId");
    itemProperties.put("type", "typeName");
    itemProperties.put("name", "name");
    test1Config.put("action", "setProperty");
    test1Config.put("propertyName", "test1");
    test2Config.put("action", "setProperty");
    test2Config.put("propertyName", "test2");
    test3Config.put("action", "setProperty");
    test3Config.put("propertyName", "test3");
    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 + "/doXMLImport");
    // wait for result (import is async.)
    try {
        Thread.sleep(300);
    } 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 XML import result, expected 3 items to be created from XML import. ", 3, items.size());
        final NodeInterface one = items.get(0);
        final NodeInterface two = items.get(1);
        final NodeInterface three = items.get(2);
        assertEquals("Invalid XML mapping result", 0, one.getProperty(conf.getPropertyKeyForJSONName(type, "originId")));
        assertEquals("Invalid XML mapping result", 1, two.getProperty(conf.getPropertyKeyForJSONName(type, "originId")));
        assertEquals("Invalid XML mapping result", 2, three.getProperty(conf.getPropertyKeyForJSONName(type, "originId")));
        assertEquals("Invalid XML mapping result", "One", one.getProperty(conf.getPropertyKeyForJSONName(type, "typeName")));
        assertEquals("Invalid XML mapping result", "Two", two.getProperty(conf.getPropertyKeyForJSONName(type, "typeName")));
        assertEquals("Invalid XML mapping result", "Three", three.getProperty(conf.getPropertyKeyForJSONName(type, "typeName")));
        assertEquals("Invalid XML mapping result", "name: one", one.getProperty(conf.getPropertyKeyForJSONName(type, "name")));
        assertEquals("Invalid XML mapping result", "name: two", two.getProperty(conf.getPropertyKeyForJSONName(type, "name")));
        assertEquals("Invalid XML mapping result", "name: three", three.getProperty(conf.getPropertyKeyForJSONName(type, "name")));
        assertEquals("Invalid XML mapping result", 11, one.getProperty(conf.getPropertyKeyForJSONName(type, "test1")));
        assertEquals("Invalid XML mapping result", 22, two.getProperty(conf.getPropertyKeyForJSONName(type, "test1")));
        assertEquals("Invalid XML mapping result", 33, three.getProperty(conf.getPropertyKeyForJSONName(type, "test1")));
        assertEquals("Invalid XML mapping result", 22, one.getProperty(conf.getPropertyKeyForJSONName(type, "test2")));
        assertEquals("Invalid XML mapping result", 33, two.getProperty(conf.getPropertyKeyForJSONName(type, "test2")));
        assertEquals("Invalid XML mapping result", 44, three.getProperty(conf.getPropertyKeyForJSONName(type, "test2")));
        assertEquals("Invalid XML mapping result", null, one.getProperty(conf.getPropertyKeyForJSONName(type, "test3")));
        assertEquals("Invalid XML mapping result", null, two.getProperty(conf.getPropertyKeyForJSONName(type, "test3")));
        assertEquals("Invalid XML mapping result", htmlPropertyData, three.getProperty(conf.getPropertyKeyForJSONName(type, "test3")));
        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 50 with ConfigurationProvider

use of org.structr.schema.ConfigurationProvider in project structr by structr.

the class HtmlServlet method findFirstNodeByName.

/**
 * Find first node whose name matches the last part of the given path
 *
 * @param securityContext
 * @param request
 * @param path
 * @return node
 * @throws FrameworkException
 */
private AbstractNode findFirstNodeByName(final SecurityContext securityContext, final HttpServletRequest request, final String path) throws FrameworkException {
    final String name = PathHelper.getName(path);
    if (!name.isEmpty()) {
        logger.debug("Requested name: {}", name);
        final Query query = StructrApp.getInstance(securityContext).nodeQuery();
        final ConfigurationProvider config = StructrApp.getConfiguration();
        if (!possiblePropertyNamesForEntityResolving.isEmpty()) {
            query.and();
            resolvePossiblePropertyNamesForObjectResolution(config, query, name);
            query.parent();
        }
        final Result results = query.getResult();
        logger.debug("{} results", results.size());
        request.setAttribute(POSSIBLE_ENTRY_POINTS_KEY, results.getResults());
        return (results.size() > 0 ? (AbstractNode) results.get(0) : null);
    }
    return null;
}
Also used : Query(org.structr.core.app.Query) AbstractNode(org.structr.core.entity.AbstractNode) ConfigurationProvider(org.structr.schema.ConfigurationProvider) Result(org.structr.core.Result)

Aggregations

ConfigurationProvider (org.structr.schema.ConfigurationProvider)50 PropertyKey (org.structr.core.property.PropertyKey)27 FrameworkException (org.structr.common.error.FrameworkException)25 GraphObject (org.structr.core.GraphObject)17 Tx (org.structr.core.graph.Tx)15 LinkedList (java.util.LinkedList)14 PropertyMap (org.structr.core.property.PropertyMap)14 NodeInterface (org.structr.core.graph.NodeInterface)12 Test (org.junit.Test)11 Map (java.util.Map)10 PropertyConverter (org.structr.core.converter.PropertyConverter)10 NodeAttribute (org.structr.core.graph.NodeAttribute)10 SecurityContext (org.structr.common.SecurityContext)9 App (org.structr.core.app.App)9 StructrApp (org.structr.core.app.StructrApp)9 SchemaNode (org.structr.core.entity.SchemaNode)8 File (org.structr.web.entity.File)8 LinkedHashMap (java.util.LinkedHashMap)7 Gson (com.google.gson.Gson)6 GsonBuilder (com.google.gson.GsonBuilder)6