View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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.plugins.campagnebp.business;
35  
36  import java.util.HashMap;
37  import java.util.List;
38  import java.util.Map;
39  
40  import fr.paris.lutece.portal.business.file.File;
41  import fr.paris.lutece.portal.business.file.FileHome;
42  import fr.paris.lutece.portal.service.plugin.Plugin;
43  import fr.paris.lutece.util.sql.DAOUtil;
44  
45  import java.util.ArrayList;
46  import java.util.Collection;
47  
48  /**
49   * This class provides Data Access methods for CampagneTheme objects
50   */
51  
52  public final class CampagneThemeDAO implements ICampagneThemeDAO
53  {
54      // Constants
55      private static final String SQL_QUERY_NEW_PK = "SELECT max( id_campagne_theme ) FROM ideation_campagnes_themes";
56      private static final String SQL_QUERY_SELECT = "SELECT id_campagne_theme, code_campagne, code_theme, title, description, active, image_file FROM ideation_campagnes_themes WHERE id_campagne_theme = ?";
57      private static final String SQL_QUERY_INSERT = "INSERT INTO ideation_campagnes_themes ( id_campagne_theme, code_campagne, code_theme, title, description, active, image_file ) VALUES ( ?, ?, ?, ?, ?, ?, ? ) ";
58      private static final String SQL_QUERY_DELETE = "DELETE FROM ideation_campagnes_themes WHERE id_campagne_theme = ? ";
59      private static final String SQL_QUERY_UPDATE = "UPDATE ideation_campagnes_themes SET id_campagne_theme = ?, code_campagne = ?, code_theme = ?, title = ?, description = ?, active = ?, image_file = ? WHERE id_campagne_theme = ?";
60      private static final String SQL_QUERY_SELECTALL = "SELECT id_campagne_theme, code_campagne, code_theme, title, description, active, image_file FROM ideation_campagnes_themes";
61      private static final String SQL_QUERY_SELECTALL_BY_CAMPAGNE = SQL_QUERY_SELECTALL + " WHERE code_campagne = ?";
62      private static final String SQL_QUERY_SELECTALL_ID = "SELECT id_campagne_theme FROM ideation_campagnes_themes";
63      private static final String SQL_QUERY_SELECT_BY_TITLETHEME = "SELECT id_campagne_theme, code_campagne, code_theme, title, description, active, image_file FROM ideation_campagnes_themes WHERE code_theme = ?";
64  
65      /**
66       * Generates a new primary key
67       * 
68       * @param plugin
69       *            The Plugin
70       * @return The new primary key
71       */
72      public int newPrimaryKey( Plugin plugin )
73      {
74          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
75          daoUtil.executeQuery( );
76  
77          int nKey = 1;
78  
79          if ( daoUtil.next( ) )
80          {
81              nKey = daoUtil.getInt( 1 ) + 1;
82          }
83  
84          daoUtil.free( );
85  
86          return nKey;
87      }
88  
89      /**
90       * {@inheritDoc }
91       */
92      @Override
93      public void insert( CampagneTheme campagneTheme, Plugin plugin )
94      {
95          int nCpt = 1;
96          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
97  
98          campagneTheme.setId( newPrimaryKey( plugin ) );
99  
100         daoUtil.setInt( nCpt++, campagneTheme.getId( ) );
101         daoUtil.setString( nCpt++, campagneTheme.getCodeCampagne( ) );
102         daoUtil.setString( nCpt++, campagneTheme.getCode( ) );
103         daoUtil.setString( nCpt++, campagneTheme.getTitle( ) );
104         daoUtil.setString( nCpt++, campagneTheme.getDescription( ) );
105         daoUtil.setBoolean( nCpt++, campagneTheme.getActive( ) );
106         File image = campagneTheme.getImage( );
107         if ( image != null )
108         {
109             daoUtil.setInt( nCpt++, image.getIdFile( ) );
110         }
111         else
112         {
113             daoUtil.setIntNull( nCpt++ );
114         }
115 
116         daoUtil.executeUpdate( );
117         daoUtil.free( );
118     }
119 
120     /**
121      * {@inheritDoc }
122      */
123     @Override
124     public CampagneTheme load( int nKey, Plugin plugin )
125     {
126         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
127         daoUtil.setInt( 1, nKey );
128         daoUtil.executeQuery( );
129 
130         CampagneTheme campagneTheme = null;
131 
132         if ( daoUtil.next( ) )
133         {
134             campagneTheme = getRow( daoUtil );
135         }
136 
137         daoUtil.free( );
138         return campagneTheme;
139     }
140 
141     /**
142      * {@inheritDoc }
143      */
144     @Override
145     public CampagneTheme loadByCodeTheme( String codeTheme, Plugin plugin )
146     {
147         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_TITLETHEME, plugin );
148         daoUtil.setString( 1, codeTheme );
149         daoUtil.executeQuery( );
150 
151         CampagneTheme campagneTheme = null;
152 
153         if ( daoUtil.next( ) )
154         {
155             campagneTheme = getRow( daoUtil );
156         }
157 
158         daoUtil.free( );
159         return campagneTheme;
160     }
161 
162     /**
163      * {@inheritDoc }
164      */
165     @Override
166     public void delete( int nKey, Plugin plugin )
167     {
168         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
169         daoUtil.setInt( 1, nKey );
170         daoUtil.executeUpdate( );
171         daoUtil.free( );
172     }
173 
174     /**
175      * {@inheritDoc }
176      */
177     @Override
178     public void store( CampagneTheme campagneTheme, Plugin plugin )
179     {
180         int nCpt = 1;
181         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
182 
183         daoUtil.setInt( nCpt++, campagneTheme.getId( ) );
184         daoUtil.setString( nCpt++, campagneTheme.getCodeCampagne( ) );
185         daoUtil.setString( nCpt++, campagneTheme.getCode( ) );
186         daoUtil.setString( nCpt++, campagneTheme.getTitle( ) );
187         daoUtil.setString( nCpt++, campagneTheme.getDescription( ) );
188         daoUtil.setBoolean( nCpt++, campagneTheme.getActive( ) );
189         File image = campagneTheme.getImage( );
190         if ( image != null )
191         {
192             daoUtil.setInt( nCpt++, image.getIdFile( ) );
193         }
194         else
195         {
196             daoUtil.setIntNull( nCpt++ );
197         }
198         daoUtil.setInt( nCpt++, campagneTheme.getId( ) );
199 
200         daoUtil.executeUpdate( );
201         daoUtil.free( );
202     }
203 
204     /**
205      * {@inheritDoc }
206      */
207     @Override
208     public Collection<CampagneTheme> selectCampagneThemesList( Plugin plugin )
209     {
210         Collection<CampagneTheme> campagneThemeList = new ArrayList<CampagneTheme>( );
211         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
212         daoUtil.executeQuery( );
213 
214         while ( daoUtil.next( ) )
215         {
216             CampagneTheme campagneTheme = getRow( daoUtil );
217 
218             campagneThemeList.add( campagneTheme );
219         }
220 
221         daoUtil.free( );
222         return campagneThemeList;
223     }
224 
225     /**
226      * {@inheritDoc }
227      */
228     @Override
229     public Collection<Integer> selectIdCampagneThemesList( Plugin plugin )
230     {
231         Collection<Integer> campagneThemeList = new ArrayList<Integer>( );
232         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_ID, plugin );
233         daoUtil.executeQuery( );
234 
235         while ( daoUtil.next( ) )
236         {
237             campagneThemeList.add( daoUtil.getInt( 1 ) );
238         }
239 
240         daoUtil.free( );
241         return campagneThemeList;
242     }
243 
244     private CampagneTheme getRow( DAOUtil daoUtil )
245     {
246         int nCpt = 1;
247         CampagneThemeusiness/CampagneTheme.html#CampagneTheme">CampagneTheme campagneTheme = new CampagneTheme( );
248 
249         campagneTheme.setId( daoUtil.getInt( nCpt++ ) );
250         campagneTheme.setCodeCampagne( daoUtil.getString( nCpt++ ) );
251         campagneTheme.setCode( daoUtil.getString( nCpt++ ) );
252         campagneTheme.setTitle( daoUtil.getString( nCpt++ ) );
253         campagneTheme.setDescription( daoUtil.getString( nCpt++ ) );
254         campagneTheme.setActive( daoUtil.getBoolean( nCpt++ ) );
255         campagneTheme.setImage( FileHome.findByPrimaryKey( daoUtil.getInt( nCpt++ ) ) );
256         return campagneTheme;
257     }
258 
259     /**
260      * {@inheritDoc }
261      */
262     @Override
263     public Collection<CampagneTheme> selectCampagneThemesListByCampagne( String campagneCode, Plugin plugin )
264     {
265         Collection<CampagneTheme> campagneThemeList = new ArrayList<CampagneTheme>( );
266         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_BY_CAMPAGNE, plugin );
267         daoUtil.setString( 1, campagneCode );
268         daoUtil.executeQuery( );
269 
270         while ( daoUtil.next( ) )
271         {
272             CampagneTheme campagneTheme = getRow( daoUtil );
273 
274             campagneThemeList.add( campagneTheme );
275         }
276 
277         daoUtil.free( );
278         return campagneThemeList;
279     }
280 
281     /**
282      * {@inheritDoc }
283      */
284     @Override
285     public Map<String, List<CampagneTheme>> selectCampagneThemesMapByCampagne( Plugin plugin )
286     {
287         Map<String, List<CampagneTheme>> campagneThemeMap = new HashMap<String, List<CampagneTheme>>( );
288         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
289         daoUtil.executeQuery( );
290 
291         while ( daoUtil.next( ) )
292         {
293             CampagneTheme campagneTheme = getRow( daoUtil );
294 
295             List<CampagneTheme> campagneThemeList = campagneThemeMap.get( campagneTheme.getCodeCampagne( ) );
296             if ( campagneThemeList == null )
297             {
298                 campagneThemeList = new ArrayList<CampagneTheme>( );
299                 campagneThemeMap.put( campagneTheme.getCodeCampagne( ), campagneThemeList );
300             }
301             campagneThemeList.add( campagneTheme );
302         }
303 
304         daoUtil.free( );
305         return campagneThemeMap;
306     }
307 }