use of com.enonic.xp.node.AttachedBinaries in project xp by enonic.
the class CreateNodeCommand method execute.
public Node execute() {
Preconditions.checkNotNull(params.getParent(), "Path of parent Node must be specified");
Preconditions.checkArgument(params.getParent().isAbsolute(), "Path to parent Node must be absolute: " + params.getParent());
NodeHelper.runAsAdmin(this::verifyNotExistsAlready);
final Node parentNode = NodeHelper.runAsAdmin(this::verifyParentExists);
if (parentNode == null) {
throw new NodeNotFoundException("Parent node to node with name '" + params.getName() + "' with parent path '" + params.getParent() + "' not found");
}
requireContextUserPermission(Permission.CREATE, parentNode);
final PrincipalKey user = getCurrentPrincipalKey();
final AccessControlList permissions = getAccessControlEntries(user);
final Long manualOrderValue = NodeHelper.runAsAdmin(() -> resolvePotentialManualOrderValue(parentNode));
final AttachedBinaries attachedBinaries = storeAndAttachBinaries();
final Node.Builder nodeBuilder = Node.create().id(this.params.getNodeId() != null ? params.getNodeId() : new NodeId()).parentPath(params.getParent()).name(NodeName.from(params.getName())).data(params.getData()).indexConfigDocument(params.getIndexConfigDocument()).childOrder(params.getChildOrder() != null ? params.getChildOrder() : ChildOrder.defaultOrder()).manualOrderValue(manualOrderValue).permissions(permissions).inheritPermissions(params.inheritPermissions()).nodeType(params.getNodeType() != null ? params.getNodeType() : NodeType.DEFAULT_NODE_COLLECTION).attachedBinaries(attachedBinaries).timestamp(this.timestamp != null ? this.timestamp : Instant.now(CLOCK));
final Node newNode = nodeBuilder.build();
return StoreNodeCommand.create(this).node(newNode).updateMetadataOnly(false).build().execute();
}
use of com.enonic.xp.node.AttachedBinaries in project xp by enonic.
the class UpdateNodeCommand method doExecute.
private Node doExecute() {
final Node persistedNode = getPersistedNode();
requireContextUserPermissionOrAdmin(Permission.MODIFY, persistedNode);
final EditableNode editableNode = new EditableNode(persistedNode);
params.getEditor().edit(editableNode);
if (editableNode.inheritPermissions != persistedNode.inheritsPermissions() || !persistedNode.getPermissions().equals(editableNode.permissions)) {
requireContextUserPermissionOrAdmin(Permission.WRITE_PERMISSIONS, persistedNode);
}
final AttachedBinaries updatedBinaries = UpdatedAttachedBinariesResolver.create().editableNode(editableNode).persistedNode(persistedNode).binaryAttachments(this.params.getBinaryAttachments()).binaryService(this.binaryService).build().resolve();
final Node editedNode = editableNode.build();
if (editedNode.equals(persistedNode) && updatedBinaries.equals(persistedNode.getAttachedBinaries())) {
return persistedNode;
}
final Node updatedNode = createUpdatedNode(Node.create(editedNode).timestamp(Instant.now(CLOCK)).attachedBinaries(updatedBinaries).build());
if (!this.params.isDryRun()) {
StoreNodeCommand.create(this).node(updatedNode).build().execute();
}
return updatedNode;
}
use of com.enonic.xp.node.AttachedBinaries in project xp by enonic.
the class UpdatedAttachedBinariesResolver method processExistingBinaries.
private void processExistingBinaries(final Map<BinaryReference, AttachedBinary> resolved, final Set<BinaryReference> referencesInEditedNode, final Set<BinaryReference> referencesInPersistedNode) {
final Set<BinaryReference> unchangedReferences = Sets.intersection(referencesInPersistedNode, referencesInEditedNode);
final AttachedBinaries attachedBinaries = persistedNode.getAttachedBinaries();
for (final BinaryReference unchangedReference : unchangedReferences) {
final AttachedBinary existingAttachedBinary = attachedBinaries.getByBinaryReference(unchangedReference);
if (existingAttachedBinary != null) {
resolved.put(unchangedReference, existingAttachedBinary);
}
}
}
use of com.enonic.xp.node.AttachedBinaries in project xp by enonic.
the class CreateNodeCommandTest method attach_binary.
@Test
public void attach_binary() throws Exception {
PropertyTree data = new PropertyTree();
data.setBinaryReference("myCar", BinaryReference.from("myImage"));
final Node node = createNode(CreateNodeParams.create().name("test").parent(NodePath.ROOT).data(data).attachBinary(BinaryReference.from("myImage"), ByteSource.empty()).build());
final AttachedBinaries attachedBinaries = node.getAttachedBinaries();
assertEquals(1, attachedBinaries.getSize());
}
use of com.enonic.xp.node.AttachedBinaries in project xp by enonic.
the class AbstractGetBinaryCommand method getByBinaryReference.
ByteSource getByBinaryReference(final Node node) {
final AttachedBinaries attachedBinaries = node.getAttachedBinaries();
if (attachedBinaries == null) {
return null;
}
final AttachedBinary attachedBinary = attachedBinaries.getByBinaryReference(this.binaryReference);
if (attachedBinary == null) {
return null;
}
return doGetByteSource(attachedBinary);
}
Aggregations