1 2 Previous Next 16 Replies Latest reply on Aug 23, 2009 7:01 PM by Nick Belaevski

    ExtendedDataModel row deletion

    Antony Antony Newbie

      Hi

      I try to use extended data model, I would like to delete a datatable row without database hint.


      <rich:dataTable id="tableEstablishment" value="#{model}" var="establishments" rows="10" >
      <a4j:commandButton ajaxSingle="true" id="editlink" value="d" action="#{model.test}" reRender="tableEstablishment">
      <f:setPropertyActionListener value="#{establishments}" target="#{model.testAjax}" />
      </a4j>
      </rich:dataTable>


      If i click on my button "d", I execute the method test, I can get the selected row to delete with my testAjax parameter, so i can delete it.

      After the system calls the "walk" method of my extended datamodel and it will call a database hint, how can I avoid this database hint ?

        • 1. Re: ExtendedDataModel row deletion
          Ilya Shaikovsky Master

          by implementing the model according to requirement. check livedemo samples. there are two data models samples present.

          • 2. Re: ExtendedDataModel row deletion
            Antony Antony Newbie

            That's what I'm trying to do


            public void walk(FacesContext context, DataVisitor visitor, Range range,
            Object argument) throws IOException {
            int firstRow = ((SequenceRange)range).getFirstRow();
            int numberOfRows = ((SequenceRange)range).getRows();
            wrappedKeys = new ArrayList<Integer>();

            //here we must call a db connection
            for (Establishment item:establishmentDao.findAll()) {
            wrappedKeys.add(item.getIdestablishmentEst());
            wrappedData.put(item.getIdestablishmentEst(), item);
            visitor.process(context, item.getIdestablishmentEst(), argument);
            }
            }


            This is my walk method. When I click on my delete button I get my selected row through

            <f:setPropertyActionListener value="#{establishments}"
            target="#{model.testAjax}" />
            so I can delete this selected row with my dataProvider.

            After this database deletion the framework calls the walk method to reRender the datatable, my wrappedKeys and wrappedData parameters are null, so it will call my dataProvider to rerender the datatable

            I would want to avoid this database hint because rows are already displayed and remove only my selected row from the grid

            Thanks for your help

            • 3. Re: ExtendedDataModel row deletion
              Nick Belaevski Master

              Hi,

              Cache data in model/bean to avoid database hit.

              • 4. Re: ExtendedDataModel row deletion
                Antony Antony Newbie

                The only solution I see to cache my data is to put them in session because my bean scope is request.

                Another way is to delete the client row with javascript I prefer avoid this way

                Tell me if I am wrongn thanks for your help.

                • 5. Re: ExtendedDataModel row deletion
                  Ilya Shaikovsky Master

                  again. caching data showed fine at richfaces-demo.

                  and b.t.w. deletion with java script isn't a solution at all.

                  • 6. Re: ExtendedDataModel row deletion
                    Antony Antony Newbie

                     


                    private AuctionDataProvider dataProvider;
                    private Integer currentPk;
                    private Map<Integer,AuctionItem> wrappedData = new HashMap<Integer,AuctionItem>();
                    private List<Integer> wrappedKeys = null;


                    My data is cached here in my extended data model like the extendedDataModel demo.

                    My model must be serialized with this method

                    public SerializableDataModel getSerializableModel(Range range);

                    till now it's ok.

                    If I change my datatable page, on the server side my cache parameters (wrappedData wrappedKeys) are filled by the framework
                    BUT when i click on my row button, I come back on the server side, wrappedData wrappedKeys are null, that's why I don't understand

                    • 7. Re: ExtendedDataModel row deletion
                      Nick Belaevski Master

                       

                      "totoranky2" wrote:
                      The only solution I see to cache my data is to put them in session because my bean scope is request.

                      Another way is to delete the client row with javascript I prefer avoid this way

                      Tell me if I am wrongn thanks for your help.

                      You can cache data in request bean, so that data stored there will be used for rendering phase. Then you'll get single DB access instead of two - for "execute" and "render" parts of lifecycle.

                      • 8. Re: ExtendedDataModel row deletion
                        Antony Antony Newbie

                         


                        You can cache data in request bean, so that data stored there will be used for rendering phase. Then you'll get single DB access instead of two - for "execute" and "render" parts of lifecycle.


                        I agree with you and I understand, but when I click on my deletion button, I can't get my old cached values, my bean parameters (wrappedData & wrappedKeys) are null, therefore I can't avoit this second DB access for the render phase.

                        On the other side, when I change my grid page, on the server my cached parameters are filled, so in this case I avoid the second DB access

                        Why do my wrappedData & wrappedKeys parameters are null during the deletion phase ? Is it normal ?
                        I recall just ajaxSingle tag of my deletion button is setted to true.



                        • 9. Re: ExtendedDataModel row deletion
                          Nick Belaevski Master

                          Yes, it's normal for ajax single components. walk() is not called to handle them.

                          • 10. Re: ExtendedDataModel row deletion
                            Antony Antony Newbie

                            Ok, I tried to put my button without ajaxsingle, but in this case when I click, the framework never calls my button action method.

                            It calls the walk method and doesn't call my "test" method, is it normal ?

                            • 11. Re: ExtendedDataModel row deletion
                              Nick Belaevski Master

                              Please post full page and beans code.

                              • 12. Re: ExtendedDataModel row deletion
                                Antony Antony Newbie

                                My bean


                                public class TestModel extends SerializableDataModel {

                                private EstablishmentDao establishmentDao;
                                private Integer currentPk;
                                private Map<Integer,Establishment> wrappedData = new HashMap<Integer,Establishment>();
                                private List<Integer> wrappedKeys = null;
                                /**
                                * @return the wrappedKeys
                                */
                                public List<Integer> getWrappedKeys() {
                                return wrappedKeys;
                                }

                                /**
                                * @param wrappedKeys the wrappedKeys to set
                                */
                                public void setWrappedKeys(List<Integer> wrappedKeys) {
                                this.wrappedKeys = wrappedKeys;
                                }

                                private Integer rowCount;

                                private HtmlDataTable datatable;

                                private Establishment testAjax;

                                public TestModel(){}

                                @PostConstruct
                                public void init(){
                                //rowCount = new Integer(10);
                                }


                                @Override
                                public void update() {
                                // TODO Auto-generated method stub

                                }

                                @Override
                                public Object getRowKey() {
                                // TODO Auto-generated method stub
                                return currentPk;
                                }

                                @Override
                                public void setRowKey(Object key) {
                                this.currentPk = (Integer) key;

                                }

                                @Override
                                public void walk(FacesContext context, DataVisitor visitor, Range range,
                                Object argument) throws IOException {
                                int firstRow = ((SequenceRange)range).getFirstRow();
                                int numberOfRows = ((SequenceRange)range).getRows();
                                wrappedKeys = new ArrayList<Integer>();


                                int i = 0;
                                for (Establishment item:establishmentDao.findAll()) {
                                wrappedKeys.add(item.getIdestablishmentEst());
                                wrappedData.put(item.getIdestablishmentEst(), item);
                                visitor.process(context, item.getIdestablishmentEst(), argument);
                                if (i == 9)
                                break;

                                i++;
                                }
                                }

                                @Override
                                public int getRowCount() {
                                if (rowCount==null) {

                                rowCount = 17;
                                return rowCount.intValue();
                                } else {
                                return rowCount.intValue();
                                }
                                }

                                @Override
                                public Object getRowData() {
                                if (currentPk==null) {
                                return null;
                                } else {
                                Establishment ret = wrappedData.get(currentPk);
                                if (ret==null) {
                                //here we must hit a db connection
                                return null;
                                } else {
                                return ret;
                                }
                                }
                                }
                                .....
                                }


                                and here my datatable description


                                <rich:dataTable id="tableEstablishment" value="#{model}"
                                var="establishments" rows="10" ajaxKeys="#{model.wrappedKeys}" rowKeyVar="row" >
                                <f:facet name="caption"><h:outputText value="United States Capitals" /></f:facet>
                                <f:facet name="header"><h:outputText value="Capitals and States Table" /></f:facet>
                                <rich:column>
                                <f:facet name="header"><h:outputText value="Nom" /> </f:facet>
                                <h:outputText value="" />
                                </rich:column>
                                <rich:column>
                                <f:facet name="header"><h:outputText value="Date de création" /></f:facet>
                                <h:outputText value="#{establishments.nameEst}" />
                                </rich:column>
                                <rich:column>
                                <f:facet name="header"><h:outputText value="Type" /> </f:facet>
                                <h:outputText value="#{establishments.nameEst}" />
                                </rich:column>
                                <rich:column>
                                <f:facet name="header"><h:outputText value="Action" /> </f:facet>

                                <a4j:commandButton ajaxSingle="true" id="editlink" value="d" action="#{model.test}" reRender="tableEstablishment" oncomplete="return false;">
                                <f:setPropertyActionListener value="#{row}"
                                target="#{model.rowKey}" />

                                <f:setPropertyActionListener value="#{establishments}"
                                target="#{model.testAjax}" />
                                </a4j:commandButton>

                                </rich:column>
                                </rich:dataTable>
                                <rich:datascroller align="left" for="tableEstablishment" />


                                • 13. Re: ExtendedDataModel row deletion
                                  Antony Antony Newbie

                                  my test method is in the TestModel class

                                  • 14. Re: ExtendedDataModel row deletion
                                    Antony Antony Newbie

                                    I recall my problem

                                    I want to delete a datatable row item .

                                    i have a bean wich extends extendeddatamodel (look above).
                                    as I said before I have a database hit despite my extended datamodel when I come back on my JSF bean, and I never rech my action method, (method test in my bean model)

                                    I note a thing, my extendeddatamodel is declared in my faces-config

                                    <managed-bean>
                                     <managed-bean-name>model</managed-bean-name>
                                     <managed-bean-class>com.TestModel</managed-bean-class>
                                     <managed-bean-scope>request</managed-bean-scope>
                                     <managed-property>
                                     <property-name>establishmentDao</property-name>
                                     <value>#{establishmentDao}</value>
                                     </managed-property>
                                     </managed-bean>
                                    


                                    When I click on my row delete button, the framework calls many times the walk method in my model bean, I have no database hit because data are in cache, till now it works as we want
                                    but my model bean is in my faces config so jsf instanciates a new bean model to call my test method I think and it calls another time the walk method and at this time my cache properties are null, so I have a database hit.

                                    I am wondering if i can put extended datamodel and actions method in the same bean ?



                                    1 2 Previous Next