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