package org.gcube.portlets.user.moving; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.portlet.ActionRequest; import javax.portlet.ActionResponse; import javax.portlet.PortletException; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; import org.gcube.common.portal.PortalContext; import com.liferay.portal.kernel.log.LogFactoryUtil; import com.liferay.util.bridges.mvc.MVCPortlet; /** * Portlet implementation class CompileForm */ public class CompileForm extends MVCPortlet { private static com.liferay.portal.kernel.log.Log _log = LogFactoryUtil.getLog(CompileForm.class); private static final String MAIL_FROM = "MOVING Gateway "; private static final String MAIL_SUBJECT = "MOVING EU Multi-Actor Platform: email confirmation"; private static final String MAIL_BODY = "Dear Sir/Madam,\n" + "Thank you for expressing your interest to join the MOVING EU-level Multi-Actor Platform (MAP). The MOVING team will carefully review the information provided and assess the suitability of your involvement to form a dynamic and relevant community.\n" + "Please receive below a copy of the information submitted. \n" + "\n" + "Best regards,\n" + "MOVING team\n"; private String mailServiceHost = "localhost"; private String mailServicePort = "25"; @Override public void render(RenderRequest renderRequest, RenderResponse renderResponse) { Connection conn = null; try { conn = DatabaseConnection.getInstance("/"+PortalContext.getConfiguration().getInfrastructureName()).getConnection(); _log.info("Trying queries for getting combo values from DB"); List organisation_types = getOrganisationTypes(conn); List main_motivations = getMainMotivations(conn); List areas_of_expertise = getAreasOfExpertise(conn); List degrees_of_participation = getDgreesOfParticipation(conn); _log.info("Succesfully got combo values from DB, organisation_types found: " +organisation_types.size()); renderRequest.setAttribute("organisation_types", organisation_types); renderRequest.setAttribute("main_motivations", main_motivations); renderRequest.setAttribute("areas_of_expertise", areas_of_expertise); renderRequest.setAttribute("degrees_of_participation", degrees_of_participation); super.render(renderRequest, renderResponse); } catch (Exception e) { _log.error("Something wrong with Databaseconnection or with queries while getting combo types", e); } } public void submittedForm(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException { String name = actionRequest.getParameter("name"); String surname = actionRequest.getParameter("surname"); String organisation = actionRequest.getParameter("organisation"); String organisationType = actionRequest.getParameter("organisationType"); String emailAddress = actionRequest.getParameter("emailAddress"); String[] itemsAreaOfExpertiseSelectValues = actionRequest.getParameterValues("areaOfExpertiseSelect"); String areaOfExpertise = ""; for (int i = 0; i < itemsAreaOfExpertiseSelectValues.length; i++) { areaOfExpertise += itemsAreaOfExpertiseSelectValues[i]; areaOfExpertise += "; "; } String country = actionRequest.getParameter("country"); String participatedInActivitiesString = (String) actionRequest.getParameter("participatedInActivities"); boolean participatedInActivities = Boolean.parseBoolean(participatedInActivitiesString); String mainMotivation = actionRequest.getParameter("mainMotivation"); String textareaMotivation = actionRequest.getParameter("textareaMotivation"); String elaborated_expertise = actionRequest.getParameter("textareaExperience"); String degree_of_participation = actionRequest.getParameter("commitment"); _log.debug("name:" + name); _log.debug("surname:" + surname); _log.debug("organisation:" + organisation); _log.debug("organisationType:" + organisationType); _log.debug("email:" + emailAddress); _log.debug("country:" + country); _log.debug("participatedInActivities:" + participatedInActivities); _log.debug("mainMotivation:" + mainMotivation); _log.debug("textareaMotivation:\n" + textareaMotivation+"\n"); _log.debug("itemsAreaOfExpertise (Multi):\n"); for (int i = 0; i < itemsAreaOfExpertiseSelectValues.length; i++) { _log.debug(itemsAreaOfExpertiseSelectValues[i]); } _log.debug("textareaExperience:\n" + elaborated_expertise+"\n"); _log.debug("degrees_of_participation: "+ degree_of_participation); _log.info("Got form compiled, inserting in DB."); try { Connection conn = DatabaseConnection.getInstance("/"+PortalContext.getConfiguration().getInfrastructureName()).getConnection(); String insertTableSQL = "INSERT INTO forms(name, surname, organisation, organisation_type, email, " + "country, activities_participation, main_motivation, elaborated_motivation, area_of_expertise, " + "elaborated_expertise, degree_of_participation, data_management, processed_form, user_accepted) " + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; PreparedStatement preparedStatement = conn.prepareStatement(insertTableSQL); preparedStatement.setString(1, name); preparedStatement.setString(2, surname); preparedStatement.setString(3, organisation); preparedStatement.setString(4, organisationType); preparedStatement.setString(5, emailAddress); preparedStatement.setString(6, country); preparedStatement.setBoolean(7, participatedInActivities); preparedStatement.setString(8, mainMotivation); preparedStatement.setString(9, textareaMotivation); preparedStatement.setString(10, areaOfExpertise); preparedStatement.setString(11, elaborated_expertise); preparedStatement.setString(12, degree_of_participation); preparedStatement.setBoolean(13, true); //data_management preparedStatement.setBoolean(14, false); //processed_form preparedStatement.setBoolean(15, false); //user_accepted // execute insert SQL stetement preparedStatement .executeUpdate(); _log.info("Inserting in DB done, sending confirmation to user and notification to managers via email"); Properties props = System.getProperties(); Session session = null; props.put("mail.smtp.host", mailServiceHost); props.put("mail.smtp.port", mailServicePort); //use localhost (probaly postfix instance) session = Session.getDefaultInstance(props); sendEmailConfirmationToUser(props, session, emailAddress); sendEmailNotificationToVREManagers(props, session, emailAddress); } catch (Exception e) { _log.error("Some error while trying to insert form"); e.printStackTrace(); } } private void sendEmailConfirmationToUser(Properties props, Session session, String emailadress) throws AddressException, MessagingException { _log.info("Sending confirmation to user"); Message message = new MimeMessage(session); message.setFrom(new InternetAddress(MAIL_FROM)); message.setRecipients( Message.RecipientType.TO, InternetAddress.parse(emailadress)); message.setSubject(MAIL_SUBJECT); message.setText(MAIL_BODY); // Send message Transport.send(message); _log.info("Sending confirmation email done"); } private void sendEmailNotificationToVREManagers(Properties props, Session session, String emailadress) { } private static List getOrganisationTypes(Connection conn) throws Exception { _log.debug("getting organisation_types "); List toReturn = new ArrayList<>(); String selectSQL = "SELECT * FROM organisation_types"; PreparedStatement preparedStatement = conn.prepareStatement(selectSQL); ResultSet rs = preparedStatement.executeQuery(); while (rs.next()) { String toAdd = rs.getString("combo_value"); _log.debug("Adding organisation_types" + toAdd); toReturn.add(toAdd); } return toReturn; } private static List getMainMotivations(Connection conn) throws Exception { _log.debug("getting main_motivations "); List toReturn = new ArrayList<>(); String selectSQL = "SELECT * FROM main_motivations"; PreparedStatement preparedStatement = conn.prepareStatement(selectSQL); ResultSet rs = preparedStatement.executeQuery(); while (rs.next()) { String toAdd = rs.getString("combo_value"); _log.debug("Adding main_motivations" + toAdd); toReturn.add(toAdd); } return toReturn; } private static List getAreasOfExpertise(Connection conn) throws Exception { _log.debug("getting areas_of_expertise "); List toReturn = new ArrayList<>(); String selectSQL = "SELECT * FROM areas_of_expertise"; PreparedStatement preparedStatement = conn.prepareStatement(selectSQL); ResultSet rs = preparedStatement.executeQuery(); while (rs.next()) { String toAdd = rs.getString("combo_value"); _log.debug("Adding areas_of_expertise" + toAdd); toReturn.add(toAdd); } return toReturn; } private static List getDgreesOfParticipation(Connection conn) throws Exception { _log.debug("getting degrees_of_participation "); List toReturn = new ArrayList<>(); String selectSQL = "SELECT * FROM degrees_of_participation"; PreparedStatement preparedStatement = conn.prepareStatement(selectSQL); ResultSet rs = preparedStatement.executeQuery(); while (rs.next()) { String toAdd = rs.getString("combo_value"); _log.debug("Adding degrees_of_participation" + toAdd); toReturn.add(toAdd); } return toReturn; } }