Skip to content

Commit

Permalink
Merge pull request #5 from odoo-java/FIX-report-for-v1X
Browse files Browse the repository at this point in the history
[FIX #4]Use jsonrpc to call report for v>=11
  • Loading branch information
flotho authored Jun 2, 2020
2 parents 1ef4ff8 + a28cba8 commit 249cacb
Show file tree
Hide file tree
Showing 4 changed files with 314 additions and 160 deletions.
114 changes: 65 additions & 49 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,54 +1,70 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.odoojava</groupId>
<artifactId>odoo-java-api</artifactId>
<version>3.1.2</version>
<packaging>jar</packaging>
<groupId>com.odoojava</groupId>
<artifactId>odoo-java-api</artifactId>
<version>3.1.2</version>
<packaging>jar</packaging>

<name>odoo-java-api</name>
<url>http://maven.apache.org</url>
<name>odoo-java-api</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.xmlrpc</groupId>
<artifactId>xmlrpc-client</artifactId>
<version>3.1.3</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-netty</artifactId>
<version>3.10.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<organization>
<name>odoo-java</name>
<url>http://odoo-java.com</url>
</organization>
<scm>
<url>https://github.com/odoo-java/odoo-java-api</url>
</scm>
<issueManagement>
<url>https://github.com/odoo-java/odoo-java-api/issues</url>
</issueManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.xmlrpc</groupId>
<artifactId>xmlrpc-client</artifactId>
<version>3.1.3</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-netty</artifactId>
<version>3.10.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.4.0-b180830.0359</version>
</dependency>
<dependency>
<groupId>org.eclipse.lsp4j</groupId>
<artifactId>org.eclipse.lsp4j.jsonrpc</artifactId>
<version>0.9.0</version>
</dependency>
<dependency>
<groupId>com.github.briandilley.jsonrpc4j</groupId>
<artifactId>jsonrpc4j</artifactId>
<version>1.5.1</version>
</dependency>
</dependencies>
<organization>
<name>odoo-java</name>
<url>http://odoo-java.com</url>
</organization>
<scm>
<url>https://github.com/odoo-java/odoo-java-api</url>
</scm>
<issueManagement>
<url>https://github.com/odoo-java/odoo-java-api/issues</url>
</issueManagement>
</project>
88 changes: 71 additions & 17 deletions src/main/java/com/odoojava/api/ReportAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
Expand All @@ -34,8 +38,11 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.xml.bind.DatatypeConverter;

import org.apache.xmlrpc.XmlRpcException;

import com.googlecode.jsonrpc4j.JsonRpcHttpClient;
import com.odoojava.api.Field.FieldType;
import com.odoojava.api.helpers.FilterHelper;

Expand All @@ -44,16 +51,18 @@
* Main class for managing reports with the server.
*
* @author Florent THOMAS
* @param: reportListCache
* . Consider Object part that will be set with name/model/type of
* the Odoo report
* @param: reportListCache . Consider Object part that will be set with
* name/model/type of the Odoo report
*/
public class ReportAdapter {

private Session session;
private Version serverVersion;
private Object[] report;
private String reportName;
private String reportModel;
private String reportMethod;
private ObjectAdapter objectReportAdapter;

/**
* @
Expand All @@ -66,39 +75,74 @@ public ReportAdapter(Session session) throws XmlRpcException {
this.serverVersion = session.getServerVersion();
try {
getReportList();

} catch (OdooApiException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/*
* Method listing the available report and their type Purpose is to use the
* list later to check the existence of the report and its type. Appropriate
* methods will be possible regarding the type
* Method listing the available report and their type Purpose is to use the list
* later to check the existence of the report and its type. Appropriate methods
* will be possible regarding the type
*/
private void getReportList() throws XmlRpcException, OdooApiException {
reportListCache.clear();
ObjectAdapter objectAd = this.session.getObjectAdapter("ir.actions.report.xml");
objectReportAdapter = this.session.getObjectAdapter(this.getReportModel());
FilterCollection filters = new FilterCollection();
String[] report_tuple = new String[] { "report_name", "model", "name", "report_type" };
RowCollection reports = objectAd.searchAndReadObject(filters, report_tuple);
String[] report_tuple = new String[] { "id", "report_name", "model", "name", "report_type" };
RowCollection reports = objectReportAdapter.searchAndReadObject(filters, report_tuple);
reports.forEach(report -> {
Object[] repName = new Object[] { report.get("name"), report.get("model"), report.get("report_type") };
Object[] repName = new Object[] { report.get("name"), report.get("model"), report.get("report_type"),
report.get("id") };
reportListCache.put(report.get("report_name").toString(), repName);
});
}

/**
* This method is fully inspire by
* https://github.com/OCA/odoorpc/blob/master/odoorpc/report.py#L113 from
* https://github.com/sebalix
*
* @return string representing the reportModel regarding the version
*/
public String getReportModel() {
reportModel = "ir.actions.report";
if (this.serverVersion.getMajor() < 11) {
reportModel = "ir.actions.report.xml";
}
return reportModel;
}

public String getReportMethod() {
reportMethod = "render";
if (this.serverVersion.getMajor() < 11) {
reportModel = "render_report";
}
return reportMethod;
}

/**
* @param reportName
* @param ids
* @return
* @throws XmlRpcException
* @throws OdooApiException
* @throws Throwable
*/
public byte[] getReportAsByte(String reportName, Object[] ids) throws XmlRpcException, OdooApiException {
public byte[] getPDFReportAsByte(String reportName, Object[] ids) throws Throwable {
checkReportName(reportName);
byte[] reportDatas = session.executeReportService(reportName, ids);
byte[] reportDatas;
if (this.serverVersion.getMajor() < 11) {
reportDatas = session.executeReportService(reportName, this.getReportMethod(), ids);
} else {
ArrayList<Object> reportParams = new ArrayList<Object>();
reportParams.add( getReportID());
reportParams.add( ids);
Object[] result = session.call_report_jsonrpc(getReportModel(), getReportMethod(), reportParams);

String pdf_string= (String) result[0];
reportDatas = pdf_string.getBytes(StandardCharsets.ISO_8859_1);
}
return reportDatas;
}

Expand All @@ -107,8 +151,7 @@ public byte[] getReportAsByte(String reportName, Object[] ids) throws XmlRpcExce
* Method to prepare the report to be generated Make some usefull tests
* regarding the
*
* @param reportName:
* can be found in Technical > report > report
* @param reportName: can be found in Technical > report > report
* @throws OdooApiException
* @throws XmlRpcException
*/
Expand Down Expand Up @@ -142,19 +185,30 @@ public String getReportType() {
return this.report[2].toString();
}

public Integer getReportID() {
return Integer.valueOf(this.report[3].toString());
}

public String PrintReportToFileName(Object[] ids) throws IOException, XmlRpcException, OdooApiException {

File tmp_file = File.createTempFile("odoo-" + report[1].toString() + "-", getReportType().replace("qweb-", "."),
null);

byte[] report_bytes = getReportAsByte(reportName, ids);
byte[] report_bytes;
FileOutputStream report_stream = new FileOutputStream(tmp_file);
try {
report_bytes = getPDFReportAsByte(reportName, ids);


report_stream.write(report_bytes);
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
report_stream.close();
}

return tmp_file.getAbsolutePath().toString();
}

}
Loading

0 comments on commit 249cacb

Please sign in to comment.