dogmac Posted February 1, 2007 Share Posted February 1, 2007 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 More sharing options...
lucianc Posted February 2, 2007 Share Posted February 2, 2007 It's not clear from your post what exactly does not work. Does your data source take care of converting the BigDecimal value from the DB to a Status value? Regards,Lucian Link to comment Share on other sites More sharing options...
dogmac Posted February 5, 2007 Author Share Posted February 5, 2007 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 More sharing options...
lucianc Posted February 6, 2007 Share Posted February 6, 2007 I'm sorry, I still don't understand what you need or what doesn't work. If the data source you are using is not able to provide a Service value for your field, then you should not declare the type of your field as Service. Regards,Lucian Link to comment Share on other sites More sharing options...
hmorris1970 Posted March 27, 2007 Share Posted March 27, 2007 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 fieldsfield = 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 More sharing options...
hmorris1970 Posted March 27, 2007 Share Posted March 27, 2007 Got it :-) field.setValueClass(java.lang.Enum.class); Link to comment Share on other sites More sharing options...
hmorris1970 Posted March 27, 2007 Share Posted March 27, 2007 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 More sharing options...
lucianc Posted March 27, 2007 Share Posted March 27, 2007 You should simply use the enum class as field type, e.g. field.setValueClass(java.util.concurrent.TimeUnit.class);. HTH,Lucian Link to comment Share on other sites More sharing options...
hmorris1970 Posted March 28, 2007 Share Posted March 28, 2007 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now