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