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.digglike.business;
35
36 import fr.paris.lutece.portal.service.image.ImageResource;
37 import fr.paris.lutece.portal.service.plugin.Plugin;
38 import fr.paris.lutece.util.sql.DAOUtil;
39
40
41
42
43
44 public final class ImageResourceDAO implements IImageResourceDAO
45 {
46
47 private static final String SQL_QUERY_NEW_PK = "SELECT max( id_resource_image ) FROM digglike_image";
48 private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = "SELECT image_content, image_mime_type" +
49 " FROM digglike_image WHERE id_resource_image = ?";
50 private static final String SQL_QUERY_INSERT = "INSERT INTO digglike_image (id_resource_image, image_content, image_mime_type) VALUES (?,?,?)";
51 private static final String SQL_QUERY_DELETE = "DELETE FROM digglike_image WHERE id_resource_image = ? ";
52
53 @Override
54 public synchronized int insert( ImageResource imageResource, Plugin plugin )
55 {
56 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
57 daoUtil.setBytes( 2, imageResource.getImage( ) );
58 daoUtil.setString( 3, imageResource.getMimeType( ) );
59
60 int nId = newPrimaryKey( plugin );
61 daoUtil.setInt( 1, nId );
62 daoUtil.executeUpdate( );
63 daoUtil.free( );
64
65 return nId;
66 }
67
68 @Override
69 public ImageResource load( int nId, Plugin plugin )
70 {
71 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY, plugin );
72 daoUtil.setInt( 1, nId );
73 daoUtil.executeQuery( );
74
75 ImageResource imageResource = null;
76
77 if ( daoUtil.next( ) )
78 {
79 imageResource = new ImageResource( );
80 imageResource.setImage( daoUtil.getBytes( 1 ) );
81 imageResource.setMimeType( daoUtil.getString( 2 ) );
82 }
83
84 daoUtil.free( );
85
86 return imageResource;
87 }
88
89 @Override
90 public void delete( int nId, Plugin plugin )
91 {
92 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
93 daoUtil.setInt( 1, nId );
94 daoUtil.executeUpdate( );
95 daoUtil.free( );
96 }
97
98
99
100
101
102
103 private int newPrimaryKey( Plugin plugin )
104 {
105 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
106 daoUtil.executeQuery( );
107
108 int nKey;
109
110 if ( !daoUtil.next( ) )
111 {
112
113 nKey = 1;
114 }
115
116 nKey = daoUtil.getInt( 1 ) + 1;
117 daoUtil.free( );
118
119 return nKey;
120 }
121 }