View Javadoc
1   /*
2    * Copyright (c) 2002-2018, 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.appointment.business.localization;
35  
36  import fr.paris.lutece.plugins.appointment.business.UtilDAO;
37  import fr.paris.lutece.portal.service.plugin.Plugin;
38  import fr.paris.lutece.util.sql.DAOUtil;
39  
40  public final class LocalizationDAO extends UtilDAO implements ILocalizationDAO
41  {
42  
43      private static final String SQL_QUERY_NEW_PK = "SELECT max(id_localization) FROM appointment_localization";
44      private static final String SQL_QUERY_INSERT = "INSERT INTO appointment_localization (id_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_SELECT_COLUMNS = "SELECT id_localization, longitude, latitude, address, id_form FROM appointment_localization";
48      private static final String SQL_QUERY_SELECT = SQL_QUERY_SELECT_COLUMNS + " WHERE id_localization = ?";
49      private static final String SQL_QUERY_SELECT_BY_ID_FORM = SQL_QUERY_SELECT_COLUMNS + " WHERE id_form = ?";
50  
51      @Override
52      public synchronized void insert( Localization localization, Plugin plugin )
53      {
54          localization.setIdLocalization( getNewPrimaryKey( SQL_QUERY_NEW_PK, plugin ) );
55          DAOUtil daoUtil = buildDaoUtil( SQL_QUERY_INSERT, localization, plugin, true );
56          executeUpdate( daoUtil );
57      }
58  
59      @Override
60      public void update( Localization localization, Plugin plugin )
61      {
62          DAOUtil daoUtil = buildDaoUtil( SQL_QUERY_UPDATE, localization, plugin, false );
63          executeUpdate( daoUtil );
64      }
65  
66      @Override
67      public void delete( int nIdLocalization, Plugin plugin )
68      {
69          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
70          daoUtil.setInt( 1, nIdLocalization );
71          executeUpdate( daoUtil );
72      }
73  
74      @Override
75      public Localization select( int nIdLocalization, Plugin plugin )
76      {
77          DAOUtil daoUtil = null;
78          Localization localization = null;
79          try
80          {
81              daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
82              daoUtil.setInt( 1, nIdLocalization );
83              daoUtil.executeQuery( );
84              if ( daoUtil.next( ) )
85              {
86                  localization = buildLocalization( daoUtil );
87              }
88          }
89          finally
90          {
91              if ( daoUtil != null )
92              {
93                  daoUtil.free( );
94              }
95          }
96          return localization;
97      }
98  
99      @Override
100     public Localization findByIdForm( int nIdForm, Plugin plugin )
101     {
102         DAOUtil daoUtil = null;
103         Localization localization = null;
104         try
105         {
106             daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_ID_FORM, plugin );
107             daoUtil.setInt( 1, nIdForm );
108             daoUtil.executeQuery( );
109             if ( daoUtil.next( ) )
110             {
111                 localization = buildLocalization( daoUtil );
112             }
113         }
114         finally
115         {
116             if ( daoUtil != null )
117             {
118                 daoUtil.free( );
119             }
120         }
121         return localization;
122     }
123 
124     /**
125      * Build a Localization business object from the resultset
126      * 
127      * @param daoUtil
128      *            the prepare statement util object
129      * @return a new Localization business object with all its attributes assigned
130      */
131     private Localization buildLocalization( DAOUtil daoUtil )
132     {
133         int nIndex = 1;
134         Localization localization = new Localization( );
135         localization.setIdLocalization( daoUtil.getInt( nIndex++ ) );
136         Float fLongitude = ( (Float) daoUtil.getObject( nIndex++ ) );
137         if ( fLongitude != null )
138         {
139             localization.setLongitude( fLongitude.doubleValue( ) );
140         }
141         Float fLatitude = ( (Float) daoUtil.getObject( nIndex++ ) );
142         if ( fLatitude != null )
143         {
144             localization.setLatitude( fLatitude.doubleValue( ) );
145         }
146         localization.setAddress( daoUtil.getString( nIndex++ ) );
147         localization.setIdForm( daoUtil.getInt( nIndex ) );
148         return localization;
149     }
150 
151     /**
152      * Build a daoUtil object with the localization business object for insert query
153      * 
154      * @param query
155      *            the query
156      * @param localization
157      *            the localization
158      * @param plugin
159      *            the plugin
160      * @param isInsert
161      *            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
162      *            set
163      * @return a new daoUtil with all its values assigned
164      */
165     private DAOUtil buildDaoUtil( String query, Localization localization, Plugin plugin, boolean isInsert )
166     {
167         int nIndex = 1;
168         DAOUtil daoUtil = new DAOUtil( query, plugin );
169         if ( isInsert )
170         {
171             daoUtil.setInt( nIndex++, localization.getIdLocalization( ) );
172         }
173         if ( localization.getLongitude( ) != null )
174         {
175             daoUtil.setDouble( nIndex++, localization.getLongitude( ) );
176         }
177         else
178         {
179             daoUtil.setDoubleNull( nIndex++ );
180         }
181         if ( localization.getLatitude( ) != null )
182         {
183             daoUtil.setDouble( nIndex++, localization.getLatitude( ) );
184         }
185         else
186         {
187             daoUtil.setDoubleNull( nIndex++ );
188         }
189         daoUtil.setString( nIndex++, localization.getAddress( ) );
190         daoUtil.setInt( nIndex++, localization.getIdForm( ) );
191         if ( !isInsert )
192         {
193             daoUtil.setInt( nIndex, localization.getIdLocalization( ) );
194         }
195         return daoUtil;
196     }
197 
198     /**
199      * Execute a safe update (Free the connection in case of error when execute the query)
200      * 
201      * @param daoUtil
202      *            the daoUtil
203      */
204     private void executeUpdate( DAOUtil daoUtil )
205     {
206         try
207         {
208             daoUtil.executeUpdate( );
209         }
210         finally
211         {
212             if ( daoUtil != null )
213             {
214                 daoUtil.free( );
215             }
216         }
217     }
218 
219 }