/*
 * RequestServlet.java
 *
 * Created on 4 de Março de 2005, 18:19
 */

package siprp.pagina;

import java.io.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

import org.apache.commons.fileupload.*;

/**
 *
 * @author  psantos
 */
public class RecruitServlet extends MailerServlet
{
	private final static String REC_FUNCAO = "rec_funcao";
	private final static String REC_NOME = "rec_nome";
	private final static String REC_MORADA = "rec_morada";
	private final static String REC_TELEFONE = "rec_telefone";
	private final static String REC_EMAIL = "rec_email";
	private final static String REC_CV = "rec_cv";

	private final static String[] FORM_FIELDS = new String[]{
								REC_FUNCAO, REC_NOME, REC_MORADA, 
								REC_TELEFONE, REC_EMAIL, REC_CV
							};

	public void doPost( HttpServletRequest req, HttpServletResponse res )
		throws IOException
	{
		Hashtable parameters;
		String fileName = "";
		String type = "";
		byte file[] = new byte[0];
		boolean isMultipart = FileUpload.isMultipartContent( req );
		
		ServletOutputStream out = res.getOutputStream();
		res.setContentType( "text/html" );
		
		if( isMultipart )
		{
			parameters = new Hashtable();
			DiskFileUpload upload = new DiskFileUpload();
			upload.setSizeThreshold(1000000);
			upload.setSizeMax(1000000);
			List items;
			try
			{
				items = upload.parseRequest(req);
			}
			catch( FileUploadException ex )
			{
				out.println( showResultPage( false, "mail/pedido_enviado.html", "mail/pedido_nao_enviado.html" ) );
				return;
			}
			Iterator iter = items.iterator();
			while( iter.hasNext() )
			{
				FileItem item = (FileItem) iter.next();

				if (item.isFormField())
				{
					String name = item.getFieldName();
					String value = item.getString();
					parameters.put( name, value );
				}
				else
				{
					String fieldName = item.getFieldName();
					fileName = item.getName();
					file = item.get();
					if( file == null || file.length == 0 )
					{
						isMultipart = false;
					}
					type = item.getContentType();
				}
			}
		}
		else
		{
			parameters = parseParameters( new Hashtable( req.getParameterMap() ) );
		}
		String content = createContent( parameters, FORM_FIELDS, "mail/envio_cv.txt" );

		String email = DEFAULT_EMAIL;
		if( PROPERTIES != null && PROPERTIES.containsKey( EMAIL_RECRUTAMENTO ) )
		{
			email = ( String ) PROPERTIES.get( EMAIL_RECRUTAMENTO );
		}
		String from = email;
		String destination = email;
		String subject = "Envio de CV através do site www.siprp.pt";
		// manda mail
		boolean success;
		String nome = ( String ) parameters.get( REC_NOME ); 
		String telefone = ( String ) parameters.get( REC_TELEFONE );
		String mail = ( String ) parameters.get( REC_EMAIL );
		if( nome == null || nome.trim().length() == 0 || telefone == null || telefone.trim().length() == 0 ||
			mail == null || mail.trim().length() == 0 || mail.indexOf( '@' ) == -1 )
		{
System.out.println( "nome: " + nome + " teefone: "  + telefone + " email : " + email  );
			success = false;
		}
		else if( isMultipart )
		{
System.out.println( "Multipart" );
			success = sendMail( from, destination, subject, content, false, fileName, file, type );
		}
		else
		{
System.out.println( "Normal" );
			success = sendMail( from, destination, subject, content, false );
		}

		// mostra pagina correspondente
		
		out.println( showResultPage( success, "mail/cv_enviado.html", "mail/cv_nao_enviado.html" ) );
	}
}
