Search in sources :

Example 21 with Template

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

the class TemplateImportVisitor method createTemplate.

private void createTemplate(final Path file, final String fileName) throws IOException, FrameworkException {
    final String templateName = StringUtils.substringBeforeLast(fileName, ".html");
    // either the template was exported with name + uuid or just the uuid
    final boolean byNameAndId = DeployCommand.endsWithUuid(templateName);
    final boolean byId = DeployCommand.isUuid(templateName);
    try (final Tx tx = app.tx(true, false, false)) {
        final PropertyMap properties = getPropertiesForTemplate(templateName);
        if (properties == null) {
            logger.info("Ignoring {} (not in templates.json)", fileName);
        } else {
            final String src = new String(Files.readAllBytes(file), Charset.forName("UTF-8"));
            final Template template;
            if (byId) {
                logger.info("Importing template {} from {}..", new Object[] { templateName, fileName });
                final DOMNode existingTemplate = app.get(DOMNode.class, templateName);
                if (existingTemplate != null) {
                    deleteTemplate(app, existingTemplate);
                }
                template = app.create(Template.class, new NodeAttribute(AbstractNode.id, templateName));
            } else if (byNameAndId) {
                // the last characters in the name string are the uuid
                final String uuid = templateName.substring(templateName.length() - 32);
                final String name = templateName.substring(0, templateName.length() - 33);
                logger.info("Importing template {} from {}..", new Object[] { name, fileName });
                final DOMNode existingTemplate = app.get(DOMNode.class, uuid);
                if (existingTemplate != null) {
                    deleteTemplate(app, existingTemplate);
                }
                template = app.create(Template.class, new NodeAttribute(AbstractNode.id, uuid));
                properties.put(Template.name, name);
            } else {
                final String name = byId ? null : templateName;
                logger.info("Importing template {} from {}..", new Object[] { name, fileName });
                final DOMNode existingTemplate = getExistingTemplate(name);
                if (existingTemplate != null) {
                    deleteTemplate(app, existingTemplate);
                }
                template = app.create(Template.class);
                properties.put(Template.name, name);
            }
            properties.put(StructrApp.key(Template.class, "content"), src);
            // insert "shared" templates into ShadowDocument
            final Object value = properties.get(internalSharedTemplateKey);
            if (value != null) {
                if ("true".equals(value)) {
                    template.setOwnerDocument(CreateComponentCommand.getOrCreateHiddenDocument());
                }
                properties.remove(internalSharedTemplateKey);
            }
            // store properties from templates.json if present
            template.setProperties(securityContext, properties);
        }
        tx.success();
    } catch (Throwable t) {
        logger.debug("Error trying to create template {}", fileName);
    }
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) DOMNode(org.structr.web.entity.dom.DOMNode) Template(org.structr.web.entity.dom.Template)

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