Skip to content

Commit f1c8755

Browse files
committed
LUT-25028 : Fix Sonar issues - Resources should be closed
1 parent 052ac38 commit f1c8755

File tree

3 files changed

+339
-330
lines changed

3 files changed

+339
-330
lines changed

src/java/fr/paris/lutece/plugins/wiki/business/ImageDAO.java

Lines changed: 112 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,15 @@ public class ImageDAO implements IImageDAO
6868
*/
6969
private int newPrimaryKey( Plugin plugin )
7070
{
71-
DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
72-
daoUtil.executeQuery( );
73-
7471
int nKey;
7572

76-
daoUtil.next( );
77-
nKey = daoUtil.getInt( 1 ) + 1;
78-
daoUtil.free( );
73+
try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin ) )
74+
{
75+
daoUtil.executeQuery( );
76+
77+
daoUtil.next( );
78+
nKey = daoUtil.getInt( 1 ) + 1;
79+
}
7980

8081
return nKey;
8182
}
@@ -86,20 +87,20 @@ private int newPrimaryKey( Plugin plugin )
8687
@Override
8788
public synchronized void insert( Image image, Plugin plugin )
8889
{
89-
DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
90-
91-
int nPos = 0;
92-
image.setId( newPrimaryKey( plugin ) );
93-
94-
daoUtil.setInt( ++nPos, image.getId( ) );
95-
daoUtil.setString( ++nPos, image.getName( ) );
96-
daoUtil.setString( ++nPos, image.getMimeType( ) );
97-
daoUtil.setBytes( ++nPos, image.getValue( ) );
98-
daoUtil.setInt( ++nPos, image.getTopicId( ) );
99-
daoUtil.setInt( ++nPos, image.getWidth( ) );
100-
daoUtil.setInt( ++nPos, image.getHeight( ) );
101-
daoUtil.executeUpdate( );
102-
daoUtil.free( );
90+
try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin ) )
91+
{
92+
int nPos = 0;
93+
image.setId( newPrimaryKey( plugin ) );
94+
95+
daoUtil.setInt( ++nPos, image.getId( ) );
96+
daoUtil.setString( ++nPos, image.getName( ) );
97+
daoUtil.setString( ++nPos, image.getMimeType( ) );
98+
daoUtil.setBytes( ++nPos, image.getValue( ) );
99+
daoUtil.setInt( ++nPos, image.getTopicId( ) );
100+
daoUtil.setInt( ++nPos, image.getWidth( ) );
101+
daoUtil.setInt( ++nPos, image.getHeight( ) );
102+
daoUtil.executeUpdate( );
103+
}
103104
}
104105

105106
/**
@@ -108,21 +109,21 @@ public synchronized void insert( Image image, Plugin plugin )
108109
@Override
109110
public void store( Image image, Plugin plugin )
110111
{
111-
DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
112-
113-
int nPos = 0;
114-
115-
daoUtil.setInt( ++nPos, image.getId( ) );
116-
daoUtil.setString( ++nPos, image.getName( ) );
117-
daoUtil.setString( ++nPos, image.getMimeType( ) );
118-
daoUtil.setBytes( ++nPos, image.getValue( ) );
119-
daoUtil.setInt( ++nPos, image.getTopicId( ) );
120-
daoUtil.setInt( ++nPos, image.getWidth( ) );
121-
daoUtil.setInt( ++nPos, image.getHeight( ) );
122-
123-
daoUtil.setInt( ++nPos, image.getId( ) );
124-
daoUtil.executeUpdate( );
125-
daoUtil.free( );
112+
try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ) )
113+
{
114+
int nPos = 0;
115+
116+
daoUtil.setInt( ++nPos, image.getId( ) );
117+
daoUtil.setString( ++nPos, image.getName( ) );
118+
daoUtil.setString( ++nPos, image.getMimeType( ) );
119+
daoUtil.setBytes( ++nPos, image.getValue( ) );
120+
daoUtil.setInt( ++nPos, image.getTopicId( ) );
121+
daoUtil.setInt( ++nPos, image.getWidth( ) );
122+
daoUtil.setInt( ++nPos, image.getHeight( ) );
123+
124+
daoUtil.setInt( ++nPos, image.getId( ) );
125+
daoUtil.executeUpdate( );
126+
}
126127
}
127128

128129
/**
@@ -131,18 +132,18 @@ public void store( Image image, Plugin plugin )
131132
@Override
132133
public void storeMetadata( Image image, Plugin plugin )
133134
{
134-
DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_METADATA, plugin );
135-
136-
int nPos = 0;
137-
138-
daoUtil.setInt( ++nPos, image.getId( ) );
139-
daoUtil.setString( ++nPos, image.getName( ) );
140-
daoUtil.setInt( ++nPos, image.getTopicId( ) );
141-
daoUtil.setInt( ++nPos, image.getWidth( ) );
142-
daoUtil.setInt( ++nPos, image.getHeight( ) );
143-
daoUtil.setInt( ++nPos, image.getId( ) );
144-
daoUtil.executeUpdate( );
145-
daoUtil.free( );
135+
try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_METADATA, plugin ) )
136+
{
137+
int nPos = 0;
138+
139+
daoUtil.setInt( ++nPos, image.getId( ) );
140+
daoUtil.setString( ++nPos, image.getName( ) );
141+
daoUtil.setInt( ++nPos, image.getTopicId( ) );
142+
daoUtil.setInt( ++nPos, image.getWidth( ) );
143+
daoUtil.setInt( ++nPos, image.getHeight( ) );
144+
daoUtil.setInt( ++nPos, image.getId( ) );
145+
daoUtil.executeUpdate( );
146+
}
146147
}
147148

148149
/**
@@ -152,28 +153,26 @@ public void storeMetadata( Image image, Plugin plugin )
152153
public Image load( int nIdImage, Plugin plugin )
153154
{
154155
Image image = null;
155-
DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY, plugin );
156-
157-
daoUtil.setInt( 1, nIdImage );
158-
159-
daoUtil.executeQuery( );
160-
161-
int nPos = 0;
162-
163-
if ( daoUtil.next( ) )
156+
try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY, plugin ) )
164157
{
165-
image = new Image( );
166-
image.setId( daoUtil.getInt( ++nPos ) );
167-
image.setName( daoUtil.getString( ++nPos ) );
168-
image.setMimeType( daoUtil.getString( ++nPos ) );
169-
image.setValue( daoUtil.getBytes( ++nPos ) );
170-
image.setTopicId( daoUtil.getInt( ++nPos ) );
171-
image.setWidth( daoUtil.getInt( ++nPos ) );
172-
image.setHeight( daoUtil.getInt( ++nPos ) );
158+
daoUtil.setInt( 1, nIdImage );
159+
daoUtil.executeQuery( );
160+
161+
int nPos = 0;
162+
163+
if ( daoUtil.next( ) )
164+
{
165+
image = new Image( );
166+
image.setId( daoUtil.getInt( ++nPos ) );
167+
image.setName( daoUtil.getString( ++nPos ) );
168+
image.setMimeType( daoUtil.getString( ++nPos ) );
169+
image.setValue( daoUtil.getBytes( ++nPos ) );
170+
image.setTopicId( daoUtil.getInt( ++nPos ) );
171+
image.setWidth( daoUtil.getInt( ++nPos ) );
172+
image.setHeight( daoUtil.getInt( ++nPos ) );
173+
}
173174
}
174175

175-
daoUtil.free( );
176-
177176
return image;
178177
}
179178

@@ -183,11 +182,11 @@ public Image load( int nIdImage, Plugin plugin )
183182
@Override
184183
public void delete( int nIdImage, Plugin plugin )
185184
{
186-
DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
187-
188-
daoUtil.setInt( 1, nIdImage );
189-
daoUtil.executeUpdate( );
190-
daoUtil.free( );
185+
try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) )
186+
{
187+
daoUtil.setInt( 1, nIdImage );
188+
daoUtil.executeUpdate( );
189+
}
191190
}
192191

193192
/**
@@ -196,11 +195,11 @@ public void delete( int nIdImage, Plugin plugin )
196195
@Override
197196
public void deleteByTopic( int nTopicId, Plugin plugin )
198197
{
199-
DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_BY_TOPIC, plugin );
200-
201-
daoUtil.setInt( 1, nTopicId );
202-
daoUtil.executeUpdate( );
203-
daoUtil.free( );
198+
try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_BY_TOPIC, plugin ) )
199+
{
200+
daoUtil.setInt( 1, nTopicId );
201+
daoUtil.executeUpdate( );
202+
}
204203
}
205204

206205
/**
@@ -212,27 +211,27 @@ public List<Image> selectAll( Plugin plugin )
212211
Image image = null;
213212
List<Image> listImage = new ArrayList<>( );
214213

215-
DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ICON, plugin );
216-
daoUtil.executeQuery( );
217-
218-
int nPos;
219-
220-
while ( daoUtil.next( ) )
214+
try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ICON, plugin ) )
221215
{
222-
nPos = 0;
223-
image = new Image( );
224-
image.setId( daoUtil.getInt( ++nPos ) );
225-
image.setName( daoUtil.getString( ++nPos ) );
226-
image.setMimeType( daoUtil.getString( ++nPos ) );
227-
image.setTopicId( daoUtil.getInt( ++nPos ) );
228-
image.setWidth( daoUtil.getInt( ++nPos ) );
229-
image.setHeight( daoUtil.getInt( ++nPos ) );
230-
231-
listImage.add( image );
216+
daoUtil.executeQuery( );
217+
218+
int nPos;
219+
220+
while ( daoUtil.next( ) )
221+
{
222+
nPos = 0;
223+
image = new Image( );
224+
image.setId( daoUtil.getInt( ++nPos ) );
225+
image.setName( daoUtil.getString( ++nPos ) );
226+
image.setMimeType( daoUtil.getString( ++nPos ) );
227+
image.setTopicId( daoUtil.getInt( ++nPos ) );
228+
image.setWidth( daoUtil.getInt( ++nPos ) );
229+
image.setHeight( daoUtil.getInt( ++nPos ) );
230+
231+
listImage.add( image );
232+
}
232233
}
233234

234-
daoUtil.free( );
235-
236235
return listImage;
237236
}
238237

@@ -245,28 +244,28 @@ public List<Image> selectByTopicId( int nTopicId, Plugin plugin )
245244
Image image = null;
246245
List<Image> listImage = new ArrayList<>( );
247246

248-
DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_TOPIC, plugin );
249-
daoUtil.setInt( 1, nTopicId );
250-
daoUtil.executeQuery( );
251-
252-
int nPos;
253-
254-
while ( daoUtil.next( ) )
247+
try( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_TOPIC, plugin ) )
255248
{
256-
nPos = 0;
257-
image = new Image( );
258-
image.setId( daoUtil.getInt( ++nPos ) );
259-
image.setName( daoUtil.getString( ++nPos ) );
260-
image.setMimeType( daoUtil.getString( ++nPos ) );
261-
image.setTopicId( daoUtil.getInt( ++nPos ) );
262-
image.setWidth( daoUtil.getInt( ++nPos ) );
263-
image.setHeight( daoUtil.getInt( ++nPos ) );
264-
265-
listImage.add( image );
249+
daoUtil.setInt( 1, nTopicId );
250+
daoUtil.executeQuery( );
251+
252+
int nPos;
253+
254+
while ( daoUtil.next( ) )
255+
{
256+
nPos = 0;
257+
image = new Image( );
258+
image.setId( daoUtil.getInt( ++nPos ) );
259+
image.setName( daoUtil.getString( ++nPos ) );
260+
image.setMimeType( daoUtil.getString( ++nPos ) );
261+
image.setTopicId( daoUtil.getInt( ++nPos ) );
262+
image.setWidth( daoUtil.getInt( ++nPos ) );
263+
image.setHeight( daoUtil.getInt( ++nPos ) );
264+
265+
listImage.add( image );
266+
}
266267
}
267268

268-
daoUtil.free( );
269-
270269
return listImage;
271270
}
272271
}

0 commit comments

Comments
 (0)