Search in sources :

Example 1 with ShadowDocument

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

the class UiSyncCommand method doImport.

private void doImport(final String fileName) throws FrameworkException {
    final App app = StructrApp.getInstance();
    final DatabaseService graphDb = app.getDatabaseService();
    SyncCommand.importFromFile(graphDb, securityContext, fileName, true);
    // import done, now the ShadowDocument needs some special care. :(
    try (final Tx tx = app.tx()) {
        final List<ShadowDocument> shadowDocuments = app.nodeQuery(ShadowDocument.class).includeDeletedAndHidden().getAsList();
        if (shadowDocuments.size() > 1) {
            final PropertyKey<List<DOMNode>> elementsKey = StructrApp.key(Page.class, "elements");
            final List<DOMNode> collectiveChildren = new LinkedList<>();
            // sort by node id (higher node ID is newer entity)
            Collections.sort(shadowDocuments, new Comparator<ShadowDocument>() {

                @Override
                public int compare(final ShadowDocument t1, final ShadowDocument t2) {
                    return Long.valueOf(t2.getId()).compareTo(t1.getId());
                }
            });
            final ShadowDocument previousShadowDoc = shadowDocuments.get(0);
            final ShadowDocument newShadowDoc = shadowDocuments.get(1);
            // collect children of both shadow documents
            collectiveChildren.addAll(previousShadowDoc.getProperty(elementsKey));
            collectiveChildren.addAll(newShadowDoc.getProperty(elementsKey));
            // delete old shadow document
            app.delete(previousShadowDoc);
            // add children to new shadow document
            newShadowDoc.setProperties(securityContext, new PropertyMap(elementsKey, collectiveChildren));
        }
        tx.success();
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) DatabaseService(org.structr.api.DatabaseService) LinkedList(java.util.LinkedList) PropertyMap(org.structr.core.property.PropertyMap) LinkedList(java.util.LinkedList) List(java.util.List) ShadowDocument(org.structr.web.entity.dom.ShadowDocument) DOMNode(org.structr.web.entity.dom.DOMNode)

Example 2 with ShadowDocument

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

the class ListUnattachedNodesCommand method getUnattachedNodes.

/**
 * Return list of nodes which are not attached to a page and have no
 * parent element (no incoming CONTAINS rel)
 *
 * @param app
 * @param securityContext
 * @param webSocketData
 * @return
 * @throws FrameworkException
 */
protected static List<NodeInterface> getUnattachedNodes(final App app, final SecurityContext securityContext, final WebSocketMessage webSocketData) throws FrameworkException {
    final String sortOrder = webSocketData.getSortOrder();
    final String sortKey = webSocketData.getSortKey();
    Query query;
    if (sortKey != null) {
        final PropertyKey sortProperty = StructrApp.key(DOMNode.class, sortKey);
        query = StructrApp.getInstance(securityContext).nodeQuery().includeDeletedAndHidden().sort(sortProperty).order("desc".equals(sortOrder));
    } else {
        query = StructrApp.getInstance(securityContext).nodeQuery().includeDeletedAndHidden();
    }
    query.orTypes(DOMElement.class);
    query.orType(Content.class);
    query.orType(Template.class);
    // do search
    final List<NodeInterface> filteredResults = new LinkedList();
    List<? extends GraphObject> resultList = null;
    try (final Tx tx = app.tx()) {
        resultList = query.getAsList();
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("Exception occured", fex);
    }
    if (resultList != null) {
        // determine which of the nodes have no incoming CONTAINS relationships and no page id
        for (GraphObject obj : resultList) {
            if (obj instanceof DOMNode) {
                DOMNode node = (DOMNode) obj;
                if (!node.hasIncomingRelationships(node.getChildLinkType()) && node.getOwnerDocument() == null && !(node instanceof ShadowDocument)) {
                    filteredResults.add(node);
                }
            }
        }
    }
    return filteredResults;
}
Also used : Query(org.structr.core.app.Query) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) GraphObject(org.structr.core.GraphObject) DOMNode(org.structr.web.entity.dom.DOMNode) ShadowDocument(org.structr.web.entity.dom.ShadowDocument) PropertyKey(org.structr.core.property.PropertyKey) NodeInterface(org.structr.core.graph.NodeInterface) LinkedList(java.util.LinkedList)

Example 3 with ShadowDocument

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

the class CreateComponentCommand method create.

public DOMNode create(final DOMNode node) throws FrameworkException {
    final DOMNode clonedNode = (DOMNode) node.cloneNode(false);
    // Child nodes of a template must stay in page tree
    if (!(clonedNode instanceof Template)) {
        moveChildNodes(node, clonedNode);
    }
    final ShadowDocument hiddenDoc = CreateComponentCommand.getOrCreateHiddenDocument();
    clonedNode.setOwnerDocument(hiddenDoc);
    // Change page (owner document) of all children recursively
    for (DOMNode child : DOMNode.getAllChildNodes(clonedNode)) {
        child.setOwnerDocument(hiddenDoc);
    }
    node.setSharedComponent(clonedNode);
    return clonedNode;
}
Also used : DOMNode(org.structr.web.entity.dom.DOMNode) ShadowDocument(org.structr.web.entity.dom.ShadowDocument) Template(org.structr.web.entity.dom.Template)

Example 4 with ShadowDocument

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

the class DeploymentTest method test06SimpleTemplateInSharedComponents.

@Test
public void test06SimpleTemplateInSharedComponents() {
    // setup
    try (final Tx tx = app.tx()) {
        final Page page = Page.createNewPage(securityContext, "test06");
        final Html html = createElement(page, page, "html");
        final Head head = createElement(page, html, "head");
        createElement(page, head, "title", "test06");
        final Body body = createElement(page, html, "body");
        createElement(page, body, "div");
        final ShadowDocument shadowDocument = CreateComponentCommand.getOrCreateHiddenDocument();
        createTemplate(shadowDocument, null, "template source - öäüÖÄÜß'\"'`");
        tx.success();
    } catch (FrameworkException fex) {
        fail("Unexpected exception.");
    }
    // test
    compare(calculateHash(), true);
}
Also used : Head(org.structr.web.entity.html.Head) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Html(org.structr.web.entity.html.Html) Page(org.structr.web.entity.dom.Page) ShadowDocument(org.structr.web.entity.dom.ShadowDocument) Body(org.structr.web.entity.html.Body) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 5 with ShadowDocument

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

the class DeploymentTest method calculateHash.

private void calculateHash(final NodeInterface start, final StringBuilder buf, final int depth) {
    buf.append(start.getType()).append("{");
    hash(start, buf);
    // indent
    for (int i = 0; i < depth; i++) {
        System.out.print("    ");
    }
    System.out.println(start.getType() + ": " + start.getUuid().substring(0, 5));
    if (start instanceof ShadowDocument) {
        for (final DOMNode child : ((ShadowDocument) start).getElements()) {
            // only include toplevel elements of the shadow document
            if (child.getParent() == null) {
                calculateHash(child, buf, depth + 1);
            }
        }
    } else if (start instanceof DOMNode) {
        for (final DOMNode child : ((DOMNode) start).getChildren()) {
            calculateHash(child, buf, depth + 1);
        }
    }
    buf.append("}");
}
Also used : ShadowDocument(org.structr.web.entity.dom.ShadowDocument) DOMNode(org.structr.web.entity.dom.DOMNode)

Aggregations

ShadowDocument (org.structr.web.entity.dom.ShadowDocument)12 DOMNode (org.structr.web.entity.dom.DOMNode)8 Tx (org.structr.core.graph.Tx)7 FrameworkException (org.structr.common.error.FrameworkException)4 App (org.structr.core.app.App)4 StructrApp (org.structr.core.app.StructrApp)4 PropertyMap (org.structr.core.property.PropertyMap)4 LinkedList (java.util.LinkedList)3 Page (org.structr.web.entity.dom.Page)3 FileOutputStream (java.io.FileOutputStream)2 FileWriter (java.io.FileWriter)2 IOException (java.io.IOException)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Writer (java.io.Writer)2 Path (java.nio.file.Path)2 TreeMap (java.util.TreeMap)2 Test (org.junit.Test)2 GraphObject (org.structr.core.GraphObject)2 NodeInterface (org.structr.core.graph.NodeInterface)2 StructrUiTest (org.structr.web.StructrUiTest)2