public int execute()

in plugins/mail/src/main/java/org/apache/unomi/plugins/mail/actions/SendMailAction.java [75:165]


    public int execute(Action action, Event event) {
        String notifType = (String) action.getParameterValues().get("notificationType");
        String notifTypeId = (String) action.getParameterValues().get("notificationTypeId");
        Boolean notifyOnce = (Boolean) action.getParameterValues().get("notifyOncePerProfile");
        String from = (String) action.getParameterValues().get("from");
        String to = (String) action.getParameterValues().get("to");
        String cc = (String) action.getParameterValues().get("cc");
        String bcc = (String) action.getParameterValues().get("bcc");
        String subject = (String) action.getParameterValues().get("subject");
        String template = (String) action.getParameterValues().get("template");

        if (notifType == null) {
            notifType = "default";
        }
        if (notifTypeId == null) {
            notifTypeId = subject;
        }
        if (notifyOnce == null) {
            notifyOnce = false;
        }

        Map profileNotif = (HashMap) event.getProfile().getSystemProperties().get("notificationAck");
        if (profileNotif != null && profileNotif.get(notifType) != null && ((HashMap) profileNotif.get(notifType)).get(notifTypeId) != null) {
            Integer notifTypeAck = (Integer) ((HashMap) profileNotif.get(notifType) ).get(notifTypeId);
            if(notifyOnce.booleanValue() && notifTypeAck > 0){
                LOGGER.info("Notification {} already sent for the profile {}", notifType, event.getProfileId());
                return EventService.NO_CHANGE;
            }else{
                ((HashMap) profileNotif.get(notifType) ).put(notifTypeId, notifTypeAck+1);
            }
        } else {
            if(profileNotif == null){
                profileNotif = new HashMap();
            }
            profileNotif.put(notifType, profileNotif.get(notifType)!=null?profileNotif.get(notifType):new HashMap());
            Integer notifTypeAck = (Integer) ((HashMap) profileNotif.get(notifType) ).get(notifTypeId);
            if(notifTypeAck == null){
                ((HashMap) profileNotif.get(notifType) ).put(notifTypeId, 1);
            }
        }

        event.getProfile().setSystemProperty("notificationAck", profileNotif);
        event.getProfile().setSystemProperty("lastUpdated", new Date());

        persistenceService.update(event.getProfile(), Profile.class, "systemProperties", event.getProfile().getSystemProperties());

        ST stringTemplate = new ST(template, '$', '$');
        stringTemplate.add("profile", event.getProfile());
        stringTemplate.add("event", event);
        // load your HTML email template
        String htmlEmailTemplate = stringTemplate.render();

        // define you base URL to resolve relative resource locations
        try {
            new URL("http://www.apache.org");
        } catch (MalformedURLException e) {
            //
        }

        // create the email message
        HtmlEmail email = new ImageHtmlEmail();
        // email.setDataSourceResolver(new DataSourceResolverImpl(url));
        email.setHostName(mailServerHostName);
        email.setSmtpPort(mailServerPort);
        email.setAuthenticator(new DefaultAuthenticator(mailServerUsername, mailServerPassword));
        email.setSSLOnConnect(mailServerSSLOnConnect);
        try {
            email.addTo(to);
            email.setFrom(from);
            if (cc != null && cc.length() > 0) {
                email.addCc(cc);
            }
            if (bcc != null && bcc.length() > 0) {
                email.addBcc(bcc);
            }
            email.setSubject(subject);

            // set the html message
            email.setHtmlMsg(htmlEmailTemplate);

            // set the alternative message
            email.setTextMsg("Your email client does not support HTML messages");

            // send the email
            email.send();
        } catch (EmailException e) {
            LOGGER.error("Cannot send mail",e);
        }

        return EventService.NO_CHANGE;
    }