2

I use netbeans 7.2.1. Is JSF CRUD Generated code efficient for real applications? I have created a test database and used netbeans CRUD generator. It uses DataModel and PaginationHelper instead of Lists for CRUD operations. There is an Entity Test.java, a TestFacade.java and TestController.java. and jsf files list.xhtml, edit.xhtml, view.xhtml, create.xhtml. I added a NemdQuery to entity file:

@Entity
@NamedQuery(name = "Test.findByTestCriteria", query = "SELECT t FROM Test t WHERE t.testCriteria = true")
public class Test implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue    
    @Column(name = "id")
    private Integer id;    
    @Column(name = "title")
    private String title;
    @Column(name = "testCrieteria")
    private boolean testCrieteria; 

public Test() {
}

//Getters and Setters

And created the query in TestFacade.java:

@Stateless
public class TestFacade extends AbstractFacade<Test> {
    @PersistenceContext(unitName = "testPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
    return em;
}

public TestFacade() {
    super(Test.class);
}

public List<Test> testCriteria(){
    Query q = em.createNamedQuery("Test.findByTestCriteria",Test.class);        
    return q.getResultList();
}    
}

And I have added a method in TestController.java to retrieve testCriteria Query:

@ManagedBean(name = "testController")
@SessionScoped
public class TestController implements Serializable {

    private Test current;
    private DataModel items = null;
    private DataModel testCriteria = null;
    @EJB
    private com.test.TestFacade ejbFacade;
    private PaginationHelper pagination;
    private int selectedItemIndex;

    public Test getSelected() {
    if (current == null) {
        current = new Test();
        selectedItemIndex = -1;
    }
    return current;
}

public PaginationHelper getPagination() {
    if (pagination == null) {
        pagination = new PaginationHelper(10) {
            @Override
            public int getItemsCount() {
                return getFacade().count();
            }

            @Override
            public DataModel createPageDataModel() {
                return new ListDataModel(getFacade().findRange(new int[]{getPageFirstItem(), getPageFirstItem() + getPageSize()}));
            }
        };
    }
    return pagination;
}

public DataModel getItems() {
    if (items == null) {
        items = getPagination().createPageDataModel();
    }
    return items;
}
public String prepareList() {
    recreateModel();
    return "List";
}

public String prepareView() {
    current = (Test) getItems().getRowData();
    selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
    return "View";
}
//getting testCriteria items
public DataModel getTestCriteria(){                
    if(items == null){
        items = getPagination().createPageDataModel();
    }
    testCriteria = new ListDataModel(ejbFacade.testCriteria());         
    return testCriteria;        
}
//custom view page
public String viewTest(){
    current = (Test) getItems().getRowData();
    selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
    return "ViewTest?faces-redirect=true";
}
//custom viewTestCriteria
public String viewTestCriteria(){
    current = (Test) getTestCriteria ().getRowData();
    selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
    return "ViewTest?faces-redirect=true";
}

And Retrieving testCriteria in a p:dataGrid in index.xhtml:

<h:form>
                        <p:dataGrid value="#{testController.testCriteria()}" var="item" columns="4">                                
                            <p:column>
                                <h:panelGrid columns="1">                                                                                
                                    <h:commandLink id="viewTestCriteria" value="#{item.title}" action="#{testController.viewTestCriteria()}"/>
                                </h:panelGrid>
                            </p:column>
                        </p:dataGrid>
                    </h:form>

With this code all testCriteria data are there in index.xhtml but when I click the commadnButton to view them they all show the first Item. It seems that in the DataGrid it doesn't get the selected item. And if I refresh the List.xhtml which contains all test data, and then coming back to index.xhtml and pressing commandLink it throws a NoRowAvailable exception.

I hope I have stated my question clearly and I would appreciate any guide because I am new to this technology.

Update: After googling and research for two days and thinking I thought of using <f:setPropertyActionListener value="#{item.id}" target="#{testController.selected.id}"> and <f:param name="#{testController.selected.id}" value="#{item.id}"> but it didn't work.

Update: So far I'm almost confident that no row is getting selected so the data grid returns first row. But I'm still not sure how to modify viewTestCriteria() to set the current item and selectedItemIndex correctly.

0 Answers0