Search in sources :

Example 1 with AttachedBinaries

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();
}
Also used : AccessControlList(com.enonic.xp.security.acl.AccessControlList) NodeNotFoundException(com.enonic.xp.node.NodeNotFoundException) Node(com.enonic.xp.node.Node) NodeId(com.enonic.xp.node.NodeId) AttachedBinaries(com.enonic.xp.node.AttachedBinaries) PrincipalKey(com.enonic.xp.security.PrincipalKey)

Example 2 with AttachedBinaries

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;
}
Also used : Node(com.enonic.xp.node.Node) EditableNode(com.enonic.xp.node.EditableNode) EditableNode(com.enonic.xp.node.EditableNode) AttachedBinaries(com.enonic.xp.node.AttachedBinaries)

Example 3 with AttachedBinaries

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);
        }
    }
}
Also used : BinaryReference(com.enonic.xp.util.BinaryReference) AttachedBinaries(com.enonic.xp.node.AttachedBinaries) AttachedBinary(com.enonic.xp.node.AttachedBinary)

Example 4 with AttachedBinaries

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());
}
Also used : PropertyTree(com.enonic.xp.data.PropertyTree) Node(com.enonic.xp.node.Node) AttachedBinaries(com.enonic.xp.node.AttachedBinaries) Test(org.junit.jupiter.api.Test)

Example 5 with AttachedBinaries

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);
}
Also used : AttachedBinaries(com.enonic.xp.node.AttachedBinaries) AttachedBinary(com.enonic.xp.node.AttachedBinary)

Aggregations

AttachedBinaries (com.enonic.xp.node.AttachedBinaries)8 PropertyTree (com.enonic.xp.data.PropertyTree)4 Node (com.enonic.xp.node.Node)4 AttachedBinary (com.enonic.xp.node.AttachedBinary)3 Test (org.junit.jupiter.api.Test)3 BinaryAttachment (com.enonic.xp.node.BinaryAttachment)2 Property (com.enonic.xp.data.Property)1 EditableNode (com.enonic.xp.node.EditableNode)1 NodeBinaryReferenceException (com.enonic.xp.node.NodeBinaryReferenceException)1 NodeId (com.enonic.xp.node.NodeId)1 NodeNotFoundException (com.enonic.xp.node.NodeNotFoundException)1 AbstractNodeTest (com.enonic.xp.repo.impl.node.AbstractNodeTest)1 UpdateRepositoryEntryParams (com.enonic.xp.repo.impl.repository.UpdateRepositoryEntryParams)1 Repository (com.enonic.xp.repository.Repository)1 RepositoryId (com.enonic.xp.repository.RepositoryId)1 PrincipalKey (com.enonic.xp.security.PrincipalKey)1 AccessControlList (com.enonic.xp.security.acl.AccessControlList)1 BinaryReference (com.enonic.xp.util.BinaryReference)1