Attachments: Copying, Moving & Renaming

Attachments can be copied, moved or renamed using appropriate built-in session methods. All of those actions are executed when saveChanges is called and are taking place on the server-side, removing the need to transfer whole attachment binary data over the network in order to perform the action.

Copying

Attachment can be copied using one of the session.advanced().attachments().copy methods:

Syntax

void copy(Object sourceEntity, String sourceName,
          Object destinationEntity, String destinationName);

void copy(String sourceDocumentId, String sourceName,
          String destinationDocumentId, String destinationName);

Example

Employee employee1 = session.load(Employee.class, "employees/1-A");
Employee employee2 = session.load(Employee.class, "employees/2-A");

session.advanced()
    .attachments()
    .copy(employee1, "photo.jpg", employee2, "photo-copy.jpg");

session.saveChanges();

Moving

Attachment can be moved using one of the session.advanced().attachments().move methods:

Syntax

void move(Object sourceEntity, String sourceName,
          Object destinationEntity, String destinationName);

void move(String sourceDocumentId, String sourceName,
          String destinationDocumentId, String destinationName);

Example

Employee employee1 = session.load(Employee.class, "employees/1-A");
Employee employee2 = session.load(Employee.class, "employees/2-A");

session.advanced()
    .attachments()
    .copy(employee1, "photo.jpg", employee2, "photo.jpg");

session.saveChanges();

Renaming

Attachment can be renamed using one of the session.advanced().attachments().rename methods:

Syntax

void rename(Object entity, String name, String newName);

void rename(String documentId, String name, String newName);

Example

Employee employee1 = session.load(Employee.class, "employees/1-A");

session.advanced()
    .attachments()
    .rename(employee1, "photo.jpg", "photo-new.jpg");

session.saveChanges();