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.plugins.calendar.business.stylesheet;
35  
36  import fr.paris.lutece.portal.business.stylesheet.StyleSheet;
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.Collection;
42  
43  import org.apache.commons.lang.StringUtils;
44  
45  
46  /**
47   * This class provides Data Access methods for StyleSheet objects
48   */
49  public final class CalendarStyleSheetDAO implements ICalendarStyleSheetDAO
50  {
51      // Constants
52      private static final String SQL_QUERY_NEW_PK = " SELECT max(id_stylesheet) FROM calendar_export_stylesheets ";
53      private static final String SQL_QUERY_SELECT = " SELECT a.description , a.file_name , a.source"
54              + " FROM calendar_export_stylesheets a WHERE a.id_stylesheet = ? ";
55      private static final String SQL_QUERY_SELECT_LIST = " SELECT a.id_stylesheet, a.description , a.file_name"
56              + " FROM calendar_export_stylesheets a where a.description NOT LIKE '%rss%' ORDER BY a.description ";
57      private static final String SQL_QUERY_INSERT = " INSERT INTO calendar_export_stylesheets ( id_stylesheet , description , file_name, source ) "
58              + " VALUES ( ?, ? ,?, ? )";
59      private static final String SQL_QUERY_DELETE = " DELETE FROM calendar_export_stylesheets WHERE id_stylesheet = ? ";
60      private static final String SQL_QUERY_UPDATE = " UPDATE calendar_export_stylesheets SET id_stylesheet = ?, description = ?, file_name = ?, source = ? WHERE id_stylesheet = ?  ";
61      private static final String SQL_QUERY_UPDATE_EXTENSION = " UPDATE calendar_export_stylesheets SET extension = ? WHERE id_stylesheet = ?  ";
62      private static final String SQL_QUERY_SELECT_EXTENSION = " SELECT a.extension FROM calendar_export_stylesheets a WHERE a.id_stylesheet = ? ";
63      private static final String SQL_QUERY_INSERT_EXTENSION = " UPDATE calendar_export_stylesheets SET extension = ? WHERE id_stylesheet = ?  ";
64  
65      ///////////////////////////////////////////////////////////////////////////////////////
66      //Access methods to data
67  
68      /**
69       * Generates a new primary key
70       * @param plugin The plugin
71       * @return The new primary key
72       */
73      int newPrimaryKey( Plugin plugin )
74      {
75          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
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  
88          daoUtil.free( );
89  
90          return nKey;
91      }
92  
93      /**
94       * Insert a new record in the table.
95       * @param stylesheet The StyleSheet object
96       * @param strExtension the extension
97       * @param plugin The plugin
98       */
99      public synchronized void insert( StyleSheet stylesheet, String strExtension, Plugin plugin )
100     {
101         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
102 
103         stylesheet.setId( newPrimaryKey( plugin ) );
104 
105         daoUtil.setInt( 1, stylesheet.getId( ) );
106         daoUtil.setString( 2, stylesheet.getDescription( ) );
107         daoUtil.setString( 3, stylesheet.getFile( ) );
108         daoUtil.setBytes( 4, stylesheet.getSource( ) );
109 
110         daoUtil.executeUpdate( );
111         daoUtil.free( );
112 
113         insertExtension( stylesheet.getId( ), strExtension, plugin );
114     }
115 
116     /**
117      * Load the data of Stylesheet from the table
118      * @param nIdStylesheet the identifier of the Stylesheet to load
119      * @param plugin Plugin
120      * @return stylesheet
121      */
122     public StyleSheet load( int nIdStylesheet, Plugin plugin )
123     {
124         StyleSheet stylesheet = null;
125         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
126         daoUtil.setInt( 1, nIdStylesheet );
127         daoUtil.executeQuery( );
128 
129         if ( daoUtil.next( ) )
130         {
131             stylesheet = new StyleSheet( );
132             stylesheet.setId( nIdStylesheet );
133             stylesheet.setDescription( daoUtil.getString( 1 ) );
134             stylesheet.setFile( daoUtil.getString( 2 ) );
135             stylesheet.setSource( daoUtil.getBytes( 3 ) );
136         }
137 
138         daoUtil.free( );
139 
140         return stylesheet;
141     }
142 
143     /**
144      * Delete the StyleSheet from the database whose identifier is specified
145      * in parameter
146      * @param nIdStylesheet the identifier of the StyleSheet to delete
147      * @param plugin Plugin
148      */
149     public void delete( int nIdStylesheet, Plugin plugin )
150     {
151         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
152         daoUtil.setInt( 1, nIdStylesheet );
153         daoUtil.executeUpdate( );
154         daoUtil.free( );
155     }
156 
157     /**
158      * Load the list of stylesheet
159      * @param plugin Plugin
160      * @return the list of the StyleSheet in form of a collection of StyleSheet
161      *         objects
162      */
163     public Collection<StyleSheet> selectStyleSheetList( Plugin plugin )
164     {
165         Collection<StyleSheet> stylesheetList = new ArrayList<StyleSheet>( );
166 
167         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_LIST, plugin );
168 
169         daoUtil.executeQuery( );
170 
171         while ( daoUtil.next( ) )
172         {
173             StyleSheet stylesheet = new StyleSheet( );
174 
175             stylesheet.setId( daoUtil.getInt( 1 ) );
176             stylesheet.setDescription( daoUtil.getString( 2 ) );
177             stylesheet.setFile( daoUtil.getString( 3 ) );
178             stylesheetList.add( stylesheet );
179         }
180 
181         daoUtil.free( );
182 
183         return stylesheetList;
184     }
185 
186     /**
187      * Update the record in the table
188      * @param stylesheet The stylesheet
189      * @param plugin Plugin
190      */
191     public void store( StyleSheet stylesheet, Plugin plugin )
192     {
193         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
194         daoUtil.setInt( 1, stylesheet.getId( ) );
195         daoUtil.setString( 2, stylesheet.getDescription( ) );
196         daoUtil.setString( 3, stylesheet.getFile( ) );
197         daoUtil.setBytes( 4, stylesheet.getSource( ) );
198         daoUtil.setInt( 5, stylesheet.getId( ) );
199         daoUtil.executeUpdate( );
200         daoUtil.free( );
201     }
202 
203     /**
204      * Insertion of the extension of an export file in the database
205      * @param nId the id
206      * @param strExtension the extension file
207      * @param plugin Plugin
208      */
209     public void insertExtension( int nId, String strExtension, Plugin plugin )
210     {
211         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_EXTENSION, plugin );
212         daoUtil.setString( 1, strExtension );
213         daoUtil.setInt( 2, nId );
214         daoUtil.executeUpdate( );
215         daoUtil.free( );
216     }
217 
218     /**
219      * Select the extension file related to the export stylesheet
220      * @param nId the identifier of the stylesheet
221      * @param plugin Plugin
222      * @return the extension file
223      */
224     public String selectExtension( int nId, Plugin plugin )
225     {
226         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_EXTENSION, plugin );
227         daoUtil.setInt( 1, nId );
228         daoUtil.executeQuery( );
229 
230         String strExtension = StringUtils.EMPTY;
231 
232         if ( daoUtil.next( ) )
233         {
234             strExtension = daoUtil.getString( 1 );
235         }
236 
237         daoUtil.free( );
238 
239         return strExtension;
240     }
241 
242     /**
243      * Update the the extension file whose identifier is specified in parameter
244      * @param nId the stylesheet id
245      * @param strExtension the new extension file
246      * @param plugin Plugin
247      */
248     public void updateExtension( int nId, String strExtension, Plugin plugin )
249     {
250         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_EXTENSION, plugin );
251         daoUtil.setString( 1, strExtension );
252         daoUtil.setInt( 2, nId );
253         daoUtil.executeUpdate( );
254         daoUtil.free( );
255     }
256 }