AttachmentListener attachmentListener = null;
try {
/* Add */
FeedReference feedRef = new FeedReference(DynamicDAO.save(fFactory.createFeed(null, new URI("http://www.feed1.com"))).getId());
NewsReference newsRef = new NewsReference(DynamicDAO.save(fFactory.createNews(null, feedRef.resolve(), new Date())).getId());
IAttachment attachment = fFactory.createAttachment(null, newsRef.resolve());
attachment.setLink(new URI("http://www.attachment.com"));
final boolean attachmentEvents[] = new boolean[3];
final AttachmentReference attachmentReference[] = new AttachmentReference[1];
attachmentListener = new AttachmentAdapter() {
@Override
public void entitiesAdded(Set<AttachmentEvent> events) {
for (AttachmentEvent event : events) {
assertTrue("Expected this Event to be Root Event", event.isRoot());
attachmentEvents[0] = true;
}
}
@Override
public void entitiesDeleted(Set<AttachmentEvent> events) {
for (AttachmentEvent event : events) {
assertTrue("Expected this Event to be Root Event", event.isRoot());
if (attachmentReference[0].references(event.getEntity()))
attachmentEvents[1] = true;
}
}
@Override
public void entitiesUpdated(Set<AttachmentEvent> events) {
for (AttachmentEvent event : events) {
assertTrue("Expected this Event to be Root Event", event.isRoot());
if (attachmentReference[0].references(event.getEntity()))
attachmentEvents[2] = true;
}
}
};
DynamicDAO.addEntityListener(IAttachment.class, attachmentListener);
attachmentReference[0] = new AttachmentReference(DynamicDAO.save(attachment).getId());
/* Update */
attachment = attachmentReference[0].resolve();
attachment.setType("MP3");
DynamicDAO.save(attachment);
/* Delete */
DynamicDAO.delete(attachmentReference[0].resolve());