/**
 * @author dgeorge
 * 
 * $Id: EmailActionImpl.java,v 1.6 2005-09-16 15:52:57 georgeda Exp $
 * 
 * $Log: not supported by cvs2svn $ Revision 1.5 2005/09/14 12:45:06 georgeda
 * Renamed mail utility
 * 
 * Revision 1.4 2005/09/13 20:26:35 georgeda More cleanup Revision 1.3
 * 2005/09/13 19:18:46 georgeda Email updates and CSM integration Revision 1.2
 * 2005/09/12 18:22:11 georgeda Curation changes and addition of e-mail
 */
package gov.nih.nci.camod.service.impl;

import gov.nih.nci.camod.Constants;
import gov.nih.nci.camod.domain.*;
import gov.nih.nci.camod.service.CurateableAction;
import gov.nih.nci.camod.util.MailUtil;
import gov.nih.nci.camod.webapp.form.AnimalModelStateForm;

import java.util.Map;

/**
 * 
 * Action called by the curation workflow that sends an e-mail
 *
 */
public class EmailActionImpl extends BaseCurateableAction {

    /**
     * Protected constructor. Use the create method to get an instance.
     */
    protected EmailActionImpl() {
    }

    /**
     * Create an instance of this curatable action
     * 
     * @param inArgs
     *            the arguments to the action
     * @param inObject
     *            the curatable object to get information from
     */
    public CurateableAction create() {
        return new EmailActionImpl();
    }

    /**
     * Execute the curatable action
     * 
     * @param inArgs
     *            the arguments to the action
     * @param inObject
     *            the curatable object to get information from
     */
    public void execute(Map inArgs, Curateable inObject) {

        log.trace("Entering execute");

        log.debug("Arguments: " + inArgs);

        if (inArgs.containsKey(Constants.FORMDATA)) {

            try {
                AnimalModelStateForm theForm = (AnimalModelStateForm) inArgs.get(Constants.FORMDATA);

                AnimalModel theAnimalModel = (AnimalModel) inObject;

                Log theLog = LogManagerSingleton.instance().getCurrentByModel(theAnimalModel);

                Person thePerson = (Person) theLog.getSubmitter();

                // Get the e-mail for the user
                String[] theRecipients = { UserManagerSingleton.instance().getEmailForUser(thePerson.getUsername()) };

                // Build the message text
                String theMailSubject = "";
                String theMailText = theForm.getComment();

                // Customize the text based on the action.
                // TODO: Should be centralized.
                if (theForm.getEvent().equals(Constants.Admin.Actions.ASSIGN_SCREENER)) {
                    theMailSubject = "You have been assigned screener for the following model: "
                            + theForm.getModelDescriptor();
                } else if (theForm.getEvent().equals(Constants.Admin.Actions.ASSIGN_EDITOR)) {
                    theMailSubject = "You have been assigned editor for the following model: "
                            + theForm.getModelDescriptor();
                } else if (theForm.getEvent().equals(Constants.Admin.Actions.NEED_MORE_INFO)) {
                    theMailSubject = "The editor is requesting more information for the following model: "
                            + theForm.getModelDescriptor();
                } else if (theForm.getEvent().equals(Constants.Admin.Actions.REJECT)) {
                    theMailSubject = "The following model has been rejected: " + theForm.getModelDescriptor();
                } else if (theForm.getEvent().equals(Constants.Admin.Actions.APPROVE)) {
                    theMailSubject = "The following model has been approved: " + theForm.getModelDescriptor();
                } else if (theForm.getEvent().equals(Constants.Admin.Actions.COMPLETE)) {
                    theMailSubject = "The following model has been completed: " + theForm.getModelDescriptor();
                } else {
                    theMailSubject = "The following model has changed: " + theForm.getModelDescriptor();
                }

                if (theRecipients.length > 0) {
                    MailUtil.sendMail(theRecipients, theMailSubject, theMailText, UserManagerSingleton.instance()
                            .getEmailForController());
                } else {
                    log.warn("No e-mail address assigned to user: " + thePerson.getUsername());
                }

            } catch (Exception e) {
                log.error("Error sending e-mail:", e);
            }
        } else {
            log.error("Missing argument formdata");
        }
        log.trace("Exiting execute");
    }
}
@@ -53,17 +53,17 @@
 ava,v 1.
-6
+7
  2005-09
@@ -68,18 +68,18 @@
 09-1
-6 15:52:57
+9 13:09:24
  geo
@@ -129,16 +129,101 @@
 vs2svn $
+%0A * Revision 1.6  2005/09/16 15:52:57  georgeda%0A * Changes due to manager re-write%0A *
  Revisio
@@ -870,16 +870,17 @@
 -mail%0A *
+ 
 %0A */%0Apub
@@ -1907,24 +1907,74 @@
 FORMDATA);%0A%0A
+                // Get the various domain objects%0A
             
@@ -2030,17 +2030,16 @@
 Object;%0A
-%0A
         
@@ -2125,17 +2125,16 @@
 Model);%0A
-%0A
         
@@ -2235,16 +2235,25 @@
 for the 
+assigned 
 user%0A   
@@ -3373,32 +3373,399 @@
 delDescriptor();
+%0A%0A                    // Special case; we're sending out e-mail to someout outside%0A                    // the curation process%0A                    Person theSubmitter = (Person) theAnimalModel.getSubmitter();%0A                    theRecipients = new String%5B%5D %7B UserManagerSingleton.instance().getEmailForUser(%0A                            theSubmitter.getUsername()) %7D;
 %0A               

Send e-mail to submitter for certain states