MailItemQueueDAO.java

  1. /*
  2.  * Copyright (c) 2002-2022, City of Paris
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  *  1. Redistributions of source code must retain the above copyright notice
  10.  *     and the following disclaimer.
  11.  *
  12.  *  2. Redistributions in binary form must reproduce the above copyright notice
  13.  *     and the following disclaimer in the documentation and/or other materials
  14.  *     provided with the distribution.
  15.  *
  16.  *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
  17.  *     contributors may be used to endorse or promote products derived from
  18.  *     this software without specific prior written permission.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
  24.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30.  * POSSIBILITY OF SUCH DAMAGE.
  31.  *
  32.  * License 1.0
  33.  */
  34. package fr.paris.lutece.portal.business.mail;

  35. import fr.paris.lutece.portal.service.mail.MailItem;
  36. import fr.paris.lutece.portal.service.util.AppLogService;
  37. import fr.paris.lutece.util.mail.FileAttachment;
  38. import fr.paris.lutece.util.mail.UrlAttachment;
  39. import fr.paris.lutece.util.sql.DAOUtil;
  40. import fr.paris.lutece.util.sql.Transaction;
  41. import fr.paris.lutece.util.sql.TransactionManager;

  42. import java.io.ByteArrayOutputStream;
  43. import java.io.IOException;
  44. import java.io.InputStream;
  45. import java.io.ObjectOutputStream;
  46. import java.net.URL;
  47. import java.sql.Statement;
  48. import java.util.ArrayList;

  49. import org.apache.commons.io.serialization.ValidatingObjectInputStream;

  50. /**
  51.  * This class provides Data Access methods for MailItemQueue objects
  52.  */
  53. public class MailItemQueueDAO implements IMailItemQueueDAO
  54. {
  55.     private static final String SQL_QUERY_SELECT_NEXT_MAIL_ITEM_QUEUE_ID = "SELECT min(id_mail_queue) FROM core_mail_queue WHERE is_locked=0";
  56.     private static final String SQL_QUERY_SELECT_COUNT = "SELECT COUNT(id_mail_queue) FROM core_mail_queue";
  57.     private static final String SQL_QUERY_LOAD_MAIL_ITEM = "SELECT id_mail_queue,mail_item FROM core_mail_item WHERE id_mail_queue=? ";
  58.     private static final String SQL_QUERY_INSERT = " INSERT INTO core_mail_queue( id_mail_queue ) VALUES( DEFAULT ) ";
  59.     private static final String SQL_QUERY_INSERT_MAIL_ITEM = " INSERT INTO core_mail_item(id_mail_queue,mail_item) VALUES(?,?) ";
  60.     private static final String SQL_QUERY_LOCK_MAIL_ITEM = " UPDATE core_mail_queue SET is_locked=1 WHERE id_mail_queue= ? ";
  61.     private static final String SQL_QUERY_DELETE = " DELETE FROM core_mail_queue WHERE id_mail_queue = ?";
  62.     private static final String SQL_QUERY_DELETE_MAIL_ITEM = " DELETE FROM core_mail_item WHERE id_mail_queue = ?";

  63.     /**
  64.      * return the next mail item queue id
  65.      *
  66.      * @return the next mail item queue id
  67.      */
  68.     @Override
  69.     public int nextMailItemQueueId( )
  70.     {
  71.         int nIdMailItemQueue = -1;
  72.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_NEXT_MAIL_ITEM_QUEUE_ID ) )
  73.         {

  74.             daoUtil.executeQuery( );

  75.             if ( daoUtil.next( ) )
  76.             {
  77.                 nIdMailItemQueue = daoUtil.getInt( 1 );
  78.             }

  79.         }

  80.         return nIdMailItemQueue;
  81.     }

  82.     /**
  83.      * Lock the mail item
  84.      *
  85.      * @param nIdMailItemQueue
  86.      *            the id of the mail item to lock
  87.      */
  88.     @Override
  89.     public void lockMailItemQueue( int nIdMailItemQueue )
  90.     {
  91.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_LOCK_MAIL_ITEM ) )
  92.         {
  93.             daoUtil.setInt( 1, nIdMailItemQueue );
  94.             daoUtil.executeUpdate( );
  95.         }
  96.     }

  97.     /**
  98.      * Insert a new mail item in the table.
  99.      *
  100.      * @param mailItemQueue
  101.      *            the mail item
  102.      */
  103.     @Override
  104.     public synchronized void insert( MailItemQueue mailItemQueue )
  105.     {
  106.         try
  107.         {
  108.             ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream( );
  109.             ObjectOutputStream objectOutputStream;
  110.             objectOutputStream = new ObjectOutputStream( byteArrayOutputStream );
  111.             objectOutputStream.writeObject( mailItemQueue.getMailItem( ) );
  112.             objectOutputStream.close( );
  113.             byteArrayOutputStream.close( );

  114.             doInsertMail( mailItemQueue, byteArrayOutputStream );
  115.         }
  116.         catch( Exception e )
  117.         {
  118.             AppLogService.error( e );
  119.         }
  120.     }

  121.     private void doInsertMail( MailItemQueue mailItemQueue, ByteArrayOutputStream byteArrayOutputStream )
  122.     {
  123.         TransactionManager.beginTransaction( null );
  124.         try ( DAOUtil daoUtilKey = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS ) ;
  125.                 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_MAIL_ITEM ) )
  126.         {
  127.             daoUtilKey.executeUpdate( );
  128.             daoUtilKey.nextGeneratedKey( );
  129.             int nNewPrimaryKey = daoUtilKey.getGeneratedKeyInt( 1 );
  130.             mailItemQueue.setIdMailItemQueue( nNewPrimaryKey );
  131.             daoUtil.setInt( 1, nNewPrimaryKey );
  132.             daoUtil.setBytes( 2, byteArrayOutputStream.toByteArray( ) );
  133.             daoUtil.executeUpdate( );
  134.             TransactionManager.commitTransaction( null );
  135.         }
  136.         catch( Exception e )
  137.         {
  138.             TransactionManager.rollBack( null );
  139.             AppLogService.error( e );
  140.         }
  141.     }

  142.     /**
  143.      * return the first mail item in the table
  144.      *
  145.      * @param nIdMailItemQueue
  146.      *            the id of the mail item
  147.      * @return the first mail item in the table
  148.      */
  149.     @Override
  150.     public MailItemQueue load( int nIdMailItemQueue )
  151.     {
  152.         MailItemQueue mailItemQueue = null;
  153.         MailItem mailItem = null;
  154.         InputStream inputStream;
  155.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_LOAD_MAIL_ITEM ) )
  156.         {
  157.             daoUtil.setInt( 1, nIdMailItemQueue );
  158.             daoUtil.executeQuery( );

  159.             if ( daoUtil.next( ) )
  160.             {
  161.                 mailItemQueue = new MailItemQueue( );
  162.                 mailItemQueue.setIdMailItemQueue( daoUtil.getInt( 1 ) );
  163.                 inputStream = daoUtil.getBinaryStream( 2 );

  164.                 try ( ValidatingObjectInputStream objectInputStream = new ValidatingObjectInputStream( inputStream ) )
  165.                 {
  166.                     objectInputStream.accept( MailItem.class, ArrayList.class, byte [ ].class, FileAttachment.class, UrlAttachment.class,
  167.                             FileAttachment [ ].class, UrlAttachment [ ].class, URL.class );
  168.                     mailItem = (MailItem) objectInputStream.readObject( );
  169.                 }
  170.                 catch( ClassNotFoundException | IOException e )
  171.                 {
  172.                     AppLogService.error( e.getMessage( ), e );
  173.                 }
  174.                 finally
  175.                 {
  176.                     try
  177.                     {
  178.                         inputStream.close( );
  179.                     }
  180.                     catch( IOException e )
  181.                     {
  182.                         AppLogService.error( e.getMessage( ), e );
  183.                     }
  184.                 }

  185.                 mailItemQueue.setMailItem( mailItem );
  186.             }

  187.         }

  188.         return mailItemQueue;
  189.     }

  190.     /**
  191.      * Delete the mail item record in the table
  192.      *
  193.      * @param nIdMailItemQueue
  194.      *            The identifier of the mail item to remove
  195.      */
  196.     @Override
  197.     public void delete( int nIdMailItemQueue )
  198.     {
  199.         Transaction transaction = new Transaction( );

  200.         try
  201.         {
  202.             transaction.prepareStatement( SQL_QUERY_DELETE_MAIL_ITEM );
  203.             transaction.getStatement( ).setInt( 1, nIdMailItemQueue );
  204.             transaction.executeStatement( );
  205.             transaction.prepareStatement( SQL_QUERY_DELETE );
  206.             transaction.getStatement( ).setInt( 1, nIdMailItemQueue );
  207.             transaction.executeStatement( );
  208.             transaction.commit( );
  209.         }
  210.         catch( Exception e )
  211.         {
  212.             transaction.rollback( e );
  213.             AppLogService.error( e );
  214.         }
  215.     }

  216.     /**
  217.      * @return the number of mail item present in the core_mail_queue
  218.      */
  219.     @Override
  220.     public int getCountMailItem( )
  221.     {
  222.         int nCount = 0;
  223.         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_COUNT ) )
  224.         {
  225.             daoUtil.executeQuery( );

  226.             if ( daoUtil.next( ) )
  227.             {
  228.                 nCount = daoUtil.getInt( 1 );
  229.             }

  230.         }

  231.         return nCount;
  232.     }
  233. }