0 Replies Latest reply on Oct 27, 2012 3:18 AM by ramakrishna62

    Problem with synchronization in datamodel

    ramakrishna62 Newbie

      Hi all,

                 I was facing one issue with the datamodel. If 2 or more persons accessing then menu item which i had developed using datamodel then it is taking too long time to open in production server. May i know what is the reason for this and how i can resolve this problem.

       

      Here is my code how i used the datamodel:

       

      @Name("myDataModel")

      public class MyDataModel extends PaginatingDataModel<MyBean, Long>{

       

                @Override

                public List<MyBean> findObjects(int arg0, int arg1, String arg2,

                                    boolean arg3) {

                          // TODO Auto-generated method stub

                          return null;  // Here retreiving List<MyBean>

                }

            @Override

                public String getDefaultSortField() {

                          // TODO Auto-generated method stub

                          return null;

                }

            @Override

                public Long getId(MyBean arg0) {

                          // TODO Auto-generated method stub

                          return null;

                }

            @Override

                public int getNumRecords() {

                          // TODO Auto-generated method stub

                          return 0;

                }

            @Override

                public MyBean getObjectById(Long arg0) {

                          // TODO Auto-generated method stub

                          return null;

                }

      }

       

      myModel.jsp

      ==========

       

      In this page am using datamodel as follows:

       

      <rich:dataTable id="resultsList"  value="#{myDataModel}" var="item"   width="500px" rowClasses="rich-table-oddRow,rich-table-evenRow" rows="20">

                               <f:facet name="footer">

                                                        <rich:datascroller align="left" for="resultsList" maxPages="20" boundaryControls="auto" fastControls="auto" stepControls="auto" renderIfSinglePage="false" />

                                     </f:facet>

                                     <f:facet name="header">

                                               <rich:columnGroup>

                                       <rich:column width="5%"> <h:outputText value="Id"/> </rich:column>

                                       <rich:column width="10%"> <h:outputText value="Name"/> </rich:column>

                                       <rich:column width="1%"> <h:outputText value="Date" /> </rich:column>

                                   </rich:columnGroup>

                               </f:facet>  

                                       <rich:column> <h:outputText value="#{item.id}"/> </rich:column>

                     <rich:column> <h:outputText value="#{item.name}"/> </rich:column>

                     <rich:column> <h:outputText value="#{item.date}"/> </rich:column>

      </rich:dataTable>

       

      MyBean.java

      ==========

       

      Class MyBean {

           private long id;

           private String name;

           private Date date;

                 // All getters and setters added

      }

       

       

      PaginatingDataModel.java

      =====================

       

      package com.sec.portal.data;

       

       

      import java.io.IOException;

      import java.util.ArrayList;

      import java.util.HashMap;

      import java.util.List;

      import java.util.Map;

       

       

      import javax.faces.context.FacesContext;

       

       

      import org.ajax4jsf.model.DataVisitor;

      import org.ajax4jsf.model.Range;

      import org.ajax4jsf.model.SequenceRange;

      import org.ajax4jsf.model.SerializableDataModel;

       

       

      /**

      * @param <T>

      * @param <U>

      */ 

      public abstract class PaginatingDataModel<T, U> extends SerializableDataModel {

       

       

          /** */

        

          /** */

          protected U currentPk;

          /** */

          protected boolean descending = true;

          /** */

          protected String sortField = getDefaultSortField();

          /** */

          protected boolean detached = false;

          /** */

          protected List<U> wrappedKeys = new ArrayList<U>();

          /** */

          protected Integer rowCount;

          /** */

          protected Map<U, T> wrappedData = new HashMap<U, T>();

         

          protected int rowIndex;

       

       

          /**

           * @see org.ajax4jsf.model.ExtendedDataModel#getRowKey()

           */

          @Override

          public Object getRowKey()

          {

              return currentPk;

          }

       

       

          /**

           * @see org.ajax4jsf.model.ExtendedDataModel#setRowKey(java.lang.Object)

           */

          @SuppressWarnings("unchecked")

          @Override

          public void setRowKey(final Object key)

          {

              this.currentPk = (U) key;

          }

       

       

          /**

           * @see org.ajax4jsf.model.SerializableDataModel#update()

           */

          @Override

          public void update()

          {

              if (getSortFieldObject() != null)

              {

                  final String newSortField = getSortFieldObject().toString();

                  if (newSortField.equals(sortField))

                  {

                      descending = !descending;

                  }

                  sortField = newSortField;

              }

              detached = false;

          }

       

       

          /**

           * @return Object

           */

          protected Object getSortFieldObject()

          {

              final FacesContext context = FacesContext.getCurrentInstance();

              final Object sortFieldObject = context.getExternalContext().getRequestParameterMap().get("sortField");

              return sortFieldObject;

          }

       

       

          /**

           * @param sortField

           */

          public void setSortField(final String sortField)

          {

              if (this.sortField.equals(sortField))

              {

                  descending = !descending;

              } else

              {

                  this.sortField = sortField;

              }

          }

       

       

          /**

           * @return String

           */

          public String getSortField()

          {

              return sortField;

          }

       

       

          /**

           * @see org.ajax4jsf.model.ExtendedDataModel#getSerializableModel(org.ajax4jsf.model.Range)

           */

          @Override

          public SerializableDataModel getSerializableModel(final Range range)

          {

              if (wrappedKeys != null)

              {

                  detached = true;

                  return this;

              }

              return null;

          }

       

       

          /**

           * @see javax.faces.model.DataModel#setRowIndex(int)

           */

          @Override

          public void setRowIndex(final int rowIndex)

          {

                    this.rowIndex = rowIndex;

         

      //        throw new UnsupportedOperationException();

       

       

          }

       

       

          /**

           * @see javax.faces.model.DataModel#setWrappedData(java.lang.Object)

           */

          @Override

          public void setWrappedData(final Object data)

          {

              throw new UnsupportedOperationException();

       

       

          }

       

       

          /**

           * @see javax.faces.model.DataModel#getRowIndex()

           */

          @Override

          public int getRowIndex()

          {

                    return this.rowIndex;

              //throw new UnsupportedOperationException();

          }

       

       

          /**

           * @see javax.faces.model.DataModel#getWrappedData()

           */

          @Override

          public Object getWrappedData()

          {

              throw new UnsupportedOperationException();

          }

       

       

          /**

           * @see org.ajax4jsf.model.ExtendedDataModel#walk(javax.faces.context.FacesContext,

           *      org.ajax4jsf.model.DataVisitor, org.ajax4jsf.model.Range,

           *      java.lang.Object)

           */

          @Override

          public void walk(final FacesContext context, final DataVisitor visitor, final Range range, final Object argument)

                  throws IOException

          {

              final int firstRow = ((SequenceRange) range).getFirstRow();

              final int numberOfRows = ((SequenceRange) range).getRows();

              if (detached && getSortFieldObject() != null)

              {

                  for (final U key : wrappedKeys)

                  {

                      setRowKey(key);

                      visitor.process(context, key, argument);

                  }

              } else

              { // if not serialized, than we request data from data

                  // provider

                  wrappedKeys = new ArrayList<U>();

                  for (final T object : findObjects(firstRow, numberOfRows, sortField, descending))

                  {

                      wrappedKeys.add(getId(object));

                      wrappedData.put(getId(object), object);

                      visitor.process(context, getId(object), argument);

                  }

              }

          }

       

       

          /**

           * @see javax.faces.model.DataModel#isRowAvailable()

           */

          @Override

          public boolean isRowAvailable()

          {

              if (currentPk == null)

              {

                  return false;

              }

              if (wrappedKeys.contains(currentPk))

              {

                  return true;

              }

              if (wrappedData.entrySet().contains(currentPk))

              {

                  return true;

              }

              try

              {

                  if (getObjectById(currentPk) != null)

                  {

                      return true;

                  }

              } catch (final Exception e)

              {

       

       

              }

              return false;

          }

       

       

          /**

           * @see javax.faces.model.DataModel#getRowData()

           */

          @Override

          public Object getRowData()

          {

              if (currentPk == null)

              {

                  return null;

              }

       

       

              T object = wrappedData.get(currentPk);

              if (object == null)

              {

                  object = getObjectById(currentPk);

                  wrappedData.put(currentPk, object);

              }

              return object;

          }

       

       

          /**

           * @see javax.faces.model.DataModel#getRowCount()

           */

          @Override

          public int getRowCount()

          {

              if (rowCount == null)

              {

                  rowCount = getNumRecords();

              }

              return rowCount;

          }

       

       

          /**

           *

           * @param object

           * @return U

           */

          public abstract U getId(T object);

       

       

          /**

           * @param firstRow

           * @param numberOfRows

           * @param sortField

           * @param descending

           * @return List<T>

           */

          public abstract List<T> findObjects(int firstRow, int numberOfRows, String sortField, boolean descending);

       

       

          /**

           * @param id

           * @return T

           */

          public abstract T getObjectById(U id);

       

       

          /**

           * @return String

           */

          public abstract String getDefaultSortField();

       

       

          /**

           * @return int

           */

          public abstract int getNumRecords();

         

      }