How can I read and write Decimal value in Java?

0
Hello everyone! I want to write a Java action. In the domain model the type off the attribute is Decimal, but in Java I have to set it as a BigDecimal. I want to write a Decimal Value from one entity to another entity. I found this https://community.mendix.com/link/space/other/questions/510 , but there is no option for decimal type. Which type is equivalent to Mendix decimal in Java? And how can I convert it to BigDecimal? And what should I have to import? An example code will be very helpful for me.
asked
3 answers
1

To set the value of one object to a second object look at the code below giving you 3 examples on how to achieve this. Example 1 via the proxies, example 2 via the IMendix objects and example 3 by first setting the value to an intermidiary variable. In none of the cases a type conversion is needed.

		// BEGIN USER CODE
		//Via the proxies
		Entity_2.setAttribute(Entity.getAttribute());
		Entity_2.commit();
		
		//via the IMendixobjects
		__Entity_2.setValue(getContext(), myfirstmodule.proxies.Entity_2.MemberNames.Attribute.toString(), __Entity.getValue(getContext(),myfirstmodule.proxies.Entity.MemberNames.Attribute.toString()));
		Core.commit(getContext(), __Entity_2);
		
		//first get the value and then set the value
		BigDecimal val = Entity.getAttribute();
		Entity_2.setAttribute(val);
		Entity_2.commit();
		
		return true;
		// END USER CODE

This example uses both the objects a input parameters for the java action.

In the example below the value is passed as decimal to the action and the target object is passed as well.

Entity_2.setAttribute(decimalValue);
Entity_2.commit();

The decimal inthe Mendix java action is apssed as a bigDecimal see the signature in the created class:
 

public Java_action(IContext context, IMendixObject Entity_2, java.math.BigDecimal decimalValue)

So again no need for type conversions

answered
0

You just use BigDecimal.

You can use the generated proxy classes to get and set the attributes from your entity. These contain generated methods to handle the attributes in an entity so you don’t have to write them yourself. You can find them in the javasource/<module>/proxies/ folder.

So if you have an entity called MyEntity in MyFirstModule and that has an attribute called MyDecimal you would first import the proxy class
 

import myfirstmodule.proxies.MyEntity;


Then, assuming you have an instance of MyEntity called myEntity and you want to set the MyDecimal attribute with the value of a BigDecimal variable called myBigDecimal, you could do the following.
 

myEntity.setMyDecimal(myBigDecimal);


I hope this helps. Good luck!

answered
0

Thank you for your answer!

I tried it, it worked with a single attribute, but I dont know how can I do this, when I looping through the attributes. 

		// BEGIN USER CODE
		//throw new com.mendix.systemwideinterfaces.MendixRuntimeException("Java action was not implemented");

		IContext ctx = this.getContext();

		ArrayList<IMendixObject> NewRatesCurrencyList = new ArrayList<IMendixObject>();

		Map<String, ? extends IMendixObjectMember<?>> members = this.__RatesOld.getMembers(this.getContext());

		Set<String> mapping = members.keySet();

		for(String key : mapping) {
			IMendixObjectMember<?> member = members.get(key);

			if( member instanceof com.mendix.core.objectmanagement.member.MendixObjectReference ) {

			} else if( member instanceof com.mendix.core.objectmanagement.member.MendixObjectReferenceSet ) {

			} else{
				RatesCurrency NewRatesCurrency = new RatesCurrency(ctx);
				NewRatesCurrency.setNameCode(key);

				// Here I want to get the Value by the key
				// fo example like this, but looping trought the keys
				BigDecimal valBD2 = this.RatesOld.getHUF(RatesOld.getContext());

				NewRatesCurrency.setValue(this.RatesOld.getContext(),valBD2);
				NewRatesCurrencyList.add(NewRatesCurrency.getMendixObject());
			}
		}
		return NewRatesCurrencyList;

		// END USER CODE

 

answered