View Javadoc
1   /*
2    * Copyright (c) 2002-2023, 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.plugins.document.business;
35  
36  import fr.paris.lutece.util.sql.DAOUtil;
37  
38  import java.util.ArrayList;
39  import java.util.List;
40  
41  /**
42   * This class provides Data Access methods for Indexer Action objects
43   */
44  public final class IndexerActionDAO implements IIndexerActionDAO
45  {
46      // Constants
47      public static final String CONSTANT_WHERE = " WHERE ";
48      public static final String CONSTANT_AND = " AND ";
49      private static final String SQL_QUERY_NEW_PK = "SELECT max( id_action ) FROM document_indexer_action";
50      private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = "SELECT id_action,id_record,id_task" + " FROM document_indexer_action WHERE id_action = ?";
51      private static final String SQL_QUERY_INSERT = "INSERT INTO document_indexer_action( id_action,id_record,id_task)" + " VALUES(?,?,?)";
52      private static final String SQL_QUERY_DELETE = "DELETE FROM document_indexer_action WHERE id_action = ? ";
53      private static final String SQL_QUERY_UPDATE = "UPDATE document_indexer_action SET id_action=?,id_record=?,id_task=? WHERE id_action = ? ";
54      private static final String SQL_QUERY_SELECT = "SELECT id_action,id_record,id_task" + " FROM document_indexer_action  ";
55      private static final String SQL_QUERY_DELETE_ALL = "DELETE from document_indexer_action";
56      private static final String SQL_FILTER_ID_TASK = " id_task = ? ";
57  
58      /*
59       * (non-Javadoc)
60       * 
61       * @see fr.paris.lutece.plugins.document.business.IIndexerActionDAO#newPrimaryKey()
62       */
63      public int newPrimaryKey( )
64      {
65          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK );
66          daoUtil.executeQuery( );
67  
68          int nKey;
69  
70          if ( !daoUtil.next( ) )
71          {
72              // if the table is empty
73              nKey = 1;
74          }
75  
76          nKey = daoUtil.getInt( 1 ) + 1;
77          daoUtil.free( );
78  
79          return nKey;
80      }
81  
82      /*
83       * (non-Javadoc)
84       * 
85       * @see fr.paris.lutece.plugins.document.business.IIndexerActionDAO#insert(fr.paris.lutece.plugins.document.business.IndexerAction)
86       */
87      public synchronized void insert( IndexerAction indexerAction )
88      {
89          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT );
90          daoUtil.setInt( 2, indexerAction.getIdDocument( ) );
91          daoUtil.setInt( 3, indexerAction.getIdTask( ) );
92  
93          indexerAction.setIdAction( newPrimaryKey( ) );
94          daoUtil.setInt( 1, indexerAction.getIdAction( ) );
95  
96          daoUtil.executeUpdate( );
97  
98          daoUtil.free( );
99      }
100 
101     /*
102      * (non-Javadoc)
103      * 
104      * @see fr.paris.lutece.plugins.document.business.IIndexerActionDAO#load(int)
105      */
106     public IndexerAction load( int nId )
107     {
108         IndexerAction indexerAction = null;
109 
110         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY );
111         daoUtil.setInt( 1, nId );
112         daoUtil.executeQuery( );
113 
114         if ( daoUtil.next( ) )
115         {
116             indexerAction = new IndexerAction( );
117             indexerAction.setIdAction( daoUtil.getInt( 1 ) );
118             indexerAction.setIdDocument( daoUtil.getInt( 2 ) );
119             indexerAction.setIdTask( daoUtil.getInt( 3 ) );
120         }
121 
122         daoUtil.free( );
123 
124         return indexerAction;
125     }
126 
127     /*
128      * (non-Javadoc)
129      * 
130      * @see fr.paris.lutece.plugins.document.business.IIndexerActionDAO#delete(int)
131      */
132     public void delete( int nId )
133     {
134         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE );
135         daoUtil.setInt( 1, nId );
136         daoUtil.executeUpdate( );
137         daoUtil.free( );
138     }
139 
140     /*
141      * (non-Javadoc)
142      * 
143      * @see fr.paris.lutece.plugins.document.business.IIndexerActionDAO#delete(int)
144      */
145     public void deleteAll( )
146     {
147         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_ALL );
148         daoUtil.executeUpdate( );
149         daoUtil.free( );
150     }
151 
152     /*
153      * (non-Javadoc)
154      * 
155      * @see fr.paris.lutece.plugins.document.business.IIndexerActionDAO#store(fr.paris.lutece.plugins.document.business.IndexerAction)
156      */
157     public void store( IndexerAction indexerAction )
158     {
159         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE );
160         daoUtil.setInt( 1, indexerAction.getIdAction( ) );
161         daoUtil.setInt( 2, indexerAction.getIdDocument( ) );
162         daoUtil.setInt( 3, indexerAction.getIdTask( ) );
163 
164         daoUtil.setInt( 4, indexerAction.getIdAction( ) );
165 
166         daoUtil.executeUpdate( );
167         daoUtil.free( );
168     }
169 
170     /*
171      * (non-Javadoc)
172      * 
173      * @see fr.paris.lutece.plugins.document.business.IIndexerActionDAO#selectList(fr.paris.lutece.plugins.document.business.IndexerActionlFilter)
174      */
175     public List<IndexerAction> selectList( IndexerActionFilter filter )
176     {
177         List<IndexerAction> indexerActionList = new ArrayList<IndexerAction>( );
178         IndexerAction indexerAction = null;
179         List<String> listStrFilter = new ArrayList<String>( );
180 
181         if ( filter.containsIdTask( ) )
182         {
183             listStrFilter.add( SQL_FILTER_ID_TASK );
184         }
185 
186         String strSQL = buildRequestWithFilter( SQL_QUERY_SELECT, listStrFilter, null );
187 
188         DAOUtil daoUtil = new DAOUtil( strSQL );
189 
190         int nIndex = 1;
191 
192         if ( filter.containsIdTask( ) )
193         {
194             daoUtil.setInt( nIndex, filter.getIdTask( ) );
195             nIndex++;
196         }
197 
198         daoUtil.executeQuery( );
199 
200         while ( daoUtil.next( ) )
201         {
202             indexerAction = new IndexerAction( );
203             indexerAction.setIdAction( daoUtil.getInt( 1 ) );
204             indexerAction.setIdDocument( daoUtil.getInt( 2 ) );
205             indexerAction.setIdTask( daoUtil.getInt( 3 ) );
206 
207             indexerActionList.add( indexerAction );
208         }
209 
210         daoUtil.free( );
211 
212         return indexerActionList;
213     }
214 
215     /**
216      * Builds a query with filters placed in parameters
217      * 
218      * @param strSelect
219      *            the select of the query
220      * @param listStrFilter
221      *            the list of filter to add in the query
222      * @param strOrder
223      *            the order by of the query
224      * @return a query
225      */
226     public static String buildRequestWithFilter( String strSelect, List<String> listStrFilter, String strOrder )
227     {
228         StringBuffer strBuffer = new StringBuffer( );
229         strBuffer.append( strSelect );
230 
231         int nCount = 0;
232 
233         for ( String strFilter : listStrFilter )
234         {
235             if ( ++nCount == 1 )
236             {
237                 strBuffer.append( CONSTANT_WHERE );
238             }
239 
240             strBuffer.append( strFilter );
241 
242             if ( nCount != listStrFilter.size( ) )
243             {
244                 strBuffer.append( CONSTANT_AND );
245             }
246         }
247 
248         if ( strOrder != null )
249         {
250             strBuffer.append( strOrder );
251         }
252 
253         return strBuffer.toString( );
254     }
255 }