Current code:
package com.company.asss.view.newsitem;
import com.company.asss.entity.NewsItem;
import com.company.asss.view.main.MainView;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.router.Route;
import io.jmix.core.Resources;
import io.jmix.email.EmailAttachment;
import io.jmix.email.EmailInfo;
import io.jmix.email.EmailInfoBuilder;
import io.jmix.email.Emailer;
import io.jmix.flowui.Dialogs;
import io.jmix.flowui.action.DialogAction;
import io.jmix.flowui.model.DataContext;
import io.jmix.flowui.view.*;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.IOException;
import java.io.InputStream;
@Route(value = “newsItems/:id”, layout = MainView.class)
@ViewController(“NewsItem.detail”)
@ViewDescriptor(“news-item-detail-view.xml”)
@EditedEntityContainer(“newsItemDc”)
public class NewsItemDetailView extends StandardDetailView {
private boolean justCreated;
@Subscribe
public void onInitEntity(InitEntityEvent<NewsItem> event) {
justCreated = true;
}
private static final Logger log = LoggerFactory.getLogger(NewsItemDetailView.class);
@Autowired
protected Dialogs dialogs;
@Subscribe(target = Target.DATA_CONTEXT)
public void onPostCommit(DataContext.PostSaveEvent event) {
if (justCreated) {
dialogs.createOptionDialog()
.withHeader("Email")
.withText("Send the news item by email?")
.withActions(
new DialogAction(DialogAction.Type.YES) {
@Override
public void actionPerform(Component component) {
try {
sendByEmail();
} catch (IOException e) {
log.error("Error sending email", e);
}
}
},
new DialogAction(DialogAction.Type.NO)
)
.open();
}
}
@Autowired
private Emailer emailer;
@Autowired
protected Resources resources;
private void sendByEmail() throws IOException {
InputStream resourceAsStream = resources.getResourceAsStream("META-INF/resources/icons/tegasre.png");
if (resourceAsStream == null) {
throw new IOException("Resource 'email/ex1/logo.png' not found.");
}
byte[] bytes = IOUtils.toByteArray(resourceAsStream);
EmailAttachment emailAtt = new EmailAttachment(bytes,
"logo.png", "logoId");
NewsItem newsItem = getEditedEntity();
EmailInfo emailInfo = EmailInfoBuilder.create()
.setAddresses("john.doe@company.com,jane.doe@company.com")
.setSubject(newsItem.getCaption())
.setFrom(null)
.setBody(newsItem.getContent())
.setAttachments(emailAtt)
.build();
emailer.sendEmailAsync(emailInfo);
}
}