Skip to content

Commit f1b355b

Browse files
committed
Clean up code
Interfaces like Map should always be used instead of implementations classes like HashMap, in particular for parameters and return types. Also refactored ObjectAdapter#writeObject to make it simpler.
1 parent 0e1aa7f commit f1b355b

2 files changed

Lines changed: 32 additions & 16 deletions

File tree

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

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.ArrayList;
2424
import java.util.Date;
2525
import java.util.HashMap;
26+
import java.util.Map;
2627

2728
import org.apache.xmlrpc.XmlRpcException;
2829

@@ -797,8 +798,7 @@ public Boolean[] writeObject(final RowCollection rows, final boolean changesOnly
797798
* @throws OpeneERPApiException
798799
* @throws XmlRpcException
799800
*/
800-
public boolean writeObject(final Row row, boolean changesOnly) throws OpeneERPApiException, XmlRpcException {
801-
HashMap<String, Object> valueList = new HashMap<String, Object>();
801+
public boolean writeObject(final Row row, boolean changesOnly) throws OpeneERPApiException {
802802

803803
Object idObj = row.get("id");
804804

@@ -807,23 +807,38 @@ public boolean writeObject(final Row row, boolean changesOnly) throws OpeneERPAp
807807

808808
int id = Integer.parseInt(idObj.toString());
809809

810-
if (changesOnly) {
811-
for (Field fld : row.getChangedFields())
812-
valueList.put(fld.getName(), formatValueForWrite(fld, row.get(fld)));
813-
} else
814-
for (Field fld : row.getFields()) {
815-
valueList.put(fld.getName(), formatValueForWrite(fld, row.get(fld)));
816-
}
810+
Map<String, Object> valueList = collectValues(row, changesOnly);
817811

818812
if (valueList.size() == 0)
819813
return false;
820814

821-
boolean success = command.writeObject(modelName, id, valueList);
815+
try {
816+
817+
boolean success = command.writeObject(modelName, id, valueList);
818+
if (success) {
819+
row.changesApplied();
820+
}
821+
return success;
822+
823+
} catch (XmlRpcException e) {
824+
throw new OpeneERPApiException(e);
825+
}
826+
827+
}
822828

823-
if (success)
824-
row.changesApplied();
829+
private Map<String, Object> collectValues(final Row row, boolean changesOnly) {
830+
Map<String, Object> valueList = new HashMap<>();
831+
FieldCollection fields;
832+
if (changesOnly) {
833+
fields = row.getChangedFields();
834+
} else {
835+
fields = row.getFields();
836+
}
825837

826-
return success;
838+
for (Field fld : fields) {
839+
valueList.put(fld.getName(), formatValueForWrite(fld, row.get(fld)));
840+
}
841+
return valueList;
827842
}
828843

829844
/**

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
package com.debortoliwines.odoo.api;
2121

2222
import java.util.HashMap;
23+
import java.util.Map;
24+
2325
import org.apache.xmlrpc.XmlRpcException;
2426

2527
/**
@@ -98,9 +100,8 @@ public Object[] readObject(String objectName, Object [] ids, String [] fields) t
98100
* @return True if the update was successful
99101
* @throws XmlRpcException
100102
*/
101-
public boolean writeObject(String objectName, int id, HashMap<String, Object> valueList) throws XmlRpcException{
102-
boolean result = false;
103-
result = (Boolean) session.executeCommand(objectName, "write", new Object[] {id, valueList});
103+
public boolean writeObject(String objectName, int id, Map<String, Object> valueList) throws XmlRpcException {
104+
boolean result = (Boolean) session.executeCommand(objectName, "write", new Object[] { id, valueList });
104105
return result;
105106
}
106107

0 commit comments

Comments
 (0)