Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMP] Allow creating a ODataRow from OValues with relations based on a OModel #328

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions app/src/main/java/com/odoo/core/orm/OValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@

import android.content.ContentValues;
import android.os.Bundle;
import android.util.Log;

import com.odoo.core.orm.fields.OColumn;
import com.odoo.core.orm.fields.OColumn.RelationType;
import com.odoo.core.orm.fields.utils.DomainFilterParser;
import com.odoo.core.utils.OObjectUtils;

Expand Down Expand Up @@ -110,6 +112,56 @@ public ODataRow toDataRow() {
return row;
}

public ODataRow toDataRow(OModel base) {
if (base == null) {
return toDataRow();
}

ODataRow row = new ODataRow();

for (OColumn column : base.getColumns()) {
String columnName = column.getName();
RelationType relationType = column.getRelationType();
Object value = _values.get(columnName);

if (value == null) {
row.put(columnName, column.getDefaultValue());
} else if (relationType == null) {
row.put(columnName, value);
} else {
Integer recordId = null;

if (value instanceof Integer) {
recordId = (Integer) value;
} else if (value instanceof String) {
try {
recordId = Integer.parseInt(value.toString());
} catch (NumberFormatException e) {
Log.d(TAG, "Could not parse integer from value " + value.toString());
}
}

if (recordId == null) {
continue;
}

switch (relationType) {
case ManyToOne:
row.put(columnName, new OM2ORecord(base, column, recordId));
break;
case OneToMany:
row.put(columnName, new OO2MRecord(base, column, recordId));
break;
case ManyToMany:
row.put(columnName, new OM2MRecord(base, column, recordId));
break;
}
}
}

return row;
}

public ContentValues toContentValues() {
ContentValues values = new ContentValues();
for (String key : _values.keySet()) {
Expand Down