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.appointment.business.localization;
35  
36  import java.sql.Statement;
37  
38  import fr.paris.lutece.portal.service.plugin.Plugin;
39  import fr.paris.lutece.util.sql.DAOUtil;
40  
41  public final class LocalizationDAO implements ILocalizationDAO
42  {
43  
44      private static final String SQL_QUERY_INSERT = "INSERT INTO appointment_localization (longitude, latitude, address, id_form) VALUES ( ?, ?, ?, ?)";
45      private static final String SQL_QUERY_UPDATE = "UPDATE appointment_localization SET longitude = ?, latitude = ?, address = ?, id_form = ? WHERE id_localization = ?";
46      private static final String SQL_QUERY_DELETE = "DELETE FROM appointment_localization WHERE id_localization = ?";
47      private static final String SQL_QUERY_DELETE_BY_ID_FORM = "DELETE FROM appointment_localization WHERE id_form = ?";
48      private static final String SQL_QUERY_SELECT_COLUMNS = "SELECT id_localization, longitude, latitude, address, id_form FROM appointment_localization";
49      private static final String SQL_QUERY_SELECT = SQL_QUERY_SELECT_COLUMNS + " WHERE id_localization = ?";
50      private static final String SQL_QUERY_SELECT_BY_ID_FORM = SQL_QUERY_SELECT_COLUMNS + " WHERE id_form = ?";
51  
52      @Override
53      public void insert( Localization localization, Plugin plugin )
54      {
55          try ( DAOUtil daoUtil = buildDaoUtil( SQL_QUERY_INSERT, localization, plugin, true ) )
56          {
57              daoUtil.executeUpdate( );
58              if ( daoUtil.nextGeneratedKey( ) )
59              {
60                  localization.setIdLocalization( daoUtil.getGeneratedKeyInt( 1 ) );
61              }
62          }
63      }
64  
65      @Override
66      public void update( Localization localization, Plugin plugin )
67      {
68          try ( DAOUtil daoUtil = buildDaoUtil( SQL_QUERY_UPDATE, localization, plugin, false ) )
69          {
70              daoUtil.executeUpdate( );
71          }
72      }
73  
74      @Override
75      public void delete( int nIdLocalization, Plugin plugin )
76      {
77          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) )
78          {
79              daoUtil.setInt( 1, nIdLocalization );
80              daoUtil.executeUpdate( );
81          }
82      }
83  
84      @Override
85      public void deleteByIdForm( int nIdForm, Plugin plugin )
86      {
87          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_BY_ID_FORM, plugin ) )
88          {
89              daoUtil.setInt( 1, nIdForm );
90              daoUtil.executeUpdate( );
91          }
92      }
93  
94      @Override
95      public Localization select( int nIdLocalization, Plugin plugin )
96      {
97          Localization localization = null;
98          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin ) )
99          {
100             daoUtil.setInt( 1, nIdLocalization );
101             daoUtil.executeQuery( );
102             if ( daoUtil.next( ) )
103             {
104                 localization = buildLocalization( daoUtil );
105             }
106         }
107         return localization;
108     }
109 
110     @Override
111     public Localization findByIdForm( int nIdForm, Plugin plugin )
112     {
113         Localization localization = null;
114         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_ID_FORM, plugin ) )
115         {
116             daoUtil.setInt( 1, nIdForm );
117             daoUtil.executeQuery( );
118             if ( daoUtil.next( ) )
119             {
120                 localization = buildLocalization( daoUtil );
121             }
122         }
123         return localization;
124     }
125 
126     /**
127      * Build a Localization business object from the resultset
128      * 
129      * @param daoUtil
130      *            the prepare statement util object
131      * @return a new Localization business object with all its attributes assigned
132      */
133     private Localization buildLocalization( DAOUtil daoUtil )
134     {
135         int nIndex = 1;
136         Localizationment/business/localization/Localization.html#Localization">Localization localization = new Localization( );
137         localization.setIdLocalization( daoUtil.getInt( nIndex++ ) );
138         localization.setLongitude( daoUtil.getDouble( nIndex++ ) );
139         localization.setLatitude( daoUtil.getDouble( nIndex++ ) );
140         localization.setAddress( daoUtil.getString( nIndex++ ) );
141         localization.setIdForm( daoUtil.getInt( nIndex ) );
142         return localization;
143     }
144 
145     /**
146      * Build a daoUtil object with the localization business object for insert query
147      * 
148      * @param query
149      *            the query
150      * @param localization
151      *            the localization
152      * @param plugin
153      *            the plugin
154      * @param isInsert
155      *            true if it is an insert query (in this case, need to set the id). If false, it is an update, in this case, there is a where parameter id to
156      *            set
157      * @return a new daoUtil with all its values assigned
158      */
159     private DAOUtil buildDaoUtil( String query, Localization localization, Plugin plugin, boolean isInsert )
160     {
161         int nIndex = 1;
162         DAOUtil daoUtil = null;
163         if ( isInsert )
164         {
165             daoUtil = new DAOUtil( query, Statement.RETURN_GENERATED_KEYS, plugin );
166         }
167         else
168         {
169             daoUtil = new DAOUtil( query, plugin );
170         }
171         if ( localization.getLongitude( ) != null )
172         {
173             daoUtil.setDouble( nIndex++, localization.getLongitude( ) );
174         }
175         else
176         {
177             daoUtil.setDoubleNull( nIndex++ );
178         }
179         if ( localization.getLatitude( ) != null )
180         {
181             daoUtil.setDouble( nIndex++, localization.getLatitude( ) );
182         }
183         else
184         {
185             daoUtil.setDoubleNull( nIndex++ );
186         }
187         daoUtil.setString( nIndex++, localization.getAddress( ) );
188         daoUtil.setInt( nIndex++, localization.getIdForm( ) );
189         if ( !isInsert )
190         {
191             daoUtil.setInt( nIndex, localization.getIdLocalization( ) );
192         }
193         return daoUtil;
194     }
195 }