1. #1
    Sencha User
    Join Date
    Sep 2012
    Posts
    5
    Vote Rating
    0
    doucampb is on a distinguished road

      0  

    Default Database Connectivity

    Database Connectivity


    Can anyone point me in the right direction on how to populate a grid or tree from an Oracle Database. I have created a connection class i can connect and query the database but do i use this result set to populate the grid or is there other steps such as RPCProxy or interfaces I should implement and use.

    Kind Regards,
    Douglas

  2. #2
    Sencha User
    Join Date
    Feb 2012
    Posts
    21
    Vote Rating
    0
    xybrek is on a distinguished road

      0  

    Default


    I think GWT RPC is quite suitable already for passing data from the database to the GXT Grid data widget. I'm still reviewing GXT 3 but in Ext GWT 2 we created some sort of PageModel which represents a query result and put that into a Map and pass it into the Grid widget:

    Code:
        @Override    public void setGridData(List<Map<String, String>> data, int offset, int total) {
            List<ModelData> list = new ArrayList<ModelData>();
            int count = data.size();
            for (int i=0; i<count; i++) {
                ModelData model = new BaseModelData();
                for (Entry<String, String> entry: data.get(i).entrySet()) {
                    model.set(entry.getKey(), entry.getValue());
                }
                list.add(model);
            }
            ListStore<ModelData> store = grid.getStore();
            store.removeAll();
            store.add(list);
            PagingLoadResult<Map<String, String>> result = new BasePagingLoadResult<Map<String,String>>(null, offset, total);
            LoadEvent event = new LoadEvent(null, null, result);
            List<Listener<? extends BaseEvent>> listeners = loader.getListeners(Loader.Load);
            count = listeners.size();
            for (int i=0; i<count; i++) {
                LoadListener listener = (LoadListener)listeners.get(i);
                listener.loaderLoad(event);
            }
            grid.unmask();        
        }

  3. #3
    Sencha User
    Join Date
    Sep 2012
    Posts
    5
    Vote Rating
    0
    doucampb is on a distinguished road

      0  

    Default


    Thanks for the sharp response.

    I am new to GXT so i am not sure how it was structured in GXT2. Can you possibly send a snippet of your database code and how the data passes into the method you have shown above.

    Thanks again,
    Douglas

  4. #4
    Sencha User
    Join Date
    Feb 2012
    Posts
    21
    Vote Rating
    0
    xybrek is on a distinguished road

      0  

    Default


    Quote Originally Posted by doucampb View Post
    Thanks for the sharp response.

    I am new to GXT so i am not sure how it was structured in GXT2. Can you possibly send a snippet of your database code and how the data passes into the method you have shown above.

    Thanks again,
    Douglas
    Its not very hard actually. A good design pattern should get you to the right track. Basically, we create separate layer for the DAO and Service layers, here try this one:

    Code:
        @Override
        public PageModel<Map<String, String>> query(String sql, final int offset, final int limit) throws QueryException {
            try {
                return jdbcTemplate.query(sql, new ResultSetExtractor<PageModel<Map<String, String>>>() {
                    @Override
                    public PageModel<Map<String, String>> extractData(ResultSet rs)
                            throws SQLException, DataAccessException {
                        PageModel<Map<String, String>> pageModel  = new PageModel<Map<String,String>>();
                        List<Map<String, String>> list = new ArrayList<Map<String,String>>();
                        int rows = 0;
                        // skip rows
                        for (int i=0; i<offset&&rs.next(); i++) {
                            rows++;
                        }
                        // get rows
                        for (int i=0; i<limit&&rs.next(); i++) {
                            Map<String, String> map = new HashMap<String, String>();
                            ResultSetMetaData metadata = rs.getMetaData();
                            int count = metadata.getColumnCount();
                            for (int j=1; j<=count; j++) {
                                map.put(metadata.getColumnName(j), rs.getString(j));
                            }
                            list.add(map);
                            rows++;
                        }
                        // iterate remaining rows to get total rows
                        while (rs.next())
                            rows++;
                        pageModel.setOffset(offset);
                        pageModel.setLimit(limit);
                        pageModel.setData(list);
                        pageModel.setTotal(rows);
                        return pageModel;
                    }
                });
            } catch (DataAccessException e) {
                throw new QueryException(e);
            }
        }
    Just check the setters in the code and you should be able to replicate the PageModel, just make sure you put this on the "shared" package or a package you defined in your gwt.xml

    After this, you should create a GWT RPC that access this query code then process the PageModel, get the data which is a
    Code:
    List<Map<String, String>>
    field that you need to pass to the
    Code:
    setGridData
    you can do another query if necessary...

  5. #5
    Sencha User
    Join Date
    Sep 2012
    Posts
    5
    Vote Rating
    0
    doucampb is on a distinguished road

      0  

    Default


    Thanks for providing that code. Still a little unsure of how your application is structured. I noticed when i tried to implement the PageModel you are using the Spring Framework do you have any examples for just using the basic grid, an RPC example and so on.

    Thanks again,
    Douglas

  6. #6
    Sencha User
    Join Date
    Feb 2012
    Posts
    21
    Vote Rating
    0
    xybrek is on a distinguished road

      0  

    Default


    Quote Originally Posted by doucampb View Post
    Thanks for providing that code. Still a little unsure of how your application is structured. I noticed when i tried to implement the PageModel you are using the Spring Framework do you have any examples for just using the basic grid, an RPC example and so on.

    Thanks again,
    Douglas
    I just installed the Spring STS Eclipse plugin and then created a Spring MVC template app then added the GWT dependencies to it, tried if it compiles, then finally add the Sencha Ext GWT dependencies, including the GXT resources folder.

    I'm not sure what approach is most suitable for you, but getting data from the Database is a different, I mean you can do a PageModel approach or ORM approach, whatever suites you best. If you're just testing out, may be you can create a "Test data" then create your column configs for the Grid and then bind a Store in that Grid maybe this can help you:

    http://www.java2s.com/Code/Java/GWT/GridStoredatabindingExtGWT.htm

    Cheers.

  7. #7
    Sencha User
    Join Date
    Sep 2012
    Posts
    5
    Vote Rating
    0
    doucampb is on a distinguished road

      0  

    Default


    The link you have sent is a lot like one of the samples included in the sencha download and did help me. I'm not sure an orm is applicable to me as I don't want tied to the implementation. Really having trouble just getting simple data from the database into this basic grid as in the example link replacing the list of test data. I don't know what the list store is, column model etc if I need these for a database populated grid. model key provider? and also an rpc class for the Communication. If I seen a simple example it would help me see how it all fits. I thought the documentation would have been better.

    Thanks for being my sole form of help,
    Douglas

  8. #8
    Sencha User
    Join Date
    Feb 2012
    Posts
    21
    Vote Rating
    0
    xybrek is on a distinguished road

      0  

    Default


    Maybe you can try looking at this showcase Ext GWT showcase: http://bit.ly/NoA5Pt
    You can select the "Grid" from the options and see the actual demo running and as well as look at its source.

Thread Participants: 1