:/sap/TRUI_TU_ CFP_MAINT_02"); Find the entity "xBTLxTRP_TU_CFP" a" /> :/sap/TRUI_TU_ CFP_MAINT_02"); Find the entity "xBTLxTRP_TU_CFP" a" /> :/sap/TRUI_TU_ CFP_MAINT_02"); Find the entity "xBTLxTRP_TU_CFP" a"/>

How to change entity property value of OData v2 service and post it back to server from Java?

93 views Asked by At

My task is to (using Java):

  1. Call OData v2 service by a given URL (e.g. "<domain_URL>:/sap/TRUI_TU_ CFP_MAINT_02");
  2. Find the entity "xBTLxTRP_TU_CFP" and its property "CapEngine";
  3. Retrieve the value for the property "CapEngine";
  4. Change the current value of the property "CapEngine" from "15.000" to "22.000" and then to post the changes back to server.

I was able to implement steps from 1 to 3 successfully. But I need a solution on how to correctly implement the last step 4 (i.e. first to set the desired value, and then to post changes).

The OData entity schema looks like this:

<entry xmlns="http://www.w3.org/2005/Atom">
  <id>...</id>
  <title type="text">...</title>
  <updated>...</updated>
  <category .../>
  <link href="..." rel="edit" title="xBTLxTRP_TU_CFPType" />
  <content type="application/xml">
    <m:properties>
      <d:ObjectNr>IEIKLWARC</d:ObjectNr>
      <d:EquipmentNr>IKLWARC</d:EquipmentNr>
      <d:TuId>fa163e34-7b50-1ee9-a4b3-03065d395f68</d:TuId>
      <d:MaintainedFlag>X</d:MaintainedFlag>
      <d:EngineCap>15.000</d:EngineCap>
      <d:CapUnit>L</d:CapUnit>
      <d:PrimaryFuel>DIESEL</d:PrimaryFuel>
      <d:EngineType>COMBUSTION</d:EngineType>
      <d:AverageConsumption>20.0</d:AverageConsumption>
      <d:BioFuelRatio>0.0</d:BioFuelRatio>
      <d:ConsumptionUnit>LHK</d:ConsumptionUnit>
      <d:EmissionStandard>EURO_5</d:EmissionStandard>
      <d:EmissionTechnology>NONE</d:EmissionTechnology>
      <d:ParticleFilter>DPF</d:ParticleFilter>
    </m:properties>
  </content>
</entry>

Here is the Java code for steps from 1 to 3:

public static void main(String[] args) {
        String serviceUrl = "https://ldcioid.devsys.net.sap:44300/sap/opu/odata/BTL/TRUI_TU_CFP_MAINT_O2";
        String entitySet = "xBTLxTRP_TU_CFP/?sap-client=200"; 
        String propertyToUpdate = "PropertyName";
        ODataV2EntityIterator entityIterator = new ODataV2EntityIterator(serviceUrl, entitySet);

        try {
            for (ClientEntity entity : entityIterator) {
                // Access entity properties as needed
                String value1 = entity.getProperty("ObjectNr").getValue().toString();
                if (value1.equals("IEIKLWARC")) {
                    System.out.println("Object Number 'IEIKLWARC' was found.");
                    for (ClientProperty prop : entity.getProperties()) {
                        Object propertyValue = prop.getName();
                        if (propertyValue.equals("EngineCap")) {
                            System.out.println("The value of 'EngineCap' property is: " + entity.getProperty(prop.getName()).getValue());
                        }
                    }
                }
                //iterate through each property of the current entity and read the property's name and value
                for (ClientProperty prop2 : entity.getProperties()) {
                    System.out.println(prop2.getName().toString() + ": " + entity.getProperty(prop2.getName()).getValue());
                }
                System.out.println("-----------------------");
            }
        } catch (Exception e) {
            System.out.println("Error while reading the properties of xBTLxTRP_TU_CFP entity: " + e.getMessage());
        }
    }

The $metadata for the odata service can be viewed by this link: https://github.com/alexgris/odate_edmx/blob/main/README.md?plain=1

So, the long story short, I need a solution on how to correctly implement the last step 4 (i.e. first to set the desired value for an entity property, and then to post changes).

0

There are 0 answers