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.blobstore.business.database;
35
36 import fr.paris.lutece.plugins.blobstore.business.BytesBlobStore;
37 import fr.paris.lutece.plugins.blobstore.business.InputStreamBlobStore;
38 import fr.paris.lutece.plugins.blobstore.util.BlobStoreUtils;
39 import fr.paris.lutece.portal.service.util.AppException;
40 import fr.paris.lutece.portal.service.util.AppLogService;
41 import fr.paris.lutece.util.sql.DAOUtil;
42
43 import org.apache.commons.lang3.StringUtils;
44
45 import java.io.InputStream;
46
47
48
49
50
51
52 public final class DatabaseBlobStoreDAO implements IDatabaseBlobStoreDAO
53 {
54
55 private static final String SQL_QUERY_SELECT_LAST_PRIMARY_KEY = " SELECT max( id_blob + 0 ) FROM blobstore_blobstore ";
56 private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = " SELECT id_blob, blob_value FROM blobstore_blobstore WHERE id_blob = ? ";
57 private static final String SQL_QUERY_INSERT = " INSERT INTO blobstore_blobstore( id_blob, blob_value ) VALUES( ?,? ) ";
58 private static final String SQL_QUERY_DELETE = " DELETE FROM blobstore_blobstore WHERE id_blob = ? ";
59 private static final String SQL_QUERY_UPDATE = " UPDATE blobstore_blobstore SET blob_value = ? WHERE id_blob = ? ";
60
61
62
63
64 @Override
65 public String loadLastPrimaryKey( )
66 {
67 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_LAST_PRIMARY_KEY, BlobStoreUtils.getPlugin( ) );
68 daoUtil.executeQuery( );
69
70 String strKey = StringUtils.EMPTY;
71
72 if ( daoUtil.next( ) )
73 {
74 strKey = daoUtil.getString( 1 );
75 }
76
77 daoUtil.free( );
78
79 return strKey;
80 }
81
82
83
84
85 @Override
86 public synchronized void insert( BytesBlobStore blobStore )
87 {
88 int nIndex = 1;
89 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, BlobStoreUtils.getPlugin( ) );
90 daoUtil.setString( nIndex++, blobStore.getId( ) );
91 daoUtil.setBytes( nIndex++, blobStore.getValue( ) );
92 daoUtil.executeUpdate( );
93
94 daoUtil.free( );
95 }
96
97
98
99
100 @Override
101 public BytesBlobStore load( String strId )
102 {
103 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY, BlobStoreUtils.getPlugin( ) );
104 daoUtil.setString( 1, strId );
105 daoUtil.executeQuery( );
106
107 BytesBlobStore blobStore = null;
108
109 if ( daoUtil.next( ) )
110 {
111 int nIndex = 1;
112 blobStore = new BytesBlobStore( );
113 blobStore.setId( daoUtil.getString( nIndex++ ) );
114 blobStore.setValue( daoUtil.getBytes( nIndex++ ) );
115 }
116
117 daoUtil.free( );
118
119 return blobStore;
120 }
121
122
123
124
125 @Override
126 public void delete( String strId )
127 {
128 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, BlobStoreUtils.getPlugin( ) );
129 daoUtil.setString( 1, strId );
130 daoUtil.executeUpdate( );
131 daoUtil.free( );
132 }
133
134
135
136
137 @Override
138 public void store( BytesBlobStore blobStore )
139 {
140 int nIndex = 1;
141 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, BlobStoreUtils.getPlugin( ) );
142 daoUtil.setBytes( nIndex++, blobStore.getValue( ) );
143 daoUtil.setString( nIndex++, blobStore.getId( ) );
144 daoUtil.executeUpdate( );
145 daoUtil.free( );
146 }
147
148
149
150
151 @Override
152 public void insert( InputStreamBlobStore blobStore )
153 {
154 int nIndex = 1;
155 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, BlobStoreUtils.getPlugin( ) );
156
157 try
158 {
159 daoUtil.setString( nIndex++, blobStore.getId( ) );
160 daoUtil.setBinaryStream( nIndex++, blobStore.getInputStream( ), -1 );
161 daoUtil.executeUpdate( );
162 }
163 catch( Exception e )
164 {
165 AppLogService.error( e.getMessage( ), e );
166 throw new AppException( e.getMessage( ), e );
167 }
168 finally
169 {
170 daoUtil.free( );
171 }
172 }
173
174
175
176
177 @Override
178 public void store( InputStreamBlobStore blobStore )
179 {
180 int nIndex = 1;
181 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, BlobStoreUtils.getPlugin( ) );
182 daoUtil.setBinaryStream( nIndex++, blobStore.getInputStream( ), -1 );
183 daoUtil.setString( nIndex++, blobStore.getId( ) );
184 daoUtil.executeUpdate( );
185 daoUtil.free( );
186 }
187
188
189
190
191 @Override
192 public InputStream loadInputStream( String strId )
193 {
194 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY, BlobStoreUtils.getPlugin( ) );
195 daoUtil.setString( 1, strId );
196 daoUtil.executeQuery( );
197
198 InputStream inputStream = null;
199
200 if ( daoUtil.next( ) )
201 {
202 inputStream = daoUtil.getBinaryStream( 2 );
203 }
204
205 daoUtil.free( );
206
207 return inputStream;
208 }
209 }