java action code help

Hi All you guru’s out there. I am not the greatest Java coder you’ve ever seen (in fact I’m total newbie) and I am stumped on some Java syntax that I’d like to ask help for.   My goal is to call the Irr function in the apache POI library. I’ve download the jar and put it in my userlib folder in my project. I’ve done all the export project to Eclipse stuff and I can find my java action in the code. I’m passing in a list of values (could be any length). In the object type in this list there are decimals as attribute values. I need to get the values from the list of objects, which I can do through the proxy methods. Of course there are type conversions I need to do (since what I want back is a decimal from the Java function. Got a lot of syntax ok I guess according to Eclipse, but where things go wrong is that I need to use the getIncome() proxy method, which by default probably returns a bigdecimal (I hope, I could not validate this since the Mendix call goes to getContext() the specific list object. I know I need to parse the list and get the double array of numbers I am looking to run the Irr function on. My declaration was double[] numbers; but that seems to be wrong. What you see in the code below is an Eclipse suggested correction but I have no clue if that is right either. Here is my code (which needs some improvement I am sure, and I hope you can help me with): // This file was generated by Mendix Studio Pro. // // WARNING: Only the following code will be retained when actions are regenerated: // - the import list // - the code between BEGIN USER CODE and END USER CODE // - the code between BEGIN EXTRA CODE and END EXTRA CODE // Other code you write will be lost the next time you deploy the project. // Special characters, e.g., é, ö, à, etc. are supported in comments.   package myfirstmodule.actions;   import com.mendix.systemwideinterfaces.core.IContext; import com.mendix.webui.CustomJavaAction; import com.mendix.systemwideinterfaces.core.IMendixObject; import java.math.BigDecimal; import;   public class IRR extends CustomJavaAction<java.math.BigDecimal> { private java.util.List<IMendixObject> __Values; private java.util.List<myfirstmodule.proxies.TestData> Values; private double numbers[];   public IRR(IContext context, java.util.List<IMendixObject> Values) { super(context); this.__Values = Values; }   @java.lang.Override public java.math.BigDecimal executeAction() throws Exception { this.Values = new java.util.ArrayList<myfirstmodule.proxies.TestData>(); if (__Values != null) for (IMendixObject __ValuesElement : __Values) this.Values.add(myfirstmodule.proxies.TestData.initialize(getContext(), __ValuesElement));         // BEGIN USER CODE numbers = null; for (int i=0;i<=Values.size();i++) { numbers[i]= Values.get(i).getIncome(); } double result = Irr.irr(numbers); return BigDecimal.valueOf(result); // END USER CODE }   /** * Returns a string representation of this action */ @java.lang.Override public java.lang.String toString() { return "IRR"; }   // BEGIN EXTRA CODE // END EXTRA CODE }
1 answers

Seems to my this is a viabke solution to your use case.

The Irr function needs an array of double values that you create by looping through the list of Values.

The the retrun is a double value and with the bigdecimal valueof method you’re getting the correct bigdecimal value to be returned. You might want to do something with the precision, for example with the scale method like below:

 BigDecimal.valueOf(d).setScale(2, RoundingMode.HALF_UP);

This makes sure that a value of 47.48111 is returned as 47.48, but that depends on what you want to do with the result of course.