I have six types of Project objects, each defined by its type (Enum). Each project type has it's own backing bean (Project1Bean, Project2Bean, etc...). After selecting a project from a list I navigate to a page selectedProject.xhtml where I would like to initialize to particular backing bean by the project type. For now I had a single bean which loads the project from the DB and this bean is initialized through the preRenderView event. Then there are a lot of methods in the bean, that always check the project type and perform some actions. I was wondering if there is some better way to initialize the particular project bean by the project type. I was thinking about having a SelectedProjectBean which in the preRenderView method loads the project and initializes the particular project bean. But I don't like this solution very much.
class SelectedProjectBean {
Project project;
@Inject
Project1Bean project1Bean;
@Inject
Project2Bean project2Bean;
public void preRenderView() {
project = loadProject();
switch(project.getType()) {
case Type1:
project1Bean.preRenderView();
break;
case Type2:
project2Bean.preRenderView();
break;
}
}
and in the selectedProject.xhtml
<ui:define name="metadata">
<f:metadata>
<f:event type="preRenderView" listener="#{selectedProjectBean.preRenderView}"/>
</f:metadata>
</ui:define>
Isn't it possible to have an interface for the projectbeans and use some qualifiers and have only one named bean for projects? I.e. let the container decide which bean would it initialize?