2021-11-02 14:51:10 +01:00
package org.gcube.portlets.user.moving ;
import java.io.IOException ;
2021-11-03 11:09:23 +01:00
import java.sql.Connection ;
import java.sql.PreparedStatement ;
import java.sql.ResultSet ;
2021-11-02 14:51:10 +01:00
import java.util.ArrayList ;
import java.util.List ;
2021-11-03 19:52:47 +01:00
import java.util.Properties ;
2021-11-02 14:51:10 +01:00
2021-11-03 19:52:47 +01:00
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 ;
2021-11-02 18:00:49 +01:00
import javax.portlet.ActionRequest ;
import javax.portlet.ActionResponse ;
2021-11-02 14:51:10 +01:00
import javax.portlet.PortletException ;
import javax.portlet.RenderRequest ;
import javax.portlet.RenderResponse ;
2021-11-03 11:09:23 +01:00
import org.gcube.common.portal.PortalContext ;
2021-11-02 14:51:10 +01:00
import com.liferay.portal.kernel.log.LogFactoryUtil ;
import com.liferay.util.bridges.mvc.MVCPortlet ;
/ * *
2021-11-03 19:52:47 +01:00
* Portlet implementation class CompileForm
2021-11-02 14:51:10 +01:00
* /
public class CompileForm extends MVCPortlet {
private static com . liferay . portal . kernel . log . Log _log = LogFactoryUtil . getLog ( CompileForm . class ) ;
2021-11-03 19:52:47 +01:00
private static final String MAIL_FROM = " MOVING Gateway <moving@d4science.org> " ;
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 " ;
2021-11-02 14:51:10 +01:00
2021-11-03 19:52:47 +01:00
private String mailServiceHost = " localhost " ;
private String mailServicePort = " 25 " ;
2021-11-02 14:51:10 +01:00
@Override
2021-11-03 19:52:47 +01:00
public void render ( RenderRequest renderRequest , RenderResponse renderResponse ) {
2021-11-03 11:09:23 +01:00
Connection conn = null ;
try {
conn = DatabaseConnection . getInstance ( " / " + PortalContext . getConfiguration ( ) . getInfrastructureName ( ) ) . getConnection ( ) ;
2021-11-03 19:52:47 +01:00
_log . info ( " Trying queries for getting combo values from DB " ) ;
List < String > organisation_types = getOrganisationTypes ( conn ) ;
List < String > main_motivations = getMainMotivations ( conn ) ;
List < String > areas_of_expertise = getAreasOfExpertise ( conn ) ;
List < String > 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 ) ;
2021-11-03 11:09:23 +01:00
} catch ( Exception e ) {
2021-11-03 19:52:47 +01:00
_log . error ( " Something wrong with Databaseconnection or with queries while getting combo types " , e ) ;
2021-11-03 11:09:23 +01:00
}
2021-11-02 14:51:10 +01:00
}
2021-11-03 19:52:47 +01:00
public void submittedForm ( ActionRequest actionRequest , ActionResponse actionResponse ) throws IOException , PortletException {
2021-11-03 12:57:27 +01:00
String name = actionRequest . getParameter ( " name " ) ;
String surname = actionRequest . getParameter ( " surname " ) ;
String organisation = actionRequest . getParameter ( " organisation " ) ;
String organisationType = actionRequest . getParameter ( " organisationType " ) ;
2021-11-03 19:52:47 +01:00
String emailAddress = actionRequest . getParameter ( " emailAddress " ) ;
2021-11-03 12:57:27 +01:00
String [ ] itemsAreaOfExpertiseSelectValues = actionRequest . getParameterValues ( " areaOfExpertiseSelect " ) ;
2021-11-03 19:52:47 +01:00
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 " ) ;
2021-11-03 12:57:27 +01:00
for ( int i = 0 ; i < itemsAreaOfExpertiseSelectValues . length ; i + + ) {
2021-11-03 19:52:47 +01:00
_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 ( ) ;
2021-11-03 12:57:27 +01:00
}
2021-11-02 18:00:49 +01:00
}
2021-11-02 14:51:10 +01:00
2021-11-03 19:52:47 +01:00
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 ) {
}
2021-11-03 11:09:23 +01:00
private static List < String > getOrganisationTypes ( Connection conn ) throws Exception {
_log . debug ( " getting organisation_types " ) ;
List < String > 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 < String > getMainMotivations ( Connection conn ) throws Exception {
_log . debug ( " getting main_motivations " ) ;
List < String > 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 ;
}
2021-11-02 14:51:10 +01:00
2021-11-03 11:09:23 +01:00
private static List < String > getAreasOfExpertise ( Connection conn ) throws Exception {
_log . debug ( " getting areas_of_expertise " ) ;
List < String > 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 ;
}
2021-11-03 12:57:27 +01:00
2021-11-03 11:09:23 +01:00
private static List < String > getDgreesOfParticipation ( Connection conn ) throws Exception {
_log . debug ( " getting degrees_of_participation " ) ;
List < String > 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 ;
}
2021-11-03 19:52:47 +01:00
2021-11-03 12:57:27 +01:00
2021-11-02 14:51:10 +01:00
}