Skip to content

Commit 7ccf319

Browse files
authored
Merge pull request DeBortoliWines#38 from gjvoosten/odoo-compatibility-fixes
@gjvoosten great thanks for this PR
2 parents 4f32dd7 + e11dadb commit 7ccf319

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

src/main/java/com/debortoliwines/odoo/api/ObjectAdapter.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -586,10 +586,9 @@ private static String getFieldNameForImport(Field field) {
586586
* @throws OdooApiException
587587
*/
588588
public boolean importData(RowCollection rows) throws OdooApiException, XmlRpcException {
589-
//TODO : Check the way importdata are handled in v8/v9/
590-
// Workaround. Odoo7 bug where old and new rows can't be sent
589+
// Workaround: old and new rows can't be sent together
591590
// together using the import_data or load function
592-
if (this.serverVersion.getMajor() >= 7 && this.serverVersion.getMinor() == 0) {
591+
if (this.serverVersion.getMajor() >= 7) {
593592
RowCollection newRows = new RowCollection();
594593
RowCollection oldRows = new RowCollection();
595594

@@ -647,8 +646,8 @@ private void importDataV7(RowCollection rows, Object[][] importRows) throws XmlR
647646

648647
String[] targetFieldList = getFieldListForImport(rows.get(0).getFields());
649648

650-
// Workaround Odoo V7 bug. Remove the .id field for new rows.
651-
if (this.serverVersion.getMinor() == 0 && !rows.isEmpty() && rows.get(0).getID() == 0) {
649+
// Remove the .id field for new rows.
650+
if (this.serverVersion.getMajor() >= 7 && !rows.isEmpty() && rows.get(0).getID() == 0) {
652651
String[] newTargetFieldList = new String[targetFieldList.length - 1];
653652
for (int i = 1; i < targetFieldList.length; i++) {
654653
newTargetFieldList[i - 1] = targetFieldList[i];

src/main/java/com/debortoliwines/odoo/api/OdooCommand.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public OdooCommand(Session session) {
5353
* @return Response that need to be parsed from the calling method to check
5454
* if successful and then adapt the result as an array.
5555
*/
56-
public Response searchObject(String objectName, Object[] filter) {
56+
public Response searchObject(String objectName, Object[] filter) throws XmlRpcException {
5757
return (Response) searchObject(objectName, filter, -1, -1, null, false);
5858
}
5959

@@ -72,11 +72,14 @@ public Response searchObject(String objectName, Object[] filter) {
7272
* returned and could be parsed as Object[] of IDs if response is
7373
* successfull
7474
*/
75-
public Response searchObject(String objectName, Object[] filter, int offset, int limit, String order, boolean count) {
75+
public Response searchObject(String objectName, Object[] filter, int offset, int limit, String order, boolean count) throws XmlRpcException {
7676
Object offsetParam = offset < 0 ? false : offset;
7777
Object limitParam = limit < 0 ? false : limit;
7878
Object orderParam = order == null || order.length() == 0 ? false : order;
79-
Object[] params = new Object[]{filter, offsetParam, limitParam, orderParam, count};
79+
// Before Odoo 10 there's a 'context' parameter between order and count
80+
Object[] params = (this.session.getServerVersion().getMajor() < 10)
81+
? new Object[]{filter, offsetParam, limitParam, orderParam, false, count}
82+
: new Object[]{filter, offsetParam, limitParam, orderParam, count};
8083

8184
try {
8285
Response response = new Response(session.executeCommand(objectName, "search", params));
@@ -97,7 +100,7 @@ public Response searchObject(String objectName, Object[] filter, int offset, int
97100
@SuppressWarnings("unchecked")
98101
public Map<String, Object> getFields(String objectName, String[] filterFields) throws XmlRpcException {
99102
Map<String, Object> fieldsArray;
100-
if (this.session.getServerVersion().getMajor() >= 10 ) {
103+
if (this.session.getServerVersion().getMajor() >= 8 ) {
101104
fieldsArray = (Map<String, Object>) session.executeCommand(objectName, "fields_get",
102105
new Object[]{filterFields});
103106
} else {
@@ -120,7 +123,7 @@ public Map<String, Object> getFields(String objectName, String[] filterFields) t
120123
*/
121124
public Object[] readObject(String objectName, Object[] ids, String[] fields) throws XmlRpcException {
122125
Object[] readResult ;
123-
if (this.session.getServerVersion().getMajor() >= 10 ) {
126+
if (this.session.getServerVersion().getMajor() >= 8 ) {
124127
readResult = (Object[]) session.executeCommand(objectName, "read", new Object[]{ids, fields});
125128
} else {
126129
readResult = (Object[]) session.executeCommand(objectName, "read", new Object[]{ids, fields, session.getContext()});
@@ -200,7 +203,7 @@ public boolean unlinkObject(String objectName, Object[] ids) throws XmlRpcExcept
200203
*/
201204
public Object createObject(String objectName, Map<String, Object> values) throws XmlRpcException {
202205
Object readResult ;
203-
if (this.session.getServerVersion().getMajor() >= 10 ) {
206+
if (this.session.getServerVersion().getMajor() >= 8 ) {
204207
readResult = (Object) session.executeCommand(objectName, "create", new Object[]{values});
205208
} else {
206209
readResult = (Object) session.executeCommand(objectName, "create", new Object[]{values, session.getContext()});

src/main/java/com/debortoliwines/odoo/api/Session.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public class Session {
4949
private RPCProtocol protocol;
5050

5151
private OdooXmlRpcProxy objectClient;
52+
private Version serverVersion;
5253

5354
/**
5455
* *
@@ -266,7 +267,11 @@ public void executeWorkflow(final String objectName, final String signal, final
266267
* @throws XmlRpcException
267268
*/
268269
public Version getServerVersion() throws XmlRpcException {
269-
return OdooXmlRpcProxy.getServerVersion(protocol, host, port);
270+
if (serverVersion == null) {
271+
// Cache server version
272+
serverVersion = OdooXmlRpcProxy.getServerVersion(protocol, host, port);
273+
}
274+
return serverVersion;
270275
}
271276

272277
/**

0 commit comments

Comments
 (0)