Search in sources :

Example 6 with ShadowDocument

use of org.structr.web.entity.dom.ShadowDocument in project structr by structr.

the class UiScriptingTest method testIncludeWithRepeaterInJavaScript.

@Test
public void testIncludeWithRepeaterInJavaScript() {
    try (final Tx tx = app.tx()) {
        final Page page = Page.createSimplePage(securityContext, "test");
        final Div div = (Div) page.getElementsByTagName("div").item(0);
        final Content content = (Content) div.getFirstChild();
        // setup scripting repeater
        content.setProperty(StructrApp.key(Content.class, "content"), "${{ var arr = []; for (var i=0; i<10; i++) { arr.push({name: 'test' + i}); } Structr.include('item', arr, 'test'); }}");
        content.setProperty(StructrApp.key(Content.class, "contentType"), "text/html");
        // setup shared component with name "table" to include
        final ShadowDocument shadowDoc = CreateComponentCommand.getOrCreateHiddenDocument();
        final Div item = (Div) shadowDoc.createElement("div");
        final Content txt = (Content) shadowDoc.createTextNode("${test.name}");
        item.setProperty(StructrApp.key(Table.class, "name"), "item");
        item.appendChild(txt);
        // create admin user
        createTestNode(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 (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception.");
    }
    RestAssured.basePath = "/";
    // test successful basic auth
    RestAssured.given().headers("X-User", "admin", "X-Password", "admin").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(401)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(403)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).expect().statusCode(200).body("html.head.title", Matchers.equalTo("Test")).body("html.body.h1", Matchers.equalTo("Test")).body("html.body.div.div[0]", Matchers.equalTo("test0")).body("html.body.div.div[1]", Matchers.equalTo("test1")).body("html.body.div.div[2]", Matchers.equalTo("test2")).body("html.body.div.div[3]", Matchers.equalTo("test3")).body("html.body.div.div[4]", Matchers.equalTo("test4")).body("html.body.div.div[5]", Matchers.equalTo("test5")).body("html.body.div.div[6]", Matchers.equalTo("test6")).body("html.body.div.div[7]", Matchers.equalTo("test7")).body("html.body.div.div[8]", Matchers.equalTo("test8")).body("html.body.div.div[9]", Matchers.equalTo("test9")).when().get("/html/test");
}
Also used : Div(org.structr.web.entity.html.Div) Table(org.structr.web.entity.html.Table) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Content(org.structr.web.entity.dom.Content) Page(org.structr.web.entity.dom.Page) ShadowDocument(org.structr.web.entity.dom.ShadowDocument) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 7 with ShadowDocument

use of org.structr.web.entity.dom.ShadowDocument in project structr by structr.

the class CloudConnection method storeNode.

public NodeInterface storeNode(final DataContainer receivedData) throws FrameworkException {
    final SecurityContext securityContext = SecurityContext.getSuperUserInstance();
    final NodeDataContainer receivedNodeData = (NodeDataContainer) receivedData;
    final String typeName = receivedNodeData.getType();
    final Class nodeType = config.getNodeEntityClass(typeName);
    if (nodeType == null) {
        logger.error("Unknown entity type {}", typeName);
        return null;
    }
    // skip builtin schema node types
    if (Boolean.TRUE.equals(receivedNodeData.getProperties().get(SchemaNode.isBuiltinType.dbName()))) {
        return null;
    }
    final String uuid = receivedNodeData.getSourceNodeId();
    GraphObject newOrExistingNode = app.get(nodeType, uuid);
    if (newOrExistingNode != null) {
        // merge properties
        newOrExistingNode.setProperties(securityContext, PropertyMap.databaseTypeToJavaType(securityContext, nodeType, receivedNodeData.getProperties()));
    } else {
        final PropertyMap properties = PropertyMap.databaseTypeToJavaType(securityContext, nodeType, receivedNodeData.getProperties());
        final List<DOMNode> existingChildren = new LinkedList<>();
        // special handling for ShadowDocument (all others must be deleted)
        if (ShadowDocument.class.getSimpleName().equals(typeName)) {
            // delete shadow document
            for (ShadowDocument existingDoc : app.nodeQuery(ShadowDocument.class).includeDeletedAndHidden().getAsList()) {
                existingChildren.addAll(existingDoc.getProperty(Page.elements));
                app.delete(existingDoc);
            }
            // add existing children to new shadow document
            properties.put(Page.elements, existingChildren);
        }
        // create node
        newOrExistingNode = app.create(nodeType, properties);
    }
    idMap.put(receivedNodeData.getSourceNodeId(), newOrExistingNode.getUuid());
    count++;
    total++;
    return (NodeInterface) newOrExistingNode;
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) SecurityContext(org.structr.common.SecurityContext) NodeDataContainer(org.structr.cloud.message.NodeDataContainer) FileNodeDataContainer(org.structr.cloud.message.FileNodeDataContainer) GraphObject(org.structr.core.GraphObject) DOMNode(org.structr.web.entity.dom.DOMNode) ShadowDocument(org.structr.web.entity.dom.ShadowDocument) LinkedList(java.util.LinkedList) NodeInterface(org.structr.core.graph.NodeInterface)

Example 8 with ShadowDocument

use of org.structr.web.entity.dom.ShadowDocument in project structr by structr.

the class DeployCommand method exportPages.

private void exportPages(final Path target, final Path configTarget) throws FrameworkException {
    logger.info("Exporting pages (unchanged pages will be skipped)");
    final Map<String, Object> pagesConfig = new TreeMap<>();
    final App app = StructrApp.getInstance();
    try (final Tx tx = app.tx()) {
        for (final Page page : app.nodeQuery(Page.class).sort(Page.name).getAsList()) {
            if (!(page instanceof ShadowDocument)) {
                final String content = page.getContent(RenderContext.EditMode.DEPLOYMENT);
                if (content != null) {
                    final Map<String, Object> properties = new TreeMap<>();
                    final String name = page.getName();
                    final Path pageFile = target.resolve(name + ".html");
                    boolean doExport = true;
                    if (Files.exists(pageFile)) {
                        try {
                            final String existingContent = new String(Files.readAllBytes(pageFile), "utf-8");
                            doExport = !existingContent.equals(content);
                        } catch (IOException ignore) {
                            logger.warn("", ignore);
                        }
                    }
                    pagesConfig.put(name, properties);
                    exportConfiguration(page, properties);
                    exportOwnershipAndSecurity(page, properties);
                    if (doExport) {
                        try (final OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(pageFile.toFile()))) {
                            writer.write(content);
                            writer.flush();
                            writer.close();
                        } catch (IOException ioex) {
                            logger.warn("", ioex);
                        }
                    }
                }
            }
        }
        tx.success();
    }
    try (final Writer fos = new OutputStreamWriter(new FileOutputStream(configTarget.toFile()))) {
        getGson().toJson(pagesConfig, fos);
    } catch (IOException ioex) {
        logger.warn("", ioex);
    }
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) Path(java.nio.file.Path) Tx(org.structr.core.graph.Tx) Page(org.structr.web.entity.dom.Page) IOException(java.io.IOException) TreeMap(java.util.TreeMap) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) ShadowDocument(org.structr.web.entity.dom.ShadowDocument) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) FileWriter(java.io.FileWriter)

Example 9 with ShadowDocument

use of org.structr.web.entity.dom.ShadowDocument in project structr by structr.

the class DeployCommand method exportComponents.

private void exportComponents(final Path target, final Path configTarget) throws FrameworkException {
    logger.info("Exporting components (unchanged components will be skipped)");
    final Map<String, Object> configuration = new TreeMap<>();
    final App app = StructrApp.getInstance();
    try (final Tx tx = app.tx()) {
        final ShadowDocument shadowDocument = app.nodeQuery(ShadowDocument.class).getFirst();
        if (shadowDocument != null) {
            for (final DOMNode node : shadowDocument.getElements()) {
                final boolean hasParent = node.getParent() != null;
                final boolean inTrash = node.inTrash();
                boolean doExport = true;
                // skip nodes in trash and non-toplevel nodes
                if (inTrash || hasParent) {
                    continue;
                }
                final String content = node.getContent(RenderContext.EditMode.DEPLOYMENT);
                if (content != null) {
                    String name = node.getProperty(AbstractNode.name);
                    if (name == null) {
                        name = node.getUuid();
                    }
                    final Map<String, Object> properties = new TreeMap<>();
                    final Path targetFile = target.resolve(name + ".html");
                    if (Files.exists(targetFile)) {
                        try {
                            final String existingContent = new String(Files.readAllBytes(targetFile), "utf-8");
                            doExport = !existingContent.equals(content);
                        } catch (IOException ignore) {
                        }
                    }
                    configuration.put(name, properties);
                    exportConfiguration(node, properties);
                    if (doExport) {
                        try (final OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(targetFile.toFile()))) {
                            writer.write(content);
                            writer.flush();
                            writer.close();
                        } catch (IOException ioex) {
                            logger.warn("", ioex);
                        }
                    }
                }
            }
        }
        tx.success();
    }
    try (final Writer fos = new OutputStreamWriter(new FileOutputStream(configTarget.toFile()))) {
        getGson().toJson(configuration, fos);
    } catch (IOException ioex) {
        logger.warn("", ioex);
    }
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) Path(java.nio.file.Path) Tx(org.structr.core.graph.Tx) IOException(java.io.IOException) TreeMap(java.util.TreeMap) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) ShadowDocument(org.structr.web.entity.dom.ShadowDocument) DOMNode(org.structr.web.entity.dom.DOMNode) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) FileWriter(java.io.FileWriter)

Example 10 with ShadowDocument

use of org.structr.web.entity.dom.ShadowDocument in project structr by structr.

the class ComponentImportVisitor method createComponent.

private void createComponent(final Path file, final String fileName) throws IOException, FrameworkException {
    final String name = StringUtils.substringBeforeLast(fileName, ".html");
    final DOMNode existingComponent = getExistingComponent(name);
    final boolean byId = DeployCommand.isUuid(name);
    try (final Tx tx = app.tx(true, false, false)) {
        final PropertyMap properties = getPropertiesForComponent(name);
        if (properties == null) {
            logger.info("Ignoring {} (not in components.json)", fileName);
        } else {
            if (existingComponent != null) {
                final PropertyKey<String> contentKey = StructrApp.key(Template.class, "content");
                properties.put(contentKey, existingComponent.getProperty(contentKey));
                existingComponent.setOwnerDocument(null);
                if (existingComponent instanceof Template) {
                    properties.put(contentKey, existingComponent.getProperty(contentKey));
                    existingComponent.setOwnerDocument(null);
                } else {
                    deleteComponent(app, name);
                }
            }
            final String src = new String(Files.readAllBytes(file), Charset.forName("UTF-8"));
            boolean visibleToPublic = get(properties, GraphObject.visibleToPublicUsers, false);
            boolean visibleToAuth = get(properties, GraphObject.visibleToAuthenticatedUsers, false);
            final Importer importer = new Importer(securityContext, src, null, name, visibleToPublic, visibleToAuth);
            // enable literal import of href attributes
            importer.setIsDeployment(true);
            final boolean parseOk = importer.parse(false);
            if (parseOk) {
                logger.info("Importing component {} from {}..", new Object[] { name, fileName });
                // set comment handler that can parse and apply special Structr comments in HTML source files
                importer.setCommentHandler(new DeploymentCommentHandler());
                // parse page
                final ShadowDocument shadowDocument = CreateComponentCommand.getOrCreateHiddenDocument();
                final DOMNode rootElement = importer.createComponentChildNodes(shadowDocument);
                if (rootElement != null) {
                    if (byId) {
                        // set UUID
                        rootElement.unlockSystemPropertiesOnce();
                        rootElement.setProperty(GraphObject.id, name);
                    } else {
                        // set name
                        rootElement.setProperty(AbstractNode.name, name);
                    }
                    // store properties from components.json if present
                    rootElement.setProperties(securityContext, properties);
                }
            }
        }
        tx.success();
    }
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) DOMNode(org.structr.web.entity.dom.DOMNode) ShadowDocument(org.structr.web.entity.dom.ShadowDocument) Template(org.structr.web.entity.dom.Template) Importer(org.structr.web.importer.Importer)

Aggregations

ShadowDocument (org.structr.web.entity.dom.ShadowDocument)12 DOMNode (org.structr.web.entity.dom.DOMNode)8 Tx (org.structr.core.graph.Tx)7 FrameworkException (org.structr.common.error.FrameworkException)4 App (org.structr.core.app.App)4 StructrApp (org.structr.core.app.StructrApp)4 PropertyMap (org.structr.core.property.PropertyMap)4 LinkedList (java.util.LinkedList)3 Page (org.structr.web.entity.dom.Page)3 FileOutputStream (java.io.FileOutputStream)2 FileWriter (java.io.FileWriter)2 IOException (java.io.IOException)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Writer (java.io.Writer)2 Path (java.nio.file.Path)2 TreeMap (java.util.TreeMap)2 Test (org.junit.Test)2 GraphObject (org.structr.core.GraphObject)2 NodeInterface (org.structr.core.graph.NodeInterface)2 StructrUiTest (org.structr.web.StructrUiTest)2