Search in sources :

Example 1 with Relation

use of org.structr.core.entity.Relation in project structr by structr.

the class DOMNodeTest method testAppendChild.

@Test
public void testAppendChild() {
    try (final Tx tx = app.tx()) {
        final Class domChildrenType = StructrApp.getConfiguration().getRelationshipEntityClass("DOMNodeCONTAINSDOMNode");
        Page document = (Page) getDocument();
        assertNotNull(document);
        DOMElement div = (DOMElement) document.createElement("div");
        assertNotNull(div);
        Content content1 = (Content) document.createTextNode("content1");
        Content content2 = (Content) document.createTextNode("content2");
        Content content3 = (Content) document.createTextNode("content3");
        assertNotNull(content1);
        assertNotNull(content2);
        assertNotNull(content3);
        // first step
        div.appendChild(content1);
        // check for correct relationship management
        List<Relation> divRels = toList(div.getOutgoingRelationships(domChildrenType));
        assertEquals(1, divRels.size());
        assertEquals(Integer.valueOf(0), divRels.get(0).getProperty(div.getPositionProperty()));
        // second step
        div.appendChild(content2);
        // check for correct relationship management
        divRels = toList(div.getOutgoingRelationships(domChildrenType));
        assertEquals(2, divRels.size());
        assertEquals(Integer.valueOf(0), divRels.get(0).getProperty(div.getPositionProperty()));
        assertEquals(Integer.valueOf(1), divRels.get(1).getProperty(div.getPositionProperty()));
        // third step: test removal of old parent when appending an existing node
        div.appendChild(content3);
        // assert that div has 3 children now
        assertEquals(3, div.getChildNodes().getLength());
        // create new container
        Element div2 = document.createElement("div");
        assertNotNull(div2);
        div.appendChild(div2);
        // div should have 4 children by now
        assertEquals(4, div.getChildNodes().getLength());
        // move text node to div2
        div2.appendChild(content3);
        // div should have 3 children now,
        // div2 should have content3 as a child now
        assertEquals(3, div.getChildNodes().getLength());
        assertEquals(content3, div2.getChildNodes().item(0));
        tx.success();
    } catch (FrameworkException fex) {
        fail("unexpected exception");
    }
}
Also used : Relation(org.structr.core.entity.Relation) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Element(org.w3c.dom.Element) DOMTest(org.structr.web.advanced.DOMTest) Test(org.junit.Test)

Example 2 with Relation

use of org.structr.core.entity.Relation in project structr by structr.

the class FileImportVisitor method handleDeferredFiles.

public void handleDeferredFiles() {
    final Class<Relation> relType = StructrApp.getConfiguration().getRelationshipEntityClass("AbstractMinifiedFileMINIFICATIONFile");
    final PropertyKey<Integer> positionKey = StructrApp.key(relType, "position");
    if (!this.deferredFiles.isEmpty()) {
        for (File file : this.deferredFiles) {
            try (final Tx tx = app.tx(true, false, false)) {
                // set properties from files.json
                final PropertyMap fileProperties = getPropertiesForFileOrFolder(file.getPath());
                final PropertyKey<Map<String, String>> sourcesPropertyKey = new GenericProperty("minificationSources");
                Map<String, String> sourcesConfig = fileProperties.get(sourcesPropertyKey);
                fileProperties.remove(sourcesPropertyKey);
                file.unlockSystemPropertiesOnce();
                file.setProperties(securityContext, fileProperties);
                for (String positionString : sourcesConfig.keySet()) {
                    final Integer position = Integer.parseInt(positionString);
                    final String sourcePath = sourcesConfig.get(positionString);
                    final AbstractFile source = FileHelper.getFileByAbsolutePath(securityContext, sourcePath);
                    if (source != null) {
                        app.create(app.get(AbstractMinifiedFile.class, file.getUuid()), (File) source, relType, new PropertyMap(positionKey, position));
                    } else {
                        logger.warn("Source file {} for minified file {} at position {} not found - please verify that it is included in the export", sourcePath, file.getPath(), positionString);
                    }
                }
                tx.success();
            } catch (FrameworkException fxe) {
            }
        }
    }
}
Also used : AbstractFile(org.structr.web.entity.AbstractFile) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Relation(org.structr.core.entity.Relation) PropertyMap(org.structr.core.property.PropertyMap) GenericProperty(org.structr.core.property.GenericProperty) AbstractMinifiedFile(org.structr.web.entity.AbstractMinifiedFile) AbstractFile(org.structr.web.entity.AbstractFile) AbstractMinifiedFile(org.structr.web.entity.AbstractMinifiedFile) File(org.structr.web.entity.File) HashMap(java.util.HashMap) PropertyMap(org.structr.core.property.PropertyMap) Map(java.util.Map)

Example 3 with Relation

use of org.structr.core.entity.Relation in project structr by structr.

the class MinifiedJavaScriptFile method getSourceFileList.

static ArrayList<SourceFile> getSourceFileList(final MinifiedJavaScriptFile thisFile) throws FrameworkException, IOException {
    final Class<Relation> type = StructrApp.getConfiguration().getRelationshipEntityClass("AbstractMinifiedFileMINIFICATIONFile");
    final PropertyKey<Integer> key = StructrApp.key(type, "position");
    final ArrayList<SourceFile> sourceList = new ArrayList();
    int cnt = 0;
    for (Relation rel : AbstractMinifiedFile.getSortedRelationships(thisFile)) {
        final File src = (File) rel.getTargetNode();
        sourceList.add(SourceFile.fromCode(src.getProperty(File.name), FileUtils.readFileToString(src.getFileOnDisk(), "utf-8")));
        // compact the relationships (if necessary)
        if (rel.getProperty(key) != cnt) {
            rel.setProperty(key, cnt);
        }
        cnt++;
    }
    return sourceList;
}
Also used : Relation(org.structr.core.entity.Relation) ArrayList(java.util.ArrayList) SourceFile(com.google.javascript.jscomp.SourceFile) SourceFile(com.google.javascript.jscomp.SourceFile)

Example 4 with Relation

use of org.structr.core.entity.Relation in project structr by structr.

the class SchemaResource method relationPropertyToMap.

// ----- private methods -----
private static GraphObjectMap relationPropertyToMap(final ConfigurationProvider config, final RelationProperty relationProperty) {
    final GraphObjectMap map = new GraphObjectMap();
    final Relation relation = relationProperty.getRelation();
    /**
     * what we need here:
     * id,
     * sourceMultiplicity,
     * targetMultiplicity,
     * relationshipType,
     */
    map.put(SchemaRelationshipNode.sourceMultiplicity, multiplictyToString(relation.getSourceMultiplicity()));
    map.put(SchemaRelationshipNode.targetMultiplicity, multiplictyToString(relation.getTargetMultiplicity()));
    map.put(typeProperty, relation.getClass().getSimpleName());
    map.put(SchemaRelationshipNode.relationshipType, relation.name());
    final Class sourceType = relation.getSourceType();
    final Class targetType = relation.getTargetType();
    // select AbstractNode and SUPERCLASSES (not subclasses!)
    if (sourceType.isAssignableFrom(AbstractNode.class)) {
        map.put(allSourceTypesPossibleProperty, true);
        map.put(htmlSourceTypesPossibleProperty, true);
        map.put(possibleSourceTypesProperty, null);
    } else if ("DOMNode".equals(sourceType.getSimpleName())) {
        map.put(allTargetTypesPossibleProperty, false);
        map.put(htmlTargetTypesPossibleProperty, true);
        map.put(possibleTargetTypesProperty, null);
    } else {
        map.put(allSourceTypesPossibleProperty, false);
        map.put(htmlSourceTypesPossibleProperty, false);
        map.put(possibleSourceTypesProperty, StringUtils.join(SearchCommand.getAllSubtypesAsStringSet(sourceType.getSimpleName()), ","));
    }
    // select AbstractNode and SUPERCLASSES (not subclasses!)
    if (targetType.isAssignableFrom(AbstractNode.class)) {
        map.put(allTargetTypesPossibleProperty, true);
        map.put(htmlTargetTypesPossibleProperty, true);
        map.put(possibleTargetTypesProperty, null);
    } else if ("DOMNode".equals(targetType.getSimpleName())) {
        map.put(allTargetTypesPossibleProperty, false);
        map.put(htmlTargetTypesPossibleProperty, true);
        map.put(possibleTargetTypesProperty, null);
    } else {
        map.put(allTargetTypesPossibleProperty, false);
        map.put(htmlTargetTypesPossibleProperty, false);
        map.put(possibleTargetTypesProperty, StringUtils.join(SearchCommand.getAllSubtypesAsStringSet(targetType.getSimpleName()), ","));
    }
    return map;
}
Also used : Relation(org.structr.core.entity.Relation) GraphObjectMap(org.structr.core.GraphObjectMap)

Example 5 with Relation

use of org.structr.core.entity.Relation in project structr by structr.

the class SyncModeCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final Class<Relation> relType = StructrApp.getConfiguration().getRelationshipEntityClass("DOMNodeSYNCDOMNode");
    final SecurityContext securityContext = getWebSocket().getSecurityContext();
    final String sourceId = webSocketData.getId();
    final Map<String, Object> properties = webSocketData.getNodeData();
    final String targetId = (String) properties.get("targetId");
    final String syncMode = (String) properties.get("syncMode");
    final DOMNode sourceNode = (DOMNode) getNode(sourceId);
    final DOMNode targetNode = (DOMNode) getNode(targetId);
    final App app = StructrApp.getInstance(securityContext);
    if ((sourceNode != null) && (targetNode != null)) {
        try {
            app.create(sourceNode, targetNode, relType);
            if (syncMode.equals("bidir")) {
                app.create(targetNode, sourceNode, relType);
            }
        } catch (Throwable t) {
            getWebSocket().send(MessageBuilder.status().code(400).message(t.getMessage()).build(), true);
        }
    } else {
        getWebSocket().send(MessageBuilder.status().code(400).message("The SYNC_MODE command needs id and targetId!").build(), true);
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Relation(org.structr.core.entity.Relation) SecurityContext(org.structr.common.SecurityContext) DOMNode(org.structr.web.entity.dom.DOMNode)

Aggregations

Relation (org.structr.core.entity.Relation)17 FrameworkException (org.structr.common.error.FrameworkException)7 App (org.structr.core.app.App)6 StructrApp (org.structr.core.app.StructrApp)6 PropertyMap (org.structr.core.property.PropertyMap)5 PropertyKey (org.structr.core.property.PropertyKey)4 LinkedList (java.util.LinkedList)3 GraphObject (org.structr.core.GraphObject)3 GraphObjectMap (org.structr.core.GraphObjectMap)3 RestMethodResult (org.structr.rest.RestMethodResult)3 Image (org.structr.web.entity.Image)3 BufferedImage (java.awt.image.BufferedImage)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 SecurityContext (org.structr.common.SecurityContext)2 Result (org.structr.core.Result)2 AbstractNode (org.structr.core.entity.AbstractNode)2 NodeInterface (org.structr.core.graph.NodeInterface)2 Tx (org.structr.core.graph.Tx)2