21
21
import java .io .File ;
22
22
import java .io .FileOutputStream ;
23
23
import java .io .IOException ;
24
+ import java .net .URL ;
25
+ import java .nio .charset .Charset ;
26
+ import java .nio .charset .StandardCharsets ;
24
27
import java .text .SimpleDateFormat ;
25
28
import java .util .ArrayList ;
29
+ import java .util .Collections ;
26
30
import java .util .Arrays ;
27
31
import java .util .Date ;
28
32
import java .util .HashMap ;
34
38
import java .util .stream .Collectors ;
35
39
import java .util .stream .Stream ;
36
40
41
+ import javax .xml .bind .DatatypeConverter ;
42
+
37
43
import org .apache .xmlrpc .XmlRpcException ;
38
44
45
+ import com .googlecode .jsonrpc4j .JsonRpcHttpClient ;
39
46
import com .odoojava .api .Field .FieldType ;
40
47
import com .odoojava .api .helpers .FilterHelper ;
41
48
44
51
* Main class for managing reports with the server.
45
52
*
46
53
* @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
50
56
*/
51
57
public class ReportAdapter {
52
58
53
59
private Session session ;
54
60
private Version serverVersion ;
55
61
private Object [] report ;
56
62
private String reportName ;
63
+ private String reportModel ;
64
+ private String reportMethod ;
65
+ private ObjectAdapter objectReportAdapter ;
57
66
58
67
/**
59
68
* @
@@ -66,39 +75,74 @@ public ReportAdapter(Session session) throws XmlRpcException {
66
75
this .serverVersion = session .getServerVersion ();
67
76
try {
68
77
getReportList ();
78
+
69
79
} catch (OdooApiException e ) {
70
80
// TODO Auto-generated catch block
71
81
e .printStackTrace ();
72
82
}
73
83
}
74
84
75
85
/*
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
79
89
*/
80
90
private void getReportList () throws XmlRpcException , OdooApiException {
81
91
reportListCache .clear ();
82
- ObjectAdapter objectAd = this .session .getObjectAdapter ("ir.actions.report.xml" );
92
+ objectReportAdapter = this .session .getObjectAdapter (this . getReportModel () );
83
93
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 );
86
96
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" ) };
88
99
reportListCache .put (report .get ("report_name" ).toString (), repName );
89
100
});
90
101
}
91
102
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
+
92
126
/**
93
127
* @param reportName
94
128
* @param ids
95
129
* @return
96
- * @throws XmlRpcException
97
- * @throws OdooApiException
130
+ * @throws Throwable
98
131
*/
99
- public byte [] getReportAsByte (String reportName , Object [] ids ) throws XmlRpcException , OdooApiException {
132
+ public byte [] getPDFReportAsByte (String reportName , Object [] ids ) throws Throwable {
100
133
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
+ }
102
146
return reportDatas ;
103
147
}
104
148
@@ -107,8 +151,7 @@ public byte[] getReportAsByte(String reportName, Object[] ids) throws XmlRpcExce
107
151
* Method to prepare the report to be generated Make some usefull tests
108
152
* regarding the
109
153
*
110
- * @param reportName:
111
- * can be found in Technical > report > report
154
+ * @param reportName: can be found in Technical > report > report
112
155
* @throws OdooApiException
113
156
* @throws XmlRpcException
114
157
*/
@@ -142,19 +185,30 @@ public String getReportType() {
142
185
return this .report [2 ].toString ();
143
186
}
144
187
188
+ public Integer getReportID () {
189
+ return Integer .valueOf (this .report [3 ].toString ());
190
+ }
191
+
145
192
public String PrintReportToFileName (Object [] ids ) throws IOException , XmlRpcException , OdooApiException {
146
193
147
194
File tmp_file = File .createTempFile ("odoo-" + report [1 ].toString () + "-" , getReportType ().replace ("qweb-" , "." ),
148
195
null );
149
196
150
- byte [] report_bytes = getReportAsByte ( reportName , ids ) ;
197
+ byte [] report_bytes ;
151
198
FileOutputStream report_stream = new FileOutputStream (tmp_file );
152
199
try {
200
+ report_bytes = getPDFReportAsByte (reportName , ids );
201
+
202
+
153
203
report_stream .write (report_bytes );
204
+ } catch (Throwable e ) {
205
+ // TODO Auto-generated catch block
206
+ e .printStackTrace ();
154
207
} finally {
155
208
report_stream .close ();
156
209
}
157
210
158
211
return tmp_file .getAbsolutePath ().toString ();
159
212
}
213
+
160
214
}
0 commit comments