View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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.jasper.business;
35  
36  import fr.paris.lutece.portal.service.plugin.Plugin;
37  import fr.paris.lutece.util.sql.DAOUtil;
38  
39  import java.util.ArrayList;
40  import java.util.Collection;
41  
42  /**
43   * This class provides Data Access methods for JasperReport objects
44   */
45  public final class JasperReportDAO implements IJasperReportDAO
46  {
47      // Constants
48      private static final String SQL_QUERY_NEW_PK = "SELECT max( id_report ) FROM jasper";
49      private static final String SQL_QUERY_SELECT = "SELECT id_report, code, url,pool,file_folder FROM jasper WHERE id_report = ?";
50      private static final String SQL_QUERY_SELECT_BY_DESC = "SELECT id_report, code, url, pool FROM jasper WHERE code = ?";
51      private static final String SQL_QUERY_INSERT = "INSERT INTO jasper ( id_report, code , url ,pool) VALUES ( ?, ? , ?,?) ";
52      private static final String SQL_QUERY_DELETE = "DELETE FROM jasper WHERE id_report = ? ";
53      private static final String SQL_QUERY_UPDATE = "UPDATE jasper SET id_report = ?, code = ? WHERE id_report = ?";
54      private static final String SQL_QUERY_SELECTALL = "SELECT id_report, code, url,pool FROM jasper";
55      private static final String SQL_QUERY_SELECT_FILE_FORMATS = "SELECT  file_format FROM jasper_file_format WHERE  id_report= ? ";
56  
57      /**
58       * Generates a new primary key
59       * 
60       * @param plugin
61       *            The Plugin
62       * @return The new primary key
63       */
64      public int newPrimaryKey( Plugin plugin )
65      {
66      	int nKey;
67      	
68      	try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin ) )
69      	{
70      		daoUtil.executeQuery( );
71      		
72      		if ( !daoUtil.next( ) )
73              {
74                  // if the table is empty
75                  nKey = 1;
76              }
77  
78              nKey = daoUtil.getInt( 1 ) + 1;
79      	}
80  
81          return nKey;
82      }
83  
84      /**
85       * Insert a new record in the table.
86       * 
87       * @param report
88       *            instance of the JasperReport object to insert
89       * @param plugin
90       *            The plugin
91       */
92      public void insert( JasperReport report, Plugin plugin )
93      {   
94      	try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin ) )
95      	{
96      		report.setIdReport( newPrimaryKey( plugin ) );
97  
98              daoUtil.setInt( 1, report.getIdReport( ) );
99              daoUtil.setString( 2, report.getCode( ) );
100             daoUtil.setString( 3, report.getUrl( ) );
101             daoUtil.setString( 4, report.getPool( ) );
102 
103             daoUtil.executeUpdate( );   
104     	}
105     }
106 
107     /**
108      * Load the data of the report from the table
109      * 
110      * @param nId
111      *            The identifier of the report
112      * @param plugin
113      *            The plugin
114      * @return the instance of the JasperReport
115      */
116     public JasperReport load( int nId, Plugin plugin )
117     {
118     	JasperReport report = null;
119     	
120     	try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin ) )
121     	{
122     		daoUtil.setInt( 1, nId );
123 	        daoUtil.executeQuery( ); 
124 	 
125 	        if ( daoUtil.next( ) )
126 	        {
127 	            report = new JasperReport( );
128 	 
129 	            report.setIdReport( daoUtil.getInt( 1 ) );
130 	            report.setCode( daoUtil.getString( 2 ) );
131 	            report.setUrl( daoUtil.getString( 3 ) );
132 	            report.setPool( daoUtil.getString( 4 ) );
133 	            report.setFileFolder( daoUtil.getString( 5 ) );
134 	            report.addFileFormats( loadFileFormats( nId, plugin ) );
135 	        }
136     	}
137     	
138     	return report;
139     }
140 
141     /**
142      * Load the file formats
143      * 
144      * @param nId
145      *            The identifier of the report
146      * @param plugin
147      *            The plugin
148      * @return the list of file formats attached to the report
149      */
150     public ArrayList<String> loadFileFormats( int nId, Plugin plugin )
151     {
152     	ArrayList<String> listFileFormats = new ArrayList<String>( );
153     	
154     	try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_FILE_FORMATS, plugin ) )
155     	{
156     		daoUtil.setInt( 1, nId );
157             daoUtil.executeQuery( );
158             
159             while ( daoUtil.next( ) )
160             {
161                 listFileFormats.add( daoUtil.getString( 1 ) );
162             }    	       
163     	}
164     	
165     	return listFileFormats;
166     }
167 
168     /**
169      * Delete a record from the table
170      * 
171      * @param nJasperReportId
172      *            The identifier of the report
173      * @param plugin
174      *            The plugin
175      */
176     public void delete( int nJasperReportId, Plugin plugin )
177     {
178     	try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) )
179     	{
180     		daoUtil.setInt( 1, nJasperReportId );
181             daoUtil.executeUpdate( );    	       
182     	}
183     }
184 
185     /**
186      * Update the record in the table
187      * 
188      * @param report
189      *            The reference of the report
190      * @param plugin
191      *            The plugin
192      */
193     public void store( JasperReport report, Plugin plugin )
194     {
195     	try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ) )
196     	{
197     		daoUtil.setInt( 1, report.getIdReport( ) );
198             daoUtil.setString( 2, report.getCode( ) );
199             daoUtil.setInt( 3, report.getIdReport( ) );
200 
201             daoUtil.executeUpdate( );    	       
202     	}
203     }
204 
205     /**
206      * Load the data of all the reports and returns them as a collection
207      * 
208      * @param plugin
209      *            The plugin
210      * @return The Collection which contains the data of all the reports
211      */
212     public Collection<JasperReport> selectJasperReportsList( Plugin plugin )
213     {
214     	Collection<JasperReport> reportList = new ArrayList<JasperReport>( );
215     	
216     	try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin ) )
217     	{
218     		daoUtil.executeQuery( );
219 
220             while ( daoUtil.next( ) )
221             {
222                 JasperReporter/business/JasperReport.html#JasperReport">JasperReport report = new JasperReport( );
223 
224                 report.setIdReport( daoUtil.getInt( 1 ) );
225                 report.setCode( daoUtil.getString( 2 ) );
226                 report.setUrl( daoUtil.getString( 3 ) );
227                 report.setPool( daoUtil.getString( 4 ) );
228 
229                 reportList.add( report );
230             }    	       
231     	}
232     	
233         return reportList;
234     }
235 
236     public JasperReport loadByCode( String strKey, Plugin plugin )
237     {
238     	JasperReport report = null;
239     	
240     	try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_DESC, plugin ) )
241     	{
242     		daoUtil.setString( 1, strKey );
243             daoUtil.executeQuery( );
244             if ( daoUtil.next( ) )
245             {
246                 report = new JasperReport( );
247 
248                 report.setIdReport( daoUtil.getInt( 1 ) );
249                 report.setCode( daoUtil.getString( 2 ) );
250                 report.setUrl( daoUtil.getString( 3 ) );
251                 report.setPool( daoUtil.getString( 4 ) );
252             }    	       
253     	}
254     	
255         return report;
256     }
257 }