1

Mojarra 2.1.5 / Java

I have two entities : i.e.

class Primary { String name; String age }
class Second { String dept; String hour }
...

In my managed bean I developed a function to generate PDF regarding my front-end prime-faces radio button (Primary or Second).

If I select in radio button the Primary option, the managed bean method will fire generatePDF() and inside the generatePDF I have :

Primary pr = new Primary();
pr.name = xxxxx;
pr.age = yyyyy;
...
...

But how can I do to re-utilize the same method generatePDF for both entities (Primary and Second ? I need to access both entity properties regarding my radio selection.

I need to instantiate the entities dynamically (Or I instantiate Primary or I intantiaty Second at a time)

Al2x
  • 1,001
  • 5
  • 26
  • 37
  • you mean you want to instantiate at a time one class as per your radio selection within a same method ? – Vivek Feb 10 '14 at 17:48
  • Is there any submit button also on click of which things will happen ? Or just by selecting radio button you want. – Vivek Feb 10 '14 at 18:22
  • Would you like to tag this question with JSF/PrimeFaces? – Tiny Feb 10 '14 at 19:07

2 Answers2

1

What about do something like this.

interface Pdfeable{  String writeToPDF();}
class Primary implements Pdfeable { String name; String age }
class Second impleaments Pdfeable { String dept; String hour }

Just override with the statements you want to send data to the PDF.

class Primary implements Pdfeable {
   String name; String age;
   public String writeToPDF(){
      return getName() +  "" + getAge();
   }
}

And write your code using the interface definition not concrete classes.

Koitoer
  • 18,778
  • 7
  • 63
  • 86
  • I have a lot of entity fields to use in pdf generator. NOt only two ones as in the sample. – Al2x Feb 10 '14 at 18:03
  • Consider use reflection to iterate over all the fields and still using interface . Check this http://stackoverflow.com/questions/3333974/how-to-loop-over-a-class-attributes-in-java – Koitoer Feb 10 '14 at 18:27
  • I got it with reflection. ty – Al2x Feb 11 '14 at 16:30
0

Assuming as per your question you need one class instantiation at a time as per radio button selection ,I would suggest you should create a valueChangeListener to your radio button.I have hardcoded the values of radio selectItem you can use any way either by bean-binding or hardcoded.

<h:selectOneRadio value="#{myBean.myRadioValue}" ... onclick="this.form.submit();" valueChangeListener="#{myBean.generatePDF}">
   <f:selectItem itemValue="Radio1" itemLabel="Primary-radio" />
    <f:selectItem itemValue="Radio2" itemLabel="Secondary-radio" />
   </h:selectOneRadio>

This code will submit the form where the radio button are contained when the onclick Javascript event is detected. On the server side, the action generatePDF will be executed. In this method, you can do your requiste as on Submit action getter and setter will be called and you can check which radio is selected by comparing using if () and do your stuff:

public void generatePDF(ValueChangeEvent evt) {
    if(getMyRadioValue.equals(Radio1)){
     Primary pr = new Primary();
pr.name = xxxxx;
pr.age = yyyyy;
}
else if(getMyRadioValue.equals(Radio2)){
 Secondary s = new Secondary();
s.dept = xxx;
s.hour = yyyy;}

... ...

}

Vivek
  • 895
  • 11
  • 23
  • no way. I have a lot of fields to be generated in pdf. This way Ill duplicate a lot of code. – Al2x Feb 10 '14 at 19:04
  • First please tell is this all after click of any Submit or just selection ? Also, please elaborate in your question exactly what you want it is not mentioned that you have numerous fields. Please edit and come again. – Vivek Feb 10 '14 at 19:09
  • if multiple fields are there then you have to use something like getting radioButton value by id from view to bean and in bean as per id selection you have to instantiate ,eg : Clazz c = Class.forName(ClassName); – Vivek Feb 10 '14 at 19:19