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.");
}
}
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.");
}
}
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);
}
}
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);
}
}
Aggregations