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