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