Search in sources :

Example 1 with EMail

use of de.metas.email.EMail in project metasfresh-webui-api by metasfresh.

the class MailRestController method changeEmail.

private void changeEmail(final WebuiEmail email, final WebuiEmailBuilder newEmailBuilder, final JSONDocumentChangedEvent event) {
    if (!event.isReplace()) {
        throw new AdempiereException("Unsupported event").setParameter("event", event);
    }
    final String fieldName = event.getPath();
    if (PATCH_FIELD_To.equals(fieldName)) {
        @SuppressWarnings("unchecked") final List<Object> jsonTo = (List<Object>) event.getValue();
        @SuppressWarnings("unchecked") final LookupValuesList to = jsonTo.stream().map(mapObj -> (Map<String, Object>) mapObj).map(map -> JSONLookupValue.integerLookupValueFromJsonMap(map)).collect(LookupValuesList.collect());
        newEmailBuilder.to(to);
    } else if (PATCH_FIELD_Subject.equals(fieldName)) {
        final String subject = (String) event.getValue();
        newEmailBuilder.subject(subject);
    } else if (PATCH_FIELD_Message.equals(fieldName)) {
        final String message = (String) event.getValue();
        newEmailBuilder.message(message);
    } else if (PATCH_FIELD_Attachments.equals(fieldName)) {
        @SuppressWarnings("unchecked") final List<Object> jsonAttachments = (List<Object>) event.getValue();
        @SuppressWarnings("unchecked") final LookupValuesList attachments = jsonAttachments.stream().map(mapObj -> (Map<String, Object>) mapObj).map(map -> JSONLookupValue.stringLookupValueFromJsonMap(map)).collect(LookupValuesList.collect());
        newEmailBuilder.attachments(attachments);
    } else if (PATCH_FIELD_TemplateId.equals(fieldName)) {
        @SuppressWarnings("unchecked") final LookupValue templateId = JSONLookupValue.integerLookupValueFromJsonMap((Map<String, Object>) event.getValue());
        applyTemplate(email, newEmailBuilder, templateId);
    } else {
        throw new AdempiereException("Unsupported event path").setParameter("event", event).setParameter("fieldName", fieldName).setParameter("availablePaths", PATCH_FIELD_ALL);
    }
}
Also used : FillMandatoryException(org.adempiere.exceptions.FillMandatoryException) PathVariable(org.springframework.web.bind.annotation.PathVariable) RequestParam(org.springframework.web.bind.annotation.RequestParam) WebuiEmailRemovedEvent(de.metas.ui.web.mail.WebuiMailRepository.WebuiEmailRemovedEvent) IntegerLookupValue(de.metas.ui.web.window.datatypes.LookupValue.IntegerLookupValue) ITrx(org.adempiere.ad.trx.api.ITrx) Env(org.compiere.util.Env) Autowired(org.springframework.beans.factory.annotation.Autowired) UnaryOperator(java.util.function.UnaryOperator) DocumentCollection(de.metas.ui.web.window.model.DocumentCollection) ApiOperation(io.swagger.annotations.ApiOperation) Map(java.util.Map) JSONEmail(de.metas.ui.web.mail.json.JSONEmail) PostMapping(org.springframework.web.bind.annotation.PostMapping) ImmutableSet(com.google.common.collect.ImmutableSet) EMail(de.metas.email.EMail) NonNull(lombok.NonNull) Set(java.util.Set) EventListener(org.springframework.context.event.EventListener) RestController(org.springframework.web.bind.annotation.RestController) JSONLookupValuesList(de.metas.ui.web.window.datatypes.json.JSONLookupValuesList) UserSession(de.metas.ui.web.session.UserSession) Services(org.adempiere.util.Services) List(java.util.List) I_AD_Client(org.compiere.model.I_AD_Client) Stream(java.util.stream.Stream) DocumentPrint(de.metas.ui.web.window.model.DocumentCollection.DocumentPrint) IMailBL(de.metas.email.IMailBL) ApiModel(io.swagger.annotations.ApiModel) LogManager(de.metas.logging.LogManager) EMailSentStatus(de.metas.email.EMailSentStatus) WebConfig(de.metas.ui.web.config.WebConfig) DocumentPath(de.metas.ui.web.window.datatypes.DocumentPath) JSONDocumentChangedEvent(de.metas.ui.web.window.datatypes.json.JSONDocumentChangedEvent) Supplier(com.google.common.base.Supplier) IUserBL(org.adempiere.user.api.IUserBL) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) IUserDAO(org.adempiere.user.api.IUserDAO) PatchMapping(org.springframework.web.bind.annotation.PatchMapping) RequestBody(org.springframework.web.bind.annotation.RequestBody) ImmutableList(com.google.common.collect.ImmutableList) GetMapping(org.springframework.web.bind.annotation.GetMapping) LookupValuesList(de.metas.ui.web.window.datatypes.LookupValuesList) I_AD_User(org.compiere.model.I_AD_User) JSONLookupValue(de.metas.ui.web.window.datatypes.json.JSONLookupValue) LookupValue(de.metas.ui.web.window.datatypes.LookupValue) MADBoilerPlate(de.metas.letters.model.MADBoilerPlate) Properties(java.util.Properties) Logger(org.slf4j.Logger) JSONDocumentPath(de.metas.ui.web.window.datatypes.json.JSONDocumentPath) IOException(java.io.IOException) WebuiEmailBuilder(de.metas.ui.web.mail.WebuiEmail.WebuiEmailBuilder) BoilerPlateContext(de.metas.letters.model.MADBoilerPlate.BoilerPlateContext) AdempiereException(org.adempiere.exceptions.AdempiereException) IClientDAO(org.adempiere.service.IClientDAO) Check(de.metas.printing.esb.base.util.Check) EMailAttachment(de.metas.email.EMailAttachment) JSONEmailRequest(de.metas.ui.web.mail.json.JSONEmailRequest) MultipartFile(org.springframework.web.multipart.MultipartFile) AdempiereException(org.adempiere.exceptions.AdempiereException) JSONLookupValuesList(de.metas.ui.web.window.datatypes.json.JSONLookupValuesList) LookupValuesList(de.metas.ui.web.window.datatypes.LookupValuesList) JSONLookupValuesList(de.metas.ui.web.window.datatypes.json.JSONLookupValuesList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) LookupValuesList(de.metas.ui.web.window.datatypes.LookupValuesList) Map(java.util.Map) IntegerLookupValue(de.metas.ui.web.window.datatypes.LookupValue.IntegerLookupValue) JSONLookupValue(de.metas.ui.web.window.datatypes.json.JSONLookupValue) LookupValue(de.metas.ui.web.window.datatypes.LookupValue)

Example 2 with EMail

use of de.metas.email.EMail in project metasfresh-webui-api by metasfresh.

the class MailRestController method sendEmail.

private WebuiEmail sendEmail(final WebuiEmail webuiEmail) {
    final String emailId = webuiEmail.getEmailId();
    // 
    // Create the email object
    final I_AD_Client adClient = Services.get(IClientDAO.class).retriveClient(Env.getCtx(), userSession.getAD_Client_ID());
    final String mailCustomType = null;
    final I_AD_User from = Services.get(IUserDAO.class).retrieveUser(webuiEmail.getFrom().getIdAsInt());
    final List<String> toList = extractEMailAddreses(webuiEmail.getTo()).collect(ImmutableList.toImmutableList());
    if (toList.isEmpty()) {
        throw new FillMandatoryException("To");
    }
    final String to = toList.get(0);
    final String subject = webuiEmail.getSubject();
    final String message = webuiEmail.getMessage();
    final boolean html = false;
    final EMail email = Services.get(IMailBL.class).createEMail(adClient, mailCustomType, from, to, subject, message, html);
    toList.stream().skip(1).forEach(email::addTo);
    webuiEmail.getAttachments().stream().map(webuiAttachment -> {
        final byte[] content = mailAttachmentsRepo.getAttachmentAsByteArray(emailId, webuiAttachment);
        return EMailAttachment.of(webuiAttachment.getDisplayName(), content);
    }).forEach(email::addAttachment);
    // 
    // Actually send the email
    final EMailSentStatus sentStatus = email.send();
    if (!sentStatus.isSentOK()) {
        throw new AdempiereException("Failed sending the email: " + sentStatus.getSentMsg());
    }
    // 
    // Delete temporary attachments
    mailAttachmentsRepo.deleteAttachments(emailId, webuiEmail.getAttachments());
    // Mark the webui email as sent
    return webuiEmail.toBuilder().sent(true).build();
}
Also used : FillMandatoryException(org.adempiere.exceptions.FillMandatoryException) PathVariable(org.springframework.web.bind.annotation.PathVariable) RequestParam(org.springframework.web.bind.annotation.RequestParam) WebuiEmailRemovedEvent(de.metas.ui.web.mail.WebuiMailRepository.WebuiEmailRemovedEvent) IntegerLookupValue(de.metas.ui.web.window.datatypes.LookupValue.IntegerLookupValue) ITrx(org.adempiere.ad.trx.api.ITrx) Env(org.compiere.util.Env) Autowired(org.springframework.beans.factory.annotation.Autowired) UnaryOperator(java.util.function.UnaryOperator) DocumentCollection(de.metas.ui.web.window.model.DocumentCollection) ApiOperation(io.swagger.annotations.ApiOperation) Map(java.util.Map) JSONEmail(de.metas.ui.web.mail.json.JSONEmail) PostMapping(org.springframework.web.bind.annotation.PostMapping) ImmutableSet(com.google.common.collect.ImmutableSet) EMail(de.metas.email.EMail) NonNull(lombok.NonNull) Set(java.util.Set) EventListener(org.springframework.context.event.EventListener) RestController(org.springframework.web.bind.annotation.RestController) JSONLookupValuesList(de.metas.ui.web.window.datatypes.json.JSONLookupValuesList) UserSession(de.metas.ui.web.session.UserSession) Services(org.adempiere.util.Services) List(java.util.List) I_AD_Client(org.compiere.model.I_AD_Client) Stream(java.util.stream.Stream) DocumentPrint(de.metas.ui.web.window.model.DocumentCollection.DocumentPrint) IMailBL(de.metas.email.IMailBL) ApiModel(io.swagger.annotations.ApiModel) LogManager(de.metas.logging.LogManager) EMailSentStatus(de.metas.email.EMailSentStatus) WebConfig(de.metas.ui.web.config.WebConfig) DocumentPath(de.metas.ui.web.window.datatypes.DocumentPath) JSONDocumentChangedEvent(de.metas.ui.web.window.datatypes.json.JSONDocumentChangedEvent) Supplier(com.google.common.base.Supplier) IUserBL(org.adempiere.user.api.IUserBL) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) IUserDAO(org.adempiere.user.api.IUserDAO) PatchMapping(org.springframework.web.bind.annotation.PatchMapping) RequestBody(org.springframework.web.bind.annotation.RequestBody) ImmutableList(com.google.common.collect.ImmutableList) GetMapping(org.springframework.web.bind.annotation.GetMapping) LookupValuesList(de.metas.ui.web.window.datatypes.LookupValuesList) I_AD_User(org.compiere.model.I_AD_User) JSONLookupValue(de.metas.ui.web.window.datatypes.json.JSONLookupValue) LookupValue(de.metas.ui.web.window.datatypes.LookupValue) MADBoilerPlate(de.metas.letters.model.MADBoilerPlate) Properties(java.util.Properties) Logger(org.slf4j.Logger) JSONDocumentPath(de.metas.ui.web.window.datatypes.json.JSONDocumentPath) IOException(java.io.IOException) WebuiEmailBuilder(de.metas.ui.web.mail.WebuiEmail.WebuiEmailBuilder) BoilerPlateContext(de.metas.letters.model.MADBoilerPlate.BoilerPlateContext) AdempiereException(org.adempiere.exceptions.AdempiereException) IClientDAO(org.adempiere.service.IClientDAO) Check(de.metas.printing.esb.base.util.Check) EMailAttachment(de.metas.email.EMailAttachment) JSONEmailRequest(de.metas.ui.web.mail.json.JSONEmailRequest) MultipartFile(org.springframework.web.multipart.MultipartFile) I_AD_User(org.compiere.model.I_AD_User) EMailSentStatus(de.metas.email.EMailSentStatus) FillMandatoryException(org.adempiere.exceptions.FillMandatoryException) EMail(de.metas.email.EMail) IUserDAO(org.adempiere.user.api.IUserDAO) AdempiereException(org.adempiere.exceptions.AdempiereException) IClientDAO(org.adempiere.service.IClientDAO) I_AD_Client(org.compiere.model.I_AD_Client) IMailBL(de.metas.email.IMailBL)

Aggregations

Supplier (com.google.common.base.Supplier)2 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 EMail (de.metas.email.EMail)2 EMailAttachment (de.metas.email.EMailAttachment)2 EMailSentStatus (de.metas.email.EMailSentStatus)2 IMailBL (de.metas.email.IMailBL)2 MADBoilerPlate (de.metas.letters.model.MADBoilerPlate)2 BoilerPlateContext (de.metas.letters.model.MADBoilerPlate.BoilerPlateContext)2 LogManager (de.metas.logging.LogManager)2 Check (de.metas.printing.esb.base.util.Check)2 WebConfig (de.metas.ui.web.config.WebConfig)2 WebuiEmailBuilder (de.metas.ui.web.mail.WebuiEmail.WebuiEmailBuilder)2 WebuiEmailRemovedEvent (de.metas.ui.web.mail.WebuiMailRepository.WebuiEmailRemovedEvent)2 JSONEmail (de.metas.ui.web.mail.json.JSONEmail)2 JSONEmailRequest (de.metas.ui.web.mail.json.JSONEmailRequest)2 UserSession (de.metas.ui.web.session.UserSession)2 DocumentPath (de.metas.ui.web.window.datatypes.DocumentPath)2 LookupValue (de.metas.ui.web.window.datatypes.LookupValue)2 IntegerLookupValue (de.metas.ui.web.window.datatypes.LookupValue.IntegerLookupValue)2