Search in sources :

Example 16 with Template

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

the class WrapDOMNodeCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final Map<String, Object> nodeData = webSocketData.getNodeData();
    final String pageId = webSocketData.getPageId();
    final String nodeId = (String) nodeData.get("nodeId");
    final Boolean inheritVisibilityFlags = (Boolean) nodeData.get("inheritVisibilityFlags");
    nodeData.remove("nodeId");
    nodeData.remove("inheritVisibilityFlags");
    if (pageId != null) {
        // check for parent ID before creating any nodes
        if (nodeId == null) {
            getWebSocket().send(MessageBuilder.status().code(422).message("Cannot wrap node without nodeId").build(), true);
            return;
        }
        // check if content node with given ID exists
        final DOMNode oldNode = getDOMNode(nodeId);
        if (oldNode == null) {
            getWebSocket().send(MessageBuilder.status().code(404).message("Node not found").build(), true);
            return;
        }
        final Document document = getPage(pageId);
        if (document != null) {
            final String tagName = (String) nodeData.get("tagName");
            nodeData.remove("tagName");
            final DOMNode parentNode = (DOMNode) oldNode.getParentNode();
            if (parentNode == null) {
                getWebSocket().send(MessageBuilder.status().code(404).message("Node has no parent node").build(), true);
                return;
            }
            try {
                DOMNode newNode;
                if (tagName != null && "comment".equals(tagName)) {
                    newNode = (DOMNode) document.createComment("#comment");
                } else if (tagName != null && "template".equals(tagName)) {
                    newNode = (DOMNode) document.createTextNode("#template");
                    try {
                        newNode.unlockSystemPropertiesOnce();
                        newNode.setProperties(newNode.getSecurityContext(), new PropertyMap(NodeInterface.type, Template.class.getSimpleName()));
                    } catch (FrameworkException fex) {
                        logger.warn("Unable to set type of node {} to Template: {}", new Object[] { newNode.getUuid(), fex.getMessage() });
                    }
                } else if (tagName != null && !tagName.isEmpty()) {
                    newNode = (DOMNode) document.createElement(tagName);
                } else {
                    getWebSocket().send(MessageBuilder.status().code(404).message("Cannot create node without tagname").build(), true);
                    return;
                }
                // Instantiate node again to get correct class
                newNode = getDOMNode(newNode.getUuid());
                // append new node to parent
                if (newNode != null) {
                    parentNode.replaceChild(newNode, oldNode);
                    newNode.appendChild(oldNode);
                    if (inheritVisibilityFlags) {
                        PropertyMap visibilityFlags = new PropertyMap();
                        visibilityFlags.put(DOMNode.visibleToAuthenticatedUsers, parentNode.getProperty(DOMNode.visibleToAuthenticatedUsers));
                        visibilityFlags.put(DOMNode.visibleToPublicUsers, parentNode.getProperty(DOMNode.visibleToPublicUsers));
                        try {
                            newNode.setProperties(newNode.getSecurityContext(), visibilityFlags);
                        } catch (FrameworkException fex) {
                            logger.warn("Unable to inherit visibility flags for node {} from parent node {}", newNode, parentNode);
                        }
                    }
                }
            } catch (DOMException dex) {
                // send DOM exception
                getWebSocket().send(MessageBuilder.status().code(422).message(dex.getMessage()).build(), true);
            }
        } else {
            getWebSocket().send(MessageBuilder.status().code(404).message("Page not found").build(), true);
        }
    } else {
        getWebSocket().send(MessageBuilder.status().code(422).message("Cannot wrap node without pageId").build(), true);
    }
}
Also used : DOMException(org.w3c.dom.DOMException) PropertyMap(org.structr.core.property.PropertyMap) FrameworkException(org.structr.common.error.FrameworkException) DOMNode(org.structr.web.entity.dom.DOMNode) Document(org.w3c.dom.Document) Template(org.structr.web.entity.dom.Template)

Example 17 with Template

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

the class Importer method createNewTemplateNode.

private Template createNewTemplateNode(final DOMNode parent, final String content, final String contentType) throws FrameworkException {
    final PropertyKey<String> contentTypeKey = StructrApp.key(Content.class, "contentType");
    final PropertyKey<String> contentKey = StructrApp.key(Content.class, "content");
    final PropertyMap map = new PropertyMap();
    map.put(AbstractNode.visibleToPublicUsers, publicVisible);
    map.put(AbstractNode.visibleToAuthenticatedUsers, authVisible);
    map.put(contentTypeKey, contentType);
    map.put(contentKey, content);
    final Template newTemplate = StructrApp.getInstance(securityContext).create(Template.class, map);
    if (parent != null) {
        parent.appendChild(newTemplate);
    }
    return newTemplate;
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) Template(org.structr.web.entity.dom.Template)

Example 18 with Template

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

the class DeployCommand method exportTemplates.

private void exportTemplates(final Path target, final Path configTarget) throws FrameworkException {
    logger.info("Exporting templates (unchanged templates will be skipped)");
    final Map<String, Object> configuration = new TreeMap<>();
    final App app = StructrApp.getInstance();
    try (final Tx tx = app.tx()) {
        // export template nodes anywhere in the pages tree which are not related to shared components
        for (final Template template : app.nodeQuery(Template.class).getAsList()) {
            final boolean isShared = template.getProperty(StructrApp.key(DOMNode.class, "sharedComponent")) != null;
            final boolean inTrash = template.inTrash();
            if (inTrash || isShared) {
                continue;
            }
            exportTemplateSource(target, template, configuration);
        }
        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) Tx(org.structr.core.graph.Tx) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) TreeMap(java.util.TreeMap) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) FileWriter(java.io.FileWriter) Template(org.structr.web.entity.dom.Template) MailTemplate(org.structr.core.entity.MailTemplate)

Example 19 with Template

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

the class DeployCommand method exportConfiguration.

private void exportConfiguration(final DOMNode node, final Map<String, Object> config) throws FrameworkException {
    if (node.isVisibleToPublicUsers()) {
        putIf(config, "visibleToPublicUsers", true);
    }
    if (node.isVisibleToAuthenticatedUsers()) {
        putIf(config, "visibleToAuthenticatedUsers", true);
    }
    putIf(config, "contentType", node.getProperty(StructrApp.key(Content.class, "contentType")));
    if (node instanceof Template) {
        // mark this template as being shared
        putIf(config, "shared", Boolean.toString(node.isSharedComponent() && node.getParent() == null));
    }
    if (node instanceof Page) {
        putIf(config, "path", node.getProperty(StructrApp.key(Page.class, "path")));
        putIf(config, "position", node.getProperty(StructrApp.key(Page.class, "position")));
        putIf(config, "category", node.getProperty(StructrApp.key(Page.class, "category")));
        putIf(config, "showOnErrorCodes", node.getProperty(StructrApp.key(Page.class, "showOnErrorCodes")));
        putIf(config, "showConditions", node.getProperty(StructrApp.key(Page.class, "showConditions")));
        putIf(config, "hideConditions", node.getProperty(StructrApp.key(Page.class, "hideConditions")));
        putIf(config, "dontCache", node.getProperty(StructrApp.key(Page.class, "dontCache")));
        putIf(config, "cacheForSeconds", node.getProperty(StructrApp.key(Page.class, "cacheForSeconds")));
        putIf(config, "pageCreatesRawData", node.getProperty(StructrApp.key(Page.class, "pageCreatesRawData")));
        putIf(config, "basicAuthRealm", node.getProperty(StructrApp.key(Page.class, "basicAuthRealm")));
        putIf(config, "enableBasicAuth", node.getProperty(StructrApp.key(Page.class, "enableBasicAuth")));
    }
    // export all dynamic properties
    for (final PropertyKey key : StructrApp.getConfiguration().getPropertySet(node.getClass(), PropertyView.All)) {
        // only export dynamic (=> additional) keys
        if (!key.isPartOfBuiltInSchema()) {
            putIf(config, key.jsonName(), node.getProperty(key));
        }
    }
}
Also used : Page(org.structr.web.entity.dom.Page) PropertyKey(org.structr.core.property.PropertyKey) Template(org.structr.web.entity.dom.Template) MailTemplate(org.structr.core.entity.MailTemplate)

Example 20 with Template

use of org.structr.web.entity.dom.Template 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

Template (org.structr.web.entity.dom.Template)21 Tx (org.structr.core.graph.Tx)15 FrameworkException (org.structr.common.error.FrameworkException)14 MailTemplate (org.structr.core.entity.MailTemplate)13 Page (org.structr.web.entity.dom.Page)13 Test (org.junit.Test)12 StructrUiTest (org.structr.web.StructrUiTest)12 Body (org.structr.web.entity.html.Body)10 Head (org.structr.web.entity.html.Head)10 Html (org.structr.web.entity.html.Html)10 PropertyMap (org.structr.core.property.PropertyMap)8 DOMNode (org.structr.web.entity.dom.DOMNode)8 Div (org.structr.web.entity.html.Div)7 NodeAttribute (org.structr.core.graph.NodeAttribute)4 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 OutputStreamWriter (java.io.OutputStreamWriter)2 TreeMap (java.util.TreeMap)2 PropertyKey (org.structr.core.property.PropertyKey)2 ShadowDocument (org.structr.web.entity.dom.ShadowDocument)2