Skip to content

Commit

Permalink
feat: Implement attachment copying and moving - EXO-74754 - Meeds-io/…
Browse files Browse the repository at this point in the history
…MIPs#145 (#4195)

Implement copy and move functionality for attachments.
  • Loading branch information
sofyenne authored and exo-swf committed Nov 22, 2024
1 parent dfad46f commit e24056a
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,27 @@ InputStream getAttachmentInputStream(String objectType,
*/
Map<String, AttachmentPlugin> getAttachmentPlugins();

/**
* Copies attachments from a specified source object type to a specified destination object type.
*
* @param sourceObjectType the type of the source object
* @param sourceObjectId the ID of the source object
* @param destinationObjectType the type of the destination object
* @param destinationObjectId the ID of the destination object
* @param destinationParentObjectId the ID of the destination's parent object
* @param userIdentityId the ID of the user performing the operation
*/
void copyAttachments(String sourceObjectType, String sourceObjectId, String destinationObjectType, String destinationObjectId, String destinationParentObjectId, long userIdentityId);

/**
* Moves attachments from a specified source object type to a specified destination object type.
*
* @param sourceObjectType the type of the source object
* @param sourceObjectId the ID of the source object
* @param destinationObjectType the type of the destination object
* @param destinationObjectId the ID of the destination object
* @param destinationParentObjectId the ID of the destination's parent object
* @param userIdentityId the ID of the user performing the operation
*/
void moveAttachments(String sourceObjectType, String sourceObjectId, String destinationObjectType, String destinationObjectId, String destinationParentObjectId, long userIdentityId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@

public class AttachmentServiceImpl implements AttachmentService {

private static final Log LOG = ExoLogger.getLogger(AttachmentServiceImpl.class);
private static final Log LOG = ExoLogger.getLogger(AttachmentServiceImpl.class);

private static final String ATTACHMENT_ALT_TEXT = "alt";

private static final String ATTACHMENT_FORMAT = "format";

private final Map<String, AttachmentPlugin> attachmentPlugins = new HashMap<>();

Expand Down Expand Up @@ -163,8 +167,8 @@ public void saveAttachment(UploadedAttachmentDetail uploadedAttachmentDetail,
String altText = uploadedAttachmentDetail.getAltText();
String format = uploadedAttachmentDetail.getFormat();
Map<String, String> properties = new HashMap<>();
properties.put("alt", altText);
properties.put("format", format);
properties.put(ATTACHMENT_ALT_TEXT, altText);
properties.put(ATTACHMENT_FORMAT, format);

Long attachmentId =
!(StringUtils.isBlank(uploadedAttachmentDetail.getId())) ?
Expand All @@ -187,6 +191,7 @@ public void saveAttachment(UploadedAttachmentDetail uploadedAttachmentDetail,
userIdentityId);
if (attachmentId == null) {
createAttachment(fileId, objectType, objectId, parentObjectId, userIdentityId, properties);
uploadedAttachmentDetail.setId(fileId);
} else {
updateAttachment(fileId, objectType, objectId, userIdentityId, properties);
}
Expand Down Expand Up @@ -254,10 +259,10 @@ public ObjectAttachmentList getAttachments(String objectType, String objectId) {
0,
0);
if (CollectionUtils.isNotEmpty(attachmentItem) && attachmentItem.get(0).getProperties() != null
&& attachmentItem.get(0).getProperties().containsKey("alt")) {
&& attachmentItem.get(0).getProperties().containsKey(ATTACHMENT_ALT_TEXT)) {
Map<String, String> metadataItemProperties = attachmentItem.get(0).getProperties();
attachment.setAltText(metadataItemProperties.get("alt"));
attachment.setFormat(metadataItemProperties.get("format"));
attachment.setAltText(metadataItemProperties.get(ATTACHMENT_ALT_TEXT));
attachment.setFormat(metadataItemProperties.get(ATTACHMENT_FORMAT));
}
});
}
Expand Down Expand Up @@ -322,6 +327,82 @@ public boolean hasEditPermission(Identity userIdentity, String objectType, Strin
return attachmentPlugin != null && attachmentPlugin.hasEditPermission(userIdentity, objectId);
}

@Override
public void copyAttachments(String sourceObjectType,
String sourceObjectId,
String destinationObjectType,
String destinationObjectId,
String destinationParentObjectId,
long userIdentityId) {
ObjectAttachmentList objectAttachmentList = getAttachments(sourceObjectType, sourceObjectId);
List<ObjectAttachmentDetail> attachments = objectAttachmentList.getAttachments();
if (CollectionUtils.isNotEmpty(attachments)) {
attachments.forEach(attachment -> {
String altText = attachment.getAltText();
String format = attachment.getFormat();
Map<String, String> properties = new HashMap<>();
properties.put(ATTACHMENT_ALT_TEXT, altText);
properties.put(ATTACHMENT_FORMAT, format);
try {
createAttachment(attachment.getId(),
destinationObjectType,
destinationObjectId,
destinationParentObjectId,
userIdentityId,
properties);
} catch (Exception e) {
LOG.error("Error when creating attachment", e);
}
});
}
}

@Override
public void moveAttachments(String sourceObjectType,
String sourceObjectId,
String destinationObjectType,
String destinationObjectId,
String destinationParentObjectId,
long userIdentityId) {
ObjectAttachmentList objectAttachmentList = getAttachments(sourceObjectType, sourceObjectId);
List<ObjectAttachmentDetail> attachments = objectAttachmentList.getAttachments();
if (CollectionUtils.isNotEmpty(attachments)) {
attachments.forEach(attachment -> {
String altText = attachment.getAltText();
String format = attachment.getFormat();
Map<String, String> properties = new HashMap<>();
properties.put(ATTACHMENT_ALT_TEXT, altText);
properties.put(ATTACHMENT_FORMAT, format);
try {
createAttachment(attachment.getId(),
destinationObjectType,
destinationObjectId,
destinationParentObjectId,
userIdentityId,
properties);
List<MetadataItem> metadataItemToDelete =
metadataService.getMetadataItemsByMetadataNameAndTypeAndObject(attachment.getId(),
AttachmentService.METADATA_TYPE.getName(),
sourceObjectType,
sourceObjectId,
0,
0);
if (CollectionUtils.isNotEmpty(metadataItemToDelete)) {
metadataItemToDelete.forEach(metadataItem -> {
try {
metadataService.deleteMetadataItem(metadataItem.getId(), true);
} catch (ObjectNotFoundException e) {
LOG.error("Error when deleting metadata item", e);
}
});
}
} catch (Exception e) {
LOG.error("Error when creating attachment", e);
}
});
}
}

private org.exoplatform.social.core.identity.model.Identity checkAccessPermission(String objectType,
String objectId,
String fileId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public class AttachmentServiceTest extends AbstractCoreTest {

private static final String OBJECT_TYPE = "objectType";

private static final String DEST_OBJECT_TYPE = "destinationObjectType";

private static final Random RANDOM = new Random();

private Map<String, Long> eventCounts = new HashMap<>();
Expand Down Expand Up @@ -390,4 +392,49 @@ private void assertListenerCount(long createdCount,
assertEquals(deletedListCount, eventCounts.getOrDefault(ATTACHMENTS_DELETED_EVENT, 0l).longValue());
}

public void testMoveAttachments() throws Exception { // NOSONAR
startSessionAndRegisterAs(USERNAME);
String identityId = identityManager.getOrCreateUserIdentity(USERNAME).getId();
String fileId = createAttachment(USERNAME);

String destinationObjectId = "destinationObjectId" + System.currentTimeMillis();
String destinationParentObjectId = null;

attachmentService.moveAttachments(OBJECT_TYPE, objectId, DEST_OBJECT_TYPE, destinationObjectId, destinationParentObjectId, Long.parseLong(identityId));

// Verify the attachments are moved to the destination object
ObjectAttachmentList destinationObjectAttachmentList = attachmentService.getAttachments(DEST_OBJECT_TYPE, destinationObjectId);
assertNotNull(destinationObjectAttachmentList);
assertEquals(1, destinationObjectAttachmentList.getAttachments().size());
assertEquals(fileId, destinationObjectAttachmentList.getAttachments().get(0).getId());

// Verify the attachments are removed from the source object
ObjectAttachmentList sourceObjectAttachmentList = attachmentService.getAttachments(OBJECT_TYPE, objectId);
assertNotNull(sourceObjectAttachmentList);
assertEquals(0, sourceObjectAttachmentList.getAttachments().size());
}

public void testCopyAttachments() throws Exception { // NOSONAR
startSessionAndRegisterAs(USERNAME);
String identityId = identityManager.getOrCreateUserIdentity(USERNAME).getId();
String fileId = createAttachment(USERNAME);

String destinationObjectId = "destinationObjectId" + System.currentTimeMillis();
String destinationParentObjectId = null;

attachmentService.copyAttachments(OBJECT_TYPE, objectId, DEST_OBJECT_TYPE, destinationObjectId, destinationParentObjectId, Long.parseLong(identityId));

// Verify the attachments are copied to the destination object
ObjectAttachmentList destinationObjectAttachmentList = attachmentService.getAttachments(DEST_OBJECT_TYPE, destinationObjectId);
assertNotNull(destinationObjectAttachmentList);
assertEquals(1, destinationObjectAttachmentList.getAttachments().size());
assertEquals(fileId, destinationObjectAttachmentList.getAttachments().get(0).getId());

// Verify the attachments are still present in the source object
ObjectAttachmentList sourceObjectAttachmentList = attachmentService.getAttachments(OBJECT_TYPE, objectId);
assertNotNull(sourceObjectAttachmentList);
assertEquals(1, sourceObjectAttachmentList.getAttachments().size());
assertEquals(fileId, destinationObjectAttachmentList.getAttachments().get(0).getId());
}

}

0 comments on commit e24056a

Please sign in to comment.