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