View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.galleryimage.business;
35  
36  import fr.paris.lutece.plugins.galleryimage.service.GalleryImagePlugin;
37  import fr.paris.lutece.util.sql.DAOUtil;
38  
39  import java.util.ArrayList;
40  import java.util.List;
41  import java.sql.Statement;
42  
43  /**
44   * This class provides Data Access methods for GalleryDAO objects
45   */
46  public final class GalleryDAO implements IGalleryDAO
47  {
48      // Constants
49      private static final String SQL_QUERY_SELECT = "SELECT id_gallery, code_gallery, label, gallery_image_type, height_gallery, width_gallery, authenticated_mode FROM galleryimage_gallery WHERE id_gallery = ?";
50      private static final String SQL_QUERY_INSERT = "INSERT INTO galleryimage_gallery ( label, gallery_image_type, height_gallery, width_gallery, authenticated_mode ) VALUES ( ?, ?, ?, ?, ? ) ";
51      private static final String SQL_QUERY_DELETE = "DELETE FROM galleryimage_gallery WHERE id_gallery = ? ";
52      private static final String SQL_QUERY_UPDATE = "UPDATE galleryimage_gallery SET id_gallery = ?, code_gallery = ?, label = ?, gallery_image_type = ?, height_gallery = ?, width_gallery = ?, authenticated_mode = ? WHERE id_gallery = ?";
53      private static final String SQL_QUERY_SELECTALL = "SELECT id_gallery, code_gallery, label, gallery_image_type, height_gallery, width_gallery, authenticated_mode FROM galleryimage_gallery";
54      private static final String SQL_QUERY_SELECT_BY_CODE = "SELECT id_gallery, code_gallery, label, gallery_image_type, height_gallery, width_gallery, authenticated_mode FROM galleryimage_gallery WHERE code_gallery = ?";
55  
56      /**
57       * {@inheritDoc }
58       */
59      @Override
60      public Gallery"../../../../../../fr/paris/lutece/plugins/galleryimage/business/Gallery.html#Gallery">Gallery insert( Gallery gallery )
61      {
62          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS, GalleryImagePlugin.getPlugin( ) ) )
63          {
64              int nIndex = 0;
65              daoUtil.setString( ++nIndex, gallery.getLabel( ) );
66              daoUtil.setString( ++nIndex, gallery.getGalleryImageType( ) );
67              daoUtil.setInt( ++nIndex, gallery.getHeightGallery( ) );
68              daoUtil.setInt( ++nIndex, gallery.getWidthGallery( ) );
69              daoUtil.setBoolean( ++nIndex, gallery.isAuthenticatedMode( ) );
70              
71              daoUtil.executeUpdate( );
72              if ( daoUtil.nextGeneratedKey( ) )
73              {
74                  gallery.setIdGallery( daoUtil.getGeneratedKeyInt( 1 ) );
75              }
76              return gallery;
77          }
78      }
79  
80      /**
81       * {@inheritDoc }
82       */
83      @Override
84      public Gallery load( int nId )
85      {
86          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, GalleryImagePlugin.getPlugin( ) ) )
87          {
88              daoUtil.setInt( 1, nId );
89              daoUtil.executeQuery( );
90  
91              Gallery gallery = null;
92  
93              if ( daoUtil.next( ) )
94              {
95                  gallery = new Gallery( );
96                  int nIndex = 0;
97  
98                  gallery.setIdGallery( daoUtil.getInt( ++nIndex ) );
99                  gallery.setCodeGallery( daoUtil.getString( ++nIndex ) );
100                 gallery.setLabel( daoUtil.getString( ++nIndex ) );
101                 gallery.setGalleryImageType( daoUtil.getString( ++nIndex ) );
102                 gallery.setHeightGallery( daoUtil.getInt( ++nIndex ) );
103                 gallery.setWidthGallery( daoUtil.getInt( ++nIndex ) );
104                 gallery.setAuthenticatedMode( daoUtil.getBoolean( ++nIndex ) );
105             }
106 
107             return gallery;
108         }
109     }
110 
111     /**
112      * {@inheritDoc }
113      */
114     @Override
115     public void delete( int nGalleryId )
116     {
117         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, GalleryImagePlugin.getPlugin( ) ) )
118         {
119             daoUtil.setInt( 1, nGalleryId );
120             daoUtil.executeUpdate( );
121         }
122     }
123 
124     /**
125      * {@inheritDoc }
126      */
127     @Override
128     public void store( Gallery gallery )
129     {
130         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, GalleryImagePlugin.getPlugin( ) ) )
131         {
132             int nIndex = 0;
133             daoUtil.setInt( ++nIndex, gallery.getIdGallery( ) );
134             daoUtil.setString( ++nIndex, gallery.getCodeGallery( ) );
135             daoUtil.setString( ++nIndex, gallery.getLabel( ) );
136             daoUtil.setString( ++nIndex, gallery.getGalleryImageType( ) );
137             daoUtil.setInt( ++nIndex, gallery.getHeightGallery( ) );
138             daoUtil.setInt( ++nIndex, gallery.getWidthGallery( ) );
139             daoUtil.setBoolean( ++nIndex, gallery.isAuthenticatedMode( ) );
140             
141             daoUtil.setInt( ++nIndex, gallery.getIdGallery( ) );
142 
143             daoUtil.executeUpdate( );
144         }
145     }
146 
147     /**
148      * {@inheritDoc }
149      */
150     @Override
151     public List<Gallery> selectGalleryList( )
152     {
153         List<Gallery> listGallery = new ArrayList<>( );
154         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, GalleryImagePlugin.getPlugin( ) ) )
155         {
156             daoUtil.executeQuery( );
157 
158             while ( daoUtil.next( ) )
159             {
160                 int nIndex = 0;
161                 Gallerygalleryimage/business/Gallery.html#Gallery">Gallery gallery = new Gallery( );
162                 gallery.setIdGallery( daoUtil.getInt( ++nIndex ) );
163                 gallery.setCodeGallery( daoUtil.getString( ++nIndex ) );
164                 gallery.setLabel( daoUtil.getString( ++nIndex ) );
165                 gallery.setGalleryImageType( daoUtil.getString( ++nIndex ) );
166                 gallery.setHeightGallery( daoUtil.getInt( ++nIndex ) );
167                 gallery.setWidthGallery( daoUtil.getInt( ++nIndex ) );
168                 gallery.setAuthenticatedMode( daoUtil.getBoolean( ++nIndex ) );
169                 
170                 listGallery.add( gallery );
171             }
172 
173             return listGallery;
174         }
175     }
176     
177     /**
178      * {@inheritDoc }
179      */
180     @Override
181     public Gallery loadByCodeGallery( String strCodeGallery )
182     {
183         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_CODE, GalleryImagePlugin.getPlugin( ) ) )
184         {
185             daoUtil.setString( 1, strCodeGallery );
186             daoUtil.executeQuery( );
187 
188             Gallery gallery = null;
189 
190             if ( daoUtil.next( ) )
191             {
192                 gallery = new Gallery( );
193                 int nIndex = 0;
194 
195                 gallery.setIdGallery( daoUtil.getInt( ++nIndex ) );
196                 gallery.setCodeGallery( daoUtil.getString( ++nIndex ) );
197                 gallery.setLabel( daoUtil.getString( ++nIndex ) );
198                 gallery.setGalleryImageType( daoUtil.getString( ++nIndex ) );
199                 gallery.setHeightGallery( daoUtil.getInt( ++nIndex ) );
200                 gallery.setWidthGallery( daoUtil.getInt( ++nIndex ) );
201                 gallery.setAuthenticatedMode( daoUtil.getBoolean( ++nIndex ) );
202             }
203 
204             return gallery;
205         }
206     }
207 
208 }