1

How do you write a converter for an entity with a compound primary key?

I want to edit the entity with a URl such as `\edit_representative.xhtml?emp_id=12345&project_id=45

I'm using Omnifaces so I have something like this for single primary keys

<o:viewParam name="emp_id" value="#{projectRepEdit.projectRep}"
converter="#{projectRepConverter}"
converterMessage="Bad request. Unknown Project Representative." required="true"
requiredMessage="Bad request. Please use a link from within the system." />

but I how do pass the project_id the employee is working on to the converter

EDIT

Based on BalusC's answer I used the second option and have the following in my projectRepConverter Converter

String project_id = (String) component.getAttributes().get("project_id");
String emp_id= (String) component.getAttributes().get("emp_id");

ProjectRep prjRep = prjRepRepository.getByEmpIdAndProjectId(emp_id,project_id);
return prjRep;

and in the facelet

<f:metadata>
<o:viewParam name="project_id" value="#{projectRepEdit.project}"
    converter="#{projectConverter}"
    converterMessage="Bad request. Unknown Project." required="true"
    requiredMessage="Bad request. Please use a link from within the system." />

<o:viewParam name="badge" value="#{projectRepEdit.projectRep}"
    converter="#{projectRepConverter}"
    converterMessage="Bad request. Unknown Project Rep." required="true"
    requiredMessage="Bad request. Please use a link from within the system.">
    <f:attribute name="project_id" value="#{param.project_id}" />
    <f:attribute name="emp_id" value="#{param.emp_id}" />
</o:viewParam>
jeff
  • 3,618
  • 9
  • 48
  • 101

1 Answers1

2

Neither the <f:viewParam> nor the <o:viewParam> supports taking multiple parameters. You'd need to manually grab the parameter. I can think of two basic ways for this:

  1. Either grab it as request parameter in the converter:

    String project_id = FacesLocal.getRequestParameter(context, "project_id");
    

  2. Or pass it as component attribute:

    <o:viewParam ...>
        <f:attribute name="project_id" value="#{param.project_id}" />
    </o:viewParam>
    

    So that you can grab it as follows in the converter:

    String project_id = (String) component.getAttributes().get("project_id");
    

Which way to choose depends on the intented reusability of the converter. For instance, you can with the 2nd way rename the project_id attribute name to compound_key or so and make it reusable.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555