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.stylesheet;
35  
36  import fr.paris.lutece.portal.service.util.AppException;
37  import fr.paris.lutece.util.sql.DAOUtil;
38  
39  import java.util.ArrayList;
40  import java.util.Collection;
41  
42  
43  /**
44   * This class provides Data Access methods for StyleSheet objects
45   */
46  public final class StyleSheetDAO implements IStyleSheetDAO
47  {
48      // Constants
49      private static final String SQL_QUERY_NEW_PK = " SELECT max(id_stylesheet) FROM core_stylesheet ";
50      private static final String SQL_QUERY_SELECT = " SELECT a.description , a.file_name , a.source , b.id_style , b.id_mode " +
51          " FROM core_stylesheet a " + " LEFT JOIN core_style_mode_stylesheet b ON a.id_stylesheet = b.id_stylesheet " +
52          " WHERE a.id_stylesheet = ? ";
53      private static final String SQL_QUERY_INSERT = " INSERT INTO core_stylesheet ( id_stylesheet , description , file_name, source ) " +
54          " VALUES ( ?, ? ,?, ? )";
55      private static final String SQL_QUERY_DELETE = " DELETE FROM core_stylesheet WHERE id_stylesheet = ? ";
56      private static final String SQL_QUERY_UPDATE = " UPDATE core_stylesheet SET id_stylesheet = ?, description = ?, file_name = ?, source = ? WHERE id_stylesheet = ?  ";
57      private static final String SQL_QUERY_SELECT_MODEID = " SELECT a.id_mode FROM core_mode a , core_style_mode_stylesheet b  " +
58          " WHERE a.id_mode = b.id_mode AND b.id_stylesheet = ?";
59      private static final String SQL_QUERY_COUNT_STYLESHEET = " SELECT count(*) FROM core_style_mode_stylesheet WHERE id_style = ? AND id_mode = ? ";
60      private static final String SQL_QUERY_INSERT_STYLEMODESTYLESHEET = " INSERT INTO core_style_mode_stylesheet ( id_style , id_mode , id_stylesheet ) " +
61          " VALUES ( ?, ? ,? )";
62      private static final String SQL_QUERY_UPDATE_STYLEMODESTYLESHEET = " UPDATE core_style_mode_stylesheet SET id_style = ? , id_mode = ?  " +
63          " WHERE id_stylesheet = ? ";
64      private static final String SQL_QUERY_DELETEE_STYLEMODESTYLESHEET = " DELETE FROM core_style_mode_stylesheet WHERE id_stylesheet = ? ";
65  
66      ///////////////////////////////////////////////////////////////////////////////////////
67      //Access methods to data
68  
69      /**
70       * Generates a new primary key
71       * @return The new primary key
72       */
73      int newPrimaryKey(  )
74      {
75          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK );
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       */
97      public synchronized void insert( StyleSheet stylesheet )
98      {
99          stylesheet.setId( newPrimaryKey(  ) );
100 
101         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT );
102 
103         daoUtil.setInt( 1, stylesheet.getId(  ) );
104         daoUtil.setString( 2, stylesheet.getDescription(  ) );
105         daoUtil.setString( 3, stylesheet.getFile(  ) );
106         daoUtil.setBytes( 4, stylesheet.getSource(  ) );
107 
108         daoUtil.executeUpdate(  );
109 
110         //Update of the table style_mode_stylesheet in the database
111         insertStyleModeStyleSheet( stylesheet );
112         daoUtil.free(  );
113     }
114 
115     /**
116      * Load the data of Stylesheet from the table
117      * @param nIdStylesheet the identifier of the Stylesheet to load
118      * @return stylesheet
119      */
120     public StyleSheet load( int nIdStylesheet )
121     {
122         StyleSheet stylesheet = null;
123         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT );
124         daoUtil.setInt( 1, nIdStylesheet );
125         daoUtil.executeQuery(  );
126 
127         if ( daoUtil.next(  ) )
128         {
129             stylesheet = new StyleSheet(  );
130             stylesheet.setId( nIdStylesheet );
131             stylesheet.setDescription( daoUtil.getString( 1 ) );
132             stylesheet.setFile( daoUtil.getString( 2 ) );
133             stylesheet.setSource( daoUtil.getBytes( 3 ) );
134             stylesheet.setStyleId( daoUtil.getInt( 4 ) );
135             stylesheet.setModeId( daoUtil.getInt( 5 ) );
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      */
148     public void delete( int nIdStylesheet )
149     {
150         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE );
151         daoUtil.setInt( 1, nIdStylesheet );
152         daoUtil.executeUpdate(  );
153         daoUtil.free(  );
154 
155         // delete also into style_mode_stylesheet
156         deleteStyleModeStyleSheet( nIdStylesheet );
157     }
158 
159     /**
160      * Load the list of stylesheet
161      * @param nModeId The Mode identifier
162      * @return the list of the StyleSheet in form of a collection of StyleSheet objects
163      */
164     public Collection<StyleSheet> selectStyleSheetList( int nModeId )
165     {
166         Collection<StyleSheet> stylesheetList = new ArrayList<StyleSheet>(  );
167 
168         String strSelect = " SELECT a.id_stylesheet , a.description , a.file_name ";
169         String strFrom = " FROM core_stylesheet a ";
170 
171         if ( nModeId != -1 )
172         {
173             strFrom = " FROM  core_stylesheet a , core_style_mode_stylesheet b " +
174                 " WHERE a.id_stylesheet = b.id_stylesheet " + " AND b.id_mode = ? ";
175         }
176 
177         strFrom += " ORDER BY a.description ";
178 
179         String strSQL = strSelect + strFrom;
180 
181         DAOUtil daoUtil = new DAOUtil( strSQL );
182 
183         if ( nModeId != -1 )
184         {
185             daoUtil.setInt( 1, nModeId );
186         }
187 
188         daoUtil.executeQuery(  );
189 
190         while ( daoUtil.next(  ) )
191         {
192             StyleSheet stylesheet = new StyleSheet(  );
193 
194             stylesheet.setId( daoUtil.getInt( 1 ) );
195             stylesheet.setDescription( daoUtil.getString( 2 ) );
196             stylesheet.setFile( daoUtil.getString( 3 ) );
197             stylesheetList.add( stylesheet );
198         }
199 
200         daoUtil.free(  );
201 
202         return stylesheetList;
203     }
204 
205     /**
206      * Update the record in the table
207      * @param stylesheet The stylesheet
208      */
209     public void store( StyleSheet stylesheet )
210     {
211         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE );
212         daoUtil.setInt( 1, stylesheet.getId(  ) );
213         daoUtil.setString( 2, stylesheet.getDescription(  ) );
214         daoUtil.setString( 3, stylesheet.getFile(  ) );
215         daoUtil.setBytes( 4, stylesheet.getSource(  ) );
216         daoUtil.setInt( 5, stylesheet.getId(  ) );
217 
218         daoUtil.executeUpdate(  );
219         daoUtil.free(  );
220 
221         // update the table style_mode_stylesheet
222         updateStyleModeStyleSheet( stylesheet );
223     }
224 
225     /**
226      * Insert a new record in the table style_mode_stylesheet
227      * @param stylesheet the instance of StyleSheet to insert in the table
228      */
229     private void insertStyleModeStyleSheet( StyleSheet stylesheet )
230     {
231         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_STYLEMODESTYLESHEET );
232 
233         daoUtil.setInt( 1, stylesheet.getStyleId(  ) );
234         daoUtil.setInt( 2, stylesheet.getModeId(  ) );
235         daoUtil.setInt( 3, stylesheet.getId(  ) );
236 
237         daoUtil.executeUpdate(  );
238         daoUtil.free(  );
239     }
240 
241     /**
242      * Updates the table style_mode_stylesheet with the data of the StyleShhet
243      * instance specified in parameter
244      * @param stylesheet the instance of the stylesheet to update
245      */
246     private void updateStyleModeStyleSheet( StyleSheet stylesheet )
247     {
248         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_STYLEMODESTYLESHEET );
249 
250         daoUtil.setInt( 1, stylesheet.getStyleId(  ) );
251         daoUtil.setInt( 2, stylesheet.getModeId(  ) );
252         daoUtil.setInt( 3, stylesheet.getId(  ) );
253 
254         daoUtil.executeUpdate(  );
255         daoUtil.free(  );
256     }
257 
258     /**
259      * Deletes the data in the table style_mode_stylesheet
260      *
261      * @param nStyleSheetId the identifier of the stylesheet
262      */
263     private void deleteStyleModeStyleSheet( int nStyleSheetId )
264     {
265         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETEE_STYLEMODESTYLESHEET );
266         daoUtil.setInt( 1, nStyleSheetId );
267         daoUtil.executeUpdate(  );
268         daoUtil.free(  );
269     }
270 
271     /**
272      * Returns the number of stylesheets associated to the style and the mode
273      * specified in parameter
274      * @param nStyleId the style id
275      * @param nModeId the mode id
276      * @return the number of stylesheet associated
277      */
278     public int selectStyleSheetNbPerStyleMode( int nStyleId, int nModeId )
279     {
280         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_COUNT_STYLESHEET );
281 
282         daoUtil.setInt( 1, nStyleId );
283         daoUtil.setInt( 2, nModeId );
284 
285         daoUtil.executeQuery(  );
286 
287         if ( !daoUtil.next(  ) )
288         {
289             daoUtil.free(  );
290             throw new AppException( DAOUtil.MSG_EXCEPTION_SELECT_ERROR + nModeId + " StyleId " + nStyleId );
291         }
292 
293         int nCount = ( daoUtil.getInt( 1 ) );
294 
295         daoUtil.free(  );
296 
297         return nCount;
298     }
299 
300     /**
301      * Returns the identifier of the mode of the stylesheet whose identifier
302      * is specified in parameter
303      * @param nIdStylesheet the identifier of the stylesheet
304      * @return the identifier of the mode
305      */
306     public int selectModeId( int nIdStylesheet )
307     {
308         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_MODEID );
309 
310         daoUtil.setInt( 1, nIdStylesheet );
311         daoUtil.executeQuery(  );
312 
313         if ( !daoUtil.next(  ) )
314         {
315             daoUtil.free(  );
316             throw new AppException( DAOUtil.MSG_EXCEPTION_SELECT_ERROR + nIdStylesheet );
317         }
318 
319         int nModeId = ( daoUtil.getInt( 1 ) );
320 
321         daoUtil.free(  );
322 
323         return nModeId;
324     }
325 }