2121import java .io .File ;
2222import java .io .FileOutputStream ;
2323import java .io .IOException ;
24+ import java .net .URL ;
25+ import java .nio .charset .Charset ;
26+ import java .nio .charset .StandardCharsets ;
2427import java .text .SimpleDateFormat ;
2528import java .util .ArrayList ;
29+ import java .util .Collections ;
2630import java .util .Arrays ;
2731import java .util .Date ;
2832import java .util .HashMap ;
3438import java .util .stream .Collectors ;
3539import java .util .stream .Stream ;
3640
41+ import javax .xml .bind .DatatypeConverter ;
42+
3743import org .apache .xmlrpc .XmlRpcException ;
3844
45+ import com .googlecode .jsonrpc4j .JsonRpcHttpClient ;
3946import com .odoojava .api .Field .FieldType ;
4047import com .odoojava .api .helpers .FilterHelper ;
4148
4451 * Main class for managing reports with the server.
4552 *
4653 * @author Florent THOMAS
47- * @param: reportListCache
48- * . Consider Object part that will be set with name/model/type of
49- * the Odoo report
54+ * @param: reportListCache . Consider Object part that will be set with
55+ * name/model/type of the Odoo report
5056 */
5157public class ReportAdapter {
5258
5359 private Session session ;
5460 private Version serverVersion ;
5561 private Object [] report ;
5662 private String reportName ;
63+ private String reportModel ;
64+ private String reportMethod ;
65+ private ObjectAdapter objectReportAdapter ;
5766
5867 /**
5968 * @
@@ -66,39 +75,74 @@ public ReportAdapter(Session session) throws XmlRpcException {
6675 this .serverVersion = session .getServerVersion ();
6776 try {
6877 getReportList ();
78+
6979 } catch (OdooApiException e ) {
7080 // TODO Auto-generated catch block
7181 e .printStackTrace ();
7282 }
7383 }
7484
7585 /*
76- * Method listing the available report and their type Purpose is to use the
77- * list later to check the existence of the report and its type. Appropriate
78- * methods will be possible regarding the type
86+ * Method listing the available report and their type Purpose is to use the list
87+ * later to check the existence of the report and its type. Appropriate methods
88+ * will be possible regarding the type
7989 */
8090 private void getReportList () throws XmlRpcException , OdooApiException {
8191 reportListCache .clear ();
82- ObjectAdapter objectAd = this .session .getObjectAdapter ("ir.actions.report.xml" );
92+ objectReportAdapter = this .session .getObjectAdapter (this . getReportModel () );
8393 FilterCollection filters = new FilterCollection ();
84- String [] report_tuple = new String [] { "report_name" , "model" , "name" , "report_type" };
85- RowCollection reports = objectAd .searchAndReadObject (filters , report_tuple );
94+ String [] report_tuple = new String [] { "id" , " report_name" , "model" , "name" , "report_type" };
95+ RowCollection reports = objectReportAdapter .searchAndReadObject (filters , report_tuple );
8696 reports .forEach (report -> {
87- Object [] repName = new Object [] { report .get ("name" ), report .get ("model" ), report .get ("report_type" ) };
97+ Object [] repName = new Object [] { report .get ("name" ), report .get ("model" ), report .get ("report_type" ),
98+ report .get ("id" ) };
8899 reportListCache .put (report .get ("report_name" ).toString (), repName );
89100 });
90101 }
91102
103+ /**
104+ * This method is fully inspire by
105+ * https://github.com/OCA/odoorpc/blob/master/odoorpc/report.py#L113 from
106+ * https://github.com/sebalix
107+ *
108+ * @return string representing the reportModel regarding the version
109+ */
110+ public String getReportModel () {
111+ reportModel = "ir.actions.report" ;
112+ if (this .serverVersion .getMajor () < 11 ) {
113+ reportModel = "ir.actions.report.xml" ;
114+ }
115+ return reportModel ;
116+ }
117+
118+ public String getReportMethod () {
119+ reportMethod = "render" ;
120+ if (this .serverVersion .getMajor () < 11 ) {
121+ reportModel = "render_report" ;
122+ }
123+ return reportMethod ;
124+ }
125+
92126 /**
93127 * @param reportName
94128 * @param ids
95129 * @return
96- * @throws XmlRpcException
97- * @throws OdooApiException
130+ * @throws Throwable
98131 */
99- public byte [] getReportAsByte (String reportName , Object [] ids ) throws XmlRpcException , OdooApiException {
132+ public byte [] getPDFReportAsByte (String reportName , Object [] ids ) throws Throwable {
100133 checkReportName (reportName );
101- byte [] reportDatas = session .executeReportService (reportName , ids );
134+ byte [] reportDatas ;
135+ if (this .serverVersion .getMajor () < 11 ) {
136+ reportDatas = session .executeReportService (reportName , this .getReportMethod (), ids );
137+ } else {
138+ ArrayList <Object > reportParams = new ArrayList <Object >();
139+ reportParams .add ( getReportID ());
140+ reportParams .add ( ids );
141+ Object [] result = session .call_report_jsonrpc (getReportModel (), getReportMethod (), reportParams );
142+
143+ String pdf_string = (String ) result [0 ];
144+ reportDatas = pdf_string .getBytes (StandardCharsets .ISO_8859_1 );
145+ }
102146 return reportDatas ;
103147 }
104148
@@ -107,8 +151,7 @@ public byte[] getReportAsByte(String reportName, Object[] ids) throws XmlRpcExce
107151 * Method to prepare the report to be generated Make some usefull tests
108152 * regarding the
109153 *
110- * @param reportName:
111- * can be found in Technical > report > report
154+ * @param reportName: can be found in Technical > report > report
112155 * @throws OdooApiException
113156 * @throws XmlRpcException
114157 */
@@ -142,19 +185,30 @@ public String getReportType() {
142185 return this .report [2 ].toString ();
143186 }
144187
188+ public Integer getReportID () {
189+ return Integer .valueOf (this .report [3 ].toString ());
190+ }
191+
145192 public String PrintReportToFileName (Object [] ids ) throws IOException , XmlRpcException , OdooApiException {
146193
147194 File tmp_file = File .createTempFile ("odoo-" + report [1 ].toString () + "-" , getReportType ().replace ("qweb-" , "." ),
148195 null );
149196
150- byte [] report_bytes = getReportAsByte ( reportName , ids ) ;
197+ byte [] report_bytes ;
151198 FileOutputStream report_stream = new FileOutputStream (tmp_file );
152199 try {
200+ report_bytes = getPDFReportAsByte (reportName , ids );
201+
202+
153203 report_stream .write (report_bytes );
204+ } catch (Throwable e ) {
205+ // TODO Auto-generated catch block
206+ e .printStackTrace ();
154207 } finally {
155208 report_stream .close ();
156209 }
157210
158211 return tmp_file .getAbsolutePath ().toString ();
159212 }
213+
160214}
0 commit comments