1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
44
45
46 public final class CampaignImageDAO implements ICampaignImageDAO
47 {
48
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
60
61
62
63
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
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
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
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
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
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
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
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 }