View Javadoc

1   /*
2    * Copyright (c) 2002-2014, Mairie de 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.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   * This class provides Data Access methods for Field objects
43   */
44  public final class ImageResourceDAO implements IImageResourceDAO
45  {
46      // Constants
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      * generate new primary key
100     * @param plugin plugin
101     * @return
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             // if the table is empty
113             nKey = 1;
114         }
115 
116         nKey = daoUtil.getInt( 1 ) + 1;
117         daoUtil.free(  );
118 
119         return nKey;
120     }
121 }