/*
 * Copyright 2011 Wyona
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.wyona.org/licenses/APACHE-LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.wyona.yanel.impl.workflow;

import org.wyona.yanel.core.api.attributes.WorkflowableV1;
import org.wyona.yanel.core.workflow.Action;
import org.wyona.yanel.core.workflow.Workflow;
import org.wyona.yanel.core.workflow.WorkflowException;

import org.apache.log4j.Logger;

/**
 * Send notifications via email
 */
public class EmailAction implements Action {

    private static Logger log = Logger.getLogger(EmailAction.class);

    private String expression;

    /**
     * @see org.wyona.yanel.core.workflow.Action#setExpression(String)
     */
    public void setExpression(String expression) {
        this.expression = expression;
    }

    /**
     * @see org.wyona.yanel.core.workflow.Action#execute(WorkflowableV1, Workflow, String)
     */
    public void execute(WorkflowableV1 workflowable, Workflow workflow, String revision) throws WorkflowException {
        String[] emailAddresses = getEmailAddresses();
        if (emailAddresses != null) {
            for (int i = 0; i < emailAddresses.length; i++) {
                log.warn("Send email to: " + emailAddresses[i]);
            }
        } else {
            throw new WorkflowException("No email addresses!");
        }
    }

    /**
     * Get email addresses
     */
    private String[] getEmailAddresses() throws WorkflowException {
        if (expression != null) {
            String[] ea = expression.split(",");
            String[] emails = new String[ea.length]; // TODO: Make more fault tolerant
            for(int i = 0; i < ea.length; i++) {
                if (ea[i].indexOf("@") < 1) {
                    throw new WorkflowException("No such email address: " + ea[i]);
                } else {
                    emails[i] = ea[i].trim();
                }
            }
            return emails;
        } else {
            return null;
        }
    }
}

@@ -637,16 +637,54 @@
 kflow;%0A%0A
+import org.wyona.yanel.core.Resource;%0A
 import o
@@ -1561,24 +1561,237 @@
 ddresses();%0A
+        String from = getSenderAddress(workflowable, workflow, revision);%0A        String subject = getSubject(workflowable, workflow, revision);%0A        String content = getText(workflowable, workflow, revision);%0A
         if (
@@ -1902,12 +1902,12 @@
 log.
-warn
+info
 (%22Se
@@ -1939,24 +1939,259 @@
 resses%5Bi%5D);%0A
+                try %7B%0A                    org.wyona.yanel.core.util.MailUtil.send(from, emailAddresses%5Bi%5D, subject, content);%0A                %7D catch(Exception e) %7B%0A                    throw new WorkflowException(e);%0A                %7D%0A
             
@@ -2937,11 +2937,911 @@
  %7D%0A    %7D
+%0A%0A    /**%0A     * Get from/sender address%0A     */%0A    protected String getSenderAddress(WorkflowableV1 workflowable, Workflow workflow, String revision) throws WorkflowException %7B%0A        return %22contact@wyona.com%22;%0A    %7D%0A%0A    /**%0A     * Get subject%0A     */%0A    protected String getSubject(WorkflowableV1 workflowable, Workflow workflow, String revision) throws WorkflowException %7B%0A        Resource resource = (Resource) workflowable;%0A        return %22%5BYanel%5D The workflow state of the resource '%22 + resource.getPath() + %22' has been changed: %22 + workflowable.getWorkflowState(revision);%0A    %7D%0A%0A    /**%0A     * Get text%0A     */%0A    protected String getText(WorkflowableV1 workflowable, Workflow workflow, String revision) throws WorkflowException %7B%0A        Resource resource = (Resource) workflowable;%0A        return %22Please review the workflow state change of the resource: %22 + resource.getPath();%0A    %7D
 %0A%7D%0A

send emails