i'm trying to use jsf and jdo and i have wrote a small application when i can insert,delete,modify a dataTable.
I use also a button that clear the form and leave only one row so i can use it for search .
The button have immediate set to true because i don't want syntatic control on the action.
I obtain only one row after the action but the fields contains the value of the previous page.
I have wrote a log in the render fase and the value of the model seems correct.
I think that the rendering phase get the value submitted and not the value of the model.
I get the last nigth build because i supposed that the problem was tied to the issue 187 but
the problem continue.
This is my code :
 
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://myfaces.sourceforge.net/tld/myfaces_ext_0_9.tld" prefix="x" %>
<f:loadBundle basename="it.poker.jsf.test.messages" var="msgs"/>
<h:form>
 <h:panelGrid columns="1">
  <h:dataTable id="data" value="#{pmDao.atts}" var="tts" >
   <h:column>
    <f:facet name="header">
     <h:outputText value="#{msgs.page1_label_codice}"/>
    </f:facet>
    <h:inputText id="codiceId"
     readonly="#{not tts.modif}" size="10"
     value="#{tts.codice}" required="#{not pmDao.modRicerca}"/>
    <h:message for="" style="color: red;"/>
   </h:column>
   <h:column>
    <f:facet name="header">
     <h:outputText value="#{msgs.page1_label_descrizione}"/>
    </f:facet>
    <h:inputText size="50" value="#{tts.descrizione}" />
   </h:column>
   <h:column rendered="#{not pmDao.modRicerca}">
    <h:selectBooleanCheckbox id="chkDeleted"  value="#{tts.toDelete}"/>
    <h:outputText value="#{msgs.page1_label_checkbox_deleted}"/>
   </h:column>
   <h:column rendered="#{not pmDao.modRicerca}">
    <h:commandLink action="">
     <h:outputText value="#{msgs.page1_label_link_aggiungi}"/>
    </h:commandLink>
   </h:column>
  </h:dataTable> 
  <h:panelGroup id="commandGroup">
   <h:commandButton value="#{msgs.page1_label_button_conferma}"
    action="" rendered="#{not pmDao.modRicerca}"/>
   <h:commandButton value="#{msgs.page1_label_button_ricerca}"
    action=""  rendered="#{not pmDao.modRicerca}"
    immediate="true" />
   <h:commandLink action="" rendered="#{pmDao.showPrev}" >
    <h:graphicImage alt="#{msgs.page1_label_button_prev_page}"
      value="images/arrow-previous.gif"/>        
   </h:commandLink>
   <h:commandLink action="" rendered="#{pmDao.showNext}" >
    <h:graphicImage alt="#{msgs.page1_label_button_next_page}"
      value="images/arrow-next.gif"/>    
   </h:commandLink>
   <h:commandButton value="#{msgs.page1_label_button_conferma_ricerca}"
    action=""  rendered="#{pmDao.modRicerca}"/>   
  </h:panelGroup>
 </h:panelGrid>
</h:form>
 
/*
 * Created on 29-mag-2005
 *
 */
package it.poker.jsf.test;
 
import it.poker.jdo.test.TestTable;
 
import java.util.Calendar;
import java.util.Collection;
import javax.faces.application.Application;
import javax.faces.context.FacesContext;
import javax.faces.el.VariableResolver;
import javax.faces.model.ArrayDataModel;
import javax.faces.model.DataModel;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;
import javax.jdo.Transaction;
 

/**
 * @author claudiot
 *
 */
public class PMDao {
 private TestTable[] atts;
 private int curPage = 0;
 private ArrayDataModel model = null;
 private boolean modRicerca = false;
 public PMDao() {
  execQuery(null);
 }
 
 public DataModel getAtts() {  
     System.out.println("begin metod getAtts ");
     TestTable[] latts = (TestTable[])model.getWrappedData();
     System.out.println("cod elemen 0 = ?" + latts[0].getCodice() + "?");
  return model;
 }
 
 public String confermaDati() 
 {
     System.out.println("inizio metodo confermaDati ");
  FacesContext context = FacesContext.getCurrentInstance();
  Application app = context.getApplication();
  VariableResolver vr = app.getVariableResolver();
  PMFactory pmfMB = (PMFactory)vr.resolveVariable(context,"pmFactory");
  if (pmfMB != null) {
   PersistenceManagerFactory pmf = pmfMB.getPMF();
   PersistenceManager pm = pmf.getPersistenceManager();
   Calendar c = Calendar.getInstance();   
         System.out.println(c.getTime().toLocaleString() + " obtained persistent manager");
         Transaction tx=pm.currentTransaction();
         c = Calendar.getInstance();
         System.out.println(c.getTime().toLocaleString() + " obtained transaction object");
         try
         {
             tx.begin();
    c = Calendar.getInstance();   
          System.out.println(c.getTime().toLocaleString() + " open transaction");
    updFromModel();
    pm.attachCopyAll(atts,true);
          for (int counter=0;counter<atts.length;counter++) {
           if (atts[counter].getToDelete()) {
      javax.jdo.identity.StringIdentity id =
       new javax.jdo.identity.StringIdentity(TestTable.class,
         atts[counter].getCodice());
      Object objl = pm.getObjectById(id);
            pm.deletePersistent(objl);
            System.out.println("try to delete " + atts[counter].getCodice());
           }          
          }
             tx.commit();
    c = Calendar.getInstance();   
          System.out.println(c.getTime().toLocaleString() + " close transaction");
         }
         finally
         {
             if (tx.isActive())
             {
                 tx.rollback();
             }
             pm.close();
          System.out.println("persistent manager closed");   
          TestTable tts = new TestTable("","");
          TestTable[] latts = new TestTable[1];
          latts[0] = tts;
          atts = latts;
          model.setWrappedData(latts);
          curPage = 0;
         } 
  }     
     return null;
 }
 
 public String addRecord() {
  int modelIndex = model.getRowIndex();
  int index = modelIndex + (curPage * 5);
     System.out.println("selezionato indice " + index);
     updFromModel();
     TestTable[] newRecords = new TestTable[atts.length + 1];
     int counterBefore;
     for (counterBefore = 0;counterBefore<=index;counterBefore++) {
      newRecords[counterBefore] = atts[counterBefore];
     }
     newRecords[counterBefore] = new TestTable("","");
     for (int counter=counterBefore;counter<atts.length;counter++) {
      newRecords[counter + 1] = atts[counter];
     }
     atts = newRecords;
     if (modelIndex == 4) {
      curPage = curPage + 1;
     }
     setModel();
  return null;
 }
 
 public boolean getModRicerca(){
  return modRicerca;
 }
 
 public void setModRicerca(boolean modRicerca) {
  this.modRicerca = modRicerca;
 }
 
 public String showFormRicerca() {
  modRicerca = true;
        System.out.println("inizio show form ricerca");   
     TestTable tts = new TestTable(" "," ");
     TestTable[] latts = new TestTable[1];
     latts[0] = tts;
     atts = latts;
     curPage = 0;
     setModel();
  FacesContext context = FacesContext.getCurrentInstance();
  return null;
 }
 
 public String confermaRicerca() {
  String filter = "";
  modRicerca = false;  
  TestTable[] latts = (TestTable[])model.getWrappedData();
  if (latts.length > 0 && latts[0] != null) {
   TestTable tts = latts[0];
   if (tts.getCodice() != null &&
    tts.getCodice().length() > 0) {
    filter = "codice like '%" + tts.getCodice() + "%'";
   }
   if (tts.getDescrizione() != null &&
     tts.getDescrizione().length() > 0) {
     if (filter != null & filter.length() > 0) {
      filter = filter + " and descrizione like '%" + tts.getDescrizione() + "%'";
     } else {
      filter = "descrizione like '%" + tts.getDescrizione() + "%'";
     }
    }
  }
  execQuery(filter);
  return null;
 }
 
 private void setModel() {
  int min = curPage * 5;
  int max = min + 5;
  if (max > atts.length) {
   max = atts.length;
  }
  TestTable[] latts = new TestTable[max - min];
  for (int index=0;index<latts.length;index++) {
   latts[index] = atts[min + index];
  }
        model = new ArrayDataModel(latts);  
 }
 
 private void updFromModel() {
  int min = curPage * 5;
  TestTable[] latts = (TestTable[])model.getWrappedData();
  for (int index=0;index<latts.length;index++) {
   atts[min + index] = latts[index];
  }
 }
 
 public String nextPage() {
  if (((curPage + 1) * 5) < atts.length) {
   updFromModel();
   curPage++;
   setModel();
  }
  return null;
 }
 
 public String prevPage() {
  if (curPage >= 1) {
   updFromModel();
   curPage = curPage -1;
   setModel();   
  }
  return null;
 }
 
 private void execQuery(String filter) {
  FacesContext context = FacesContext.getCurrentInstance();
  Application app = context.getApplication();
  VariableResolver vr = app.getVariableResolver();
  PMFactory pmfMB = (PMFactory)vr.resolveVariable(context,"pmFactory");
  if (pmfMB != null) {
   PersistenceManagerFactory pmf = pmfMB.getPMF();
   PersistenceManager pm = pmf.getPersistenceManager();
   Calendar c = Calendar.getInstance();   
         System.out.println(c.getTime().toLocaleString() + " obtained persistent manager");
         Transaction tx=pm.currentTransaction();
         c = Calendar.getInstance();
         System.out.println(c.getTime().toLocaleString() + " obtained transaction object");
   Collection tts = null;            
         try
         {
             tx.begin();
    c = Calendar.getInstance();   
          System.out.println(c.getTime().toLocaleString() + " open transaction");
          String sqlQuery = null;
          if (filter != null && filter.length() > 0) {
           sqlQuery = "select * from testtable where " +
      filter + " order by codice";
          } else {
           sqlQuery = "select * from testtable order by codice";
          }
          Query q=pm.newQuery("javax.jdo.query.SQL",
            sqlQuery);
          q.setClass(it.poker.jdo.test.TestTable.class);
             Collection query_tts = (Collection)q.execute();
             tts = pm.detachCopyAll(query_tts);         
             tx.commit();
    c = Calendar.getInstance();   
          System.out.println(c.getTime().toLocaleString() + " close transaction");
         }
         finally
         {
             if (tx.isActive())
             {
                 tx.rollback();
             }
             pm.close();
          System.out.println("persistent manager closed");   
         } 
         atts  = (TestTable[]) tts.toArray(new TestTable[0]);
         if (atts.length > 0) {
          for (int counter=0;counter<atts.length;counter++) {
           atts[counter].setModif(false);
           atts[counter].setIndex(counter);
          }
         } else {
          TestTable vtts = new TestTable(" "," ");
          TestTable[] vatts = new TestTable[1];
          vatts[0] = vtts;
          vtts.setIndex(0);
          atts = vatts;          
         }
         setModel();
         curPage = 0;
  }  
 }
 
 public boolean getPaging() { 
  System.out.println("inizio get paging");
  boolean needPaging =(atts.length > 5);
  return (needPaging && !modRicerca);
 }
 
 public boolean getShowNext(){
  boolean needPaging =(atts.length > 5);
  boolean isLastPage = false;
  if (!(((curPage + 1) * 5) < atts.length)) {
   isLastPage = true;
  }
  return (needPaging && !modRicerca && !isLastPage);  
 }
 
 public boolean getShowPrev() {
  boolean needPaging =(atts.length > 5);
  boolean isFirstPage = false;
  if (curPage < 1) {
   isFirstPage = true;
  }
  return (needPaging && !modRicerca && !isFirstPage);    
 }
}
 

Reply via email to