diff --git a/pom.xml b/pom.xml index 4d5f73f..d5e1f50 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ lutece-global-pom fr.paris.lutece.tools - 4.0.6 + 5.1.1 4.0.0 diff --git a/src/java/fr/paris/lutece/plugins/newsletter/business/AwaitingActivationDAO.java b/src/java/fr/paris/lutece/plugins/newsletter/business/AwaitingActivationDAO.java index 7fa430f..c605159 100644 --- a/src/java/fr/paris/lutece/plugins/newsletter/business/AwaitingActivationDAO.java +++ b/src/java/fr/paris/lutece/plugins/newsletter/business/AwaitingActivationDAO.java @@ -43,63 +43,69 @@ */ public class AwaitingActivationDAO implements IAwaitingActivationDAO { - private static final String SQL_QUERY_INSERT = "INSERT INTO newsletter_awaiting_confirmation(id_user, generated_key) VALUES (?, ?)"; - private static final String SQL_QUERY_EXISTS = "SELECT id_user, generated_key FROM newsletter_awaiting_confirmation WHERE id_user = ? AND generated_key = ?"; - private static final String SQL_QUERY_DELETE = "DELETE FROM newsletter_awaiting_confirmation WHERE id_user = ? AND generated_key = ?"; + private static final String SQL_QUERY_INSERT = "INSERT INTO newsletter_awaiting_confirmation(id_user, generated_key) VALUES (?, ?)"; + private static final String SQL_QUERY_EXISTS = "SELECT id_user, generated_key FROM newsletter_awaiting_confirmation WHERE id_user = ? AND generated_key = ?"; + private static final String SQL_QUERY_DELETE = "DELETE FROM newsletter_awaiting_confirmation WHERE id_user = ? AND generated_key = ?"; - /** - * Removes the entry - * @param nIdUser the user id - * @param nKey the key - * @param plugin the plugin - */ - public void delete( int nIdUser, int nKey, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ); - daoUtil.setInt( 1, nIdUser ); - daoUtil.setInt( 2, nKey ); + /** + * Removes the entry + * @param nIdUser the user id + * @param nKey the key + * @param plugin the plugin + */ + public void delete( int nIdUser, int nKey, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) ) + { + daoUtil.setInt( 1, nIdUser ); + daoUtil.setInt( 2, nKey ); - daoUtil.executeUpdate( ); + daoUtil.executeUpdate( ); - daoUtil.free( ); - } + daoUtil.free( ); + } + } - /** - * Checks if the pair user/key already exists. - * @param nIdUser the user id - * @param nKey the generated key - * @param plugin the plugin - * @return true if the pair already exists, false otherwise. - */ - public boolean exists( int nIdUser, int nKey, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_EXISTS, plugin ); - daoUtil.setInt( 1, nIdUser ); - daoUtil.setInt( 2, nKey ); + /** + * Checks if the pair user/key already exists. + * @param nIdUser the user id + * @param nKey the generated key + * @param plugin the plugin + * @return true if the pair already exists, false otherwise. + */ + public boolean exists( int nIdUser, int nKey, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_EXISTS, plugin ) ) + { + daoUtil.setInt( 1, nIdUser ); + daoUtil.setInt( 2, nKey ); - daoUtil.executeQuery( ); + daoUtil.executeQuery( ); - boolean bExists = daoUtil.next( ); + boolean bExists = daoUtil.next( ); - daoUtil.free( ); + daoUtil.free( ); - return bExists; - } + return bExists; + } + } - /** - * Adds a new pair user/key entry - * @param nIdUser the user id - * @param nKey the generated key - * @param plugin the plugin - */ - public void insert( int nIdUser, int nKey, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin ); - daoUtil.setInt( 1, nIdUser ); - daoUtil.setInt( 2, nKey ); + /** + * Adds a new pair user/key entry + * @param nIdUser the user id + * @param nKey the generated key + * @param plugin the plugin + */ + public void insert( int nIdUser, int nKey, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin ) ) + { + daoUtil.setInt( 1, nIdUser ); + daoUtil.setInt( 2, nKey ); - daoUtil.executeUpdate( ); + daoUtil.executeUpdate( ); - daoUtil.free( ); - } + daoUtil.free( ); + } + } } diff --git a/src/java/fr/paris/lutece/plugins/newsletter/business/NewsLetterDAO.java b/src/java/fr/paris/lutece/plugins/newsletter/business/NewsLetterDAO.java index f5f39de..e56b005 100644 --- a/src/java/fr/paris/lutece/plugins/newsletter/business/NewsLetterDAO.java +++ b/src/java/fr/paris/lutece/plugins/newsletter/business/NewsLetterDAO.java @@ -49,482 +49,518 @@ */ public final class NewsLetterDAO implements INewsLetterDAO { - // Constants - private static final String SQL_QUERY_SELECT = "SELECT name, description, date_last_send, html, id_newsletter_template, workgroup_key, unsubscribe, sender_mail, sender_name, test_recipients, test_subject, nb_sections FROM newsletter_description WHERE id_newsletter = ? "; - private static final String SQL_QUERY_SELECT_ALL = "SELECT id_newsletter , name, description, date_last_send, html, id_newsletter_template, workgroup_key, test_recipients , sender_mail, sender_name, test_subject, nb_sections FROM newsletter_description "; - private static final String SQL_QUERY_SELECT_ALL_ID = "SELECT id_newsletter, name FROM newsletter_description "; - private static final String SQL_QUERY_SELECT_ALL_BY_ID_TEMPLATE = "SELECT id_newsletter , name, description, date_last_send, html, id_newsletter_template, workgroup_key, test_recipients , sender_mail, sender_name, test_subject, nb_sections FROM newsletter_description WHERE id_newsletter_template = ? "; - private static final String SQL_QUERY_SELECT_NBR_SUBSCRIBERS = "SELECT count(*) FROM newsletter_subscriber a, newsletter_subscriber_details b WHERE a.id_subscriber = b.id_subscriber AND b.email LIKE ? AND id_newsletter = ? "; - private static final String SQL_QUERY_SELECT_NBR_ACTIVE_SUBSCRIBERS = "SELECT count(*) FROM newsletter_subscriber a, newsletter_subscriber_details b WHERE a.id_subscriber = b.id_subscriber AND b.email LIKE ? AND id_newsletter = ? AND a.confirmed = 1"; - private static final String SQL_QUERY_UPDATE = "UPDATE newsletter_description SET name = ?, description = ?, date_last_send = ?, html = ?, id_newsletter_template = ?, workgroup_key = ? , unsubscribe = ? ,sender_mail = ? ,sender_name = ? , test_recipients = ?, test_subject = ?, nb_sections = ? WHERE id_newsletter = ? "; - private static final String SQL_QUERY_INSERT = "INSERT INTO newsletter_description ( id_newsletter , name, description, date_last_send, html, id_newsletter_template, workgroup_key, unsubscribe, sender_mail, sender_name, test_recipients , test_subject, nb_sections ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ? , ?, ?, ?, ?, ? )"; - private static final String SQL_QUERY_INSERT_SUBSCRIBER = "INSERT INTO newsletter_subscriber ( id_newsletter , id_subscriber, date_subscription, confirmed ) VALUES ( ?, ?, ?, ? )"; - private static final String SQL_QUERY_VALIDATE_SUBSCRIBER = "UPDATE newsletter_subscriber SET confirmed = 1 WHERE id_newsletter = ? AND id_subscriber = ?"; - private static final String SQL_QUERY_DELETE = "DELETE FROM newsletter_description WHERE id_newsletter = ? "; - private static final String SQL_QUERY_DELETE_FROM_SUBSCRIBER = "DELETE FROM newsletter_subscriber WHERE id_newsletter = ? and id_subscriber = ? "; - private static final String SQL_QUERY_DELETE_OLD_FROM_SUBSCRIBER = "DELETE FROM newsletter_subscriber WHERE date_subscription < ? and confirmed = ? "; - private static final String SQL_QUERY_CHECK_PRIMARY_KEY = "SELECT id_newsletter FROM newsletter_description WHERE id_newsletter = ?"; - private static final String SQL_QUERY_CHECK_LINKED_PORTLET = "SELECT id_newsletter FROM newsletter_portlet_subscribe WHERE id_newsletter = ?"; - private static final String SQL_QUERY_NEW_PRIMARY_KEY = "SELECT max(id_newsletter) FROM newsletter_description "; - private static final String SQL_QUERY_CHECK_IS_REGISTERED = "SELECT id_newsletter FROM newsletter_subscriber WHERE id_newsletter = ? AND id_subscriber = ? "; - private static final String SQL_QUERY_CHECK_IS_TEMPLATE_USED = "SELECT id_newsletter FROM newsletter_description WHERE id_newsletter_template = ? "; - private static final String SQL_QUERY_DELETE_UNUSED_EMAIL = "DELETE FROM newsletter_subscriber_details WHERE id_subscriber NOT IN (SELECT id_subscriber FROM newsletter_subscriber)"; - - private static final String CONSTANT_PERCENT = "%"; - - /////////////////////////////////////////////////////////////////////////////////////// - //Access methods to data - - /** - * {@inheritDoc} - */ - @Override - public void insert( NewsLetter newsLetter, Plugin plugin ) - { - int nNewPrimaryKey = newPrimaryKey( plugin ); - newsLetter.setId( nNewPrimaryKey ); - - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin ); - - int nIndex = 1; - daoUtil.setInt( nIndex++, newsLetter.getId( ) ); - daoUtil.setString( nIndex++, newsLetter.getName( ) ); - daoUtil.setString( nIndex++, newsLetter.getDescription( ) ); - daoUtil.setTimestamp( nIndex++, newsLetter.getDateLastSending( ) ); - daoUtil.setString( nIndex++, newsLetter.getHtml( ) ); - daoUtil.setInt( nIndex++, newsLetter.getNewsLetterTemplateId( ) ); - daoUtil.setString( nIndex++, newsLetter.getWorkgroup( ) ); - daoUtil.setString( nIndex++, newsLetter.getUnsubscribe( ) ); - daoUtil.setString( nIndex++, newsLetter.getNewsletterSenderMail( ) ); - daoUtil.setString( nIndex++, newsLetter.getNewsletterSenderName( ) ); - daoUtil.setString( nIndex++, newsLetter.getTestRecipients( ) ); - daoUtil.setString( nIndex++, newsLetter.getTestSubject( ) ); - daoUtil.setInt( nIndex, newsLetter.getNbSections( ) ); - - daoUtil.executeUpdate( ); - daoUtil.free( ); - } - - /** - * {@inheritDoc} - */ - @Override - public void delete( int nNewsLetterId, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ); - daoUtil.setInt( 1, nNewsLetterId ); - - daoUtil.executeUpdate( ); - daoUtil.free( ); - } - - /** - * {@inheritDoc} - */ - @Override - public NewsLetter load( int nNewsLetterId, Plugin plugin ) - { - NewsLetter newsLetter = new NewsLetter( ); - - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin ); - daoUtil.setInt( 1, nNewsLetterId ); - daoUtil.executeQuery( ); - - if ( daoUtil.next( ) ) - { - int nIndex = 1; - newsLetter.setId( nNewsLetterId ); - newsLetter.setName( daoUtil.getString( nIndex++ ) ); - newsLetter.setDescription( daoUtil.getString( nIndex++ ) ); - newsLetter.setDateLastSending( daoUtil.getTimestamp( nIndex++ ) ); - newsLetter.setHtml( daoUtil.getString( nIndex++ ) ); - newsLetter.setNewsLetterTemplateId( daoUtil.getInt( nIndex++ ) ); - newsLetter.setWorkgroup( daoUtil.getString( nIndex++ ) ); - newsLetter.setUnsubscribe( daoUtil.getString( nIndex++ ) ); - newsLetter.setNewsletterSenderMail( daoUtil.getString( nIndex++ ) ); - newsLetter.setNewsletterSenderName( daoUtil.getString( nIndex++ ) ); - newsLetter.setTestRecipients( daoUtil.getString( nIndex++ ) ); - newsLetter.setTestSubject( daoUtil.getString( nIndex++ ) ); - newsLetter.setNbSections( daoUtil.getInt( nIndex ) ); - } - - daoUtil.free( ); - - return newsLetter; - } - - /** - * {@inheritDoc} - */ - @Override - public void store( NewsLetter newsLetter, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ); - int nIndex = 1; - daoUtil.setString( nIndex++, newsLetter.getName( ) ); - daoUtil.setString( nIndex++, newsLetter.getDescription( ) ); - daoUtil.setTimestamp( nIndex++, newsLetter.getDateLastSending( ) ); - daoUtil.setString( nIndex++, newsLetter.getHtml( ) ); - daoUtil.setInt( nIndex++, newsLetter.getNewsLetterTemplateId( ) ); - daoUtil.setString( nIndex++, newsLetter.getWorkgroup( ) ); - daoUtil.setString( nIndex++, newsLetter.getUnsubscribe( ) ); - daoUtil.setString( nIndex++, newsLetter.getNewsletterSenderMail( ) ); - daoUtil.setString( nIndex++, newsLetter.getNewsletterSenderName( ) ); - daoUtil.setString( nIndex++, newsLetter.getTestRecipients( ) ); - daoUtil.setString( nIndex++, newsLetter.getTestSubject( ) ); - daoUtil.setInt( nIndex++, newsLetter.getNbSections( ) ); - daoUtil.setInt( nIndex, newsLetter.getId( ) ); - - daoUtil.executeUpdate( ); - daoUtil.free( ); - } - - /** - * {@inheritDoc} - */ - @Override - public boolean checkPrimaryKey( int nKey, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_CHECK_PRIMARY_KEY, plugin ); - daoUtil.setInt( 1, nKey ); - daoUtil.executeQuery( ); - - if ( !daoUtil.next( ) ) - { - daoUtil.free( ); - - return false; - } - - daoUtil.free( ); - - return true; - } - - /** - * {@inheritDoc} - */ - @Override - public boolean checkLinkedPortlet( int nIdNewsletter ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_CHECK_LINKED_PORTLET, - PluginService.getPlugin( NewsletterPlugin.PLUGIN_NAME ) ); - daoUtil.setInt( 1, nIdNewsletter ); - daoUtil.executeQuery( ); - - if ( !daoUtil.next( ) ) - { - daoUtil.free( ); - - return false; - } - - daoUtil.free( ); - - return true; - } - - /** - * {@inheritDoc} - */ - @Override - public int newPrimaryKey( Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PRIMARY_KEY, plugin ); - int nKey; - - daoUtil.executeQuery( ); - - if ( !daoUtil.next( ) ) - { - // If the table is empty - nKey = 1; - } - - nKey = daoUtil.getInt( 1 ) + 1; - - daoUtil.free( ); - - return nKey; - } - - /** - * {@inheritDoc} - */ - @Override - public Collection selectAll( Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL, plugin ); - daoUtil.executeQuery( ); - - ArrayList list = new ArrayList( ); - - while ( daoUtil.next( ) ) - { - int nIndex = 1; - NewsLetter newsLetter = new NewsLetter( ); - newsLetter.setId( daoUtil.getInt( nIndex++ ) ); - newsLetter.setName( daoUtil.getString( nIndex++ ) ); - newsLetter.setDescription( daoUtil.getString( nIndex++ ) ); - newsLetter.setDateLastSending( daoUtil.getTimestamp( nIndex++ ) ); - newsLetter.setHtml( daoUtil.getString( nIndex++ ) ); - newsLetter.setNewsLetterTemplateId( daoUtil.getInt( nIndex++ ) ); - newsLetter.setWorkgroup( daoUtil.getString( nIndex++ ) ); - newsLetter.setTestRecipients( daoUtil.getString( nIndex++ ) ); - newsLetter.setNewsletterSenderMail( daoUtil.getString( nIndex++ ) ); - newsLetter.setNewsletterSenderName( daoUtil.getString( nIndex++ ) ); - newsLetter.setTestSubject( daoUtil.getString( nIndex++ ) ); - newsLetter.setNbSections( daoUtil.getInt( nIndex ) ); - list.add( newsLetter ); - } - - daoUtil.free( ); - - return list; - } - - /** - * {@inheritDoc} - */ - @Override - public ReferenceList selectAllId( Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL_ID, plugin ); - daoUtil.executeQuery( ); - - ReferenceList list = new ReferenceList( ); - - while ( daoUtil.next( ) ) - { - list.addItem( daoUtil.getInt( 1 ), daoUtil.getString( 2 ) ); - } - - daoUtil.free( ); - - return list; - } - - /** - * {@inheritDoc} - */ - @Override - public Collection selectAllByTemplateId( int nTemplateId, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL_BY_ID_TEMPLATE, plugin ); - daoUtil.setInt( 1, nTemplateId ); - daoUtil.executeQuery( ); - - ArrayList list = new ArrayList( ); - - while ( daoUtil.next( ) ) - { - int nIndex = 1; - NewsLetter newsLetter = new NewsLetter( ); - newsLetter.setId( daoUtil.getInt( nIndex++ ) ); - newsLetter.setName( daoUtil.getString( nIndex++ ) ); - newsLetter.setDescription( daoUtil.getString( nIndex++ ) ); - newsLetter.setDateLastSending( daoUtil.getTimestamp( nIndex++ ) ); - newsLetter.setHtml( daoUtil.getString( nIndex++ ) ); - newsLetter.setNewsLetterTemplateId( daoUtil.getInt( nIndex++ ) ); - newsLetter.setWorkgroup( daoUtil.getString( nIndex++ ) ); - newsLetter.setTestRecipients( daoUtil.getString( nIndex++ ) ); - newsLetter.setNewsletterSenderMail( daoUtil.getString( nIndex++ ) ); - newsLetter.setNewsletterSenderName( daoUtil.getString( nIndex++ ) ); - newsLetter.setTestSubject( daoUtil.getString( nIndex++ ) ); - newsLetter.setNbSections( daoUtil.getInt( nIndex ) ); - list.add( newsLetter ); - } - - daoUtil.free( ); - - return list; - } - - /** - * {@inheritDoc} - */ - @Override - public void insertSubscriber( int nNewsLetterId, int nSubscriberId, Timestamp tToday, Plugin plugin ) - { - insertSubscriber( nNewsLetterId, nSubscriberId, true, tToday, plugin ); - } - - /** - * {@inheritDoc} - */ - @Override - public void insertSubscriber( int nNewsLetterId, int nSubscriberId, boolean bValidate, Timestamp tToday, - Plugin plugin ) - { - // Check if the subscriber is yet registered for the newsletter - if ( isRegistered( nNewsLetterId, nSubscriberId, plugin ) ) - { - return; - } - - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_SUBSCRIBER, plugin ); - - daoUtil.setInt( 1, nNewsLetterId ); - daoUtil.setInt( 2, nSubscriberId ); - daoUtil.setTimestamp( 3, tToday ); - daoUtil.setBoolean( 4, bValidate ); - - daoUtil.executeUpdate( ); - daoUtil.free( ); - } - - /** - * {@inheritDoc} - */ - @Override - public void deleteSubscriber( int nNewsLetterId, int nSubscriberId, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_FROM_SUBSCRIBER, plugin ); - - daoUtil.setInt( 1, nNewsLetterId ); - daoUtil.setInt( 2, nSubscriberId ); - - daoUtil.executeUpdate( ); - daoUtil.free( ); - } - - /** - * {@inheritDoc} - */ - @Override - public void deleteOldUnconfirmed( Timestamp confirmLimitDate, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_OLD_FROM_SUBSCRIBER, plugin ); - daoUtil.setTimestamp( 1, confirmLimitDate ); - daoUtil.setBoolean( 2, false ); - - daoUtil.executeUpdate( ); - daoUtil.free( ); - - daoUtil = new DAOUtil( SQL_QUERY_DELETE_UNUSED_EMAIL, plugin ); - daoUtil.executeUpdate( ); - daoUtil.free( ); - } - - /** - * {@inheritDoc} - */ - @Override - public boolean isRegistered( int nNewsLetterId, int nSubscriberId, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_CHECK_IS_REGISTERED, plugin ); - - daoUtil.setInt( 1, nNewsLetterId ); - daoUtil.setInt( 2, nSubscriberId ); - daoUtil.executeQuery( ); - - if ( !daoUtil.next( ) ) - { - daoUtil.free( ); - - return false; - } - - daoUtil.free( ); - - return true; - } - - /** - * {@inheritDoc} - */ - @Override - public boolean isTemplateUsed( int nTemplateId, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_CHECK_IS_TEMPLATE_USED, plugin ); - - daoUtil.setInt( 1, nTemplateId ); - daoUtil.executeQuery( ); - - if ( !daoUtil.next( ) ) - { - daoUtil.free( ); - - return false; - } - - daoUtil.free( ); - - return true; - } - - /** - * {@inheritDoc} - */ - @Override - public int selectNbrSubscribers( int nNewsLetterId, String strSearchString, Plugin plugin ) - { - int nCount; - - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_NBR_SUBSCRIBERS, plugin ); - - daoUtil.setString( 1, CONSTANT_PERCENT + strSearchString + CONSTANT_PERCENT ); - daoUtil.setInt( 2, nNewsLetterId ); - - daoUtil.executeQuery( ); - - if ( !daoUtil.next( ) ) - { - // If the table is empty - nCount = 0; - } - else - { - nCount = daoUtil.getInt( 1 ); - } - - daoUtil.free( ); - - return nCount; - } - - /** - * {@inheritDoc} - */ - @Override - public int selectNbrActiveSubscribers( int nNewsLetterId, String strSearchString, Plugin plugin ) - { - int nCount; - - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_NBR_ACTIVE_SUBSCRIBERS, plugin ); - - daoUtil.setString( 1, CONSTANT_PERCENT + strSearchString + CONSTANT_PERCENT ); - daoUtil.setInt( 2, nNewsLetterId ); - - daoUtil.executeQuery( ); - - if ( !daoUtil.next( ) ) - { - // If the table is empty - nCount = 0; - } - else - { - nCount = daoUtil.getInt( 1 ); - } - - daoUtil.free( ); - - return nCount; - } - - /** - * {@inheritDoc} - */ - @Override - public void validateSubscriber( int nNewsLetterId, int nSubscriberId, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_VALIDATE_SUBSCRIBER, plugin ); - - daoUtil.setInt( 1, nNewsLetterId ); - daoUtil.setInt( 2, nSubscriberId ); - - daoUtil.executeUpdate( ); - daoUtil.free( ); - } + // Constants + private static final String SQL_QUERY_SELECT = "SELECT name, description, date_last_send, html, id_newsletter_template, workgroup_key, unsubscribe, sender_mail, sender_name, test_recipients, test_subject, nb_sections FROM newsletter_description WHERE id_newsletter = ? "; + private static final String SQL_QUERY_SELECT_ALL = "SELECT id_newsletter , name, description, date_last_send, html, id_newsletter_template, workgroup_key, test_recipients , sender_mail, sender_name, test_subject, nb_sections FROM newsletter_description "; + private static final String SQL_QUERY_SELECT_ALL_ID = "SELECT id_newsletter, name FROM newsletter_description "; + private static final String SQL_QUERY_SELECT_ALL_BY_ID_TEMPLATE = "SELECT id_newsletter , name, description, date_last_send, html, id_newsletter_template, workgroup_key, test_recipients , sender_mail, sender_name, test_subject, nb_sections FROM newsletter_description WHERE id_newsletter_template = ? "; + private static final String SQL_QUERY_SELECT_NBR_SUBSCRIBERS = "SELECT count(*) FROM newsletter_subscriber a, newsletter_subscriber_details b WHERE a.id_subscriber = b.id_subscriber AND b.email LIKE ? AND id_newsletter = ? "; + private static final String SQL_QUERY_SELECT_NBR_ACTIVE_SUBSCRIBERS = "SELECT count(*) FROM newsletter_subscriber a, newsletter_subscriber_details b WHERE a.id_subscriber = b.id_subscriber AND b.email LIKE ? AND id_newsletter = ? AND a.confirmed = 1"; + private static final String SQL_QUERY_UPDATE = "UPDATE newsletter_description SET name = ?, description = ?, date_last_send = ?, html = ?, id_newsletter_template = ?, workgroup_key = ? , unsubscribe = ? ,sender_mail = ? ,sender_name = ? , test_recipients = ?, test_subject = ?, nb_sections = ? WHERE id_newsletter = ? "; + private static final String SQL_QUERY_INSERT = "INSERT INTO newsletter_description ( id_newsletter , name, description, date_last_send, html, id_newsletter_template, workgroup_key, unsubscribe, sender_mail, sender_name, test_recipients , test_subject, nb_sections ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ? , ?, ?, ?, ?, ? )"; + private static final String SQL_QUERY_INSERT_SUBSCRIBER = "INSERT INTO newsletter_subscriber ( id_newsletter , id_subscriber, date_subscription, confirmed ) VALUES ( ?, ?, ?, ? )"; + private static final String SQL_QUERY_VALIDATE_SUBSCRIBER = "UPDATE newsletter_subscriber SET confirmed = 1 WHERE id_newsletter = ? AND id_subscriber = ?"; + private static final String SQL_QUERY_DELETE = "DELETE FROM newsletter_description WHERE id_newsletter = ? "; + private static final String SQL_QUERY_DELETE_FROM_SUBSCRIBER = "DELETE FROM newsletter_subscriber WHERE id_newsletter = ? and id_subscriber = ? "; + private static final String SQL_QUERY_DELETE_OLD_FROM_SUBSCRIBER = "DELETE FROM newsletter_subscriber WHERE date_subscription < ? and confirmed = ? "; + private static final String SQL_QUERY_CHECK_PRIMARY_KEY = "SELECT id_newsletter FROM newsletter_description WHERE id_newsletter = ?"; + private static final String SQL_QUERY_CHECK_LINKED_PORTLET = "SELECT id_newsletter FROM newsletter_portlet_subscribe WHERE id_newsletter = ?"; + private static final String SQL_QUERY_NEW_PRIMARY_KEY = "SELECT max(id_newsletter) FROM newsletter_description "; + private static final String SQL_QUERY_CHECK_IS_REGISTERED = "SELECT id_newsletter FROM newsletter_subscriber WHERE id_newsletter = ? AND id_subscriber = ? "; + private static final String SQL_QUERY_CHECK_IS_TEMPLATE_USED = "SELECT id_newsletter FROM newsletter_description WHERE id_newsletter_template = ? "; + private static final String SQL_QUERY_DELETE_UNUSED_EMAIL = "DELETE FROM newsletter_subscriber_details WHERE id_subscriber NOT IN (SELECT id_subscriber FROM newsletter_subscriber)"; + + private static final String CONSTANT_PERCENT = "%"; + + /////////////////////////////////////////////////////////////////////////////////////// + //Access methods to data + + /** + * {@inheritDoc} + */ + @Override + public void insert( NewsLetter newsLetter, Plugin plugin ) + { + int nNewPrimaryKey = newPrimaryKey( plugin ); + newsLetter.setId( nNewPrimaryKey ); + + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin ) ) + { + + int nIndex = 1; + daoUtil.setInt( nIndex++, newsLetter.getId( ) ); + daoUtil.setString( nIndex++, newsLetter.getName( ) ); + daoUtil.setString( nIndex++, newsLetter.getDescription( ) ); + daoUtil.setTimestamp( nIndex++, newsLetter.getDateLastSending( ) ); + daoUtil.setString( nIndex++, newsLetter.getHtml( ) ); + daoUtil.setInt( nIndex++, newsLetter.getNewsLetterTemplateId( ) ); + daoUtil.setString( nIndex++, newsLetter.getWorkgroup( ) ); + daoUtil.setString( nIndex++, newsLetter.getUnsubscribe( ) ); + daoUtil.setString( nIndex++, newsLetter.getNewsletterSenderMail( ) ); + daoUtil.setString( nIndex++, newsLetter.getNewsletterSenderName( ) ); + daoUtil.setString( nIndex++, newsLetter.getTestRecipients( ) ); + daoUtil.setString( nIndex++, newsLetter.getTestSubject( ) ); + daoUtil.setInt( nIndex, newsLetter.getNbSections( ) ); + + daoUtil.executeUpdate( ); + daoUtil.free( ); + } + } + + /** + * {@inheritDoc} + */ + @Override + public void delete( int nNewsLetterId, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) ) + { + daoUtil.setInt( 1, nNewsLetterId ); + + daoUtil.executeUpdate( ); + daoUtil.free( ); + } + } + + /** + * {@inheritDoc} + */ + @Override + public NewsLetter load( int nNewsLetterId, Plugin plugin ) + { + NewsLetter newsLetter = new NewsLetter( ); + + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin ) ) + { + daoUtil.setInt( 1, nNewsLetterId ); + daoUtil.executeQuery( ); + + if ( daoUtil.next( ) ) + { + int nIndex = 1; + newsLetter.setId( nNewsLetterId ); + newsLetter.setName( daoUtil.getString( nIndex++ ) ); + newsLetter.setDescription( daoUtil.getString( nIndex++ ) ); + newsLetter.setDateLastSending( daoUtil.getTimestamp( nIndex++ ) ); + newsLetter.setHtml( daoUtil.getString( nIndex++ ) ); + newsLetter.setNewsLetterTemplateId( daoUtil.getInt( nIndex++ ) ); + newsLetter.setWorkgroup( daoUtil.getString( nIndex++ ) ); + newsLetter.setUnsubscribe( daoUtil.getString( nIndex++ ) ); + newsLetter.setNewsletterSenderMail( daoUtil.getString( nIndex++ ) ); + newsLetter.setNewsletterSenderName( daoUtil.getString( nIndex++ ) ); + newsLetter.setTestRecipients( daoUtil.getString( nIndex++ ) ); + newsLetter.setTestSubject( daoUtil.getString( nIndex++ ) ); + newsLetter.setNbSections( daoUtil.getInt( nIndex ) ); + } + + daoUtil.free( ); + } + + return newsLetter; + } + + /** + * {@inheritDoc} + */ + @Override + public void store( NewsLetter newsLetter, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ) ) + { + int nIndex = 1; + daoUtil.setString( nIndex++, newsLetter.getName( ) ); + daoUtil.setString( nIndex++, newsLetter.getDescription( ) ); + daoUtil.setTimestamp( nIndex++, newsLetter.getDateLastSending( ) ); + daoUtil.setString( nIndex++, newsLetter.getHtml( ) ); + daoUtil.setInt( nIndex++, newsLetter.getNewsLetterTemplateId( ) ); + daoUtil.setString( nIndex++, newsLetter.getWorkgroup( ) ); + daoUtil.setString( nIndex++, newsLetter.getUnsubscribe( ) ); + daoUtil.setString( nIndex++, newsLetter.getNewsletterSenderMail( ) ); + daoUtil.setString( nIndex++, newsLetter.getNewsletterSenderName( ) ); + daoUtil.setString( nIndex++, newsLetter.getTestRecipients( ) ); + daoUtil.setString( nIndex++, newsLetter.getTestSubject( ) ); + daoUtil.setInt( nIndex++, newsLetter.getNbSections( ) ); + daoUtil.setInt( nIndex, newsLetter.getId( ) ); + + daoUtil.executeUpdate( ); + daoUtil.free( ); + } + } + + /** + * {@inheritDoc} + */ + @Override + public boolean checkPrimaryKey( int nKey, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_CHECK_PRIMARY_KEY, plugin ) ) + { + daoUtil.setInt( 1, nKey ); + daoUtil.executeQuery( ); + + if ( !daoUtil.next( ) ) + { + daoUtil.free( ); + + return false; + } + + daoUtil.free( ); + + return true; + } + } + + /** + * {@inheritDoc} + */ + @Override + public boolean checkLinkedPortlet( int nIdNewsletter ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_CHECK_LINKED_PORTLET, + PluginService.getPlugin( NewsletterPlugin.PLUGIN_NAME ) ) ) + { + daoUtil.setInt( 1, nIdNewsletter ); + daoUtil.executeQuery( ); + + if ( !daoUtil.next( ) ) + { + daoUtil.free( ); + + return false; + } + + daoUtil.free( ); + + return true; + } + } + + /** + * {@inheritDoc} + */ + @Override + public int newPrimaryKey( Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PRIMARY_KEY, plugin ) ) + { + int nKey; + + daoUtil.executeQuery( ); + + if ( !daoUtil.next( ) ) + { + // If the table is empty + nKey = 1; + } + + nKey = daoUtil.getInt( 1 ) + 1; + + daoUtil.free( ); + + return nKey; + } + } + + /** + * {@inheritDoc} + */ + @Override + public Collection selectAll( Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL, plugin ) ) + { + daoUtil.executeQuery( ); + + ArrayList list = new ArrayList<>( ); + + while ( daoUtil.next( ) ) + { + int nIndex = 1; + NewsLetter newsLetter = new NewsLetter( ); + newsLetter.setId( daoUtil.getInt( nIndex++ ) ); + newsLetter.setName( daoUtil.getString( nIndex++ ) ); + newsLetter.setDescription( daoUtil.getString( nIndex++ ) ); + newsLetter.setDateLastSending( daoUtil.getTimestamp( nIndex++ ) ); + newsLetter.setHtml( daoUtil.getString( nIndex++ ) ); + newsLetter.setNewsLetterTemplateId( daoUtil.getInt( nIndex++ ) ); + newsLetter.setWorkgroup( daoUtil.getString( nIndex++ ) ); + newsLetter.setTestRecipients( daoUtil.getString( nIndex++ ) ); + newsLetter.setNewsletterSenderMail( daoUtil.getString( nIndex++ ) ); + newsLetter.setNewsletterSenderName( daoUtil.getString( nIndex++ ) ); + newsLetter.setTestSubject( daoUtil.getString( nIndex++ ) ); + newsLetter.setNbSections( daoUtil.getInt( nIndex ) ); + list.add( newsLetter ); + } + + daoUtil.free( ); + + return list; + } + } + + /** + * {@inheritDoc} + */ + @Override + public ReferenceList selectAllId( Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL_ID, plugin ) ) + { + daoUtil.executeQuery( ); + + ReferenceList list = new ReferenceList( ); + + while ( daoUtil.next( ) ) + { + list.addItem( daoUtil.getInt( 1 ), daoUtil.getString( 2 ) ); + } + + daoUtil.free( ); + + return list; + } + } + + /** + * {@inheritDoc} + */ + @Override + public Collection selectAllByTemplateId( int nTemplateId, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL_BY_ID_TEMPLATE, plugin ) ) + { + daoUtil.setInt( 1, nTemplateId ); + daoUtil.executeQuery( ); + + ArrayList list = new ArrayList<>( ); + + while ( daoUtil.next( ) ) + { + int nIndex = 1; + NewsLetter newsLetter = new NewsLetter( ); + newsLetter.setId( daoUtil.getInt( nIndex++ ) ); + newsLetter.setName( daoUtil.getString( nIndex++ ) ); + newsLetter.setDescription( daoUtil.getString( nIndex++ ) ); + newsLetter.setDateLastSending( daoUtil.getTimestamp( nIndex++ ) ); + newsLetter.setHtml( daoUtil.getString( nIndex++ ) ); + newsLetter.setNewsLetterTemplateId( daoUtil.getInt( nIndex++ ) ); + newsLetter.setWorkgroup( daoUtil.getString( nIndex++ ) ); + newsLetter.setTestRecipients( daoUtil.getString( nIndex++ ) ); + newsLetter.setNewsletterSenderMail( daoUtil.getString( nIndex++ ) ); + newsLetter.setNewsletterSenderName( daoUtil.getString( nIndex++ ) ); + newsLetter.setTestSubject( daoUtil.getString( nIndex++ ) ); + newsLetter.setNbSections( daoUtil.getInt( nIndex ) ); + list.add( newsLetter ); + } + + daoUtil.free( ); + + return list; + } + } + + /** + * {@inheritDoc} + */ + @Override + public void insertSubscriber( int nNewsLetterId, int nSubscriberId, Timestamp tToday, Plugin plugin ) + { + insertSubscriber( nNewsLetterId, nSubscriberId, true, tToday, plugin ); + } + + /** + * {@inheritDoc} + */ + @Override + public void insertSubscriber( int nNewsLetterId, int nSubscriberId, boolean bValidate, Timestamp tToday, + Plugin plugin ) + { + // Check if the subscriber is yet registered for the newsletter + if ( isRegistered( nNewsLetterId, nSubscriberId, plugin ) ) + { + return; + } + + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_SUBSCRIBER, plugin ) ) + { + + daoUtil.setInt( 1, nNewsLetterId ); + daoUtil.setInt( 2, nSubscriberId ); + daoUtil.setTimestamp( 3, tToday ); + daoUtil.setBoolean( 4, bValidate ); + + daoUtil.executeUpdate( ); + daoUtil.free( ); + } + } + + /** + * {@inheritDoc} + */ + @Override + public void deleteSubscriber( int nNewsLetterId, int nSubscriberId, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_FROM_SUBSCRIBER, plugin ) ) + { + + daoUtil.setInt( 1, nNewsLetterId ); + daoUtil.setInt( 2, nSubscriberId ); + + daoUtil.executeUpdate( ); + daoUtil.free( ); + } + } + + /** + * {@inheritDoc} + */ + @Override + public void deleteOldUnconfirmed( Timestamp confirmLimitDate, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_OLD_FROM_SUBSCRIBER, plugin ) ) + { + daoUtil.setTimestamp( 1, confirmLimitDate ); + daoUtil.setBoolean( 2, false ); + + daoUtil.executeUpdate( ); + daoUtil.free( ); + } + + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_UNUSED_EMAIL, plugin ) ) + { + daoUtil.executeUpdate( ); + daoUtil.free( ); + } + } + + /** + * {@inheritDoc} + */ + @Override + public boolean isRegistered( int nNewsLetterId, int nSubscriberId, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_CHECK_IS_REGISTERED, plugin ) ) + { + + daoUtil.setInt( 1, nNewsLetterId ); + daoUtil.setInt( 2, nSubscriberId ); + daoUtil.executeQuery( ); + + if ( !daoUtil.next( ) ) + { + daoUtil.free( ); + + return false; + } + + daoUtil.free( ); + + return true; + } + } + + /** + * {@inheritDoc} + */ + @Override + public boolean isTemplateUsed( int nTemplateId, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_CHECK_IS_TEMPLATE_USED, plugin ) ) + { + + daoUtil.setInt( 1, nTemplateId ); + daoUtil.executeQuery( ); + + if ( !daoUtil.next( ) ) + { + daoUtil.free( ); + + return false; + } + + daoUtil.free( ); + } + + return true; + } + + /** + * {@inheritDoc} + */ + @Override + public int selectNbrSubscribers( int nNewsLetterId, String strSearchString, Plugin plugin ) + { + int nCount; + + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_NBR_SUBSCRIBERS, plugin ) ) + { + daoUtil.setString( 1, CONSTANT_PERCENT + strSearchString + CONSTANT_PERCENT ); + daoUtil.setInt( 2, nNewsLetterId ); + + daoUtil.executeQuery( ); + + if ( !daoUtil.next( ) ) + { + // If the table is empty + nCount = 0; + } + else + { + nCount = daoUtil.getInt( 1 ); + } + + daoUtil.free( ); + } + + return nCount; + } + + /** + * {@inheritDoc} + */ + @Override + public int selectNbrActiveSubscribers( int nNewsLetterId, String strSearchString, Plugin plugin ) + { + int nCount; + + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_NBR_ACTIVE_SUBSCRIBERS, plugin ) ) + { + + daoUtil.setString( 1, CONSTANT_PERCENT + strSearchString + CONSTANT_PERCENT ); + daoUtil.setInt( 2, nNewsLetterId ); + + daoUtil.executeQuery( ); + + if ( !daoUtil.next( ) ) + { + // If the table is empty + nCount = 0; + } + else + { + nCount = daoUtil.getInt( 1 ); + } + + daoUtil.free( ); + } + + return nCount; + } + + /** + * {@inheritDoc} + */ + @Override + public void validateSubscriber( int nNewsLetterId, int nSubscriberId, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_VALIDATE_SUBSCRIBER, plugin ) ) + { + daoUtil.setInt( 1, nNewsLetterId ); + daoUtil.setInt( 2, nSubscriberId ); + + daoUtil.executeUpdate( ); + daoUtil.free( ); + } + } } diff --git a/src/java/fr/paris/lutece/plugins/newsletter/business/NewsletterPropertiesDAO.java b/src/java/fr/paris/lutece/plugins/newsletter/business/NewsletterPropertiesDAO.java index 0884409..4afcca9 100644 --- a/src/java/fr/paris/lutece/plugins/newsletter/business/NewsletterPropertiesDAO.java +++ b/src/java/fr/paris/lutece/plugins/newsletter/business/NewsletterPropertiesDAO.java @@ -44,56 +44,60 @@ */ public final class NewsletterPropertiesDAO implements INewsletterPropertiesDAO { - // Constants - private static final String SQL_QUERY_SELECT = "SELECT validation_activated, captcha_activated, tos FROM newsletter_properties "; - private static final String SQL_QUERY_UPDATE = "UPDATE newsletter_properties SET validation_activated = ?, captcha_activated = ?, tos = ?"; + // Constants + private static final String SQL_QUERY_SELECT = "SELECT validation_activated, captcha_activated, tos FROM newsletter_properties "; + private static final String SQL_QUERY_UPDATE = "UPDATE newsletter_properties SET validation_activated = ?, captcha_activated = ?, tos = ?"; - /** - * loads data from NewsLetterProperties - * @param plugin the Plugin - * @return an object NewsLetterProperties - */ - public NewsLetterProperties load( Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin ); - daoUtil.executeQuery( ); + /** + * loads data from NewsLetterProperties + * @param plugin the Plugin + * @return an object NewsLetterProperties + */ + public NewsLetterProperties load( Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin ) ) + { + daoUtil.executeQuery( ); - NewsLetterProperties properties = new NewsLetterProperties( ); + NewsLetterProperties properties = new NewsLetterProperties( ); - if ( daoUtil.next( ) ) - { - properties.setValidationActive( daoUtil.getBoolean( 1 ) ); - properties.setCaptchaActive( daoUtil.getBoolean( 2 ) ); + if ( daoUtil.next( ) ) + { + properties.setValidationActive( daoUtil.getBoolean( 1 ) ); + properties.setCaptchaActive( daoUtil.getBoolean( 2 ) ); - String strTos = daoUtil.getString( 3 ); - if ( StringUtils.isNotEmpty( strTos ) ) - { - properties.setTOS( strTos ); - } - else - { - properties.setTOS( null ); - } - } + String strTos = daoUtil.getString( 3 ); + if ( StringUtils.isNotEmpty( strTos ) ) + { + properties.setTOS( strTos ); + } + else + { + properties.setTOS( null ); + } + } - daoUtil.free( ); + daoUtil.free( ); - return properties; - } + return properties; + } + } - /** - * Update the record in the table - * @param properties the instance of properties class to be updated - * @param plugin the Plugin - */ - public void store( NewsLetterProperties properties, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ); - daoUtil.setBoolean( 1, properties.isValidationActive( ) ); - daoUtil.setBoolean( 2, properties.isCaptchaActive( ) ); - daoUtil.setString( 3, properties.getTOS( ) ); + /** + * Update the record in the table + * @param properties the instance of properties class to be updated + * @param plugin the Plugin + */ + public void store( NewsLetterProperties properties, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ) ) + { + daoUtil.setBoolean( 1, properties.isValidationActive( ) ); + daoUtil.setBoolean( 2, properties.isCaptchaActive( ) ); + daoUtil.setString( 3, properties.getTOS( ) ); - daoUtil.executeUpdate( ); - daoUtil.free( ); - } + daoUtil.executeUpdate( ); + daoUtil.free( ); + } + } } diff --git a/src/java/fr/paris/lutece/plugins/newsletter/business/SendingNewsLetterDAO.java b/src/java/fr/paris/lutece/plugins/newsletter/business/SendingNewsLetterDAO.java index 968d2e0..4338d48 100644 --- a/src/java/fr/paris/lutece/plugins/newsletter/business/SendingNewsLetterDAO.java +++ b/src/java/fr/paris/lutece/plugins/newsletter/business/SendingNewsLetterDAO.java @@ -45,279 +45,298 @@ */ public final class SendingNewsLetterDAO implements ISendingNewsLetterDAO { - // Constants - private static final String SQL_QUERY_INSERT = "INSERT INTO newsletter_sending ( id_sending, id_newsletter , date_sending, subscriber_count, html, email_subject ) VALUES ( ?, ?, ?, ?, ?, ? )"; - private static final String SQL_QUERY_DELETE = "DELETE FROM newsletter_sending WHERE id_sending = ? "; - private static final String SQL_QUERY_SELECT = "SELECT id_newsletter, date_sending, subscriber_count, html, email_subject FROM newsletter_sending WHERE id_sending = ? "; - private static final String SQL_QUERY_SELECT_LAST_SENDIND_BY_NEWSLETTER = " SELECT id_sending, id_newsletter, date_sending, subscriber_count, html, email_subject FROM newsletter_sending WHERE id_newsletter = ? ORDER BY date_sending DESC"; - private static final String SQL_QUERY_SELECT_ALL_SENDINDS = "SELECT id_sending, id_newsletter, date_sending, subscriber_count, html, email_subject FROM newsletter_sending ORDER BY date_sending DESC"; - private static final String SQL_QUERY_UPDATE = "UPDATE newsletter_sending SET date_sending = ? WHERE id_sending = ?"; - private static final String SQL_QUERY_CHECK_PRIMARY_KEY = "SELECT id_sending FROM newsletter_sending WHERE id_sending = ?"; - private static final String SQL_QUERY_NEW_PRIMARY_KEY = "SELECT max(id_sending) FROM newsletter_sending "; - - /////////////////////////////////////////////////////////////////////////////////////// - //Access methods to data - - /** - * Insert a new record in the table. - * - * @param sending the object to be inserted - * @param plugin the Plugin - */ - public void insert( SendingNewsLetter sending, Plugin plugin ) - { - int nNewPrimaryKey = newPrimaryKey( plugin ); - sending.setId( nNewPrimaryKey ); - - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin ); - - daoUtil.setInt( 1, sending.getId( ) ); - daoUtil.setInt( 2, sending.getNewsLetterId( ) ); - daoUtil.setTimestamp( 3, sending.getDate( ) ); - daoUtil.setInt( 4, sending.getCountSubscribers( ) ); - daoUtil.setString( 5, sending.getHtml( ) ); - daoUtil.setString( 6, sending.getEmailSubject( ) ); - daoUtil.executeUpdate( ); - daoUtil.free( ); - } - - /** - * Delete the record in the database - * - * @param nSendingId the sending Identifier - * @param plugin the Plugin - */ - public void delete( int nSendingId, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ); - daoUtil.setInt( 1, nSendingId ); - daoUtil.executeUpdate( ); - daoUtil.free( ); - } - - /** - * Loads the data from the database - * - * @param nSendingId the sending identifier - * @param plugin the Plugin - * @return an object SendingNewsLetter - */ - public SendingNewsLetter load( int nSendingId, Plugin plugin ) - { - SendingNewsLetter sending = null; - - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin ); - - daoUtil.setInt( 1, nSendingId ); - daoUtil.executeQuery( ); - - if ( daoUtil.next( ) ) - { - sending = new SendingNewsLetter( ); - sending.setId( nSendingId ); - sending.setNewsLetterId( daoUtil.getInt( 1 ) ); - sending.setDate( daoUtil.getTimestamp( 2 ) ); - sending.setCountSubscribers( daoUtil.getInt( 3 ) ); - sending.setHtml( daoUtil.getString( 4 ) ); - sending.setEmailSubject( daoUtil.getString( 5 ) ); - } - - daoUtil.free( ); - - return sending; - } - - /** - * Update the record in the table - * - * @param sending an instance of the class SendingNewsLetter - * @param plugin the Plugin - */ - public void store( SendingNewsLetter sending, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ); - daoUtil.setTimestamp( 1, sending.getDate( ) ); - daoUtil.setInt( 2, sending.getId( ) ); - daoUtil.executeUpdate( ); - daoUtil.free( ); - } - - /** - * Check the unicity of a primary key - * - * @param nKey the identifier to check - * @param plugin the Plugin - * @return true if the identifier exist and false if not - */ - boolean checkPrimaryKey( int nKey, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_CHECK_PRIMARY_KEY, plugin ); - daoUtil.setInt( 1, nKey ); - daoUtil.executeQuery( ); - - if ( !daoUtil.next( ) ) - { - daoUtil.free( ); - - return false; - } - - daoUtil.free( ); - - return true; - } - - /** - * Generate a new primary key to add a new sending - * - * @param plugin the Plugin - * @return the primary key - */ - int newPrimaryKey( Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PRIMARY_KEY, plugin ); - int nKey; - - daoUtil.executeQuery( ); - - if ( !daoUtil.next( ) ) - { - // If the table is empty - nKey = 1; - } - - nKey = daoUtil.getInt( 1 ) + 1; - - daoUtil.free( ); - - return nKey; - } - - /** - * Returns the last sending performed for the newsletter of given id - * @param newsletterId the newsletter id for wich we need the last sending - * @param plugin the plugin - * @return the last sending for the given newsletter id - null if no sending - * found - */ - public SendingNewsLetter selectLastSendingForNewsletterId( int newsletterId, Plugin plugin ) - { - SendingNewsLetter sending = null; - - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_LAST_SENDIND_BY_NEWSLETTER, plugin ); - daoUtil.setInt( 1, newsletterId ); - daoUtil.executeQuery( ); - - if ( daoUtil.next( ) ) - { - sending = new SendingNewsLetter( ); - sending.setId( daoUtil.getInt( 1 ) ); - sending.setNewsLetterId( daoUtil.getInt( 2 ) ); - sending.setDate( daoUtil.getTimestamp( 3 ) ); - sending.setCountSubscribers( daoUtil.getInt( 4 ) ); - sending.setHtml( daoUtil.getString( 5 ) ); - sending.setEmailSubject( daoUtil.getString( 6 ) ); - } - - daoUtil.free( ); - - return sending; - } - - /** - * Returns all the last sending performed for the newsletter of given id - * @param newsletterId the newsletter id for wich we need the last sending - * @param plugin the plugin - * @return the last sending for the given newsletter id - null if no sending - * found - */ - public List selectAllLastSendingForNewsletterId( int newsletterId, Plugin plugin ) - { - SendingNewsLetter sending = null; - List results = new ArrayList( ); - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_LAST_SENDIND_BY_NEWSLETTER, plugin ); - daoUtil.setInt( 1, newsletterId ); - daoUtil.executeQuery( ); - - while ( daoUtil.next( ) ) - { - sending = new SendingNewsLetter( ); - sending.setId( daoUtil.getInt( 1 ) ); - sending.setNewsLetterId( daoUtil.getInt( 2 ) ); - sending.setDate( daoUtil.getTimestamp( 3 ) ); - sending.setCountSubscribers( daoUtil.getInt( 4 ) ); - sending.setHtml( daoUtil.getString( 5 ) ); - sending.setEmailSubject( daoUtil.getString( 6 ) ); - - results.add( sending ); - } - - daoUtil.free( ); - - return results; - } - - /** - * Returns all the sendings in the database. - * - * @param plugin the plugin - * @return a list of SendingNewsLetter objects. - */ - public List findAllSendings( Plugin plugin ) - { - List results = new ArrayList( ); - - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL_SENDINDS, plugin ); - daoUtil.executeQuery( ); - - while ( daoUtil.next( ) ) - { - SendingNewsLetter sending = new SendingNewsLetter( ); - sending.setId( daoUtil.getInt( 1 ) ); - sending.setNewsLetterId( daoUtil.getInt( 2 ) ); - sending.setDate( daoUtil.getTimestamp( 3 ) ); - sending.setCountSubscribers( daoUtil.getInt( 4 ) ); - sending.setHtml( daoUtil.getString( 5 ) ); - sending.setEmailSubject( daoUtil.getString( 6 ) ); - - results.add( sending ); - } - - daoUtil.free( ); - - return results; - } - - /** - * Returns all the sendings in the database. - * @return a list of SendingNewsLetter objects. - * @param listId A list of newsletter id - * @param plugin the plugin - */ - public ArrayList findSendingsByIds( ArrayList listId, Plugin plugin ) - { - ArrayList results = new ArrayList( ); - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL_SENDINDS, plugin ); - daoUtil.executeQuery( ); - - while ( daoUtil.next( ) ) - { - if ( listId.contains( Integer.valueOf( daoUtil.getInt( 1 ) ) ) ) - { - SendingNewsLetter sending = new SendingNewsLetter( ); - sending.setId( daoUtil.getInt( 1 ) ); - sending.setNewsLetterId( daoUtil.getInt( 2 ) ); - sending.setDate( daoUtil.getTimestamp( 3 ) ); - sending.setCountSubscribers( daoUtil.getInt( 4 ) ); - sending.setHtml( daoUtil.getString( 5 ) ); - sending.setEmailSubject( daoUtil.getString( 6 ) ); - - results.add( sending ); - } - } - - daoUtil.free( ); - - return results; - } + // Constants + private static final String SQL_QUERY_INSERT = "INSERT INTO newsletter_sending ( id_sending, id_newsletter , date_sending, subscriber_count, html, email_subject ) VALUES ( ?, ?, ?, ?, ?, ? )"; + private static final String SQL_QUERY_DELETE = "DELETE FROM newsletter_sending WHERE id_sending = ? "; + private static final String SQL_QUERY_SELECT = "SELECT id_newsletter, date_sending, subscriber_count, html, email_subject FROM newsletter_sending WHERE id_sending = ? "; + private static final String SQL_QUERY_SELECT_LAST_SENDIND_BY_NEWSLETTER = " SELECT id_sending, id_newsletter, date_sending, subscriber_count, html, email_subject FROM newsletter_sending WHERE id_newsletter = ? ORDER BY date_sending DESC"; + private static final String SQL_QUERY_SELECT_ALL_SENDINDS = "SELECT id_sending, id_newsletter, date_sending, subscriber_count, html, email_subject FROM newsletter_sending ORDER BY date_sending DESC"; + private static final String SQL_QUERY_UPDATE = "UPDATE newsletter_sending SET date_sending = ? WHERE id_sending = ?"; + private static final String SQL_QUERY_CHECK_PRIMARY_KEY = "SELECT id_sending FROM newsletter_sending WHERE id_sending = ?"; + private static final String SQL_QUERY_NEW_PRIMARY_KEY = "SELECT max(id_sending) FROM newsletter_sending "; + + /////////////////////////////////////////////////////////////////////////////////////// + //Access methods to data + + /** + * Insert a new record in the table. + * + * @param sending the object to be inserted + * @param plugin the Plugin + */ + public void insert( SendingNewsLetter sending, Plugin plugin ) + { + int nNewPrimaryKey = newPrimaryKey( plugin ); + sending.setId( nNewPrimaryKey ); + + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin ) ) + { + + daoUtil.setInt( 1, sending.getId( ) ); + daoUtil.setInt( 2, sending.getNewsLetterId( ) ); + daoUtil.setTimestamp( 3, sending.getDate( ) ); + daoUtil.setInt( 4, sending.getCountSubscribers( ) ); + daoUtil.setString( 5, sending.getHtml( ) ); + daoUtil.setString( 6, sending.getEmailSubject( ) ); + daoUtil.executeUpdate( ); + daoUtil.free( ); + } + } + + /** + * Delete the record in the database + * + * @param nSendingId the sending Identifier + * @param plugin the Plugin + */ + public void delete( int nSendingId, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) ) + { + daoUtil.setInt( 1, nSendingId ); + daoUtil.executeUpdate( ); + daoUtil.free( ); + } + } + + /** + * Loads the data from the database + * + * @param nSendingId the sending identifier + * @param plugin the Plugin + * @return an object SendingNewsLetter + */ + public SendingNewsLetter load( int nSendingId, Plugin plugin ) + { + SendingNewsLetter sending = null; + + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin ) ) + { + + daoUtil.setInt( 1, nSendingId ); + daoUtil.executeQuery( ); + + if ( daoUtil.next( ) ) + { + sending = new SendingNewsLetter( ); + sending.setId( nSendingId ); + sending.setNewsLetterId( daoUtil.getInt( 1 ) ); + sending.setDate( daoUtil.getTimestamp( 2 ) ); + sending.setCountSubscribers( daoUtil.getInt( 3 ) ); + sending.setHtml( daoUtil.getString( 4 ) ); + sending.setEmailSubject( daoUtil.getString( 5 ) ); + } + + daoUtil.free( ); + + return sending; + } + } + + /** + * Update the record in the table + * + * @param sending an instance of the class SendingNewsLetter + * @param plugin the Plugin + */ + public void store( SendingNewsLetter sending, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ) ) + { + daoUtil.setTimestamp( 1, sending.getDate( ) ); + daoUtil.setInt( 2, sending.getId( ) ); + daoUtil.executeUpdate( ); + daoUtil.free( ); + } + } + + /** + * Check the unicity of a primary key + * + * @param nKey the identifier to check + * @param plugin the Plugin + * @return true if the identifier exist and false if not + */ + boolean checkPrimaryKey( int nKey, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_CHECK_PRIMARY_KEY, plugin ) ) + { + daoUtil.setInt( 1, nKey ); + daoUtil.executeQuery( ); + + if ( !daoUtil.next( ) ) + { + daoUtil.free( ); + + return false; + } + + daoUtil.free( ); + + return true; + } + } + + /** + * Generate a new primary key to add a new sending + * + * @param plugin the Plugin + * @return the primary key + */ + int newPrimaryKey( Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PRIMARY_KEY, plugin ) ) + { + int nKey; + + daoUtil.executeQuery( ); + + if ( !daoUtil.next( ) ) + { + // If the table is empty + nKey = 1; + } + + nKey = daoUtil.getInt( 1 ) + 1; + + daoUtil.free( ); + + return nKey; + } + } + + /** + * Returns the last sending performed for the newsletter of given id + * @param newsletterId the newsletter id for wich we need the last sending + * @param plugin the plugin + * @return the last sending for the given newsletter id - null if no sending + * found + */ + public SendingNewsLetter selectLastSendingForNewsletterId( int newsletterId, Plugin plugin ) + { + SendingNewsLetter sending = null; + + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_LAST_SENDIND_BY_NEWSLETTER, plugin ) ) + { + daoUtil.setInt( 1, newsletterId ); + daoUtil.executeQuery( ); + + if ( daoUtil.next( ) ) + { + sending = new SendingNewsLetter( ); + sending.setId( daoUtil.getInt( 1 ) ); + sending.setNewsLetterId( daoUtil.getInt( 2 ) ); + sending.setDate( daoUtil.getTimestamp( 3 ) ); + sending.setCountSubscribers( daoUtil.getInt( 4 ) ); + sending.setHtml( daoUtil.getString( 5 ) ); + sending.setEmailSubject( daoUtil.getString( 6 ) ); + } + + daoUtil.free( ); + + return sending; + } + } + + /** + * Returns all the last sending performed for the newsletter of given id + * @param newsletterId the newsletter id for wich we need the last sending + * @param plugin the plugin + * @return the last sending for the given newsletter id - null if no sending + * found + */ + public List selectAllLastSendingForNewsletterId( int newsletterId, Plugin plugin ) + { + SendingNewsLetter sending = null; + List results = new ArrayList<>( ); + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_LAST_SENDIND_BY_NEWSLETTER, plugin ) ) + { + daoUtil.setInt( 1, newsletterId ); + daoUtil.executeQuery( ); + + while ( daoUtil.next( ) ) + { + sending = new SendingNewsLetter( ); + sending.setId( daoUtil.getInt( 1 ) ); + sending.setNewsLetterId( daoUtil.getInt( 2 ) ); + sending.setDate( daoUtil.getTimestamp( 3 ) ); + sending.setCountSubscribers( daoUtil.getInt( 4 ) ); + sending.setHtml( daoUtil.getString( 5 ) ); + sending.setEmailSubject( daoUtil.getString( 6 ) ); + + results.add( sending ); + } + + daoUtil.free( ); + + return results; + } + } + + /** + * Returns all the sendings in the database. + * + * @param plugin the plugin + * @return a list of SendingNewsLetter objects. + */ + public List findAllSendings( Plugin plugin ) + { + List results = new ArrayList<>( ); + + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL_SENDINDS, plugin ) ) + { + daoUtil.executeQuery( ); + + while ( daoUtil.next( ) ) + { + SendingNewsLetter sending = new SendingNewsLetter( ); + sending.setId( daoUtil.getInt( 1 ) ); + sending.setNewsLetterId( daoUtil.getInt( 2 ) ); + sending.setDate( daoUtil.getTimestamp( 3 ) ); + sending.setCountSubscribers( daoUtil.getInt( 4 ) ); + sending.setHtml( daoUtil.getString( 5 ) ); + sending.setEmailSubject( daoUtil.getString( 6 ) ); + + results.add( sending ); + } + + daoUtil.free( ); + } + + return results; + } + + /** + * Returns all the sendings in the database. + * @return a list of SendingNewsLetter objects. + * @param listId A list of newsletter id + * @param plugin the plugin + */ + public ArrayList findSendingsByIds( ArrayList listId, Plugin plugin ) + { + ArrayList results = new ArrayList<>( ); + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL_SENDINDS, plugin ) ) + { + daoUtil.executeQuery( ); + + while ( daoUtil.next( ) ) + { + if ( listId.contains( Integer.valueOf( daoUtil.getInt( 1 ) ) ) ) + { + SendingNewsLetter sending = new SendingNewsLetter( ); + sending.setId( daoUtil.getInt( 1 ) ); + sending.setNewsLetterId( daoUtil.getInt( 2 ) ); + sending.setDate( daoUtil.getTimestamp( 3 ) ); + sending.setCountSubscribers( daoUtil.getInt( 4 ) ); + sending.setHtml( daoUtil.getString( 5 ) ); + sending.setEmailSubject( daoUtil.getString( 6 ) ); + + results.add( sending ); + } + } + + daoUtil.free( ); + } + return results; + } } diff --git a/src/java/fr/paris/lutece/plugins/newsletter/business/SubscriberDAO.java b/src/java/fr/paris/lutece/plugins/newsletter/business/SubscriberDAO.java index 61d078c..a3ce82f 100644 --- a/src/java/fr/paris/lutece/plugins/newsletter/business/SubscriberDAO.java +++ b/src/java/fr/paris/lutece/plugins/newsletter/business/SubscriberDAO.java @@ -45,355 +45,379 @@ */ public final class SubscriberDAO implements ISubscriberDAO { - // Constants - private static final String SQL_QUERY_INSERT = "INSERT INTO newsletter_subscriber_details ( id_subscriber , email ) VALUES ( ?, ? )"; - private static final String SQL_QUERY_DELETE = "DELETE FROM newsletter_subscriber_details WHERE id_subscriber = ? "; - private static final String SQL_QUERY_SELECT = "SELECT email FROM newsletter_subscriber_details WHERE id_subscriber = ? "; - private static final String SQL_QUERY_SELECT_ALL = "SELECT id_subscriber, email FROM newsletter_subscriber_details "; - private static final String SQL_QUERY_SELECT_SUBSCRIBERS_LIST = "SELECT id_subscriber , email FROM newsletter_subscriber_details "; - private static final String SQL_QUERY_SELECT_BY_EMAIL = "SELECT id_subscriber , email FROM newsletter_subscriber_details WHERE email = ? "; - private static final String SQL_QUERY_SELECT_SUBSCRIBERS_BY_NEWSLETTER = "SELECT a.id_subscriber , a.email, b.confirmed, b.date_subscription FROM newsletter_subscriber_details a, newsletter_subscriber b WHERE a.id_subscriber = b.id_subscriber AND b.id_newsletter = ? AND b.confirmed = 1 "; - private static final String SQL_QUERY_SELECT_SUBSCRIBERS_FOR_SENDING = " SELECT a.id_subscriber , a.email, b.confirmed, b.date_subscription FROM newsletter_subscriber_details a, newsletter_subscriber b WHERE a.id_subscriber = b.id_subscriber AND b.id_newsletter = ? AND a.email LIKE ? AND confirmed = 1 ORDER BY a.email LIMIT ? OFFSET ? "; - private static final String SQL_QUERY_SELECT_SUBSCRIBERS = " SELECT a.id_subscriber , a.email, b.confirmed, b.date_subscription FROM newsletter_subscriber_details a, newsletter_subscriber b WHERE a.id_subscriber = b.id_subscriber AND b.id_newsletter = ? AND a.email LIKE ? ORDER BY a.email LIMIT ? OFFSET ? "; - private static final String SQL_QUERY_COUNT_NEWSLETTERS_BY_SUBSCRIBER = "SELECT count(*) FROM newsletter_subscriber where id_subscriber = ? "; - private static final String SQL_QUERY_CHECK_PRIMARY_KEY = "SELECT id_subscriber FROM newsletter_subscriber_details WHERE id_subscriber = ?"; - private static final String SQL_QUERY_NEW_PRIMARY_KEY = "SELECT max(id_subscriber) FROM newsletter_subscriber_details "; - - /////////////////////////////////////////////////////////////////////////////////////// - //Access methods to data - - /** - * Insert a new record in the table. - * - * @param subscriber the object to be inserted - * @param plugin the Plugin - */ - public void insert( Subscriber subscriber, Plugin plugin ) - { - int nNewPrimaryKey = newPrimaryKey( plugin ); - subscriber.setId( nNewPrimaryKey ); - - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin ); - - daoUtil.setInt( 1, subscriber.getId( ) ); - daoUtil.setString( 2, subscriber.getEmail( ) ); - - daoUtil.executeUpdate( ); - daoUtil.free( ); - } - - /** - * Delete a record from the table - * - * @param nId the subscriber's identifier - * @param plugin the Plugin - */ - public void delete( int nId, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ); - daoUtil.setInt( 1, nId ); - daoUtil.executeUpdate( ); - daoUtil.free( ); - } - - /** - * loads data from a subscriber's identifier - * - * @param nId the subscriber's identifier - * @param plugin the Plugin - * @return an object Subscriber - */ - public Subscriber load( int nId, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin ); - daoUtil.setInt( 1, nId ); - daoUtil.executeQuery( ); - - Subscriber subscriber = new Subscriber( ); - - if ( daoUtil.next( ) ) - { - subscriber.setId( nId ); - subscriber.setEmail( daoUtil.getString( 1 ) ); - } - - daoUtil.free( ); - - return subscriber; - } - - /** - * Check the unicity of a primary key - * - * @param nKey the primary key to be checked - * @param plugin the Plugin - * @return true if the key exists, false if not - */ - boolean checkPrimaryKey( int nKey, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_CHECK_PRIMARY_KEY, plugin ); - daoUtil.setInt( 1, nKey ); - daoUtil.executeQuery( ); - - if ( !daoUtil.next( ) ) - { - daoUtil.free( ); - - return false; - } - - daoUtil.free( ); - - return true; - } - - /** - * Generates a new primary key - * - * @param plugin the Plugin - * @return the new primary key - */ - int newPrimaryKey( Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PRIMARY_KEY, plugin ); - - int nKey; - - daoUtil.executeQuery( ); - - if ( !daoUtil.next( ) ) - { - // If the table is empty - nKey = 1; - } - - nKey = daoUtil.getInt( 1 ) + 1; - - daoUtil.free( ); - - return nKey; - } - - /** - * Loads the list of subscribers - * - * @param plugin the Plugin - * @return a collection of objects Subscriber - */ - public Collection selectAll( Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL, plugin ); - daoUtil.executeQuery( ); - - ArrayList list = new ArrayList( ); - - while ( daoUtil.next( ) ) - { - Subscriber subscriber = new Subscriber( ); - subscriber.setId( daoUtil.getInt( 1 ) ); - subscriber.setEmail( daoUtil.getString( 2 ) ); - subscriber.setConfirmed( daoUtil.getBoolean( 3 ) ); - list.add( subscriber ); - } - - daoUtil.free( ); - - return list; - } - - /** - * Finds a subscriber from his email - * - * @param strEmail the subscriber's email - * @param plugin the Plugin - * @return a subscriber object if it exists, null if not - */ - public Subscriber selectByEmail( String strEmail, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_EMAIL, plugin ); - daoUtil.setString( 1, strEmail.toLowerCase( ) ); - daoUtil.executeQuery( ); - - Subscriber subscriber = null; - - if ( daoUtil.next( ) ) - { - subscriber = new Subscriber( ); - subscriber.setId( daoUtil.getInt( 1 ) ); - subscriber.setEmail( daoUtil.getString( 2 ) ); - } - - daoUtil.free( ); - - return subscriber; - } - - /** - * loads the list of subscribers for a newsletter - * - * @param nNewsLetterId the newsletter identifier - * @param plugin the Plugin - * @return a collection of subscribers - */ - public Collection selectSubscribers( int nNewsLetterId, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_SUBSCRIBERS_BY_NEWSLETTER, plugin ); - daoUtil.setInt( 1, nNewsLetterId ); - daoUtil.executeQuery( ); - - ArrayList list = new ArrayList( ); - - while ( daoUtil.next( ) ) - { - Subscriber subscriber = new Subscriber( ); - subscriber.setId( daoUtil.getInt( 1 ) ); - subscriber.setEmail( daoUtil.getString( 2 ) ); - subscriber.setConfirmed( daoUtil.getBoolean( 3 ) ); - subscriber.setDateSubscription( daoUtil.getTimestamp( 4 ) ); - list.add( subscriber ); - } - - daoUtil.free( ); - - return list; - } - - /** - * loads the list of subscribers for a newsletter - * - * @param nNewsLetterId the newsletter identifier - * @param strSearchString gets all the subscribers if null or empty - * and gets the subscribers whith an email containing this string otherwise - * @param nBegin the rank of the first subscriber to return - * @param nEnd the maximum number of suscribers to return - * @param plugin the Plugin - * @return a collection of subscribers - */ - public Collection selectSubscribers( int nNewsLetterId, String strSearchString, int nBegin, int nEnd, - Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_SUBSCRIBERS, plugin ); - - daoUtil.setInt( 1, nNewsLetterId ); - daoUtil.setString( 2, "%" + strSearchString + "%" ); - daoUtil.setInt( 3, nEnd ); - daoUtil.setInt( 4, nBegin ); - - daoUtil.executeQuery( ); - - ArrayList list = new ArrayList( ); - - while ( daoUtil.next( ) ) - { - Subscriber subscriber = new Subscriber( ); - subscriber.setId( daoUtil.getInt( 1 ) ); - subscriber.setEmail( daoUtil.getString( 2 ) ); - subscriber.setConfirmed( daoUtil.getBoolean( 3 ) ); - subscriber.setDateSubscription( daoUtil.getTimestamp( 4 ) ); - - list.add( subscriber ); - } - - daoUtil.free( ); - - return list; - } - - /** - * loads the list of subscribers for a newsletter - * - * @param nNewsLetterId the newsletter identifier - * @param strSearchString gets all the subscribers if null or empty - * and gets the subscribers whith an email containing this string otherwise - * @param nBegin the rank of the first subscriber to return - * @param nEnd the maximum number of suscribers to return - * @param plugin the Plugin - * @return a collection of subscribers - */ - public Collection selectSubscribersForSending( int nNewsLetterId, String strSearchString, int nBegin, - int nEnd, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_SUBSCRIBERS_FOR_SENDING, plugin ); - - daoUtil.setInt( 1, nNewsLetterId ); - daoUtil.setString( 2, "%" + strSearchString + "%" ); - daoUtil.setInt( 3, nEnd ); - daoUtil.setInt( 4, nBegin ); - - daoUtil.executeQuery( ); - - ArrayList list = new ArrayList( ); - - while ( daoUtil.next( ) ) - { - Subscriber subscriber = new Subscriber( ); - subscriber.setId( daoUtil.getInt( 1 ) ); - subscriber.setEmail( daoUtil.getString( 2 ) ); - subscriber.setConfirmed( daoUtil.getBoolean( 3 ) ); - subscriber.setDateSubscription( daoUtil.getTimestamp( 4 ) ); - - list.add( subscriber ); - } - - daoUtil.free( ); - - return list; - } - - /** - * Returns, for a subscriber, the number of his subscriptions - * - * @param nSubscriberId the subscriber's identifier - * @param plugin the Plugin - * @return the number of subscriptions - */ - public int selectNewsLetters( int nSubscriberId, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_COUNT_NEWSLETTERS_BY_SUBSCRIBER, plugin ); - - int nCount; - - daoUtil.setInt( 1, nSubscriberId ); - - daoUtil.executeQuery( ); - - if ( !daoUtil.next( ) ) - { - // If the table is empty - nCount = 0; - } - - nCount = daoUtil.getInt( 1 ); - - daoUtil.free( ); - - return nCount; - } - - /** - * loads the list of subscribers - * - * @param plugin the Plugin - * @return a collection of subscribers - */ - public Collection selectSubscribersList( Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_SUBSCRIBERS_LIST, plugin ); - - daoUtil.executeQuery( ); - - ArrayList list = new ArrayList( ); - - while ( daoUtil.next( ) ) - { - Subscriber subscriber = new Subscriber( ); - subscriber.setId( daoUtil.getInt( 1 ) ); - subscriber.setEmail( daoUtil.getString( 2 ) ); - list.add( subscriber ); - } - - daoUtil.free( ); - - return list; - } + // Constants + private static final String SQL_QUERY_INSERT = "INSERT INTO newsletter_subscriber_details ( id_subscriber , email ) VALUES ( ?, ? )"; + private static final String SQL_QUERY_DELETE = "DELETE FROM newsletter_subscriber_details WHERE id_subscriber = ? "; + private static final String SQL_QUERY_SELECT = "SELECT email FROM newsletter_subscriber_details WHERE id_subscriber = ? "; + private static final String SQL_QUERY_SELECT_ALL = "SELECT id_subscriber, email FROM newsletter_subscriber_details "; + private static final String SQL_QUERY_SELECT_SUBSCRIBERS_LIST = "SELECT id_subscriber , email FROM newsletter_subscriber_details "; + private static final String SQL_QUERY_SELECT_BY_EMAIL = "SELECT id_subscriber , email FROM newsletter_subscriber_details WHERE email = ? "; + private static final String SQL_QUERY_SELECT_SUBSCRIBERS_BY_NEWSLETTER = "SELECT a.id_subscriber , a.email, b.confirmed, b.date_subscription FROM newsletter_subscriber_details a, newsletter_subscriber b WHERE a.id_subscriber = b.id_subscriber AND b.id_newsletter = ? AND b.confirmed = 1 "; + private static final String SQL_QUERY_SELECT_SUBSCRIBERS_FOR_SENDING = " SELECT a.id_subscriber , a.email, b.confirmed, b.date_subscription FROM newsletter_subscriber_details a, newsletter_subscriber b WHERE a.id_subscriber = b.id_subscriber AND b.id_newsletter = ? AND a.email LIKE ? AND confirmed = 1 ORDER BY a.email LIMIT ? OFFSET ? "; + private static final String SQL_QUERY_SELECT_SUBSCRIBERS = " SELECT a.id_subscriber , a.email, b.confirmed, b.date_subscription FROM newsletter_subscriber_details a, newsletter_subscriber b WHERE a.id_subscriber = b.id_subscriber AND b.id_newsletter = ? AND a.email LIKE ? ORDER BY a.email LIMIT ? OFFSET ? "; + private static final String SQL_QUERY_COUNT_NEWSLETTERS_BY_SUBSCRIBER = "SELECT count(*) FROM newsletter_subscriber where id_subscriber = ? "; + private static final String SQL_QUERY_CHECK_PRIMARY_KEY = "SELECT id_subscriber FROM newsletter_subscriber_details WHERE id_subscriber = ?"; + private static final String SQL_QUERY_NEW_PRIMARY_KEY = "SELECT max(id_subscriber) FROM newsletter_subscriber_details "; + + /////////////////////////////////////////////////////////////////////////////////////// + //Access methods to data + + /** + * Insert a new record in the table. + * + * @param subscriber the object to be inserted + * @param plugin the Plugin + */ + public void insert( Subscriber subscriber, Plugin plugin ) + { + int nNewPrimaryKey = newPrimaryKey( plugin ); + subscriber.setId( nNewPrimaryKey ); + + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin ) ) + { + + daoUtil.setInt( 1, subscriber.getId( ) ); + daoUtil.setString( 2, subscriber.getEmail( ) ); + + daoUtil.executeUpdate( ); + daoUtil.free( ); + } + } + + /** + * Delete a record from the table + * + * @param nId the subscriber's identifier + * @param plugin the Plugin + */ + public void delete( int nId, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) ) + { + daoUtil.setInt( 1, nId ); + daoUtil.executeUpdate( ); + daoUtil.free( ); + } + } + + /** + * loads data from a subscriber's identifier + * + * @param nId the subscriber's identifier + * @param plugin the Plugin + * @return an object Subscriber + */ + public Subscriber load( int nId, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin ) ) + { + daoUtil.setInt( 1, nId ); + daoUtil.executeQuery( ); + + Subscriber subscriber = new Subscriber( ); + + if ( daoUtil.next( ) ) + { + subscriber.setId( nId ); + subscriber.setEmail( daoUtil.getString( 1 ) ); + } + + daoUtil.free( ); + + return subscriber; + } + } + + /** + * Check the unicity of a primary key + * + * @param nKey the primary key to be checked + * @param plugin the Plugin + * @return true if the key exists, false if not + */ + boolean checkPrimaryKey( int nKey, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_CHECK_PRIMARY_KEY, plugin ) ) + { + daoUtil.setInt( 1, nKey ); + daoUtil.executeQuery( ); + + if ( !daoUtil.next( ) ) + { + daoUtil.free( ); + + return false; + } + + daoUtil.free( ); + + return true; + } + } + + /** + * Generates a new primary key + * + * @param plugin the Plugin + * @return the new primary key + */ + int newPrimaryKey( Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PRIMARY_KEY, plugin ) ) + { + + int nKey; + + daoUtil.executeQuery( ); + + if ( !daoUtil.next( ) ) + { + // If the table is empty + nKey = 1; + } + + nKey = daoUtil.getInt( 1 ) + 1; + + daoUtil.free( ); + + return nKey; + } + } + + /** + * Loads the list of subscribers + * + * @param plugin the Plugin + * @return a collection of objects Subscriber + */ + public Collection selectAll( Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL, plugin ) ) + { + daoUtil.executeQuery( ); + + ArrayList list = new ArrayList<>( ); + + while ( daoUtil.next( ) ) + { + Subscriber subscriber = new Subscriber( ); + subscriber.setId( daoUtil.getInt( 1 ) ); + subscriber.setEmail( daoUtil.getString( 2 ) ); + subscriber.setConfirmed( daoUtil.getBoolean( 3 ) ); + list.add( subscriber ); + } + + daoUtil.free( ); + + return list; + } + } + + /** + * Finds a subscriber from his email + * + * @param strEmail the subscriber's email + * @param plugin the Plugin + * @return a subscriber object if it exists, null if not + */ + public Subscriber selectByEmail( String strEmail, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_EMAIL, plugin ) ) + { + daoUtil.setString( 1, strEmail.toLowerCase( ) ); + daoUtil.executeQuery( ); + + Subscriber subscriber = null; + + if ( daoUtil.next( ) ) + { + subscriber = new Subscriber( ); + subscriber.setId( daoUtil.getInt( 1 ) ); + subscriber.setEmail( daoUtil.getString( 2 ) ); + } + + daoUtil.free( ); + + return subscriber; + } + } + + /** + * loads the list of subscribers for a newsletter + * + * @param nNewsLetterId the newsletter identifier + * @param plugin the Plugin + * @return a collection of subscribers + */ + public Collection selectSubscribers( int nNewsLetterId, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_SUBSCRIBERS_BY_NEWSLETTER, plugin ) ) + { + daoUtil.setInt( 1, nNewsLetterId ); + daoUtil.executeQuery( ); + + ArrayList list = new ArrayList<>( ); + + while ( daoUtil.next( ) ) + { + Subscriber subscriber = new Subscriber( ); + subscriber.setId( daoUtil.getInt( 1 ) ); + subscriber.setEmail( daoUtil.getString( 2 ) ); + subscriber.setConfirmed( daoUtil.getBoolean( 3 ) ); + subscriber.setDateSubscription( daoUtil.getTimestamp( 4 ) ); + list.add( subscriber ); + } + + daoUtil.free( ); + + return list; + } + } + + /** + * loads the list of subscribers for a newsletter + * + * @param nNewsLetterId the newsletter identifier + * @param strSearchString gets all the subscribers if null or empty + * and gets the subscribers whith an email containing this string otherwise + * @param nBegin the rank of the first subscriber to return + * @param nEnd the maximum number of suscribers to return + * @param plugin the Plugin + * @return a collection of subscribers + */ + public Collection selectSubscribers( int nNewsLetterId, String strSearchString, int nBegin, int nEnd, + Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_SUBSCRIBERS, plugin ) ) + { + + daoUtil.setInt( 1, nNewsLetterId ); + daoUtil.setString( 2, "%" + strSearchString + "%" ); + daoUtil.setInt( 3, nEnd ); + daoUtil.setInt( 4, nBegin ); + + daoUtil.executeQuery( ); + + ArrayList list = new ArrayList<>( ); + + while ( daoUtil.next( ) ) + { + Subscriber subscriber = new Subscriber( ); + subscriber.setId( daoUtil.getInt( 1 ) ); + subscriber.setEmail( daoUtil.getString( 2 ) ); + subscriber.setConfirmed( daoUtil.getBoolean( 3 ) ); + subscriber.setDateSubscription( daoUtil.getTimestamp( 4 ) ); + + list.add( subscriber ); + } + + daoUtil.free( ); + + return list; + } + } + + /** + * loads the list of subscribers for a newsletter + * + * @param nNewsLetterId the newsletter identifier + * @param strSearchString gets all the subscribers if null or empty + * and gets the subscribers whith an email containing this string otherwise + * @param nBegin the rank of the first subscriber to return + * @param nEnd the maximum number of suscribers to return + * @param plugin the Plugin + * @return a collection of subscribers + */ + public Collection selectSubscribersForSending( int nNewsLetterId, String strSearchString, int nBegin, + int nEnd, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_SUBSCRIBERS_FOR_SENDING, plugin ) ) + { + + daoUtil.setInt( 1, nNewsLetterId ); + daoUtil.setString( 2, "%" + strSearchString + "%" ); + daoUtil.setInt( 3, nEnd ); + daoUtil.setInt( 4, nBegin ); + + daoUtil.executeQuery( ); + + ArrayList list = new ArrayList( ); + + while ( daoUtil.next( ) ) + { + Subscriber subscriber = new Subscriber( ); + subscriber.setId( daoUtil.getInt( 1 ) ); + subscriber.setEmail( daoUtil.getString( 2 ) ); + subscriber.setConfirmed( daoUtil.getBoolean( 3 ) ); + subscriber.setDateSubscription( daoUtil.getTimestamp( 4 ) ); + + list.add( subscriber ); + } + + daoUtil.free( ); + + return list; + } + } + + /** + * Returns, for a subscriber, the number of his subscriptions + * + * @param nSubscriberId the subscriber's identifier + * @param plugin the Plugin + * @return the number of subscriptions + */ + public int selectNewsLetters( int nSubscriberId, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_COUNT_NEWSLETTERS_BY_SUBSCRIBER, plugin ) ) + { + + int nCount; + + daoUtil.setInt( 1, nSubscriberId ); + + daoUtil.executeQuery( ); + + if ( !daoUtil.next( ) ) + { + // If the table is empty + nCount = 0; + } + + nCount = daoUtil.getInt( 1 ); + + daoUtil.free( ); + + return nCount; + } + } + + /** + * loads the list of subscribers + * + * @param plugin the Plugin + * @return a collection of subscribers + */ + public Collection selectSubscribersList( Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_SUBSCRIBERS_LIST, plugin ) ) + { + + daoUtil.executeQuery( ); + + ArrayList list = new ArrayList<>( ); + + while ( daoUtil.next( ) ) + { + Subscriber subscriber = new Subscriber( ); + subscriber.setId( daoUtil.getInt( 1 ) ); + subscriber.setEmail( daoUtil.getString( 2 ) ); + list.add( subscriber ); + } + + daoUtil.free( ); + + return list; + } + } } diff --git a/src/java/fr/paris/lutece/plugins/newsletter/business/portlet/NewsLetterArchivePortletDAO.java b/src/java/fr/paris/lutece/plugins/newsletter/business/portlet/NewsLetterArchivePortletDAO.java index dddfa4b..b5e2971 100644 --- a/src/java/fr/paris/lutece/plugins/newsletter/business/portlet/NewsLetterArchivePortletDAO.java +++ b/src/java/fr/paris/lutece/plugins/newsletter/business/portlet/NewsLetterArchivePortletDAO.java @@ -47,137 +47,147 @@ */ public final class NewsLetterArchivePortletDAO implements INewsLetterArchivePortletDAO { - // Constants - private static final String SQL_QUERY_SELECT = "SELECT id_portlet FROM core_portlet WHERE id_portlet = ?"; - private static final String SQL_QUERY_SELECT_SENDINGS_BY_PORTLET = "SELECT id_sending FROM newsletter_portlet_archive WHERE id_portlet = ?"; - private static final String SQL_QUERY_INSERT_SENDING = "INSERT INTO newsletter_portlet_archive ( id_portlet, id_sending ) VALUES ( ?, ? )"; - private static final String SQL_QUERY_DELETE_SENDING = "DELETE FROM newsletter_portlet_archive WHERE id_portlet = ? AND id_sending = ?"; - private static final String SQL_QUERY_DELETE = "DELETE FROM newsletter_portlet_archive WHERE id_portlet=? "; - - /////////////////////////////////////////////////////////////////////////////////////// - //Access methods to data - - /** - * Inserts a new record in the table. Not implemented. - * - * @param portlet - * the object to be inserted - */ - public void insert( Portlet portlet ) - { - // Not implemented. - } - - /** - * Deletes a record from the table. - * - * @param nPortletId the portlet id - * - */ - public void delete( int nPortletId ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, PluginService.getPlugin( NewsletterPlugin.PLUGIN_NAME ) ); - daoUtil.setInt( 1, nPortletId ); - daoUtil.executeUpdate( ); - daoUtil.free( ); - } - - /** - * Loads the data of the portlet from the table. - * - * @param nPortletId the portlet id - * @return the Portlet object - */ - public Portlet load( int nPortletId ) - { - NewsLetterArchivePortlet portlet = new NewsLetterArchivePortlet( ); - - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ); - - daoUtil.setInt( 1, nPortletId ); - - daoUtil.executeQuery( ); - - if ( daoUtil.next( ) ) - { - portlet.setId( nPortletId ); - } - - daoUtil.free( ); - - return portlet; - } - - /** - * Updates the record in the table. Not implemented. - * - * @param portlet - * the instance of Portlet class to be updated - */ - public void store( Portlet portlet ) - { - // Not implemented. - } - - /** - * Associates a new sending to a given portlet. - * - * @param nPortletId - * the identifier of the portlet. - * @param nSendingId - * the identifier of the sending. - */ - public void insertSending( int nPortletId, int nSendingId, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_SENDING, plugin ); - daoUtil.setInt( 1, nPortletId ); - daoUtil.setInt( 2, nSendingId ); - - daoUtil.executeUpdate( ); - daoUtil.free( ); - } - - /** - * De-associate a sending from a given portlet. - * - * @param nPortletId - * the identifier of the portlet. - * @param nSendingId - * the identifier of the sending. - */ - public void removeSending( int nPortletId, int nSendingId, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_SENDING, plugin ); - daoUtil.setInt( 1, nPortletId ); - daoUtil.setInt( 2, nSendingId ); - - daoUtil.executeUpdate( ); - daoUtil.free( ); - } - - /** - * Returns all the sendings associated with a given portlet. - * - * @param nPortletId - * the identifier of the portlet. - * @return a Set of Integer objects containing the identifers of the - * sendings. - */ - public ArrayList findSendingsInPortlet( int nPortletId, Plugin plugin ) - { - ArrayList results = new ArrayList( ); - - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_SENDINGS_BY_PORTLET, plugin ); - daoUtil.setInt( 1, nPortletId ); - daoUtil.executeQuery( ); - - while ( daoUtil.next( ) ) - { - results.add( Integer.valueOf( daoUtil.getInt( 1 ) ) ); - } - - daoUtil.free( ); - - return results; - } + // Constants + private static final String SQL_QUERY_SELECT = "SELECT id_portlet FROM core_portlet WHERE id_portlet = ?"; + private static final String SQL_QUERY_SELECT_SENDINGS_BY_PORTLET = "SELECT id_sending FROM newsletter_portlet_archive WHERE id_portlet = ?"; + private static final String SQL_QUERY_INSERT_SENDING = "INSERT INTO newsletter_portlet_archive ( id_portlet, id_sending ) VALUES ( ?, ? )"; + private static final String SQL_QUERY_DELETE_SENDING = "DELETE FROM newsletter_portlet_archive WHERE id_portlet = ? AND id_sending = ?"; + private static final String SQL_QUERY_DELETE = "DELETE FROM newsletter_portlet_archive WHERE id_portlet=? "; + + /////////////////////////////////////////////////////////////////////////////////////// + //Access methods to data + + /** + * Inserts a new record in the table. Not implemented. + * + * @param portlet + * the object to be inserted + */ + public void insert( Portlet portlet ) + { + // Not implemented. + } + + /** + * Deletes a record from the table. + * + * @param nPortletId the portlet id + * + */ + public void delete( int nPortletId ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, PluginService.getPlugin( NewsletterPlugin.PLUGIN_NAME ) ) ) + { + daoUtil.setInt( 1, nPortletId ); + daoUtil.executeUpdate( ); + daoUtil.free( ); + } + } + + /** + * Loads the data of the portlet from the table. + * + * @param nPortletId the portlet id + * @return the Portlet object + */ + public Portlet load( int nPortletId ) + { + NewsLetterArchivePortlet portlet = new NewsLetterArchivePortlet( ); + + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) ) + { + + daoUtil.setInt( 1, nPortletId ); + + daoUtil.executeQuery( ); + + if ( daoUtil.next( ) ) + { + portlet.setId( nPortletId ); + } + + daoUtil.free( ); + } + + return portlet; + } + + /** + * Updates the record in the table. Not implemented. + * + * @param portlet + * the instance of Portlet class to be updated + */ + public void store( Portlet portlet ) + { + // Not implemented. + } + + /** + * Associates a new sending to a given portlet. + * + * @param nPortletId + * the identifier of the portlet. + * @param nSendingId + * the identifier of the sending. + */ + public void insertSending( int nPortletId, int nSendingId, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_SENDING, plugin ) ) + { + daoUtil.setInt( 1, nPortletId ); + daoUtil.setInt( 2, nSendingId ); + + daoUtil.executeUpdate( ); + daoUtil.free( ); + } + } + + /** + * De-associate a sending from a given portlet. + * + * @param nPortletId + * the identifier of the portlet. + * @param nSendingId + * the identifier of the sending. + */ + public void removeSending( int nPortletId, int nSendingId, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_SENDING, plugin ) ) + { + daoUtil.setInt( 1, nPortletId ); + daoUtil.setInt( 2, nSendingId ); + + daoUtil.executeUpdate( ); + daoUtil.free( ); + } + } + + /** + * Returns all the sendings associated with a given portlet. + * + * @param nPortletId + * the identifier of the portlet. + * @return a Set of Integer objects containing the identifers of the + * sendings. + */ + public ArrayList findSendingsInPortlet( int nPortletId, Plugin plugin ) + { + ArrayList results = new ArrayList<>( ); + + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_SENDINGS_BY_PORTLET, plugin ) ) + { + daoUtil.setInt( 1, nPortletId ); + daoUtil.executeQuery( ); + + while ( daoUtil.next( ) ) + { + results.add( Integer.valueOf( daoUtil.getInt( 1 ) ) ); + } + + daoUtil.free( ); + } + + return results; + } } diff --git a/src/java/fr/paris/lutece/plugins/newsletter/business/portlet/NewsLetterSubscriptionPortletDAO.java b/src/java/fr/paris/lutece/plugins/newsletter/business/portlet/NewsLetterSubscriptionPortletDAO.java index ed67fbf..d8dc2a9 100644 --- a/src/java/fr/paris/lutece/plugins/newsletter/business/portlet/NewsLetterSubscriptionPortletDAO.java +++ b/src/java/fr/paris/lutece/plugins/newsletter/business/portlet/NewsLetterSubscriptionPortletDAO.java @@ -48,140 +48,150 @@ */ public final class NewsLetterSubscriptionPortletDAO implements INewsLetterSubscriptionPortletDAO { - // Constants - private static final String SQL_QUERY_SELECT = "SELECT id_portlet FROM core_portlet WHERE id_portlet = ?"; - private static final String SQL_QUERY_SELECT_SUBSCRIPTION_BY_PORTLET = "SELECT id_newsletter FROM newsletter_portlet_subscribe WHERE id_portlet = ?"; - private static final String SQL_QUERY_INSERT_NEWSLETTER = "INSERT INTO newsletter_portlet_subscribe( id_portlet, id_newsletter ) VALUES ( ?, ? )"; - private static final String SQL_QUERY_DELETE_NEWSLETTER = "DELETE FROM newsletter_portlet_subscribe WHERE id_portlet = ? AND id_newsletter = ?"; - private static final String SQL_QUERY_DELETE = "DELETE FROM newsletter_portlet_subscribe WHERE id_portlet=? "; - - /////////////////////////////////////////////////////////////////////////////////////// - //Access methods to data - - /** - * Inserts a new record in the table. Not implemented. - * - * @param portlet - * the object to be inserted - */ - public void insert( Portlet portlet ) - { - // Not implemented. - } - - /** - * Deletes a record from the table. - * - * @param nPortletId the portlet id - * - */ - public void delete( int nPortletId ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, PluginService.getPlugin( NewsletterPlugin.PLUGIN_NAME ) ); - daoUtil.setInt( 1, nPortletId ); - daoUtil.executeUpdate( ); - daoUtil.free( ); - } - - /** - * Loads the data of the portlet from the table. - * - * @param nPortletId the portlet id - * @return the Portlet object - */ - public Portlet load( int nPortletId ) - { - NewsLetterSubscriptionPortlet portlet = new NewsLetterSubscriptionPortlet( ); - - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ); - - daoUtil.setInt( 1, nPortletId ); - - daoUtil.executeQuery( ); - - if ( daoUtil.next( ) ) - { - portlet.setId( nPortletId ); - } - - daoUtil.free( ); - - return portlet; - } - - /** - * Updates the record in the table. Not implemented. - * - * @param portlet - * the instance of Portlet class to be updated - */ - public void store( Portlet portlet ) - { - // Not implemented. - } - - /** - * Associates a new subscription to a given portlet. - * - * @param nPortletId - * the identifier of the portlet. - * @param nNewsletterId - * the identifier of the subscription. - */ - public void insertSubscription( int nPortletId, int nNewsletterId ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_NEWSLETTER, - PluginService.getPlugin( NewsletterPlugin.PLUGIN_NAME ) ); - daoUtil.setInt( 1, nPortletId ); - daoUtil.setInt( 2, nNewsletterId ); - - daoUtil.executeUpdate( ); - daoUtil.free( ); - } - - /** - * De-associate a subscription from a given portlet. - * - * @param nPortletId - * the identifier of the portlet. - * @param nNewsletterId - * the identifier of the subscription. - */ - public void removeSubscription( int nPortletId, int nNewsletterId ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_NEWSLETTER, - PluginService.getPlugin( NewsletterPlugin.PLUGIN_NAME ) ); - daoUtil.setInt( 1, nPortletId ); - daoUtil.setInt( 2, nNewsletterId ); - - daoUtil.executeUpdate( ); - daoUtil.free( ); - } - - /** - * Returns all the newsletters associated to a given portlet. - * - * @param nPortletId - * the identifier of the portlet. - * @return a Set of Integer objects containing the identifers of the - * susbscriptions. - */ - public Set findSelectedNewsletters( int nPortletId ) - { - HashSet results = new HashSet( ); - - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_SUBSCRIPTION_BY_PORTLET, - PluginService.getPlugin( NewsletterPlugin.PLUGIN_NAME ) ); - daoUtil.setInt( 1, nPortletId ); - daoUtil.executeQuery( ); - - while ( daoUtil.next( ) ) - { - results.add( Integer.valueOf( daoUtil.getInt( 1 ) ) ); - } - - daoUtil.free( ); - - return results; - } + // Constants + private static final String SQL_QUERY_SELECT = "SELECT id_portlet FROM core_portlet WHERE id_portlet = ?"; + private static final String SQL_QUERY_SELECT_SUBSCRIPTION_BY_PORTLET = "SELECT id_newsletter FROM newsletter_portlet_subscribe WHERE id_portlet = ?"; + private static final String SQL_QUERY_INSERT_NEWSLETTER = "INSERT INTO newsletter_portlet_subscribe( id_portlet, id_newsletter ) VALUES ( ?, ? )"; + private static final String SQL_QUERY_DELETE_NEWSLETTER = "DELETE FROM newsletter_portlet_subscribe WHERE id_portlet = ? AND id_newsletter = ?"; + private static final String SQL_QUERY_DELETE = "DELETE FROM newsletter_portlet_subscribe WHERE id_portlet=? "; + + /////////////////////////////////////////////////////////////////////////////////////// + //Access methods to data + + /** + * Inserts a new record in the table. Not implemented. + * + * @param portlet + * the object to be inserted + */ + public void insert( Portlet portlet ) + { + // Not implemented. + } + + /** + * Deletes a record from the table. + * + * @param nPortletId the portlet id + * + */ + public void delete( int nPortletId ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, PluginService.getPlugin( NewsletterPlugin.PLUGIN_NAME ) ) ) + { + daoUtil.setInt( 1, nPortletId ); + daoUtil.executeUpdate( ); + daoUtil.free( ); + } + } + + /** + * Loads the data of the portlet from the table. + * + * @param nPortletId the portlet id + * @return the Portlet object + */ + public Portlet load( int nPortletId ) + { + NewsLetterSubscriptionPortlet portlet = new NewsLetterSubscriptionPortlet( ); + + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) ) + { + + daoUtil.setInt( 1, nPortletId ); + + daoUtil.executeQuery( ); + + if ( daoUtil.next( ) ) + { + portlet.setId( nPortletId ); + } + + daoUtil.free( ); + } + + return portlet; + } + + /** + * Updates the record in the table. Not implemented. + * + * @param portlet + * the instance of Portlet class to be updated + */ + public void store( Portlet portlet ) + { + // Not implemented. + } + + /** + * Associates a new subscription to a given portlet. + * + * @param nPortletId + * the identifier of the portlet. + * @param nNewsletterId + * the identifier of the subscription. + */ + public void insertSubscription( int nPortletId, int nNewsletterId ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_NEWSLETTER, + PluginService.getPlugin( NewsletterPlugin.PLUGIN_NAME ) ) ) + { + daoUtil.setInt( 1, nPortletId ); + daoUtil.setInt( 2, nNewsletterId ); + + daoUtil.executeUpdate( ); + daoUtil.free( ); + } + } + + /** + * De-associate a subscription from a given portlet. + * + * @param nPortletId + * the identifier of the portlet. + * @param nNewsletterId + * the identifier of the subscription. + */ + public void removeSubscription( int nPortletId, int nNewsletterId ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_NEWSLETTER, + PluginService.getPlugin( NewsletterPlugin.PLUGIN_NAME ) ) ) + { + daoUtil.setInt( 1, nPortletId ); + daoUtil.setInt( 2, nNewsletterId ); + + daoUtil.executeUpdate( ); + daoUtil.free( ); + } + } + + /** + * Returns all the newsletters associated to a given portlet. + * + * @param nPortletId + * the identifier of the portlet. + * @return a Set of Integer objects containing the identifers of the + * susbscriptions. + */ + public Set findSelectedNewsletters( int nPortletId ) + { + HashSet results = new HashSet<>( ); + + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_SUBSCRIPTION_BY_PORTLET, + PluginService.getPlugin( NewsletterPlugin.PLUGIN_NAME ) ) ) + { + daoUtil.setInt( 1, nPortletId ); + daoUtil.executeQuery( ); + + while ( daoUtil.next( ) ) + { + results.add( Integer.valueOf( daoUtil.getInt( 1 ) ) ); + } + + daoUtil.free( ); + } + + return results; + } } diff --git a/src/java/fr/paris/lutece/plugins/newsletter/business/topic/FreeHtmlTopicDao.java b/src/java/fr/paris/lutece/plugins/newsletter/business/topic/FreeHtmlTopicDao.java index 3b56b69..60b11ab 100644 --- a/src/java/fr/paris/lutece/plugins/newsletter/business/topic/FreeHtmlTopicDao.java +++ b/src/java/fr/paris/lutece/plugins/newsletter/business/topic/FreeHtmlTopicDao.java @@ -12,113 +12,123 @@ */ public class FreeHtmlTopicDao implements IFreeHtmlTopicDAO { - private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = " SELECT id_topic, html_content FROM newsletter_topic_free_html WHERE id_topic = ? "; - private static final String SQL_QUERY_UPDATE = " UPDATE newsletter_topic_free_html SET html_content = ? WHERE id_topic = ? "; - private static final String SQL_QUERY_INSERT = " INSERT INTO newsletter_topic_free_html (id_topic, html_content) VALUES (?,?) "; - private static final String SQL_QUERY_DELETE = " DELETE FROM newsletter_topic_free_html WHERE id_topic = ? "; - private static final String SQL_QUERY_FIND_BY_ID_LIST = " SELECT id_topic, html_content FROM newsletter_topic_free_html WHERE id_topic IN ( "; + private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = " SELECT id_topic, html_content FROM newsletter_topic_free_html WHERE id_topic = ? "; + private static final String SQL_QUERY_UPDATE = " UPDATE newsletter_topic_free_html SET html_content = ? WHERE id_topic = ? "; + private static final String SQL_QUERY_INSERT = " INSERT INTO newsletter_topic_free_html (id_topic, html_content) VALUES (?,?) "; + private static final String SQL_QUERY_DELETE = " DELETE FROM newsletter_topic_free_html WHERE id_topic = ? "; + private static final String SQL_QUERY_FIND_BY_ID_LIST = " SELECT id_topic, html_content FROM newsletter_topic_free_html WHERE id_topic IN ( "; - private static final String CONSTANT_COMMA = ","; - private static final String CONSTANT_CLOSE_PARENTHESIS = ")"; + private static final String CONSTANT_COMMA = ","; + private static final String CONSTANT_CLOSE_PARENTHESIS = ")"; - /** - * {@inheritDoc} - */ - @Override - public FreeHtmlTopic findByPrimaryKey( int nId, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY, plugin ); - daoUtil.setInt( 1, nId ); - FreeHtmlTopic topic = null; + /** + * {@inheritDoc} + */ + @Override + public FreeHtmlTopic findByPrimaryKey( int nId, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY, plugin ) ) + { + daoUtil.setInt( 1, nId ); + FreeHtmlTopic topic = null; - daoUtil.executeQuery( ); - if ( daoUtil.next( ) ) - { - topic = new FreeHtmlTopic( ); - topic.setId( daoUtil.getInt( 1 ) ); - topic.setHtmlContent( daoUtil.getString( 2 ) ); - } - daoUtil.free( ); - return topic; - } + daoUtil.executeQuery( ); + if ( daoUtil.next( ) ) + { + topic = new FreeHtmlTopic( ); + topic.setId( daoUtil.getInt( 1 ) ); + topic.setHtmlContent( daoUtil.getString( 2 ) ); + } + daoUtil.free( ); + return topic; + } + } - /** - * {@inheritDoc} - */ - @Override - public void insert( FreeHtmlTopic freeHtmlTopic, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin ); - daoUtil.setInt( 1, freeHtmlTopic.getId( ) ); - daoUtil.setString( 2, freeHtmlTopic.getHtmlContent( ) ); - daoUtil.executeUpdate( ); - daoUtil.free( ); - } + /** + * {@inheritDoc} + */ + @Override + public void insert( FreeHtmlTopic freeHtmlTopic, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin ) ) + { + daoUtil.setInt( 1, freeHtmlTopic.getId( ) ); + daoUtil.setString( 2, freeHtmlTopic.getHtmlContent( ) ); + daoUtil.executeUpdate( ); + daoUtil.free( ); + } + } - /** - * {@inheritDoc} - */ - @Override - public void update( FreeHtmlTopic freeHtmlTopic, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ); - daoUtil.setString( 1, freeHtmlTopic.getHtmlContent( ) ); - daoUtil.setInt( 2, freeHtmlTopic.getId( ) ); - daoUtil.executeUpdate( ); - daoUtil.free( ); - } + /** + * {@inheritDoc} + */ + @Override + public void update( FreeHtmlTopic freeHtmlTopic, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ) ) + { + daoUtil.setString( 1, freeHtmlTopic.getHtmlContent( ) ); + daoUtil.setInt( 2, freeHtmlTopic.getId( ) ); + daoUtil.executeUpdate( ); + daoUtil.free( ); + } + } - /** - * {@inheritDoc} - */ - @Override - public void remove( int nId, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ); - daoUtil.setInt( 1, nId ); - daoUtil.executeUpdate( ); - daoUtil.free( ); - } + /** + * {@inheritDoc} + */ + @Override + public void remove( int nId, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) ) + { + daoUtil.setInt( 1, nId ); + daoUtil.executeUpdate( ); + daoUtil.free( ); + } + } - /** - * {@inheritDoc} - */ - @Override - public List findCollection( List listIds, Plugin plugin ) - { - List listTopic = new ArrayList( ); - if ( listIds != null ) - { - List listPrivIds = new ArrayList( listIds ); - StringBuilder sbSql = new StringBuilder( SQL_QUERY_FIND_BY_ID_LIST ); - if ( listIds.size( ) > 0 ) - { - sbSql.append( listPrivIds.get( 0 ) ); - listPrivIds.remove( 0 ); - } - for ( int nId : listPrivIds ) - { - sbSql.append( CONSTANT_COMMA ); - sbSql.append( nId ); - } - sbSql.append( CONSTANT_CLOSE_PARENTHESIS ); - DAOUtil daoUtil = new DAOUtil( sbSql.toString( ), plugin ); - int nIndex = 1; - for ( int nId : listIds ) - { - daoUtil.setInt( nIndex++, nId ); - } + /** + * {@inheritDoc} + */ + @Override + public List findCollection( List listIds, Plugin plugin ) + { + List listTopic = new ArrayList<>( ); + if ( listIds != null ) + { + List listPrivIds = new ArrayList<>( listIds ); + StringBuilder sbSql = new StringBuilder( SQL_QUERY_FIND_BY_ID_LIST ); + if ( !listIds.isEmpty( ) ) + { + sbSql.append( listPrivIds.get( 0 ) ); + listPrivIds.remove( 0 ); + } + for ( int nId : listPrivIds ) + { + sbSql.append( CONSTANT_COMMA ); + sbSql.append( nId ); + } + sbSql.append( CONSTANT_CLOSE_PARENTHESIS ); + try( DAOUtil daoUtil = new DAOUtil( sbSql.toString( ), plugin ) ) + { + int nIndex = 1; + for ( int nId : listIds ) + { + daoUtil.setInt( nIndex++, nId ); + } - daoUtil.executeQuery( ); - while ( daoUtil.next( ) ) - { - FreeHtmlTopic topic = new FreeHtmlTopic( ); - topic.setId( daoUtil.getInt( 1 ) ); - topic.setHtmlContent( daoUtil.getString( 2 ) ); - listTopic.add( topic ); - } - daoUtil.free( ); - } - return listTopic; - } + daoUtil.executeQuery( ); + while ( daoUtil.next( ) ) + { + FreeHtmlTopic topic = new FreeHtmlTopic( ); + topic.setId( daoUtil.getInt( 1 ) ); + topic.setHtmlContent( daoUtil.getString( 2 ) ); + listTopic.add( topic ); + } + daoUtil.free( ); + } + } + return listTopic; + } } diff --git a/src/java/fr/paris/lutece/plugins/newsletter/business/topic/NewsletterTopicDao.java b/src/java/fr/paris/lutece/plugins/newsletter/business/topic/NewsletterTopicDao.java index 6c11814..cab4d15 100644 --- a/src/java/fr/paris/lutece/plugins/newsletter/business/topic/NewsletterTopicDao.java +++ b/src/java/fr/paris/lutece/plugins/newsletter/business/topic/NewsletterTopicDao.java @@ -12,237 +12,261 @@ */ public class NewsletterTopicDao implements INewsletterTopicDAO { - private static final String SQL_QUERY_NEW_PRIMARY_KEY = " SELECT MAX(id_topic) FROM newsletter_topic "; - - private static final String SQL_QUERY_SELECT = " SELECT id_topic, id_newsletter, topic_type, title, topic_order, section FROM newsletter_topic WHERE id_topic = ? "; - private static final String SQL_QUERY_SELECT_ALL_BY_ID_NEWSLETTER = " SELECT id_topic, id_newsletter, topic_type, title, topic_order, section FROM newsletter_topic WHERE id_newsletter = ? ORDER BY section, topic_order asc "; - private static final String SQL_QUERY_SELECT_ALL_BY_ID_NEWSLETTER_AND_ORDER = " SELECT id_topic, id_newsletter, topic_type, title, topic_order, section FROM newsletter_topic WHERE id_newsletter = ? AND topic_order = ? AND section = ? "; - private static final String SQL_QUERY_INSERT = " INSERT INTO newsletter_topic (id_topic, id_newsletter, topic_type, title, topic_order, section) VALUES (?,?,?,?,?,?) "; - private static final String SQL_QUERY_UPDATE = " UPDATE newsletter_topic SET id_newsletter = ?, topic_type = ?, title = ?, topic_order = ?, section = ? WHERE id_topic = ? "; - private static final String SQL_QUERY_DELETE = " DELETE FROM newsletter_topic WHERE id_topic = ? "; - private static final String SQL_QUERY_UPDATE_ORDER = " UPDATE newsletter_topic SET topic_order = ? WHERE id_topic = ? "; - private static final String SQL_QUERY_FIND_LAST_ORDER = " SELECT MAX(topic_order) FROM newsletter_topic WHERE id_newsletter = ? AND section = ? "; - private static final String SQL_QUERY_FILL_ORDER_BLANK = " UPDATE newsletter_topic SET topic_order = topic_order - 1 WHERE id_newsletter = ? AND section = ? and topic_order > ? "; - private static final String SQL_QUERY_REMOVE_ALL_BY_ID_NEWSLETTER = " DELETE FROM newsletter_topic WHERE id_newsletter = ? "; - - /** - * {@inheritDoc} - */ - @Override - public NewsletterTopic findByPrimaryKey( int nId, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin ); - NewsletterTopic topic = null; - daoUtil.setInt( 1, nId ); - daoUtil.executeQuery( ); - if ( daoUtil.next( ) ) - { - int nIndex = 1; - topic = new NewsletterTopic( ); - topic.setId( daoUtil.getInt( nIndex++ ) ); - topic.setIdNewsletter( daoUtil.getInt( nIndex++ ) ); - topic.setTopicTypeCode( daoUtil.getString( nIndex++ ) ); - topic.setTitle( daoUtil.getString( nIndex++ ) ); - topic.setOrder( daoUtil.getInt( nIndex++ ) ); - topic.setSection( daoUtil.getInt( nIndex ) ); - } - daoUtil.free( ); - return topic; - } - - /** - * {@inheritDoc} - */ - @Override - public void insert( NewsletterTopic newsletterTopic, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin ); - newsletterTopic.setId( newPrimaryKey( plugin ) ); - daoUtil.setInt( 1, newsletterTopic.getId( ) ); - daoUtil.setInt( 2, newsletterTopic.getIdNewsletter( ) ); - daoUtil.setString( 3, newsletterTopic.getTopicTypeCode( ) ); - daoUtil.setString( 4, newsletterTopic.getTitle( ) ); - daoUtil.setInt( 5, newsletterTopic.getOrder( ) ); - daoUtil.setInt( 6, newsletterTopic.getSection( ) ); - daoUtil.executeUpdate( ); - daoUtil.free( ); - } - - /** - * {@inheritDoc} - */ - @Override - public void update( NewsletterTopic newsletterTopic, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ); - daoUtil.setInt( 1, newsletterTopic.getIdNewsletter( ) ); - daoUtil.setString( 2, newsletterTopic.getTopicTypeCode( ) ); - daoUtil.setString( 3, newsletterTopic.getTitle( ) ); - daoUtil.setInt( 4, newsletterTopic.getOrder( ) ); - daoUtil.setInt( 5, newsletterTopic.getSection( ) ); - daoUtil.setInt( 6, newsletterTopic.getId( ) ); - daoUtil.executeUpdate( ); - daoUtil.free( ); - } - - /** - * {@inheritDoc} - */ - @Override - public void remove( int nId, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ); - daoUtil.setInt( 1, nId ); - daoUtil.executeUpdate( ); - daoUtil.free( ); - } - - /** - * {@inheritDoc} - */ - @Override - public List findAllByIdNewsletter( int nIdNewsletter, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL_BY_ID_NEWSLETTER, plugin ); - List listNewsletterTopics = new ArrayList( ); - daoUtil.setInt( 1, nIdNewsletter ); - daoUtil.executeQuery( ); - while ( daoUtil.next( ) ) - { - int nIndex = 1; - NewsletterTopic topic = new NewsletterTopic( ); - topic.setId( daoUtil.getInt( nIndex++ ) ); - topic.setIdNewsletter( daoUtil.getInt( nIndex++ ) ); - topic.setTopicTypeCode( daoUtil.getString( nIndex++ ) ); - topic.setTitle( daoUtil.getString( nIndex++ ) ); - topic.setOrder( daoUtil.getInt( nIndex++ ) ); - topic.setSection( daoUtil.getInt( nIndex ) ); - listNewsletterTopics.add( topic ); - } - daoUtil.free( ); - return listNewsletterTopics; - } - - /** - * {@inheritDoc} - */ - @Override - public void updateNewsletterTopicOrder( int nIdNewsletterTopic, int nNewOrder, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_ORDER, plugin ); - daoUtil.setInt( 1, nNewOrder ); - daoUtil.setInt( 2, nIdNewsletterTopic ); - daoUtil.executeUpdate( ); - daoUtil.free( ); - } - - /** - * {@inheritDoc} - */ - @Override - public List findByNewsletterIdAndOrder( int nIdNewsletter, int nOrder, int nSection, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL_BY_ID_NEWSLETTER_AND_ORDER, plugin ); - List listNewsletterTopics = new ArrayList( ); - daoUtil.setInt( 1, nIdNewsletter ); - daoUtil.setInt( 2, nOrder ); - daoUtil.setInt( 3, nSection ); - daoUtil.executeQuery( ); - while ( daoUtil.next( ) ) - { - int nIndex = 1; - NewsletterTopic topic = new NewsletterTopic( ); - topic.setId( daoUtil.getInt( nIndex++ ) ); - topic.setIdNewsletter( daoUtil.getInt( nIndex++ ) ); - topic.setTopicTypeCode( daoUtil.getString( nIndex++ ) ); - topic.setTitle( daoUtil.getString( nIndex++ ) ); - topic.setOrder( daoUtil.getInt( nIndex++ ) ); - topic.setSection( daoUtil.getInt( nIndex ) ); - listNewsletterTopics.add( topic ); - } - daoUtil.free( ); - return listNewsletterTopics; - } - - /** - * {@inheritDoc} - */ - @Override - public int getNewOrder( int nIdNewsletter, int nSection, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_LAST_ORDER, plugin ); - daoUtil.setInt( 1, nIdNewsletter ); - daoUtil.setInt( 2, nSection ); - daoUtil.executeQuery( ); - int nNewOrder = 1; - if ( daoUtil.next( ) ) - { - // We get the last order, and we add 1 to have the next - nNewOrder = daoUtil.getInt( 1 ) + 1; - } - daoUtil.free( ); - return nNewOrder; - } - - /** - * {@inheritDoc} - */ - @Override - public int getLastOrder( int nIdNewsletter, int nSection, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_LAST_ORDER, plugin ); - daoUtil.setInt( 1, nIdNewsletter ); - daoUtil.setInt( 2, nSection ); - daoUtil.executeQuery( ); - int nLastOrder = 1; - if ( daoUtil.next( ) ) - { - nLastOrder = daoUtil.getInt( 1 ); - } - daoUtil.free( ); - return nLastOrder; - } - - /** - * {@inheritDoc} - */ - @Override - public void fillBlankInOrder( int nIdNewsletter, int nOrder, int nSection, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FILL_ORDER_BLANK, plugin ); - daoUtil.setInt( 1, nIdNewsletter ); - daoUtil.setInt( 2, nSection ); - daoUtil.setInt( 3, nOrder ); - daoUtil.executeUpdate( ); - daoUtil.free( ); - } - - /** - * {@inheritDoc} - */ - @Override - public void removeAllByIdNewsletter( int nIdNewsletter, Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_REMOVE_ALL_BY_ID_NEWSLETTER, plugin ); - daoUtil.setInt( 1, nIdNewsletter ); - daoUtil.executeUpdate( ); - daoUtil.free( ); - } - - /** - * Get a new primary key - * @return A new primary key - */ - private int newPrimaryKey( Plugin plugin ) - { - DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PRIMARY_KEY, plugin ); - daoUtil.executeQuery( ); - int nId = 1; - if ( daoUtil.next( ) ) - { - nId = daoUtil.getInt( 1 ) + 1; - } - daoUtil.free( ); - return nId; - } + private static final String SQL_QUERY_NEW_PRIMARY_KEY = " SELECT MAX(id_topic) FROM newsletter_topic "; + + private static final String SQL_QUERY_SELECT = " SELECT id_topic, id_newsletter, topic_type, title, topic_order, section FROM newsletter_topic WHERE id_topic = ? "; + private static final String SQL_QUERY_SELECT_ALL_BY_ID_NEWSLETTER = " SELECT id_topic, id_newsletter, topic_type, title, topic_order, section FROM newsletter_topic WHERE id_newsletter = ? ORDER BY section, topic_order asc "; + private static final String SQL_QUERY_SELECT_ALL_BY_ID_NEWSLETTER_AND_ORDER = " SELECT id_topic, id_newsletter, topic_type, title, topic_order, section FROM newsletter_topic WHERE id_newsletter = ? AND topic_order = ? AND section = ? "; + private static final String SQL_QUERY_INSERT = " INSERT INTO newsletter_topic (id_topic, id_newsletter, topic_type, title, topic_order, section) VALUES (?,?,?,?,?,?) "; + private static final String SQL_QUERY_UPDATE = " UPDATE newsletter_topic SET id_newsletter = ?, topic_type = ?, title = ?, topic_order = ?, section = ? WHERE id_topic = ? "; + private static final String SQL_QUERY_DELETE = " DELETE FROM newsletter_topic WHERE id_topic = ? "; + private static final String SQL_QUERY_UPDATE_ORDER = " UPDATE newsletter_topic SET topic_order = ? WHERE id_topic = ? "; + private static final String SQL_QUERY_FIND_LAST_ORDER = " SELECT MAX(topic_order) FROM newsletter_topic WHERE id_newsletter = ? AND section = ? "; + private static final String SQL_QUERY_FILL_ORDER_BLANK = " UPDATE newsletter_topic SET topic_order = topic_order - 1 WHERE id_newsletter = ? AND section = ? and topic_order > ? "; + private static final String SQL_QUERY_REMOVE_ALL_BY_ID_NEWSLETTER = " DELETE FROM newsletter_topic WHERE id_newsletter = ? "; + + /** + * {@inheritDoc} + */ + @Override + public NewsletterTopic findByPrimaryKey( int nId, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin ) ) + { + NewsletterTopic topic = null; + daoUtil.setInt( 1, nId ); + daoUtil.executeQuery( ); + if ( daoUtil.next( ) ) + { + int nIndex = 1; + topic = new NewsletterTopic( ); + topic.setId( daoUtil.getInt( nIndex++ ) ); + topic.setIdNewsletter( daoUtil.getInt( nIndex++ ) ); + topic.setTopicTypeCode( daoUtil.getString( nIndex++ ) ); + topic.setTitle( daoUtil.getString( nIndex++ ) ); + topic.setOrder( daoUtil.getInt( nIndex++ ) ); + topic.setSection( daoUtil.getInt( nIndex ) ); + } + daoUtil.free( ); + return topic; + } + } + + /** + * {@inheritDoc} + */ + @Override + public void insert( NewsletterTopic newsletterTopic, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin ) ) + { + newsletterTopic.setId( newPrimaryKey( plugin ) ); + daoUtil.setInt( 1, newsletterTopic.getId( ) ); + daoUtil.setInt( 2, newsletterTopic.getIdNewsletter( ) ); + daoUtil.setString( 3, newsletterTopic.getTopicTypeCode( ) ); + daoUtil.setString( 4, newsletterTopic.getTitle( ) ); + daoUtil.setInt( 5, newsletterTopic.getOrder( ) ); + daoUtil.setInt( 6, newsletterTopic.getSection( ) ); + daoUtil.executeUpdate( ); + daoUtil.free( ); + } + } + + /** + * {@inheritDoc} + */ + @Override + public void update( NewsletterTopic newsletterTopic, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ) ) + { + daoUtil.setInt( 1, newsletterTopic.getIdNewsletter( ) ); + daoUtil.setString( 2, newsletterTopic.getTopicTypeCode( ) ); + daoUtil.setString( 3, newsletterTopic.getTitle( ) ); + daoUtil.setInt( 4, newsletterTopic.getOrder( ) ); + daoUtil.setInt( 5, newsletterTopic.getSection( ) ); + daoUtil.setInt( 6, newsletterTopic.getId( ) ); + daoUtil.executeUpdate( ); + daoUtil.free( ); + } + } + + /** + * {@inheritDoc} + */ + @Override + public void remove( int nId, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) ) + { + daoUtil.setInt( 1, nId ); + daoUtil.executeUpdate( ); + daoUtil.free( ); + } + } + + /** + * {@inheritDoc} + */ + @Override + public List findAllByIdNewsletter( int nIdNewsletter, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL_BY_ID_NEWSLETTER, plugin ) ) + { + List listNewsletterTopics = new ArrayList<>( ); + daoUtil.setInt( 1, nIdNewsletter ); + daoUtil.executeQuery( ); + while ( daoUtil.next( ) ) + { + int nIndex = 1; + NewsletterTopic topic = new NewsletterTopic( ); + topic.setId( daoUtil.getInt( nIndex++ ) ); + topic.setIdNewsletter( daoUtil.getInt( nIndex++ ) ); + topic.setTopicTypeCode( daoUtil.getString( nIndex++ ) ); + topic.setTitle( daoUtil.getString( nIndex++ ) ); + topic.setOrder( daoUtil.getInt( nIndex++ ) ); + topic.setSection( daoUtil.getInt( nIndex ) ); + listNewsletterTopics.add( topic ); + } + daoUtil.free( ); + return listNewsletterTopics; + } + } + + /** + * {@inheritDoc} + */ + @Override + public void updateNewsletterTopicOrder( int nIdNewsletterTopic, int nNewOrder, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_ORDER, plugin ) ) + { + daoUtil.setInt( 1, nNewOrder ); + daoUtil.setInt( 2, nIdNewsletterTopic ); + daoUtil.executeUpdate( ); + daoUtil.free( ); + } + } + + /** + * {@inheritDoc} + */ + @Override + public List findByNewsletterIdAndOrder( int nIdNewsletter, int nOrder, int nSection, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL_BY_ID_NEWSLETTER_AND_ORDER, plugin ) ) + { + List listNewsletterTopics = new ArrayList<>( ); + daoUtil.setInt( 1, nIdNewsletter ); + daoUtil.setInt( 2, nOrder ); + daoUtil.setInt( 3, nSection ); + daoUtil.executeQuery( ); + while ( daoUtil.next( ) ) + { + int nIndex = 1; + NewsletterTopic topic = new NewsletterTopic( ); + topic.setId( daoUtil.getInt( nIndex++ ) ); + topic.setIdNewsletter( daoUtil.getInt( nIndex++ ) ); + topic.setTopicTypeCode( daoUtil.getString( nIndex++ ) ); + topic.setTitle( daoUtil.getString( nIndex++ ) ); + topic.setOrder( daoUtil.getInt( nIndex++ ) ); + topic.setSection( daoUtil.getInt( nIndex ) ); + listNewsletterTopics.add( topic ); + } + daoUtil.free( ); + return listNewsletterTopics; + } + } + + /** + * {@inheritDoc} + */ + @Override + public int getNewOrder( int nIdNewsletter, int nSection, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_LAST_ORDER, plugin ) ) + { + daoUtil.setInt( 1, nIdNewsletter ); + daoUtil.setInt( 2, nSection ); + daoUtil.executeQuery( ); + int nNewOrder = 1; + if ( daoUtil.next( ) ) + { + // We get the last order, and we add 1 to have the next + nNewOrder = daoUtil.getInt( 1 ) + 1; + } + daoUtil.free( ); + return nNewOrder; + } + } + + /** + * {@inheritDoc} + */ + @Override + public int getLastOrder( int nIdNewsletter, int nSection, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_LAST_ORDER, plugin ) ) + { + daoUtil.setInt( 1, nIdNewsletter ); + daoUtil.setInt( 2, nSection ); + daoUtil.executeQuery( ); + int nLastOrder = 1; + if ( daoUtil.next( ) ) + { + nLastOrder = daoUtil.getInt( 1 ); + } + daoUtil.free( ); + return nLastOrder; + } + } + + /** + * {@inheritDoc} + */ + @Override + public void fillBlankInOrder( int nIdNewsletter, int nOrder, int nSection, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FILL_ORDER_BLANK, plugin ) ) + { + daoUtil.setInt( 1, nIdNewsletter ); + daoUtil.setInt( 2, nSection ); + daoUtil.setInt( 3, nOrder ); + daoUtil.executeUpdate( ); + daoUtil.free( ); + } + } + + /** + * {@inheritDoc} + */ + @Override + public void removeAllByIdNewsletter( int nIdNewsletter, Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_REMOVE_ALL_BY_ID_NEWSLETTER, plugin ) ) + { + daoUtil.setInt( 1, nIdNewsletter ); + daoUtil.executeUpdate( ); + daoUtil.free( ); + } + } + + /** + * Get a new primary key + * @return A new primary key + */ + private int newPrimaryKey( Plugin plugin ) + { + try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PRIMARY_KEY, plugin ) ) + { + daoUtil.executeQuery( ); + int nId = 1; + if ( daoUtil.next( ) ) + { + nId = daoUtil.getInt( 1 ) + 1; + } + daoUtil.free( ); + return nId; + } + } } diff --git a/src/java/fr/paris/lutece/plugins/newsletter/resources/newsletter_messages.properties b/src/java/fr/paris/lutece/plugins/newsletter/resources/newsletter_messages.properties index a038ed5..28c0ab7 100644 --- a/src/java/fr/paris/lutece/plugins/newsletter/resources/newsletter_messages.properties +++ b/src/java/fr/paris/lutece/plugins/newsletter/resources/newsletter_messages.properties @@ -374,7 +374,6 @@ modify_newsletter.manageTopics=Manage topics manage_topics.pageTitle=Manage topics manage_topics.labelAddTopic=Add topic manage_topics.labelTopicTitle=Topic title -manage_topics.labelTopicType=Topic type manage_topics.labelTopicOrder=Order manage_topics.labelTopicSection=Section manage_topics.labelActions=Actions diff --git a/src/java/fr/paris/lutece/plugins/newsletter/resources/newsletter_messages_fr.properties b/src/java/fr/paris/lutece/plugins/newsletter/resources/newsletter_messages_fr.properties index d92941a..9d38971 100644 --- a/src/java/fr/paris/lutece/plugins/newsletter/resources/newsletter_messages_fr.properties +++ b/src/java/fr/paris/lutece/plugins/newsletter/resources/newsletter_messages_fr.properties @@ -199,7 +199,6 @@ manage_newsletters.columnTitleDelete=Supprimer manage_newsletters.columnTitleCopy=Dupliquer manage_newsletters.buttonAddNewsletter=Ajouter une lettre manage_newsletters.columnTitleWorkGroup=Groupe de Travail -manage_newsletters.columnTitleActions=Actions manage_newsletters.buttonManageTemplates=Gestion des templates manage_newsletters.columnTitleWebMasterMail=E-mail du Webmestre manage_newsletters.columnTitleExportCsv=Exporter les abonn\u00e9s @@ -368,7 +367,6 @@ modify_newsletter.manageTopics=Configuration de la g manage_topics.pageTitle=Configuration de la génération manage_topics.labelAddTopic=Ajouter une rubrique manage_topics.labelTopicTitle=Titre de la rubrique -manage_topics.labelTopicType=Type de rubrique manage_topics.labelTopicOrder=Position manage_topics.labelTopicSection=Section manage_topics.labelActions=Actions diff --git a/src/java/fr/paris/lutece/plugins/newsletter/service/NewsletterService.java b/src/java/fr/paris/lutece/plugins/newsletter/service/NewsletterService.java index ae69295..f611872 100644 --- a/src/java/fr/paris/lutece/plugins/newsletter/service/NewsletterService.java +++ b/src/java/fr/paris/lutece/plugins/newsletter/service/NewsletterService.java @@ -62,7 +62,6 @@ import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Serializable; -import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -161,7 +160,7 @@ public void sendMail( NewsLetter newsletter, String strObject, String strBaseUrl String strNewsLetterCode = t.getHtml( ); - if ( ( urlAttachments == null ) || ( urlAttachments.size( ) == 0 ) ) + if ( ( urlAttachments == null ) || ( urlAttachments.isEmpty( ) ) ) { MailService.sendMailHtml( subscriber.getEmail( ), newsletter.getNewsletterSenderName( ), newsletter.getNewsletterSenderMail( ), strObject, strNewsLetterCode ); @@ -199,7 +198,7 @@ public boolean isAbsoluteUrl( ) public boolean isMhtmlActivated( ) { String strProperty = AppPropertiesService.getProperty( NewsLetterConstants.PROPERTY_MAIL_MULTIPART ); - return ( strProperty != null ) && Boolean.valueOf( strProperty ).booleanValue( ); + return ( strProperty != null ) && Boolean.parseBoolean( strProperty ); } /** @@ -229,10 +228,6 @@ public byte[] getSubscribersCsvExport( int nNewsletterId ) writer.close( ); byteSubscribersList = byteArrayStream.toByteArray( ); } - catch ( UnsupportedEncodingException e ) - { - AppLogService.error( e ); - } catch ( IOException e ) { AppLogService.error( e ); @@ -298,7 +293,7 @@ public String generateNewsletterHtmlCode( NewsLetter newsletter, int nTemplateNe return null; } - Map model = new HashMap( ); + Map model = new HashMap<>( ); List listTopics = NewsletterTopicHome .findAllByIdNewsletter( newsletter.getId( ), getPlugin( ) ); @@ -307,7 +302,7 @@ public String generateNewsletterHtmlCode( NewsLetter newsletter, int nTemplateNe int nCurrentSection = 0; String[] strContentBySection = new String[newsletter.getNbSections( )]; - List listSelectedTopics = new ArrayList( ); + List listSelectedTopics = new ArrayList<>( ); for ( int i = 0; i < listTopics.size( ) + 1; i++ ) { NewsletterTopic newsletterTopic = null; @@ -336,7 +331,7 @@ public String generateNewsletterHtmlCode( NewsLetter newsletter, int nTemplateNe if ( newsletterTopic != null ) { nCurrentSection = newsletterTopic.getSection( ); - listSelectedTopics = new ArrayList( ); + listSelectedTopics = new ArrayList<>( ); listSelectedTopics.add( newsletterTopic ); } } diff --git a/src/java/fr/paris/lutece/plugins/newsletter/service/topic/FreeHtmlTopicService.java b/src/java/fr/paris/lutece/plugins/newsletter/service/topic/FreeHtmlTopicService.java index ccc8317..9480155 100644 --- a/src/java/fr/paris/lutece/plugins/newsletter/service/topic/FreeHtmlTopicService.java +++ b/src/java/fr/paris/lutece/plugins/newsletter/service/topic/FreeHtmlTopicService.java @@ -80,7 +80,7 @@ public String getConfigurationPage( NewsletterTopic newsletterTopic, String strB Locale locale ) { FreeHtmlTopic htmlTopic = FreeHtmlTopicHome.findByPrimaryKey( newsletterTopic.getId( ), getPlugin( ) ); - Map model = new HashMap( ); + Map model = new HashMap<>( ); model.put( MARK_HTML_TOPIC, htmlTopic ); model.put( MARK_WEBAPP_URL, strBaseUrl ); diff --git a/src/java/fr/paris/lutece/plugins/newsletter/util/HtmlDomDocNewsletter.java b/src/java/fr/paris/lutece/plugins/newsletter/util/HtmlDomDocNewsletter.java index ea5263b..982c761 100644 --- a/src/java/fr/paris/lutece/plugins/newsletter/util/HtmlDomDocNewsletter.java +++ b/src/java/fr/paris/lutece/plugins/newsletter/util/HtmlDomDocNewsletter.java @@ -41,7 +41,6 @@ import java.io.UnsupportedEncodingException; import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; @@ -214,12 +213,6 @@ public String getContent( ) transformer = tf.newTransformer( ); transformer.transform( domSource, result ); } - catch ( TransformerConfigurationException e ) - { - AppLogService.error( e.getMessage( ) ); - - return null; - } catch ( TransformerException e ) { AppLogService.error( e.getMessage( ) ); @@ -227,9 +220,7 @@ public String getContent( ) return null; } - String stringResult = writer.toString( ); - - return stringResult; + return writer.toString( ); } /** diff --git a/src/java/fr/paris/lutece/plugins/newsletter/web/NewsLetterApp.java b/src/java/fr/paris/lutece/plugins/newsletter/web/NewsLetterApp.java index cf3f7ed..6f2ce8f 100644 --- a/src/java/fr/paris/lutece/plugins/newsletter/web/NewsLetterApp.java +++ b/src/java/fr/paris/lutece/plugins/newsletter/web/NewsLetterApp.java @@ -46,6 +46,7 @@ import fr.paris.lutece.portal.service.plugin.Plugin; import fr.paris.lutece.portal.service.plugin.PluginService; import fr.paris.lutece.portal.service.template.AppTemplateService; +import fr.paris.lutece.portal.web.l10n.LocaleService; import fr.paris.lutece.portal.web.xpages.XPage; import fr.paris.lutece.portal.web.xpages.XPageApplication; import fr.paris.lutece.util.html.HtmlTemplate; @@ -63,7 +64,11 @@ */ public class NewsLetterApp implements XPageApplication { - // Templates used to generate the HTML code + /** + * + */ + private static final long serialVersionUID = 3944877194624481667L; + // Templates used to generate the HTML code private static final String TEMPLATE_XPAGE_NEWSLETTER = "skin/plugins/newsletter/page_newsletter.html"; private static final String TEMPLATE_XPAGE_TOS = "skin/plugins/newsletter/tos.html"; @@ -154,16 +159,16 @@ private XPage getPage( HttpServletRequest request, Plugin plugin ) if ( request.getParameter( PARAMETER_VIEW_REQUIREMENT ) != null ) { //See conditional use - page.setTitle( I18nService.getLocalizedString( PROPERTY_PATHLABEL, request.getLocale( ) ) ); - page.setPathLabel( I18nService.getLocalizedString( PROPERTY_PAGETITLE, request.getLocale( ) ) ); + page.setTitle( I18nService.getLocalizedString( PROPERTY_PATHLABEL, getLocale( request ) ) ); + page.setPathLabel( I18nService.getLocalizedString( PROPERTY_PAGETITLE, getLocale( request ) ) ); page.setContent( getRequirement( request, plugin ) ); } else { - page.setPathLabel( I18nService.getLocalizedString( PROPERTY_PATHLABEL, request.getLocale( ) ) ); - page.setTitle( I18nService.getLocalizedString( PROPERTY_PAGETITLE, request.getLocale( ) ) ); + page.setPathLabel( I18nService.getLocalizedString( PROPERTY_PATHLABEL, getLocale( request ) ) ); + page.setTitle( I18nService.getLocalizedString( PROPERTY_PAGETITLE, getLocale( request ) ) ); - HashMap model = new HashMap( ); + HashMap model = new HashMap<>( ); Collection list = NewsLetterHome.findAll( plugin ); NewsLetterProperties properties = NewsletterPropertiesHome.find( plugin ); model.put( MARK_PROPERTIES, properties ); @@ -180,7 +185,7 @@ private XPage getPage( HttpServletRequest request, Plugin plugin ) _captchaService = new CaptchaSecurityService( ); model.put( MARK_CAPTCHA, _captchaService.getHtmlCode( ) ); } - HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_XPAGE_NEWSLETTER, request.getLocale( ), + HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_XPAGE_NEWSLETTER, getLocale( request ), model ); page.setContent( template.getHtml( ) ); } @@ -240,8 +245,8 @@ public void doConfirmSubscribe( HttpServletRequest request ) throws SiteMessageE **/ private String getRequirement( HttpServletRequest request, Plugin plugin ) { - Map model = new HashMap( ); - Locale locale = request.getLocale( ); + Map model = new HashMap<>( ); + Locale locale = getLocale( request ); NewsLetterProperties properties = NewsletterPropertiesHome.find( plugin ); model.put( MARK_TOS, properties.getTOS( ) ); @@ -250,5 +255,17 @@ private String getRequirement( HttpServletRequest request, Plugin plugin ) return template.getHtml( ); } + + /** + * Default getLocale() implementation. Could be overriden + * + * @param request + * The HTTP request + * @return The Locale + */ + protected Locale getLocale( HttpServletRequest request ) + { + return LocaleService.getContextUserLocale( request ); + } } diff --git a/src/java/fr/paris/lutece/plugins/newsletter/web/NewsletterJspBean.java b/src/java/fr/paris/lutece/plugins/newsletter/web/NewsletterJspBean.java index 1dd83a1..c98a3fc 100644 --- a/src/java/fr/paris/lutece/plugins/newsletter/web/NewsletterJspBean.java +++ b/src/java/fr/paris/lutece/plugins/newsletter/web/NewsletterJspBean.java @@ -350,18 +350,19 @@ public NewsletterJspBean( ) * @param request the HTTP request * @return the html code for display the newsletters list */ - public String getManageNewsLetters( HttpServletRequest request ) + @SuppressWarnings("deprecation") + public String getManageNewsLetters( HttpServletRequest request ) { setPageTitleProperty( PROPERTY_PAGE_TITLE_NEWSLETTERS ); _strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex ); _nItemsPerPage = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, _nDefaultItemsPerPage ); - Map model = new HashMap( ); + Map model = new HashMap<>( ); Collection listNewsletter = NewsLetterHome.findAll( getPlugin( ) ); listNewsletter = AdminWorkgroupService.getAuthorizedCollection( listNewsletter, getUser( ) ); - Collection> listNewsletterDisplay = new ArrayList>( ); + Collection> listNewsletterDisplay = new ArrayList<>( ); for ( NewsLetter newsletter : listNewsletter ) { @@ -401,7 +402,7 @@ public String getManageNewsLetters( HttpServletRequest request ) listNewsletterDisplay.add( newsletterDisplay ); } - IPaginator> paginator = new Paginator>( + IPaginator> paginator = new Paginator<>( (List>) listNewsletterDisplay, _nItemsPerPage, getHomeUrl( request ), PARAMETER_PAGE_INDEX, _strCurrentPageIndex ); @@ -432,14 +433,14 @@ public String getManageArchive( HttpServletRequest request ) _nDefaultItemsPerPage ); int nIdNewsletter = Integer.parseInt( request.getParameter( PARAMETER_NEWSLETTER_ID ) ); - Map model = new HashMap( ); + Map model = new HashMap<>( ); List listNewsletter = SendingNewsLetterHome.findAllLastSendingForNewsletterId( nIdNewsletter, getPlugin( ) ); UrlItem url = new UrlItem( request.getRequestURI( ) ); url.addParameter( PARAMETER_NEWSLETTER_ID, nIdNewsletter ); - IPaginator paginator = new Paginator( listNewsletter, _nItemsPerPage, + IPaginator paginator = new Paginator<>( listNewsletter, _nItemsPerPage, url.getUrl( ), PARAMETER_PAGE_INDEX, _strCurrentPageIndex ); model.put( MARK_NEWSLETTER_LIST, paginator.getPageItems( ) ); @@ -471,7 +472,7 @@ NewsletterResourceIdService.PERMISSION_NEWSLETTER_ADVANCED_SETTINGS, getUser( ) setPageTitleProperty( PROPERTY_PAGE_TITLE_NEWSLETTERS_PROPERTIES ); - Map model = new HashMap( ); + Map model = new HashMap<>( ); NewsLetterProperties properties = NewsletterPropertiesHome.find( getPlugin( ) ); @@ -551,7 +552,7 @@ public String getCreateNewsLetter( HttpServletRequest request ) setPageTitleProperty( PROPERTY_PAGE_TITLE_CREATE ); - Map model = new HashMap( ); + Map model = new HashMap<>( ); // get the list of document lists ReferenceList listUnsubscribe = new ReferenceList( ); @@ -599,7 +600,7 @@ NewsletterResourceIdService.PERMISSION_MODIFY, getUser( ) ) ) String strPathImageTemplate = _newsletterService.getImageFolderPath( AppPathService.getBaseUrl( request ) ); - Map model = new HashMap( ); + Map model = new HashMap<>( ); // Fills the template with specific values String strGenerate = request.getParameter( PARAMETER_GENERATE ); @@ -686,7 +687,7 @@ NewsletterResourceIdService.PERMISSION_MODIFY, getUser( ) ) ) NewsLetterTemplate.RESOURCE_TYPE, getPlugin( ) ); // composition not possible if not at least one template for newsletter - if ( ( newsletterTemplatesList.size( ) == 0 ) ) + if ( newsletterTemplatesList.isEmpty( ) ) { return AdminMessageService.getMessageUrl( request, MESSAGE_NO_TEMPLATE, AdminMessage.TYPE_STOP ); } @@ -811,7 +812,7 @@ public String getModifyNewsLetter( HttpServletRequest request ) { setPageTitleProperty( PROPERTY_PAGE_TITLE_MODIFY ); - Map model = new HashMap( ); + Map model = new HashMap<>( ); String strNewsletterId = request.getParameter( PARAMETER_NEWSLETTER_ID ); int nNewsletterId = Integer.parseInt( strNewsletterId ); NewsLetter newsletter = NewsLetterHome.findByPrimaryKey( nNewsletterId, getPlugin( ) ); @@ -1315,10 +1316,10 @@ NewsletterResourceIdService.PERMISSION_MANAGE_SUBSCRIBERS, getUser( ) ) ) Collections.sort( refListSubscribers, new AttributeComparator( strSortedAttributeName, bIsAscSort ) ); } - IPaginator paginator = new LocalizedPaginator( refListSubscribers, _nItemsPerPage, + IPaginator paginator = new LocalizedPaginator<>( refListSubscribers, _nItemsPerPage, url.getUrl( ), Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex, getLocale( ) ); - Map model = new HashMap( ); + Map model = new HashMap<>( ); model.put( MARK_NEWSLETTER, newsletter ); model.put( MARK_NB_ITEMS_PER_PAGE, Integer.toString( _nItemsPerPage ) ); model.put( MARK_SEARCH_STRING, strSearchString ); @@ -1416,7 +1417,7 @@ NewsletterResourceIdService.PERMISSION_SEND, getUser( ) ) ) setPageTitleProperty( PROPERTY_PAGE_TITLE_PREPARE ); String strBaseUrl = AppPathService.getBaseUrl( request ); - Map model = new HashMap( ); + Map model = new HashMap<>( ); String strObject = request.getParameter( PARAMETER_NEWSLETTER_OBJECT ); if ( strObject != null ) @@ -1551,7 +1552,7 @@ NewsletterResourceIdService.PERMISSION_SEND, getUser( ) ) ) } UrlItem urlItem = new UrlItem( JSP_URL_SEND_NEWSLETTER ); - HashMap requestedParameters = new HashMap( ); + HashMap requestedParameters = new HashMap<>( ); requestedParameters.put( PARAMETER_NEWSLETTER_OBJECT, strObject ); requestedParameters.put( PARAMETER_NEWSLETTER_ID, strNewsletterId ); @@ -1802,7 +1803,7 @@ NewsletterResourceIdService.PERMISSION_MANAGE_SUBSCRIBERS, getUser( ) ) ) setPageTitleProperty( PROPERTY_PAGE_TITLE_IMPORT ); - Map model = new HashMap( ); + Map model = new HashMap<>( ); model.put( MARK_NEWSLETTER_ID, nNewsletterId ); model.put( MARK_IMPORT_DELETE, false ); @@ -1832,7 +1833,7 @@ NewsletterResourceIdService.PERMISSION_MANAGE_SUBSCRIBERS, getUser( ) ) ) setPageTitleProperty( PROPERTY_PAGE_TITLE_IMPORT_DELETE ); - Map model = new HashMap( ); + Map model = new HashMap<>( ); model.put( MARK_NEWSLETTER_ID, nNewsletterId ); model.put( MARK_IMPORT_DELETE, true ); @@ -1897,7 +1898,7 @@ NewsletterResourceIdService.PERMISSION_IMPORT_SUBSCRIBERS, getUser( ) ) ) List tabUsers = csvReader.readAll( ); // the file is empty - if ( ( tabUsers == null ) || ( tabUsers.size( ) == 0 ) ) + if ( ( tabUsers == null ) || ( tabUsers.isEmpty( ) ) ) { return AdminMessageService.getMessageUrl( request, MESSAGE_CSV_FILE_EMPTY_OR_NOT_VALID_EMAILS, AdminMessage.TYPE_STOP ); @@ -2052,7 +2053,7 @@ NewsletterResourceIdService.PERMISSION_MODIFY, getUser( ) ) ) } } - Map model = new HashMap( ); + Map model = new HashMap<>( ); // We create an array with the number of topics in every section Integer[] tblSectionSize = new Integer[newsletter.getNbSections( )]; @@ -2080,7 +2081,7 @@ NewsletterResourceIdService.PERMISSION_MODIFY, getUser( ) ) ) } if ( tableManager == null ) { - tableManager = new DataTableManager( url.getUrl( ), url.getUrl( ), + tableManager = new DataTableManager<>( url.getUrl( ), url.getUrl( ), AppPropertiesService.getPropertyInt( PROPERTY_ITEMS_PER_PAGE, CONSTANT_DEFAULT_ITEM_PER_PAGE ), true ); tableManager.addFreeColumn( LABEL_NEWSLETTER_TOPIC_SECTION, CONSTANT_FREEMARKER_MACRO_COLUMN_SECTION ); @@ -2262,7 +2263,7 @@ public String getModifyTopicConfig( HttpServletRequest request ) String strContent = _newsletterTopicService.getConfigurationPage( newsletterTopic, AppPathService.getBaseUrl( request ), AdminUserService.getAdminUser( request ), locale ); - Map model = new HashMap( ); + Map model = new HashMap<>( ); model.put( MARK_CONTENT, strContent ); model.put( MARK_TOPIC, newsletterTopic ); HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_TOPIC_CONFIG, locale, model ); @@ -2421,7 +2422,7 @@ private boolean isNewsletterCreationAllowed( HttpServletRequest request ) */ private Collection convertToList( String strRecipients ) { - Collection listRecipients = new ArrayList( ); + Collection listRecipients = new ArrayList<>( ); if ( StringUtils.isNotEmpty( strRecipients ) ) { @@ -2517,7 +2518,7 @@ private String getTextFileContent( String strFileName ) */ private HtmlTemplate setHtmlTemplateEmail( NewsLetter newsletter, String strBaseUrl, String strUnsubscribe ) { - Map sendingModel = new HashMap( ); + Map sendingModel = new HashMap<>( ); sendingModel.put( MARK_CSS, getCssContent( ) ); sendingModel.put( MARK_UNSUBSCRIBE, strUnsubscribe ); sendingModel.put( MARK_NEWSLETTER_ID, newsletter.getId( ) ); diff --git a/src/java/fr/paris/lutece/plugins/newsletter/web/NewsletterTemplateJspBean.java b/src/java/fr/paris/lutece/plugins/newsletter/web/NewsletterTemplateJspBean.java index 72b8557..e94badb 100644 --- a/src/java/fr/paris/lutece/plugins/newsletter/web/NewsletterTemplateJspBean.java +++ b/src/java/fr/paris/lutece/plugins/newsletter/web/NewsletterTemplateJspBean.java @@ -19,6 +19,7 @@ import fr.paris.lutece.portal.service.plugin.PluginService; import fr.paris.lutece.portal.service.rbac.RBACService; import fr.paris.lutece.portal.service.template.AppTemplateService; +import fr.paris.lutece.portal.service.util.AppException; import fr.paris.lutece.portal.service.util.AppLogService; import fr.paris.lutece.portal.service.util.AppPathService; import fr.paris.lutece.portal.service.util.AppPropertiesService; @@ -35,6 +36,9 @@ import java.io.BufferedReader; import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; @@ -422,8 +426,8 @@ NewsletterTemplateResourceIdService.PERMISSION_MODIFY, getUser( ) ) ) } // we delete the old picture - File oldImageFile = new File( strFullPathOldImageFileName ); - oldImageFile.delete( ); + removeFile( strFullPathOldImageFileName ); + } FileItem modelItem = multi.getFile( PARAMETER_NEWSLETTER_TEMPLATE_NEW_FILE ); @@ -449,8 +453,7 @@ NewsletterTemplateResourceIdService.PERMISSION_MODIFY, getUser( ) ) ) } // we delete the old file - File oldFile = new File( strFullPathOldFileName ); - oldFile.delete( ); + removeFile( strFullPathOldFileName ); modelItem.write( fileTemplate ); newsletterTemplate.setFileName( strFileName ); @@ -497,7 +500,7 @@ public String getModifyNewsLetterTemplateFile( HttpServletRequest request ) { setPageTitleProperty( MESSAGE_PAGE_TITLE_MODIFY_TEMPLATE_FILE ); - Map model = new HashMap( ); + Map model = new HashMap<>( ); BufferedReader fileReader = null; try @@ -621,8 +624,8 @@ NewsletterTemplateResourceIdService.PERMISSION_MODIFY, getUser( ) ) ) newsletterTemplate.setPicture( strFileName ); // we delete the old picture - File oldImageFile = new File( strPathImageNewsletterTemplate + File.separator + strOldImageName ); - oldImageFile.delete( ); + removeFile( strPathImageNewsletterTemplate + File.separator + strOldImageName ); + } // Writes the new content of the file. @@ -669,7 +672,8 @@ NewsletterTemplateResourceIdService.PERMISSION_MODIFY, getUser( ) ) ) * @param request The Http request * @return the html code to confirm */ - public String getRemoveNewsLetterTemplate( HttpServletRequest request ) + @SuppressWarnings("deprecation") + public String getRemoveNewsLetterTemplate( HttpServletRequest request ) { String strNewsletterTemplateId = request.getParameter( NewsLetterConstants.PARAMETER_NEWSLETTER_TEMPLATE_ID ); int nNewsletterTemplateId = Integer.parseInt( strNewsletterTemplateId ); @@ -684,7 +688,7 @@ NewsletterTemplateResourceIdService.PERMISSION_DELETE, getUser( ) ) ) { return AdminMessageService.getMessageUrl( request, Messages.USER_ACCESS_DENIED, AdminMessage.TYPE_ERROR ); } - List listMessages = new ArrayList( ); + List listMessages = new ArrayList<>( ); if ( !NewsletterTemplateRemovalService.getService( ).checkForRemoval( strNewsletterTemplateId, listMessages, AdminUserService.getLocale( request ) ) ) { @@ -706,7 +710,8 @@ NewsletterTemplateResourceIdService.PERMISSION_DELETE, getUser( ) ) ) * @param request The Http request * @return the jsp URL to display the form to manage newsletter templates */ - public String doRemoveNewsLetterTemplate( HttpServletRequest request ) + @SuppressWarnings("deprecation") + public String doRemoveNewsLetterTemplate( HttpServletRequest request ) { int nNewsletterTemplateId = Integer.parseInt( request .getParameter( NewsLetterConstants.PARAMETER_NEWSLETTER_TEMPLATE_ID ) ); @@ -733,7 +738,7 @@ NewsletterTemplateResourceIdService.PERMISSION_DELETE, getUser( ) ) ) if ( file.exists( ) ) { - file.delete( ); + removeFile( file.getAbsolutePath( ) ); } // removes the picture @@ -742,7 +747,7 @@ NewsletterTemplateResourceIdService.PERMISSION_DELETE, getUser( ) ) ) if ( picture.exists( ) ) { - picture.delete( ); + removeFile( picture.getAbsolutePath( ) ); } // removes the newsletter template from the database @@ -785,7 +790,8 @@ private ReferenceList buildTemplateTypeList( Locale locale ) * @param request The {@link HttpServletRequest} * @return true if creation is authorized, false otherwise */ - private boolean isNewsletterTemplateCreationAllowed( HttpServletRequest request ) + @SuppressWarnings("deprecation") + private boolean isNewsletterTemplateCreationAllowed( HttpServletRequest request ) { //RBAC permission AdminUser user = AdminUserService.getAdminUser( request ); @@ -809,5 +815,17 @@ private boolean isNewsletterTemplateCreationAllowed( HttpServletRequest request return false; } + + private void removeFile( String strFilePath ) + { + Path file = Paths.get( strFilePath ); + try { + Files.delete( file ); + } + catch (IOException e) + { + throw new AppException("Removal failed", e); + } + } }