use of org.structr.web.entity.AbstractFile in project structr by structr.
the class FileHelper method transformFile.
/**
* Transform an existing file into the target class.
*
* @param <T>
* @param securityContext
* @param uuid
* @param fileType
* @return transformed file
* @throws FrameworkException
* @throws IOException
*/
public static <T extends File> T transformFile(final SecurityContext securityContext, final String uuid, final Class<T> fileType) throws FrameworkException, IOException {
AbstractFile existingFile = getFileByUuid(securityContext, uuid);
if (existingFile != null) {
existingFile.unlockSystemPropertiesOnce();
existingFile.setProperties(securityContext, new PropertyMap(AbstractNode.type, fileType == null ? File.class.getSimpleName() : fileType.getSimpleName()));
existingFile = getFileByUuid(securityContext, uuid);
return (T) (fileType != null ? fileType.cast(existingFile) : (File) existingFile);
}
return null;
}
use of org.structr.web.entity.AbstractFile in project structr by structr.
the class FileSyncWatchEventListener method getOrCreate.
private AbstractFile getOrCreate(final Folder parentFolder, final Path fullPath, final Path relativePath, final boolean doCreate) throws FrameworkException {
final PropertyKey<Boolean> isExternalKey = StructrApp.key(AbstractFile.class, "isExternal");
final PropertyKey<Folder> parentKey = StructrApp.key(AbstractFile.class, "parent");
final String fileName = relativePath.getFileName().toString();
final boolean isFile = !Files.isDirectory(fullPath);
final Class<? extends AbstractFile> type = isFile ? org.structr.web.entity.File.class : Folder.class;
final App app = StructrApp.getInstance();
AbstractFile file = app.nodeQuery(type).and(AbstractFile.name, fileName).and(parentKey, parentFolder).getFirst();
if (file == null && doCreate) {
file = app.create(type, new NodeAttribute<>(AbstractFile.name, fileName), new NodeAttribute<>(parentKey, parentFolder), new NodeAttribute<>(isExternalKey, true));
}
return file;
}
use of org.structr.web.entity.AbstractFile in project structr by structr.
the class WebsocketController method getMessageForEvent.
// ----- private methods -----
private WebSocketMessage getMessageForEvent(final SecurityContext securityContext, final ModificationEvent modificationEvent) throws FrameworkException {
final String callbackId = modificationEvent.getCallbackId();
if (modificationEvent.isNode()) {
final NodeInterface node = (NodeInterface) modificationEvent.getGraphObject();
if (modificationEvent.isDeleted()) {
final WebSocketMessage message = createMessage("DELETE", callbackId);
message.setId(modificationEvent.getRemovedProperties().get(GraphObject.id));
message.setCode(200);
return message;
}
if (modificationEvent.isCreated()) {
final WebSocketMessage message = createMessage("CREATE", callbackId);
message.setGraphObject(node);
message.setResult(Arrays.asList(new GraphObject[] { node }));
message.setCode(201);
return message;
}
if (modificationEvent.isModified()) {
final WebSocketMessage message = createMessage("UPDATE", callbackId);
// at login the securityContext is still null
if (securityContext != null) {
// only include changed properties (+ id and type)
LinkedHashSet<String> propertySet = new LinkedHashSet();
propertySet.add("id");
propertySet.add("type");
for (Iterator<PropertyKey> it = modificationEvent.getModifiedProperties().keySet().iterator(); it.hasNext(); ) {
final String jsonName = ((PropertyKey) it.next()).jsonName();
if (!propertySet.contains(jsonName)) {
propertySet.add(jsonName);
}
}
for (Iterator<PropertyKey> it = modificationEvent.getRemovedProperties().keySet().iterator(); it.hasNext(); ) {
final String jsonName = ((PropertyKey) it.next()).jsonName();
if (!propertySet.contains(jsonName)) {
propertySet.add(jsonName);
}
}
if (propertySet.size() > 2) {
securityContext.setCustomView(propertySet);
}
}
message.setGraphObject(node);
message.setResult(Arrays.asList(new GraphObject[] { node }));
message.setId(node.getUuid());
message.getModifiedProperties().addAll(modificationEvent.getModifiedProperties().keySet());
message.getRemovedProperties().addAll(modificationEvent.getRemovedProperties().keySet());
message.setNodeData(modificationEvent.getData(securityContext));
message.setCode(200);
if (securityContext != null) {
// Clear custom view here. This is necessary because the security context is reused for all websocket frames.
securityContext.clearCustomView();
}
return message;
}
} else {
// handle relationship
final RelationshipInterface relationship = (RelationshipInterface) modificationEvent.getGraphObject();
final RelationshipType relType = modificationEvent.getRelationshipType();
// special treatment of CONTAINS relationships
if ("CONTAINS".equals(relType.name())) {
if (modificationEvent.isDeleted()) {
final WebSocketMessage message = createMessage("REMOVE_CHILD", callbackId);
message.setNodeData("parentId", relationship.getSourceNodeId());
message.setId(relationship.getTargetNodeId());
message.setCode(200);
return message;
}
if (modificationEvent.isCreated()) {
final WebSocketMessage message = new WebSocketMessage();
final NodeInterface startNode = relationship.getSourceNode();
final NodeInterface endNode = relationship.getTargetNode();
// don't send a notification
if (startNode == null || endNode == null) {
return null;
}
message.setResult(Arrays.asList(new GraphObject[] { endNode }));
message.setId(endNode.getUuid());
message.setNodeData("parentId", startNode.getUuid());
message.setCode(200);
message.setCommand("APPEND_CHILD");
if (endNode instanceof DOMNode) {
org.w3c.dom.Node refNode = ((DOMNode) endNode).getNextSibling();
if (refNode != null) {
message.setCommand("INSERT_BEFORE");
message.setNodeData("refId", ((AbstractNode) refNode).getUuid());
}
} else if (endNode instanceof User) {
message.setCommand("APPEND_USER");
message.setNodeData("refId", startNode.getUuid());
} else if (endNode instanceof AbstractFile) {
message.setCommand("APPEND_FILE");
message.setNodeData("refId", startNode.getUuid());
}
return message;
}
}
if (modificationEvent.isDeleted()) {
final WebSocketMessage message = createMessage("DELETE", callbackId);
message.setId(modificationEvent.getRemovedProperties().get(GraphObject.id));
message.setCode(200);
return message;
}
if (modificationEvent.isModified()) {
final WebSocketMessage message = createMessage("UPDATE", callbackId);
message.getModifiedProperties().addAll(modificationEvent.getModifiedProperties().keySet());
message.getRemovedProperties().addAll(modificationEvent.getRemovedProperties().keySet());
message.setNodeData(modificationEvent.getData(securityContext));
message.setGraphObject(relationship);
message.setId(relationship.getUuid());
message.setCode(200);
final PropertyMap relProperties = relationship.getProperties();
// final NodeInterface startNode = relationship.getSourceNode();
// final NodeInterface endNode = relationship.getTargetNode();
// relProperties.put(new StringProperty("startNodeId"), startNode.getUuid());
// relProperties.put(new StringProperty("endNodeId"), endNode.getUuid());
final Map<String, Object> properties = PropertyMap.javaTypeToInputType(securityContext, relationship.getClass(), relProperties);
message.setRelData(properties);
return message;
}
}
return null;
}
use of org.structr.web.entity.AbstractFile in project structr by structr.
the class FindDuplicateFilesCommand method processMessage.
@Override
public void processMessage(final WebSocketMessage webSocketData) {
final PropertyKey<String> path = StructrApp.key(AbstractFile.class, "path");
final SecurityContext securityContext = getWebSocket().getSecurityContext();
final Query query = StructrApp.getInstance(securityContext).nodeQuery(AbstractFile.class).sort(path);
try {
AbstractFile lastFile = null;
String lastFilepath = null;
boolean lastWasDupe = false;
final ArrayList<GraphObject> filesWithSamePath = new ArrayList<>();
final Iterator it = query.getAsList().iterator();
while (it.hasNext()) {
final AbstractNode node = (AbstractNode) it.next();
try {
final AbstractFile file = (AbstractFile) node;
final String currentFilepath = file.getProperty(path);
// skip the first file as we can not compare it to the previous one
if (lastFile != null) {
if (currentFilepath.equals(lastFilepath)) {
if (!lastWasDupe) {
// if this is the first duplicate found we need to add both files
filesWithSamePath.add(lastFile);
}
filesWithSamePath.add(file);
lastWasDupe = true;
} else {
lastWasDupe = false;
}
}
lastFilepath = currentFilepath;
lastFile = file;
} catch (ClassCastException cce) {
logger.warn("Tried casting node '{}' of type '{}' to AbstractFile. Most likely a node type inheriting from File was deleted and an instance remains. Please delete this node or change its type.", node.getUuid(), node.getType());
}
}
// set full result list
webSocketData.setResult(filesWithSamePath);
webSocketData.setRawResultCount(filesWithSamePath.size());
// send only over local connection
getWebSocket().send(webSocketData, true);
} catch (FrameworkException fex) {
logger.warn("Exception occured", fex);
getWebSocket().send(MessageBuilder.status().code(fex.getStatus()).message(fex.getMessage()).build(), true);
}
}
use of org.structr.web.entity.AbstractFile in project structr by structr.
the class AppendFileCommand method processMessage.
@Override
public void processMessage(final WebSocketMessage webSocketData) {
String id = webSocketData.getId();
Map<String, Object> nodeData = webSocketData.getNodeData();
String parentId = (String) nodeData.get("parentId");
// check node to append
if (id == null) {
getWebSocket().send(MessageBuilder.status().code(422).message("Cannot append node, no id is given").build(), true);
return;
}
// check for parent ID
if (parentId == null) {
getWebSocket().send(MessageBuilder.status().code(422).message("Cannot append node without parentId").build(), true);
return;
}
// never append to self
if (parentId.equals(id)) {
getWebSocket().send(MessageBuilder.status().code(422).message("Cannot append node as its own child.").build(), true);
return;
}
// check if parent node with given ID exists
AbstractNode parentNode = getNode(parentId);
if (parentNode == null) {
getWebSocket().send(MessageBuilder.status().code(404).message("Parent node not found").build(), true);
return;
}
if (parentNode instanceof Folder) {
Folder folder = (Folder) parentNode;
AbstractFile file = (AbstractFile) getNode(id);
if (file != null) {
try {
// Remove from existing parent
Folder currentParent = (Folder) file.treeGetParent();
if (currentParent != null) {
currentParent.treeRemoveChild(file);
}
folder.treeAppendChild(file);
TransactionCommand.registerNodeCallback(file, callback);
} catch (FrameworkException ex) {
logger.error("", ex);
getWebSocket().send(MessageBuilder.status().code(422).message("Cannot append file").build(), true);
}
}
} else {
// send exception
getWebSocket().send(MessageBuilder.status().code(422).message("Parent node is not instance of Folder").build(), true);
}
}
Aggregations