Search in sources :

Example 1 with Widget

use of org.structr.web.entity.Widget in project structr by structr.

the class DeploymentTest method test35WidgetWithSharedComponentCreation.

@Test
public void test35WidgetWithSharedComponentCreation() {
    // setup
    try (final Tx tx = app.tx()) {
        final Page testPage = Page.createNewPage(securityContext, "WidgetTestPage");
        final Html html = createElement(testPage, testPage, "html");
        final Head head = createElement(testPage, html, "head");
        final Body body = createElement(testPage, html, "body");
        final Div div = createElement(testPage, body, "div");
        final Div div2 = createElement(testPage, body, "div");
        div.setProperty(AbstractNode.name, "WidgetTestPage-Div");
        div2.setProperty(AbstractNode.name, "WidgetTestPage-Div2");
        Widget widgetToImport = app.create(Widget.class, new NodeAttribute<>(StructrApp.key(Widget.class, "name"), "TestWidget"), new NodeAttribute<>(StructrApp.key(Widget.class, "source"), "<structr:component src=\"TestComponent\">\n" + "	<div data-structr-meta-name=\"TestComponent\">\n" + "		Test123\n" + "	</div>\n" + "</structr:component>"), new NodeAttribute<>(StructrApp.key(Widget.class, "configuration"), ""), new NodeAttribute<>(StructrApp.key(Widget.class, "visibleToPublicUsers"), true), new NodeAttribute<>(StructrApp.key(Widget.class, "visibleToAuthenticatedUsers"), true));
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("widgetHostBaseUrl", "https://widgets.structr.org/structr/rest/widgets");
        paramMap.put("parentId", widgetToImport.getProperty(new StartNode<>("owner", PrincipalOwnsNode.class)));
        paramMap.put("source", widgetToImport.getProperty(new StringProperty("source")));
        paramMap.put("processDeploymentInfo", false);
        Widget.expandWidget(securityContext, testPage, div, baseUri, paramMap, false);
        Widget.expandWidget(securityContext, testPage, div, baseUri, paramMap, false);
        makePublic(testPage, html, head, body, div, div2);
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
    // test
    try (final Tx tx = app.tx()) {
        Div div = app.nodeQuery(Div.class).andName("WidgetTestPage-Div").getFirst();
        assertEquals(2, div.treeGetChildCount());
        Object obj = null;
        for (DOMNode n : div.getAllChildNodes()) {
            obj = n;
            break;
        }
        assertTrue(Div.class.isAssignableFrom(obj.getClass()));
        Div clonedNode = (Div) obj;
        assertEquals(0, clonedNode.getChildNodes().getLength());
        assertEquals(3, app.nodeQuery(Div.class).andName("TestComponent").getResult().size());
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
}
Also used : StartNode(org.structr.core.property.StartNode) Head(org.structr.web.entity.html.Head) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) HashMap(java.util.HashMap) Widget(org.structr.web.entity.Widget) Html(org.structr.web.entity.html.Html) StringProperty(org.structr.core.property.StringProperty) Page(org.structr.web.entity.dom.Page) Div(org.structr.web.entity.html.Div) GraphObject(org.structr.core.GraphObject) DOMNode(org.structr.web.entity.dom.DOMNode) Body(org.structr.web.entity.html.Body) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 2 with Widget

use of org.structr.web.entity.Widget in project structr by structr.

the class DeploymentTest method test34WidgetWithTemplate.

@Test
public void test34WidgetWithTemplate() {
    // setup
    try (final Tx tx = app.tx()) {
        final Page testPage = Page.createNewPage(securityContext, "WidgetTestPage");
        final Html html = createElement(testPage, testPage, "html");
        final Head head = createElement(testPage, html, "head");
        final Body body = createElement(testPage, html, "body");
        final Div div = createElement(testPage, body, "div");
        final Div div2 = createElement(testPage, body, "div");
        div.setProperty(AbstractNode.name, "WidgetTestPage-Div");
        div2.setProperty(AbstractNode.name, "WidgetTestPage-Div2");
        Widget widgetToImport = app.create(Widget.class, new NodeAttribute<>(StructrApp.key(Widget.class, "name"), "TestWidget"), new NodeAttribute<>(StructrApp.key(Widget.class, "source"), "<!-- @structr:content(text/html) --><structr:template>${{Structr.print(\"<div>Test</div>\");}}</structr:template>"), new NodeAttribute<>(StructrApp.key(Widget.class, "configuration"), "{\"processDeploymentInfo\": true}"), new NodeAttribute<>(StructrApp.key(Widget.class, "visibleToPublicUsers"), true), new NodeAttribute<>(StructrApp.key(Widget.class, "visibleToAuthenticatedUsers"), true));
        Importer importer = new Importer(securityContext, widgetToImport.getProperty(new StringProperty("source")), null, null, true, true);
        importer.setIsDeployment(true);
        importer.setCommentHandler(new DeploymentCommentHandler());
        importer.parse(true);
        DOMNode template = importer.createComponentChildNodes(div, testPage);
        div.appendChild(template);
        makePublic(testPage, html, head, body, div, div2, template);
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
    // test
    try (final Tx tx = app.tx()) {
        Div div = (Div) app.nodeQuery().andName("WidgetTestPage-Div").getFirst();
        assertEquals(1, div.treeGetChildCount());
        Object obj = div.treeGetFirstChild();
        assertTrue(Template.class.isAssignableFrom(obj.getClass()));
        Template template = (Template) obj;
        assertEquals("${{Structr.print(\"<div>Test</div>\");}}", template.getTextContent());
        assertEquals("text/html", template.getContentType());
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
}
Also used : Head(org.structr.web.entity.html.Head) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Widget(org.structr.web.entity.Widget) Html(org.structr.web.entity.html.Html) StringProperty(org.structr.core.property.StringProperty) Page(org.structr.web.entity.dom.Page) Template(org.structr.web.entity.dom.Template) MailTemplate(org.structr.core.entity.MailTemplate) Div(org.structr.web.entity.html.Div) DeploymentCommentHandler(org.structr.web.maintenance.deploy.DeploymentCommentHandler) GraphObject(org.structr.core.GraphObject) DOMNode(org.structr.web.entity.dom.DOMNode) Body(org.structr.web.entity.html.Body) Importer(org.structr.web.importer.Importer) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 3 with Widget

use of org.structr.web.entity.Widget in project structr by structr.

the class CreateLocalWidgetCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final App app = StructrApp.getInstance(getWebSocket().getSecurityContext());
    final String id = webSocketData.getId();
    final Map<String, Object> nodeData = webSocketData.getNodeData();
    final String source = (String) nodeData.get("source");
    final String name = (String) nodeData.get("name");
    // check for ID
    if (id == null) {
        getWebSocket().send(MessageBuilder.status().code(422).message("Cannot create widget without id").build(), true);
        return;
    }
    // check if parent node with given ID exists
    DOMNode node = getDOMNode(id);
    if (node == null) {
        getWebSocket().send(MessageBuilder.status().code(404).message("Node not found").build(), true);
        return;
    }
    try {
        // convertFromInput
        PropertyMap properties = new PropertyMap();
        properties.put(AbstractNode.type, Widget.class.getSimpleName());
        properties.put(AbstractNode.name, name);
        properties.put(StructrApp.key(Widget.class, "source"), source);
        final Widget widget = app.create(Widget.class, properties);
        TransactionCommand.registerNodeCallback(widget, callback);
    } catch (Throwable t) {
        logger.warn(t.toString());
        // send exception
        getWebSocket().send(MessageBuilder.status().code(422).message(t.toString()).build(), true);
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) PropertyMap(org.structr.core.property.PropertyMap) Widget(org.structr.web.entity.Widget) DOMNode(org.structr.web.entity.dom.DOMNode)

Example 4 with Widget

use of org.structr.web.entity.Widget in project structr by structr.

the class DeployCommand method exportWidgets.

private void exportWidgets(final Path target) throws FrameworkException {
    logger.info("Exporting widgets");
    final List<Map<String, Object>> widgets = new LinkedList<>();
    final App app = StructrApp.getInstance();
    try (final Tx tx = app.tx()) {
        for (final Widget widget : app.nodeQuery(Widget.class).sort(Widget.name).getAsList()) {
            final Map<String, Object> entry = new TreeMap<>();
            widgets.add(entry);
            entry.put("name", widget.getProperty(Widget.name));
            entry.put("visibleToAuthenticatedUsers", widget.getProperty(Widget.visibleToAuthenticatedUsers));
            entry.put("visibleToPublicUsers", widget.getProperty(Widget.visibleToPublicUsers));
            entry.put("source", widget.getProperty(StructrApp.key(Widget.class, "source")));
            entry.put("description", widget.getProperty(StructrApp.key(Widget.class, "description")));
            entry.put("isWidget", widget.getProperty(StructrApp.key(Widget.class, "isWidget")));
            entry.put("treePath", widget.getProperty(StructrApp.key(Widget.class, "treePath")));
            entry.put("pictures", widget.getProperty(StructrApp.key(Widget.class, "pictures")));
            entry.put("configuration", widget.getProperty(StructrApp.key(Widget.class, "configuration")));
        }
        tx.success();
    }
    try (final Writer fos = new OutputStreamWriter(new FileOutputStream(target.toFile()))) {
        getGson().toJson(widgets, fos);
    } catch (IOException ioex) {
        logger.warn("", ioex);
    }
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) Tx(org.structr.core.graph.Tx) Widget(org.structr.web.entity.Widget) IOException(java.io.IOException) TreeMap(java.util.TreeMap) LinkedList(java.util.LinkedList) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) PropertyMap(org.structr.core.property.PropertyMap) TreeMap(java.util.TreeMap) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) FileWriter(java.io.FileWriter)

Aggregations

Widget (org.structr.web.entity.Widget)4 Tx (org.structr.core.graph.Tx)3 DOMNode (org.structr.web.entity.dom.DOMNode)3 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 FrameworkException (org.structr.common.error.FrameworkException)2 GraphObject (org.structr.core.GraphObject)2 App (org.structr.core.app.App)2 StructrApp (org.structr.core.app.StructrApp)2 PropertyMap (org.structr.core.property.PropertyMap)2 StringProperty (org.structr.core.property.StringProperty)2 StructrUiTest (org.structr.web.StructrUiTest)2 Page (org.structr.web.entity.dom.Page)2 Body (org.structr.web.entity.html.Body)2 Div (org.structr.web.entity.html.Div)2 Head (org.structr.web.entity.html.Head)2 Html (org.structr.web.entity.html.Html)2 FileOutputStream (java.io.FileOutputStream)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1