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.calendar;
35  
36  import java.util.ArrayList;
37  import java.util.List;
38  
39  import fr.paris.lutece.plugins.appointment.business.UtilDAO;
40  import fr.paris.lutece.portal.service.plugin.Plugin;
41  import fr.paris.lutece.util.sql.DAOUtil;
42  
43  /**
44   * This class provides Data Access methods for Calendar Template objects
45   * 
46   * @author Laurent Payen
47   *
48   */
49  public final class CalendarTemplateDAO extends UtilDAO implements ICalendarTemplateDAO
50  {
51      private static final String SQL_QUERY_NEW_PK = "SELECT max(id_calendar_template) FROM appointment_calendar_template";
52      private static final String SQL_QUERY_INSERT = "INSERT INTO appointment_calendar_template (id_calendar_template, title, description, template_path) VALUES (?,?,?,?)";
53      private static final String SQL_QUERY_UPDATE = "UPDATE appointment_calendar_template SET title = ?, description = ?, template_path = ? WHERE id_calendar_template = ?";
54      private static final String SQL_QUERY_DELETE = "DELETE FROM appointment_calendar_template WHERE id_calendar_template = ?";
55      private static final String SQL_QUERY_SELECT = "SELECT id_calendar_template, title, description, template_path FROM appointment_calendar_template WHERE id_calendar_template = ?";
56      private static final String SQL_QUERY_SELECT_ALL = "SELECT id_calendar_template, title, description, template_path FROM appointment_calendar_template";
57  
58      @Override
59      public synchronized void insert( CalendarTemplate calendarTemplate, Plugin plugin )
60      {
61          calendarTemplate.setIdCalendarTemplate( getNewPrimaryKey( SQL_QUERY_NEW_PK, plugin ) );
62          DAOUtil daoUtil = buildDaoUtil( SQL_QUERY_INSERT, calendarTemplate, plugin, true );
63          executeUpdate( daoUtil );
64      }
65  
66      @Override
67      public void update( CalendarTemplate calendarTemplate, Plugin plugin )
68      {
69          DAOUtil daoUtil = buildDaoUtil( SQL_QUERY_UPDATE, calendarTemplate, plugin, false );
70          executeUpdate( daoUtil );
71      }
72  
73      @Override
74      public void delete( int nIdCalendarTemplate, Plugin plugin )
75      {
76          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
77          daoUtil.setInt( 1, nIdCalendarTemplate );
78          executeUpdate( daoUtil );
79      }
80  
81      @Override
82      public CalendarTemplate select( int nIdCalendarTemplate, Plugin plugin )
83      {
84          DAOUtil daoUtil = null;
85          CalendarTemplate calendarTemplate = null;
86          try
87          {
88              daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
89              daoUtil.setInt( 1, nIdCalendarTemplate );
90              daoUtil.executeQuery( );
91              if ( daoUtil.next( ) )
92              {
93                  calendarTemplate = buildCalendarTemplate( daoUtil );
94              }
95          }
96          finally
97          {
98              if ( daoUtil != null )
99              {
100                 daoUtil.free( );
101             }
102         }
103         return calendarTemplate;
104     }
105 
106     @Override
107     public List<CalendarTemplate> selectAll( Plugin plugin )
108     {
109         DAOUtil daoUtil = null;
110         List<CalendarTemplate> listTemplates = new ArrayList<CalendarTemplate>( );
111         try
112         {
113             daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL, plugin );
114             daoUtil.executeQuery( );
115             while ( daoUtil.next( ) )
116             {
117                 listTemplates.add( buildCalendarTemplate( daoUtil ) );
118             }
119         }
120         finally
121         {
122             if ( daoUtil != null )
123             {
124                 daoUtil.free( );
125             }
126         }
127         return listTemplates;
128     }
129 
130     /**
131      * Build a Calendar Template business object from the resultset
132      * 
133      * @param daoUtil
134      *            the prepare statement util object
135      * @return a new Calendar Template with all its attributes assigned
136      */
137     private CalendarTemplate buildCalendarTemplate( DAOUtil daoUtil )
138     {
139         int nIndex = 1;
140         CalendarTemplate calendarTemplate = new CalendarTemplate( );
141         calendarTemplate.setIdCalendarTemplate( daoUtil.getInt( nIndex++ ) );
142         calendarTemplate.setTitle( daoUtil.getString( nIndex++ ) );
143         calendarTemplate.setDescription( daoUtil.getString( nIndex++ ) );
144         calendarTemplate.setTemplatePath( daoUtil.getString( nIndex ) );
145         return calendarTemplate;
146     }
147 
148     /**
149      * Build a daoUtil object with the Calendar Template business object
150      * 
151      * @param query
152      *            the query
153      * @param calendarTemplate
154      *            the CalendarTemplate
155      * @param plugin
156      *            the plugin
157      * @param isInsert
158      *            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
159      *            set
160      * @return a new daoUtil with all its values assigned
161      */
162     private DAOUtil buildDaoUtil( String query, CalendarTemplate calendarTemplate, Plugin plugin, boolean isInsert )
163     {
164         int nIndex = 1;
165         DAOUtil daoUtil = new DAOUtil( query, plugin );
166         if ( isInsert )
167         {
168             daoUtil.setInt( nIndex++, calendarTemplate.getIdCalendarTemplate( ) );
169         }
170         daoUtil.setString( nIndex++, calendarTemplate.getTitle( ) );
171         daoUtil.setString( nIndex++, calendarTemplate.getDescription( ) );
172         daoUtil.setString( nIndex++, calendarTemplate.getTemplatePath( ) );
173         if ( !isInsert )
174         {
175             daoUtil.setInt( nIndex, calendarTemplate.getIdCalendarTemplate( ) );
176         }
177         return daoUtil;
178     }
179 
180     /**
181      * Execute a safe update (Free the connection in case of error when execute the query)
182      * 
183      * @param daoUtil
184      *            the daoUtil
185      */
186     private void executeUpdate( DAOUtil daoUtil )
187     {
188         try
189         {
190             daoUtil.executeUpdate( );
191         }
192         finally
193         {
194             if ( daoUtil != null )
195             {
196                 daoUtil.free( );
197             }
198         }
199     }
200 
201 }