Jump to content

Referencing Enums in JRs - Newbie Question


dogmac

Recommended Posts

Hi everyone,

 

I am new to Jasper Reports and really like things thus far, but am currently stuck. I haven't been able to find a similar query in the archives.

 

Our database is recording enum values, enums like:

 

public enum Status {

active,

inactive,

closed,

deleted;

 

public String getLabel() {

String key = "status_"+this.toString();

try {

return Messages.instance().get(key).toString();

} catch (Exception e) {

return key;

}

}

 

public static List<Status> valuesAsList() {

return Arrays.asList(Status.values());

}

 

So what is recorded is just a BigDecimal value.

 

How do I translate this on a report back to something that the user can understand.

 

I am importing the class in which the enum lives, and trying to set either a variable or a field with the details from there:

 

eg:

 

<import value="net.sf.jasperreports.engine.data.*" />

<import value="com.ibas.domain.Service" />

 

 

<queryString><![CDATA[select s.type from AMT from service_t s where...]]></queryString>

 

<field name="TYPE" class="com.ibas.domain.Service"/>

 

<variable name="ENUMType" class="java.lang.String" resetType="Report" calculation="Nothing">

<variableExpression><![CDATA[$F{TYPE}.getLabel()]]></variableExpression>

</variable>

 

 

I think logically I should have access to the Class declarations, but I can't work out exactly how to do it?

 

I hope this makes sense.

 

Thanks

 

Dianne

Link to comment
Share on other sites

  • Replies 8
  • Created
  • Last Reply

Top Posters In This Topic

Hi Lucian,

 

Thankyou for your reply.

 

Hopefully I'll be able to clarify.

 

No, the Datasource doesn't take care of it. What we thought we would be able to do (and please tell me if we are wrong) is to import that class with the enums into the jrxml file and then use some of the existing procedures from there.

 

So, we would want to import it and then use $F{TYPE}.getLabel, which would convert the bigDecimal to a string.

 

Is this possible?

 

Thanks

 

Dianne

Link to comment
Share on other sites

  • 1 month later...

I too am a beginner at this and I am having trouble referencing enums from a class.

 

we are using a bean JRBeanCollectionDataSource. Each bean has a get and set Status that returns type Status, type Status is an enum. Once I have the Status there is a toString() that will return my string for reporting. Below is my field and detail band declarations. what I am missing is what I should set the value of field.setValueClass to.

 

// Add field to list of fields

field = new JRDesignField();

field.setName("status");

field.setValueClass(?????);

jasperDesign.addField(field);

 

 

// Add the field to the detail band.

textField = new JRDesignTextField();

expression = new JRDesignExpression();

expression.setValueClass(java.lang.String.class);

expression.setText("$F{status}.toString()");

textField.setExpression(expression);

detailBand.addElement(textField);

Link to comment
Share on other sites

Ok that got me past my first hurdle but now I am using an enum which also has a method declared that I need to use.

 

expression.setText("$F{status}.methodInEnum()");

 

In this case setting the class to enum gives me the error "cannot find symbol methodInEnum()". what would be the correct class to set so that I can access the methods in the enum declaration? I tried the class containing the enum but of course that did not work.

Link to comment
Share on other sites

Thank you so very much for your help, these things are driving me nuts at this point. Yes that is what I thought should work and in fact after playing with it some more I was able to determine that it does work fine if I am loading from an xml file but if I am creating my report from a jasperDesign then using the class of the enum like you suggested does not work. I explain this in more detail in a new thread "Problem with enums in JasperDesign but not file".
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...