/**
 * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.liferay.sampleicefaces.bean.backing;

import java.io.File;

import javax.faces.context.FacesContext;
import javax.mail.internet.InternetAddress;
import javax.portlet.PortletPreferences;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.liferay.sampleicefaces.bean.model.Applicant;
import com.liferay.util.bridges.jsf.common.ActionOutcomes;
import com.liferay.util.bridges.jsf.common.FacesMessageUtil;
import com.liferay.util.bridges.jsf.common.JSFPortletUtil;
import com.liferay.util.mail.MailEngine;

/**
 * <a href="ApplicantForm.java.html"><b><i>View Source</i></b></a>
 *
 * <p>
 * Note that the email will only be sent if the portlet is configured to work
 * with your mail server as defined in the META-INF/context.xml file or
 * $JBOSS_HOME/server/default/deploy/mail-service.xml file. Also, the file
 * upload directory is defined in the WEB-INF/web.xml file.
 * </p>
 *
 * @author Neil Griffin
 *
 */
public class ApplicantForm {

	public ApplicantForm() {

		// Store the portlet preferences as a bean property because of ICE-1625.
		// When using normal JSF, this constructor will get called each time a
		// request is made. This is a little inefficient, but it's a coding
		// tradeoff to make things work with both normal JSF and ICEfaces 1.6.0.

		_portletPreferences = JSFPortletUtil.getPortletPreferences(
			FacesContext.getCurrentInstance());
	}

	public Applicant getApplicant() {
		return _applicant;
	}

	public void setApplicant(Applicant applicant) {
		_applicant = applicant;
	}

	public String submit() {
		String actionOutcome = ActionOutcomes.SUCCESS;

		FacesContext facesContext = FacesContext.getCurrentInstance();

		String toEmailAddress = _portletPreferences.getValue(
			"recipientEmailAddress", null);

		try {
			InternetAddress fromInternetAddress = new InternetAddress(
				_applicant.getEmailAddress());
			InternetAddress[] toInternetAddresses =
				new InternetAddress[] {new InternetAddress(toEmailAddress)};

			String subject =
				_applicant.getFirstName() + " " + _applicant.getLastName() + " has applied for a job";

			StringBuffer body = new StringBuffer();

			body.append("First Name: " + _applicant.getFirstName() + "\n");
			body.append("Last Name: " + _applicant.getLastName() + "\n");
			body.append("Date Of Birth: " + _applicant.getDateOfBirth() + "\n");
			body.append("Phone Number: " + _applicant.getPhoneNumber() + "\n");
			body.append("Phone Type: " + _applicant.getPhoneNumberType() + "\n");
			body.append("Fax Number: " + _applicant.getFaxNumber() + "\n");
			body.append("Comments:\n");
			body.append(_applicant.getComments());

			if (_log.isDebugEnabled()) {
				_log.debug("Sending email to " + toEmailAddress);
			}

			MailEngine.send(
				fromInternetAddress, toInternetAddresses, null, null, null,
				subject, body.toString(), false, null, null, null,
				new File[] {_applicant.getFile()});

			FacesMessageUtil.info(
				facesContext,
				"thank-you-for-applying-for-a-job-with-our-organization",
				_applicant.getFirstName());

			_applicant.clear();
		}
		catch (Exception e) {
			FacesMessageUtil.error(
				facesContext,
				"an-unexpected-error-occurred-while-sending-your-message");

			if (_log.isErrorEnabled()) {
				_log.error(e.getMessage(), e);
			}

			actionOutcome = ActionOutcomes.FAILURE;
		}

		return actionOutcome;
	}


	private static Log _log = LogFactory.getLog(ApplicantForm.class);

	private Applicant _applicant;
	private PortletPreferences _portletPreferences;

}