SAP / openui5

OpenUI5 lets you build enterprise-ready web applications, responsive to all devices, running on almost any browser of your choice.
http://openui5.org
Apache License 2.0
2.96k stars 1.24k forks source link

sap/ui/model/type/Boolean and v2 OData Model (1.26) #302

Closed hschaefer123 closed 9 years ago

hschaefer123 commented 9 years ago

I am currently developing my CRUD UI5 Framework getting Data from SAP HANA OData Service.

HANA is currently not able to use CDS Data Type BOOLEAN, so instead i am serving ["X"|" "].

The default implementation of the Boolean Type does not work for me! It seems, that internally the used formatValue and parseValue has been swapped (or they are wrong filled from the v2 ODATA Property Binding!?!

There is code to be able to parse ABAP Booleans, but ABAP true is "X" and false is " " (the origin uses "" as false?!?.

My current workaround is the following prototyp, but i think this should be solved generally.

You can see, that i swapped the reworked coding of the two methods. Also the incomming sInternalType is always the same so i did it different.

The following solution makes it possible to wrap Boolean back to HANA by defining something like an sExternalTyp to write defined booleans on TwoWay binding.

Can you confirm that this is a bug in general? Sorry, but no easy way to give an online example.

sap.ui.define(['sap/ui/model/type/Boolean'], function(Boolean) { "use strict";

Boolean.prototype.formatValue = function(oValue, sInternalType) {
    var sValueType = typeof oValue;
    switch (sValueType) {
        case "boolean":
            return oValue;
        case "string":
            oValue = oValue.toUpperCase(); 
            return (oValue === "X" || oValue === "TRUE" || oValue === "1");
        case "int":
        case "float":
            return (oValue === 1);
        default:
            return oValue;
    }
};

Boolean.prototype.parseValue = function(bValue, sInternalType) {
    var sStyle = "ABAP";
    switch (sStyle) {
        case "ABAP":        // EDM.String
            return bValue ? 'X' : ' ';
        case "BOOLSTRING":  // EDM.String
            return bValue ? 'true' : 'false';
        default:            // EDM.Boolean
            return bValue;
    }
};

// eof sap.ui.app.Prototype }, /* bExport= */ false);

goligo commented 9 years ago

Using the type Boolean implies that the model does contain a boolean value. In your case there is a string value contained in the model, so the Boolean type doesn't work.

For Number and Date types we have introduced a source-Property in the format options, to define how this value is represented in the model. I guess we should also offer this for Boolean. For now instead of changing the sap.ui.model.type.Boolean, you should create a custom type, by extending sap.ui.model.SimpleType.

hschaefer123 commented 9 years ago

Hi Goligo, that sounds good.

I think i prefer the prototyped way, because our solution runs with PHP, SAP HANA and SAP ABAP and ABAP and HANA does not support EDM.Boolean, Our PHP OData is able.

So i do not want to use countless custom types if someday the default Boolean Type will do this job. I do not know how SAPUI5 is currently handling Booleans on HANA. There is no Core Data Service Type that will do the job.

It would be great if you will add source-property to the core.

Thanks Holger