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");
}
}
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) {
}
}
}
}
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;
}
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;
}
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);
}
}
Aggregations