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