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.display;
35  
36  import fr.paris.lutece.plugins.appointment.business.UtilDAO;
37  import fr.paris.lutece.portal.service.image.ImageResource;
38  import fr.paris.lutece.portal.service.plugin.Plugin;
39  import fr.paris.lutece.util.sql.DAOUtil;
40  
41  /**
42   * This class provides Data Access methods for Display objects
43   * 
44   * @author Laurent Payen
45   *
46   */
47  public final class DisplayDAO extends UtilDAO implements IDisplayDAO
48  {
49  
50      private static final String SQL_QUERY_NEW_PK = "SELECT max(id_display) FROM appointment_display";
51      private static final String SQL_QUERY_INSERT = "INSERT INTO appointment_display (id_display, display_title_fo, icon_form_content, icon_form_mime_type, nb_weeks_to_display, is_displayed_on_portlet, id_calendar_template, id_form) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
52      private static final String SQL_QUERY_UPDATE = "UPDATE appointment_display SET display_title_fo = ?, icon_form_content = ?, icon_form_mime_type = ?, nb_weeks_to_display = ?, is_displayed_on_portlet = ?, id_calendar_template = ?, id_form = ? WHERE id_display = ?";
53      private static final String SQL_QUERY_DELETE = "DELETE FROM appointment_display WHERE id_display = ?";
54      private static final String SQL_QUERY_SELECT_COLUMNS = "SELECT id_display, display_title_fo, icon_form_content, icon_form_mime_type, nb_weeks_to_display, is_displayed_on_portlet, id_calendar_template, id_form FROM appointment_display";
55      private static final String SQL_QUERY_SELECT = SQL_QUERY_SELECT_COLUMNS + " WHERE id_display = ?";
56      private static final String SQL_QUERY_SELECT_BY_ID_FORM = SQL_QUERY_SELECT_COLUMNS + " WHERE id_form = ?";
57  
58      @Override
59      public synchronized void insert( Display display, Plugin plugin )
60      {
61          display.setIdDisplay( getNewPrimaryKey( SQL_QUERY_NEW_PK, plugin ) );
62          DAOUtil daoUtil = buildDaoUtil( SQL_QUERY_INSERT, display, plugin, true );
63          executeUpdate( daoUtil );
64      }
65  
66      @Override
67      public void update( Display display, Plugin plugin )
68      {
69          DAOUtil daoUtil = buildDaoUtil( SQL_QUERY_UPDATE, display, plugin, false );
70          executeUpdate( daoUtil );
71      }
72  
73      @Override
74      public void delete( int nIdDisplay, Plugin plugin )
75      {
76          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
77          daoUtil.setInt( 1, nIdDisplay );
78          executeUpdate( daoUtil );
79      }
80  
81      @Override
82      public Display select( int nIdDisplay, Plugin plugin )
83      {
84          DAOUtil daoUtil = null;
85          Display display = null;
86          try
87          {
88              daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
89              daoUtil.setInt( 1, nIdDisplay );
90              daoUtil.executeQuery( );
91              if ( daoUtil.next( ) )
92              {
93                  display = buildDisplay( daoUtil );
94              }
95          }
96          finally
97          {
98              if ( daoUtil != null )
99              {
100                 daoUtil.free( );
101             }
102         }
103         return display;
104     }
105 
106     @Override
107     public Display findByIdForm( int nIdForm, Plugin plugin )
108     {
109         DAOUtil daoUtil = null;
110         Display display = null;
111         try
112         {
113             daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_ID_FORM, plugin );
114             daoUtil.setInt( 1, nIdForm );
115             daoUtil.executeQuery( );
116             if ( daoUtil.next( ) )
117             {
118                 display = buildDisplay( daoUtil );
119             }
120         }
121         finally
122         {
123             if ( daoUtil != null )
124             {
125                 daoUtil.free( );
126             }
127         }
128         return display;
129     }
130 
131     /**
132      * Build a Display business object from the resultset
133      * 
134      * @param daoUtil
135      *            the prepare statement util object
136      * @return a new display business object with all its attributes assigned
137      */
138     private Display buildDisplay( DAOUtil daoUtil )
139     {
140         int nIndex = 1;
141         Display display = new Display( );
142         display.setIdDisplay( daoUtil.getInt( nIndex++ ) );
143         display.setDisplayTitleFo( daoUtil.getBoolean( nIndex++ ) );
144         display.setIcon( buildIcon( daoUtil.getBytes( nIndex++ ), daoUtil.getString( nIndex++ ) ) );
145         display.setNbWeeksToDisplay( daoUtil.getInt( nIndex++ ) );
146         display.setIsDisplayedOnPortlet( daoUtil.getBoolean( nIndex++ ) );
147         display.setIdCalendarTemplate( daoUtil.getInt( nIndex++ ) );
148         display.setIdForm( daoUtil.getInt( nIndex ) );
149         return display;
150     }
151 
152     /**
153      * Build a daoUtil object with the display business object for insert query
154      * 
155      * @param query
156      *            the query
157      * @param display
158      *            the display
159      * @param plugin
160      *            the plugin
161      * @param isInsert
162      *            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
163      *            set
164      * @return a new daoUtil with all its values assigned
165      */
166     private DAOUtil buildDaoUtil( String query, Display display, Plugin plugin, boolean isInsert )
167     {
168         int nIndex = 1;
169         DAOUtil daoUtil = new DAOUtil( query, plugin );
170         if ( isInsert )
171         {
172             daoUtil.setInt( nIndex++, display.getIdDisplay( ) );
173         }
174         daoUtil.setBoolean( nIndex++, display.isDisplayTitleFo( ) );
175         daoUtil.setBytes( nIndex++, display.getIcon( ).getImage( ) );
176         daoUtil.setString( nIndex++, display.getIcon( ).getMimeType( ) );
177         daoUtil.setInt( nIndex++, display.getNbWeeksToDisplay( ) );
178         daoUtil.setBoolean( nIndex++, display.isDisplayedOnPortlet( ) );
179         daoUtil.setInt( nIndex++, display.getIdCalendarTemplate( ) );
180         daoUtil.setInt( nIndex++, display.getIdForm( ) );
181         if ( !isInsert )
182         {
183             daoUtil.setInt( nIndex, display.getIdDisplay( ) );
184         }
185         return daoUtil;
186     }
187 
188     /**
189      * Execute a safe update (Free the connection in case of error when execute the query)
190      * 
191      * @param daoUtil
192      *            the daoUtil
193      */
194     private void executeUpdate( DAOUtil daoUtil )
195     {
196         try
197         {
198             daoUtil.executeUpdate( );
199         }
200         finally
201         {
202             if ( daoUtil != null )
203             {
204                 daoUtil.free( );
205             }
206         }
207     }
208 
209     /**
210      * Build an icon (imageResource)
211      * 
212      * @param strImage
213      *            the icon form content
214      * @param strMimeType
215      *            the icon form mime type
216      * @return the icon (imageResource) built
217      */
218     private ImageResource buildIcon( byte [ ] strImage, String strMimeType )
219     {
220         ImageResource imageResource = new ImageResource( );
221         imageResource.setImage( strImage );
222         imageResource.setMimeType( strMimeType );
223         return imageResource;
224     }
225 
226 }