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 fr.paris.lutece.portal.service.plugin.Plugin;
37  import fr.paris.lutece.util.sql.DAOUtil;
38  
39  import java.util.ArrayList;
40  import java.util.Collection;
41  
42  /**
43   * This class provides Data Access methods for CampagneImage objects
44   */
45  
46  public final class CampagneImageDAO implements ICampagneImageDAO
47  {
48      // Constants
49      private static final String SQL_QUERY_NEW_PK = "SELECT max( id_campagne_image ) FROM ideation_campagnes_images";
50      private static final String SQL_QUERY_SELECT = "SELECT id_campagne_image, code_campagne, id_file FROM ideation_campagnes_images WHERE id_campagne_image = ?";
51      private static final String SQL_QUERY_INSERT = "INSERT INTO ideation_campagnes_images ( id_campagne_image, code_campagne, id_file ) VALUES ( ?, ?, ? ) ";
52      private static final String SQL_QUERY_DELETE = "DELETE FROM ideation_campagnes_images WHERE id_campagne_image = ? ";
53      private static final String SQL_QUERY_UPDATE = "UPDATE ideation_campagnes_images SET id_campagne_image = ?, code_campagne = ?, id_file = ? WHERE id_campagne_image = ?";
54      private static final String SQL_QUERY_SELECTALL = "SELECT id_campagne_image, code_campagne, id_file FROM ideation_campagnes_images";
55      private static final String SQL_QUERY_SELECTALL_ID = "SELECT id_campagne_image FROM ideation_campagnes_images";
56  
57      /**
58       * Generates a new primary key
59       * 
60       * @param plugin
61       *            The Plugin
62       * @return The new primary key
63       */
64      public int newPrimaryKey( Plugin plugin )
65      {
66          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
67          daoUtil.executeQuery( );
68  
69          int nKey = 1;
70  
71          if ( daoUtil.next( ) )
72          {
73              nKey = daoUtil.getInt( 1 ) + 1;
74          }
75  
76          daoUtil.free( );
77  
78          return nKey;
79      }
80  
81      /**
82       * {@inheritDoc }
83       */
84      @Override
85      public void insert( CampagneImage campagneImage, Plugin plugin )
86      {
87          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
88  
89          campagneImage.setId( newPrimaryKey( plugin ) );
90  
91          daoUtil.setInt( 1, campagneImage.getId( ) );
92          daoUtil.setString( 2, campagneImage.getCodeCampagne( ) );
93          daoUtil.setInt( 3, campagneImage.getFile( ) );
94  
95          daoUtil.executeUpdate( );
96          daoUtil.free( );
97      }
98  
99      /**
100      * {@inheritDoc }
101      */
102     @Override
103     public CampagneImage load( int nKey, Plugin plugin )
104     {
105         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
106         daoUtil.setInt( 1, nKey );
107         daoUtil.executeQuery( );
108 
109         CampagneImage campagneImage = null;
110 
111         if ( daoUtil.next( ) )
112         {
113             campagneImage = new CampagneImage( );
114             campagneImage.setId( daoUtil.getInt( 1 ) );
115             campagneImage.setCodeCampagne( daoUtil.getString( 2 ) );
116             campagneImage.setFile( daoUtil.getInt( 3 ) );
117         }
118 
119         daoUtil.free( );
120         return campagneImage;
121     }
122 
123     /**
124      * {@inheritDoc }
125      */
126     @Override
127     public void delete( int nKey, Plugin plugin )
128     {
129         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
130         daoUtil.setInt( 1, nKey );
131         daoUtil.executeUpdate( );
132         daoUtil.free( );
133     }
134 
135     /**
136      * {@inheritDoc }
137      */
138     @Override
139     public void store( CampagneImage campagneImage, Plugin plugin )
140     {
141         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
142 
143         daoUtil.setInt( 1, campagneImage.getId( ) );
144         daoUtil.setString( 2, campagneImage.getCodeCampagne( ) );
145         daoUtil.setInt( 3, campagneImage.getFile( ) );
146         daoUtil.setInt( 4, campagneImage.getId( ) );
147 
148         daoUtil.executeUpdate( );
149         daoUtil.free( );
150     }
151 
152     /**
153      * {@inheritDoc }
154      */
155     @Override
156     public Collection<CampagneImage> selectCampagneImagesList( Plugin plugin )
157     {
158         Collection<CampagneImage> campagneImageList = new ArrayList<CampagneImage>( );
159         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
160         daoUtil.executeQuery( );
161 
162         while ( daoUtil.next( ) )
163         {
164             CampagneImageusiness/CampagneImage.html#CampagneImage">CampagneImage campagneImage = new CampagneImage( );
165 
166             campagneImage.setId( daoUtil.getInt( 1 ) );
167             campagneImage.setCodeCampagne( daoUtil.getString( 2 ) );
168             campagneImage.setFile( daoUtil.getInt( 3 ) );
169 
170             campagneImageList.add( campagneImage );
171         }
172 
173         daoUtil.free( );
174         return campagneImageList;
175     }
176 
177     /**
178      * {@inheritDoc }
179      */
180     @Override
181     public Collection<Integer> selectIdCampagneImagesList( Plugin plugin )
182     {
183         Collection<Integer> campagneImageList = new ArrayList<Integer>( );
184         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_ID, plugin );
185         daoUtil.executeQuery( );
186 
187         while ( daoUtil.next( ) )
188         {
189             campagneImageList.add( daoUtil.getInt( 1 ) );
190         }
191 
192         daoUtil.free( );
193         return campagneImageList;
194     }
195 }