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.dila.business.stylesheet.dao.impl;
35  
36  import fr.paris.lutece.plugins.dila.business.stylesheet.dao.IDilaStyleSheetDAO;
37  import fr.paris.lutece.plugins.dila.business.stylesheet.dto.ContentType;
38  import fr.paris.lutece.plugins.dila.business.stylesheet.dto.DilaStyleSheet;
39  import fr.paris.lutece.plugins.dila.service.DilaPlugin;
40  import fr.paris.lutece.portal.service.plugin.PluginService;
41  import fr.paris.lutece.portal.service.util.AppException;
42  import fr.paris.lutece.util.sql.DAOUtil;
43  
44  import java.io.Serializable;
45  import java.util.ArrayList;
46  import java.util.List;
47  
48  import org.apache.commons.lang.StringUtils;
49  
50  
51  /**
52   * The Class DilaStyleSheetDAO. Gives access to "DilaStyleSheet" data.
53   */
54  public class DilaStyleSheetDAO implements IDilaStyleSheetDAO, Serializable
55  {
56      /** Serial ID */
57      private static final long serialVersionUID = 7670029586036059570L;
58  
59      /** The Constant SQL_QUERY_NEW_PK. */
60      private static final String SQL_QUERY_NEW_PK = "SELECT max(id_stylesheet) FROM dila_stylesheet";
61      private static final String SQL_QUERY_SELECT_ALL_STYLE_SHEET = " SELECT a.id_stylesheet , a.description , a.file_name , a.content_type_id , b.label, a.source FROM  dila_stylesheet a LEFT OUTER JOIN dila_content_type b ON a.content_type_id = b.id ";
62      private static final String SQL_QUERY_COUNT_STYLESHEET = " SELECT count(*) FROM dila_stylesheet WHERE content_type_id = ? ";
63      private static final String SQL_QUERY_INSERT = " INSERT INTO dila_stylesheet ( id_stylesheet , description , file_name, source, content_type_id ) "
64              + " VALUES ( ?, ? ,?, ?, ? )";
65      private static final String SQL_QUERY_SELECT = " SELECT a.id_stylesheet , a.description , a.file_name , a.content_type_id , b.label FROM  dila_stylesheet a LEFT OUTER JOIN dila_content_type b ON a.content_type_id = b.id WHERE a.id_stylesheet = ? ";
66      private static final String SQL_QUERY_SELECT_SOURCE = " SELECT source FROM dila_stylesheet WHERE id_stylesheet = ? ";
67      private static final String SQL_QUERY_UPDATE = " UPDATE dila_stylesheet SET id_stylesheet = ?, description = ?, file_name = ?, source = ?, content_type_id = ? WHERE id_stylesheet = ?  ";
68      private static final String SQL_QUERY_UPDATE_WITHOUT_FILE = " UPDATE dila_stylesheet SET id_stylesheet = ?, description = ?, content_type_id = ? WHERE id_stylesheet = ?  ";
69      private static final String SQL_QUERY_DELETE = " DELETE FROM dila_stylesheet WHERE id_stylesheet = ? ";
70      private static final String SQL_AND = " AND ";
71      private static final String SQL_WHERE = " WHERE ";
72  
73      ///////////////////////////////////////////////////////////////////////////////////////
74      //Access methods to data
75  
76      /**
77       * Generates a new primary key
78       * @return The new primary key
79       */
80      int newPrimaryKey( )
81      {
82          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
83          daoUtil.executeQuery( );
84  
85          int nKey;
86  
87          if ( !daoUtil.next( ) )
88          {
89              // if the table is empty
90              nKey = 1;
91          }
92  
93          nKey = daoUtil.getInt( 1 ) + 1;
94  
95          daoUtil.free( );
96  
97          return nKey;
98      }
99  
100     @Override
101     public List<DilaStyleSheet> getDilaStyleSheetList( Integer nContentTypeId, String strStyleSheetName )
102     {
103         List<DilaStyleSheet> dilaStylesheetList = new ArrayList<DilaStyleSheet>( );
104         String strQuery = SQL_QUERY_SELECT_ALL_STYLE_SHEET;
105         boolean allreadyWhere = false;
106 
107         if ( ( nContentTypeId != null ) && ( nContentTypeId != 0 ) )
108         {
109             strQuery += " WHERE a.content_type_id = ? ";
110             allreadyWhere = true;
111         }
112 
113         boolean isNameNotEmpty = StringUtils.isNotEmpty( strStyleSheetName );
114 
115         if ( isNameNotEmpty )
116         {
117             if ( allreadyWhere )
118             {
119                 strQuery += SQL_AND;
120             }
121             else
122             {
123                 strQuery += SQL_WHERE;
124             }
125 
126             strQuery += " a.description LIKE ? ";
127         }
128 
129         String strOrder = " ORDER BY a.description ";
130 
131         String strSQL = strQuery + strOrder;
132 
133         DAOUtil daoUtil = new DAOUtil( strSQL, PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
134 
135         Integer nIndex = 1;
136 
137         if ( ( nContentTypeId != null ) && ( nContentTypeId != 0 ) )
138         {
139             daoUtil.setInt( nIndex, nContentTypeId );
140             nIndex = 2;
141         }
142 
143         if ( isNameNotEmpty )
144         {
145             daoUtil.setString( nIndex, '%' + strStyleSheetName + '%' );
146         }
147 
148         daoUtil.executeQuery( );
149 
150         while ( daoUtil.next( ) )
151         {
152             DilaStyleSheet dilaStylesheet = new DilaStyleSheet( );
153             dilaStylesheet.setId( daoUtil.getInt( 1 ) );
154             dilaStylesheet.setDescription( daoUtil.getString( 2 ) );
155             dilaStylesheet.setFile( daoUtil.getString( 3 ) );
156 
157             ContentType contentType = new ContentType( );
158             contentType.setId( daoUtil.getInt( 4 ) );
159             contentType.setLabel( daoUtil.getString( 5 ) );
160             dilaStylesheet.setContentType( contentType );
161             dilaStylesheetList.add( dilaStylesheet );
162             dilaStylesheet.setSource( daoUtil.getBytes( 6 ) );
163         }
164 
165         daoUtil.free( );
166 
167         return dilaStylesheetList;
168     }
169 
170     @Override
171     public Integer getStyleSheetNbPerContentType( int nContentTypeId )
172     {
173         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_COUNT_STYLESHEET, PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
174 
175         daoUtil.setInt( 1, nContentTypeId );
176 
177         daoUtil.executeQuery( );
178 
179         if ( !daoUtil.next( ) )
180         {
181             daoUtil.free( );
182             throw new AppException( DAOUtil.MSG_EXCEPTION_SELECT_ERROR + nContentTypeId );
183         }
184 
185         int nCount = ( daoUtil.getInt( 1 ) );
186 
187         daoUtil.free( );
188 
189         return nCount;
190     }
191 
192     @Override
193     public void create( DilaStyleSheet stylesheet )
194     {
195         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
196 
197         stylesheet.setId( newPrimaryKey( ) );
198 
199         daoUtil.setInt( 1, stylesheet.getId( ) );
200         daoUtil.setString( 2, stylesheet.getDescription( ) );
201         daoUtil.setString( 3, stylesheet.getFile( ) );
202         daoUtil.setBytes( 4, stylesheet.getSource( ) );
203         daoUtil.setInt( 5, stylesheet.getContentType( ).getId( ) );
204 
205         daoUtil.executeUpdate( );
206         daoUtil.free( );
207     }
208 
209     @Override
210     public DilaStyleSheet findByPrimaryKey( Integer nIdStyleSheet )
211     {
212         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
213         daoUtil.setInt( 1, nIdStyleSheet );
214         daoUtil.executeQuery( );
215 
216         DilaStyleSheet dilaStylesheet = new DilaStyleSheet( );
217 
218         if ( daoUtil.next( ) )
219         {
220             dilaStylesheet.setId( daoUtil.getInt( 1 ) );
221             dilaStylesheet.setDescription( daoUtil.getString( 2 ) );
222             dilaStylesheet.setFile( daoUtil.getString( 3 ) );
223 
224             ContentType typeContenu = new ContentType( );
225             typeContenu.setId( daoUtil.getInt( 4 ) );
226             typeContenu.setLabel( daoUtil.getString( 5 ) );
227             dilaStylesheet.setContentType( typeContenu );
228         }
229 
230         daoUtil.free( );
231 
232         return dilaStylesheet;
233     }
234 
235     @Override
236     public void update( DilaStyleSheet stylesheet )
237     {
238         DAOUtil daoUtil;
239 
240         if ( stylesheet.getSource( ) != null )
241         {
242             daoUtil = new DAOUtil( SQL_QUERY_UPDATE, PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
243         }
244         else
245         {
246             daoUtil = new DAOUtil( SQL_QUERY_UPDATE_WITHOUT_FILE, PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
247         }
248 
249         Integer nIndex = 1;
250         daoUtil.setInt( nIndex++, stylesheet.getId( ) );
251         daoUtil.setString( nIndex++, stylesheet.getDescription( ) );
252 
253         if ( stylesheet.getSource( ) != null )
254         {
255             daoUtil.setString( nIndex++, stylesheet.getFile( ) );
256             daoUtil.setBytes( nIndex++, stylesheet.getSource( ) );
257         }
258 
259         daoUtil.setInt( nIndex++, stylesheet.getContentType( ).getId( ) );
260         daoUtil.setInt( nIndex++, stylesheet.getId( ) );
261 
262         daoUtil.executeUpdate( );
263         daoUtil.free( );
264     }
265 
266     @Override
267     public void doDeleteStyleSheet( Integer nIdStyleSheet )
268     {
269         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
270         daoUtil.setInt( 1, nIdStyleSheet );
271         daoUtil.executeUpdate( );
272         daoUtil.free( );
273     }
274 
275     @Override
276     public byte[] getSourceByStyleSheetId( Integer nIdStyleSheet )
277     {
278         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_SOURCE, PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
279         daoUtil.setInt( 1, nIdStyleSheet );
280         daoUtil.executeQuery( );
281 
282         byte[] source = null;
283 
284         if ( daoUtil.next( ) )
285         {
286             source = daoUtil.getBytes( 1 );
287         }
288 
289         daoUtil.free( );
290 
291         return source;
292     }
293 }