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.xsl;
35  
36  import fr.paris.lutece.portal.business.file.File;
37  import fr.paris.lutece.portal.service.plugin.Plugin;
38  import fr.paris.lutece.util.sql.DAOUtil;
39  
40  import java.sql.Statement;
41  import java.util.ArrayList;
42  import java.util.List;
43  
44  /**
45   * This class provides Data Access methods for XSL objects
46   */
47  public final class XslExportDAO implements IXslExportDAO
48  {
49      // Constants
50      private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = "SELECT id_xsl_export,title,description,extension,id_file,plugin FROM core_xsl_export WHERE id_xsl_export = ?";
51      private static final String SQL_QUERY_INSERT = "INSERT INTO core_xsl_export( title,description,extension,id_file,plugin) VALUES(?,?,?,?,?)";
52      private static final String SQL_QUERY_DELETE = "DELETE FROM core_xsl_export WHERE id_xsl_export = ? ";
53      private static final String SQL_QUERY_UPDATE = "UPDATE core_xsl_export SET id_xsl_export=?,title=?,description=?,extension=?,id_file=?,plugin=? WHERE id_xsl_export = ? ";
54      private static final String SQL_QUERY_SELECT = "SELECT id_xsl_export,title,description,extension,id_file,plugin FROM core_xsl_export ";
55      private static final String SQL_WHERE = " WHERE ";
56      private static final String SQL_FILTER_PLUGIN = " plugin = ? ";
57  
58      /**
59       * {@inheritDoc}
60       */
61      @Override
62      public void insert( XslExport xslExport )
63      {
64          try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS ) )
65          {
66              int nIndex = 1;
67              daoUtil.setString( nIndex++, xslExport.getTitle( ) );
68              daoUtil.setString( nIndex++, xslExport.getDescription( ) );
69              daoUtil.setString( nIndex++, xslExport.getExtension( ) );
70  
71              if ( xslExport.getFile( ) != null )
72              {
73                  daoUtil.setInt( nIndex++, xslExport.getFile( ).getIdFile( ) );
74              }
75              else
76              {
77                  daoUtil.setIntNull( nIndex++ );
78              }
79  
80              daoUtil.setString( nIndex, xslExport.getPlugin( ) );
81  
82              daoUtil.executeUpdate( );
83  
84              if ( daoUtil.nextGeneratedKey( ) )
85              {
86                  xslExport.setIdXslExport( daoUtil.getGeneratedKeyInt( 1 ) );
87              }
88          }
89      }
90  
91      /**
92       * {@inheritDoc}
93       */
94      @Override
95      public XslExport load( int nId )
96      {
97          XslExport xslExport = null;
98          try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY ) )
99          {
100             daoUtil.setInt( 1, nId );
101             daoUtil.executeQuery( );
102 
103             File file = null;
104 
105             if ( daoUtil.next( ) )
106             {
107                 xslExport = new XslExport( );
108                 xslExport.setIdXslExport( daoUtil.getInt( 1 ) );
109                 xslExport.setTitle( daoUtil.getString( 2 ) );
110                 xslExport.setDescription( daoUtil.getString( 3 ) );
111                 xslExport.setExtension( daoUtil.getString( 4 ) );
112 
113                 if ( daoUtil.getObject( 5 ) != null )
114                 {
115                     file = new File( );
116                     file.setIdFile( daoUtil.getInt( 5 ) );
117                     xslExport.setFile( file );
118                 }
119 
120                 xslExport.setPlugin( daoUtil.getString( 6 ) );
121             }
122 
123         }
124 
125         return xslExport;
126     }
127 
128     /**
129      * {@inheritDoc}
130      */
131     @Override
132     public void delete( int nIdXslExport )
133     {
134         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE ) )
135         {
136             daoUtil.setInt( 1, nIdXslExport );
137             daoUtil.executeUpdate( );
138         }
139     }
140 
141     /**
142      * {@inheritDoc}
143      */
144     @Override
145     public void store( XslExport xslExport )
146     {
147         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE ) )
148         {
149             daoUtil.setInt( 1, xslExport.getIdXslExport( ) );
150             daoUtil.setString( 2, xslExport.getTitle( ) );
151             daoUtil.setString( 3, xslExport.getDescription( ) );
152             daoUtil.setString( 4, xslExport.getExtension( ) );
153 
154             if ( xslExport.getFile( ) != null )
155             {
156                 daoUtil.setInt( 5, xslExport.getFile( ).getIdFile( ) );
157             }
158             else
159             {
160                 daoUtil.setIntNull( 5 );
161             }
162 
163             daoUtil.setString( 6, xslExport.getPlugin( ) );
164 
165             daoUtil.setInt( 7, xslExport.getIdXslExport( ) );
166             daoUtil.executeUpdate( );
167         }
168     }
169 
170     /**
171      * {@inheritDoc}
172      */
173     @Override
174     public List<XslExport> selectList( )
175     {
176         List<XslExport> listXslExport = new ArrayList<>( );
177         XslExport xslExport = null;
178         File file = null;
179 
180         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) )
181         {
182 
183             daoUtil.executeQuery( );
184 
185             while ( daoUtil.next( ) )
186             {
187                 xslExport = new XslExport( );
188                 xslExport.setIdXslExport( daoUtil.getInt( 1 ) );
189                 xslExport.setTitle( daoUtil.getString( 2 ) );
190                 xslExport.setDescription( daoUtil.getString( 3 ) );
191                 xslExport.setExtension( daoUtil.getString( 4 ) );
192 
193                 if ( daoUtil.getObject( 5 ) != null )
194                 {
195                     file = new File( );
196                     file.setIdFile( daoUtil.getInt( 5 ) );
197                     xslExport.setFile( file );
198                 }
199 
200                 xslExport.setPlugin( daoUtil.getString( 6 ) );
201 
202                 listXslExport.add( xslExport );
203             }
204 
205         }
206 
207         return listXslExport;
208     }
209 
210     /**
211      * {@inheritDoc}
212      */
213     @Override
214     public List<XslExport> selectListByPlugin( Plugin plugin )
215     {
216         List<XslExport> listXslExport = new ArrayList<>( );
217         XslExport xslExport = null;
218         File file = null;
219 
220         StringBuilder sbSql = new StringBuilder( SQL_QUERY_SELECT );
221         sbSql.append( SQL_WHERE );
222         sbSql.append( SQL_FILTER_PLUGIN );
223 
224         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( sbSql.toString( ) ) )
225         {
226             daoUtil.setString( 1, plugin.getName( ) );
227             daoUtil.executeQuery( );
228 
229             while ( daoUtil.next( ) )
230             {
231                 xslExport = new XslExport( );
232                 xslExport.setIdXslExport( daoUtil.getInt( 1 ) );
233                 xslExport.setTitle( daoUtil.getString( 2 ) );
234                 xslExport.setDescription( daoUtil.getString( 3 ) );
235                 xslExport.setExtension( daoUtil.getString( 4 ) );
236 
237                 if ( daoUtil.getObject( 5 ) != null )
238                 {
239                     file = new File( );
240                     file.setIdFile( daoUtil.getInt( 5 ) );
241                     xslExport.setFile( file );
242                 }
243 
244                 xslExport.setPlugin( daoUtil.getString( 6 ) );
245 
246                 listXslExport.add( xslExport );
247             }
248 
249         }
250 
251         return listXslExport;
252     }
253 }