View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.form.business;
35  
36  import fr.paris.lutece.portal.service.plugin.Plugin;
37  import fr.paris.lutece.util.sql.DAOUtil;
38  
39  /**
40   * This class provides Data Access methods for ReportingFiche objects
41   */
42  public final class RecapDAO implements IRecapDAO
43  {
44      // Constants
45      private static final String SQL_QUERY_NEW_PK = "SELECT max( id_recap ) FROM form_recap";
46      private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = "SELECT id_recap,back_url,id_graph_type,recap_message,recap_data,"
47              + "graph,graph_three_dimension,graph_legende,graph_value_legende,graph_label" + " FROM form_recap  WHERE id_recap = ?";
48      private static final String SQL_QUERY_INSERT = "INSERT INTO form_recap(id_recap,back_url,id_graph_type,recap_message,recap_data,"
49              + "graph,graph_three_dimension,graph_legende,graph_value_legende,graph_label)" + " VALUES(?,?,?,?,?,?,?,?,?,?)";
50      private static final String SQL_QUERY_DELETE = "DELETE FROM form_recap WHERE id_recap = ? ";
51      private static final String SQL_QUERY_UPDATE = "UPDATE  form_recap SET " + "id_recap=?,back_url=?,id_graph_type=?,recap_message=?,recap_data=?,"
52              + "graph=?,graph_three_dimension=?,graph_legende=?,graph_value_legende=?,graph_label=? WHERE id_recap= ?";
53  
54      /**
55       * Generates a new primary key
56       *
57       * @param plugin
58       *            the plugin
59       * @return The new primary key
60       */
61      public int newPrimaryKey( Plugin plugin )
62      {
63          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
64          daoUtil.executeQuery( );
65  
66          int nKey;
67  
68          if ( !daoUtil.next( ) )
69          {
70              // if the table is empty
71              nKey = 1;
72          }
73  
74          nKey = daoUtil.getInt( 1 ) + 1;
75          daoUtil.free( );
76  
77          return nKey;
78      }
79  
80      /**
81       * {@inheritDoc}
82       */
83      @Override
84      public synchronized int insert( Recap recap, Plugin plugin )
85      {
86          recap.setIdRecap( newPrimaryKey( plugin ) );
87  
88          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
89          daoUtil.setInt( 1, recap.getIdRecap( ) );
90          daoUtil.setString( 2, recap.getBackUrl( ) );
91  
92          if ( recap.getGraphType( ) == null )
93          {
94              daoUtil.setIntNull( 3 );
95          }
96          else
97          {
98              daoUtil.setInt( 3, recap.getGraphType( ).getIdGraphType( ) );
99          }
100 
101         daoUtil.setString( 4, recap.getRecapMessage( ) );
102         daoUtil.setBoolean( 5, recap.isRecapData( ) );
103         daoUtil.setBoolean( 6, recap.isGraph( ) );
104         daoUtil.setBoolean( 7, recap.isGraphThreeDimension( ) );
105         daoUtil.setBoolean( 8, recap.isGraphLegende( ) );
106         daoUtil.setString( 9, recap.getGraphValueLegende( ) );
107         daoUtil.setBoolean( 10, recap.isGraphLabelValue( ) );
108         daoUtil.executeUpdate( );
109         daoUtil.free( );
110 
111         return recap.getIdRecap( );
112     }
113 
114     /**
115      * {@inheritDoc}
116      */
117     @Override
118     public Recap load( int nId, Plugin plugin )
119     {
120         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY, plugin );
121         daoUtil.setInt( 1, nId );
122         daoUtil.executeQuery( );
123 
124         Recap recap = null;
125         GraphType graphType = null;
126 
127         if ( daoUtil.next( ) )
128         {
129             recap = new Recap( );
130             recap.setIdRecap( daoUtil.getInt( 1 ) );
131             recap.setBackUrl( daoUtil.getString( 2 ) );
132 
133             if ( daoUtil.getObject( 3 ) != null )
134             {
135                 graphType = new GraphType( );
136                 graphType.setIdGraphType( daoUtil.getInt( 3 ) );
137                 recap.setGraphType( graphType );
138             }
139 
140             recap.setRecapMessage( daoUtil.getString( 4 ) );
141             recap.setRecapData( daoUtil.getBoolean( 5 ) );
142             recap.setGraph( daoUtil.getBoolean( 6 ) );
143 
144             recap.setGraphThreeDimension( daoUtil.getBoolean( 7 ) );
145             recap.setGraphLegende( daoUtil.getBoolean( 8 ) );
146             recap.setGraphValueLegende( daoUtil.getString( 9 ) );
147             recap.setGraphLabelValue( daoUtil.getBoolean( 10 ) );
148         }
149 
150         daoUtil.free( );
151 
152         return recap;
153     }
154 
155     /**
156      * {@inheritDoc}
157      */
158     @Override
159     public void delete( int nIdRecap, Plugin plugin )
160     {
161         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
162         daoUtil.setInt( 1, nIdRecap );
163         daoUtil.executeUpdate( );
164         daoUtil.free( );
165     }
166 
167     /**
168      * {@inheritDoc}
169      */
170     @Override
171     public void store( Recap recap, Plugin plugin )
172     {
173         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
174         daoUtil.setInt( 1, recap.getIdRecap( ) );
175         daoUtil.setString( 2, recap.getBackUrl( ) );
176 
177         if ( recap.getGraphType( ) == null )
178         {
179             daoUtil.setIntNull( 3 );
180         }
181         else
182         {
183             daoUtil.setInt( 3, recap.getGraphType( ).getIdGraphType( ) );
184         }
185 
186         daoUtil.setString( 4, recap.getRecapMessage( ) );
187         daoUtil.setBoolean( 5, recap.isRecapData( ) );
188         daoUtil.setBoolean( 6, recap.isGraph( ) );
189         daoUtil.setBoolean( 7, recap.isGraphThreeDimension( ) );
190         daoUtil.setBoolean( 8, recap.isGraphLegende( ) );
191         daoUtil.setString( 9, recap.getGraphValueLegende( ) );
192         daoUtil.setBoolean( 10, recap.isGraphLabelValue( ) );
193         daoUtil.setInt( 11, recap.getIdRecap( ) );
194         daoUtil.executeUpdate( );
195         daoUtil.free( );
196     }
197 }