View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.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   * BlobStoreDAO
50   *
51   */
52  public final class DatabaseBlobStoreDAO implements IDatabaseBlobStoreDAO
53  {
54      // SQL QUERIES
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       * {@inheritDoc}
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       * {@inheritDoc}
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       * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
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 }