View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.plugins.directory.business;
35  
36  import fr.paris.lutece.plugins.directory.utils.DirectoryUtils;
37  import fr.paris.lutece.portal.service.plugin.Plugin;
38  import fr.paris.lutece.util.sql.DAOUtil;
39  
40  import java.util.ArrayList;
41  import java.util.List;
42  
43  /**
44   * This class provides Data Access methods for Directory XSL objects
45   */
46  public final class DirectoryXslDAO implements IDirectoryXslDAO
47  {
48      // Constants
49      private static final String SQL_QUERY_NEW_PK = "SELECT max( id_directory_xsl ) FROM directory_xsl";
50      private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = "SELECT id_directory_xsl,title,description,extension,id_file,id_category"
51              + " FROM directory_xsl WHERE id_directory_xsl = ?";
52      private static final String SQL_QUERY_INSERT = "INSERT INTO directory_xsl( id_directory_xsl,title,description,extension,id_file,id_category)"
53              + " VALUES(?,?,?,?,?,?)";
54      private static final String SQL_QUERY_DELETE = "DELETE FROM directory_xsl WHERE id_directory_xsl = ? ";
55      private static final String SQL_QUERY_UPDATE = "UPDATE directory_xsl SET id_directory_xsl=?,title=?,description=?,extension=?,id_file=?,id_category=? WHERE id_directory_xsl = ? ";
56      private static final String SQL_QUERY_SELECT = "SELECT id_directory_xsl,title,description,extension,id_file,id_category" + " FROM directory_xsl ";
57      private static final String SQL_FILTER_ID_CATEGORY = " id_category = ? ";
58      private static final String SQL_ORDER_BY_ID_CATEGORY = " ORDER BY id_category ";
59  
60      // Security on files
61      private static final String SQL_QUERY_FIND_BY_FILE = "SELECT id_directory_xsl,title,description,extension,id_file,id_category"
62              + " FROM directory_xsl WHERE id_file = ?";
63  
64      /**
65       * {@inheritDoc}
66       */
67      @Override
68      public int newPrimaryKey( Plugin plugin )
69      {
70          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
71          daoUtil.executeQuery( );
72  
73          int nKey;
74  
75          if ( !daoUtil.next( ) )
76          {
77              // if the table is empty
78              nKey = 1;
79          }
80  
81          nKey = daoUtil.getInt( 1 ) + 1;
82          daoUtil.free( );
83  
84          return nKey;
85      }
86  
87      /**
88       * {@inheritDoc}
89       */
90      @Override
91      public synchronized void insert( DirectoryXsl directoryXsl, Plugin plugin )
92      {
93          directoryXsl.setIdDirectoryXsl( newPrimaryKey( plugin ) );
94  
95          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
96          daoUtil.setInt( 1, directoryXsl.getIdDirectoryXsl( ) );
97          daoUtil.setString( 2, directoryXsl.getTitle( ) );
98          daoUtil.setString( 3, directoryXsl.getDescription( ) );
99          daoUtil.setString( 4, directoryXsl.getExtension( ) );
100 
101         if ( directoryXsl.getFile( ) != null )
102         {
103             daoUtil.setInt( 5, directoryXsl.getFile( ).getIdFile( ) );
104         }
105         else
106         {
107             daoUtil.setIntNull( 5 );
108         }
109 
110         if ( directoryXsl.getCategory( ) != null )
111         {
112             daoUtil.setInt( 6, directoryXsl.getCategory( ).getIdCategory( ) );
113         }
114         else
115         {
116             daoUtil.setIntNull( 6 );
117         }
118 
119         daoUtil.executeUpdate( );
120 
121         daoUtil.free( );
122     }
123 
124     /**
125      * {@inheritDoc}
126      */
127     @Override
128     public DirectoryXsl load( int nId, Plugin plugin )
129     {
130         return load( nId, SQL_QUERY_FIND_BY_PRIMARY_KEY, plugin );
131     }
132 
133     /**
134      * {@inheritDoc}
135      */
136     @Override
137     public DirectoryXsl loadByFile( int nIdFile, Plugin plugin )
138     {
139         return load( nIdFile, SQL_QUERY_FIND_BY_FILE, plugin );
140     }
141 
142     private DirectoryXsl load( int nId, String strSQL, Plugin plugin )
143     {
144         DAOUtil daoUtil = new DAOUtil( strSQL, plugin );
145         daoUtil.setInt( 1, nId );
146         daoUtil.executeQuery( );
147 
148         DirectoryXsl directoryXsl = null;
149         Category category = null;
150         File file = null;
151 
152         if ( daoUtil.next( ) )
153         {
154             directoryXsl = new DirectoryXsl( );
155             directoryXsl.setIdDirectoryXsl( daoUtil.getInt( 1 ) );
156             directoryXsl.setTitle( daoUtil.getString( 2 ) );
157             directoryXsl.setDescription( daoUtil.getString( 3 ) );
158             directoryXsl.setExtension( daoUtil.getString( 4 ) );
159 
160             if ( daoUtil.getObject( 5 ) != null )
161             {
162                 file = new File( );
163                 file.setIdFile( daoUtil.getInt( 5 ) );
164                 directoryXsl.setFile( file );
165             }
166 
167             if ( daoUtil.getObject( 6 ) != null )
168             {
169                 category = new Category( );
170                 category.setIdCategory( daoUtil.getInt( 6 ) );
171                 directoryXsl.setCategory( category );
172             }
173         }
174 
175         daoUtil.free( );
176 
177         return directoryXsl;
178     }
179 
180     /**
181      * {@inheritDoc}
182      */
183     @Override
184     public void delete( int nIdDirectoryXsl, Plugin plugin )
185     {
186         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
187         daoUtil.setInt( 1, nIdDirectoryXsl );
188         daoUtil.executeUpdate( );
189         daoUtil.free( );
190     }
191 
192     /**
193      * {@inheritDoc}
194      */
195     @Override
196     public void store( DirectoryXsl directoryXsl, Plugin plugin )
197     {
198         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
199         daoUtil.setInt( 1, directoryXsl.getIdDirectoryXsl( ) );
200         daoUtil.setString( 2, directoryXsl.getTitle( ) );
201         daoUtil.setString( 3, directoryXsl.getDescription( ) );
202         daoUtil.setString( 4, directoryXsl.getExtension( ) );
203 
204         if ( directoryXsl.getFile( ) != null )
205         {
206             daoUtil.setInt( 5, directoryXsl.getFile( ).getIdFile( ) );
207         }
208         else
209         {
210             daoUtil.setIntNull( 5 );
211         }
212 
213         if ( directoryXsl.getCategory( ) != null )
214         {
215             daoUtil.setInt( 6, directoryXsl.getCategory( ).getIdCategory( ) );
216         }
217         else
218         {
219             daoUtil.setIntNull( 6 );
220         }
221 
222         daoUtil.setInt( 7, directoryXsl.getIdDirectoryXsl( ) );
223         daoUtil.executeUpdate( );
224         daoUtil.free( );
225     }
226 
227     /**
228      * {@inheritDoc}
229      */
230     @Override
231     public List<DirectoryXsl> selectList( DirectoryXslFilter filter, Plugin plugin )
232     {
233         List<DirectoryXsl> directoryXslList = new ArrayList<DirectoryXsl>( );
234         List<String> listStrFilter = new ArrayList<String>( );
235         DirectoryXsl directoryXsl = null;
236         Category category = null;
237         File file = null;
238 
239         if ( filter.containsIdCategory( ) )
240         {
241             listStrFilter.add( SQL_FILTER_ID_CATEGORY );
242         }
243 
244         String strSQL = DirectoryUtils.buildRequetteWithFilter( SQL_QUERY_SELECT, listStrFilter, SQL_ORDER_BY_ID_CATEGORY );
245 
246         DAOUtil daoUtil = new DAOUtil( strSQL, plugin );
247 
248         int nIndex = 1;
249 
250         if ( filter.containsIdCategory( ) )
251         {
252             daoUtil.setInt( nIndex, filter.getIdCategory( ) );
253             nIndex++;
254         }
255 
256         daoUtil.executeQuery( );
257 
258         while ( daoUtil.next( ) )
259         {
260             directoryXsl = new DirectoryXsl( );
261             directoryXsl.setIdDirectoryXsl( daoUtil.getInt( 1 ) );
262             directoryXsl.setTitle( daoUtil.getString( 2 ) );
263             directoryXsl.setDescription( daoUtil.getString( 3 ) );
264             directoryXsl.setExtension( daoUtil.getString( 4 ) );
265 
266             if ( daoUtil.getObject( 5 ) != null )
267             {
268                 file = new File( );
269                 file.setIdFile( daoUtil.getInt( 5 ) );
270                 directoryXsl.setFile( file );
271             }
272 
273             if ( daoUtil.getObject( 6 ) != null )
274             {
275                 category = new Category( );
276                 category.setIdCategory( daoUtil.getInt( 6 ) );
277                 directoryXsl.setCategory( category );
278             }
279 
280             directoryXslList.add( directoryXsl );
281         }
282 
283         daoUtil.free( );
284 
285         return directoryXslList;
286     }
287 }