Search in sources :

Example 6 with Importer

use of org.structr.web.importer.Importer 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)

Example 7 with Importer

use of org.structr.web.importer.Importer in project structr by structr.

the class PageImportVisitor method createPage.

private void createPage(final Path file, final String fileName) throws IOException, FrameworkException {
    final String name = StringUtils.substringBeforeLast(fileName, ".html");
    try (final Tx tx = app.tx(true, false, false)) {
        final PropertyMap properties = getPropertiesForPage(name);
        if (properties == null) {
            logger.info("Ignoring {} (not in pages.json)", fileName);
        } else {
            final Page existingPage = getExistingPage(name);
            if (existingPage != null) {
                deletePage(app, name);
            }
            final String src = new String(Files.readAllBytes(file), Charset.forName("UTF-8"));
            final String contentType = get(properties, StructrApp.key(Page.class, "contentType"), "text/html");
            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);
            if (StringUtils.startsWithIgnoreCase(src, DoctypeString) && "text/html".equals(contentType)) {
                // Import document starts with <!DOCTYPE> definition, so we treat it as an HTML
                // document and use the Structr HTML importer.
                final boolean parseOk = importer.parse();
                if (parseOk) {
                    logger.info("Importing page {} 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 Page newPage = importer.readPage();
                    // remove duplicate elements
                    fixDocumentElements(newPage);
                    // store properties from pages.json if present
                    if (properties != null) {
                        newPage.setProperties(securityContext, properties);
                    }
                }
            } else {
                // Import document does NOT start with a <!DOCTYPE> definition, so we assume a
                // template or shared component that we need to parse.
                final boolean parseOk = importer.parse(true);
                if (parseOk) {
                    logger.info("Importing page {} 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 Page newPage = app.create(Page.class, name);
                    // store properties from pages.json if present
                    if (properties != null) {
                        newPage.setProperties(securityContext, properties);
                    }
                    // add children
                    importer.createChildNodes(newPage, newPage);
                }
            }
        }
        tx.success();
    }
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) Page(org.structr.web.entity.dom.Page) Importer(org.structr.web.importer.Importer)

Aggregations

Importer (org.structr.web.importer.Importer)7 Tx (org.structr.core.graph.Tx)5 Page (org.structr.web.entity.dom.Page)4 FrameworkException (org.structr.common.error.FrameworkException)3 DeploymentCommentHandler (org.structr.web.maintenance.deploy.DeploymentCommentHandler)3 PropertyMap (org.structr.core.property.PropertyMap)2 DOMNode (org.structr.web.entity.dom.DOMNode)2 Template (org.structr.web.entity.dom.Template)2 HashMap (java.util.HashMap)1 Matcher (java.util.regex.Matcher)1 Test (org.junit.Test)1 SecurityContext (org.structr.common.SecurityContext)1 ThreadLocalMatcher (org.structr.common.ThreadLocalMatcher)1 EmptyPropertyToken (org.structr.common.error.EmptyPropertyToken)1 ErrorBuffer (org.structr.common.error.ErrorBuffer)1 GraphObject (org.structr.core.GraphObject)1 MailTemplate (org.structr.core.entity.MailTemplate)1 StringProperty (org.structr.core.property.StringProperty)1 StructrUiTest (org.structr.web.StructrUiTest)1 Widget (org.structr.web.entity.Widget)1