Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » virtual tables, DAOs and the Job API: how to combine?(design issue: how to make a job behaving like a swingworker?)
virtual tables, DAOs and the Job API: how to combine? [message #512343] Thu, 04 February 2010 17:43 Go to next message
Luca Ferrari is currently offline Luca FerrariFriend
Messages: 159
Registered: November 2009
Senior Member
Hi all,
I'm going to ask you a suggestion (or a pointer to some resources) about a design issue I'm experiencing in my RCP application. This post is quite long, since I'd like to express all my choices and doubts.
I've got an application where each perspective must load a set of views, and each view represents data fetched from a database and shown to the user as a table. So, to summarize, each view as a table viewer that represents database data chunks. Since each view has a similar behaviour (it only changes the set of data and its type, but the common behaviour is to show data), I've created a base set of classes (view, content provider, and so on) that I specialize with the details about the chunk of data to load from the database. This means that if I change something in the base data handling, I'm changing it everywhere in the whole application.
Now, my base view has a table viewer that exploits a virtual table, and such virtual table has an event listener as the following:

    public final void handleEvent(final Event event) {

	// get the current table item
	TableItem tableItem = (TableItem) event.item;
	// get the index
	int index = event.index;


	// now get the next element from the dao
	T nextElement = ((ILazyLoadingDAO<T>) getDao()).next();
	// add the element to the list model
	WritableList model = getWritableListDataModel( false );
	if( ! model.contains( nextElement ) )
	    model.add( nextElement );

	// set the table item in the table
	tableItem.setData( nextElement );
	        
	
       
    }



The important part of the code is that the event listener gets the DAO for the specified type of data T and then get the next element from the DAO. The next element is cached within the DAO, so my application loads for instance the first 20 elements and provide them one by one to the event listener; when the event listener fetches the 21st entry, the DAO loads another sets of elements (21-40) and caches them, so to return the elements in a fast way.
The next method of the DAO performs like the following;

    public synchronized final T next() {
	// if the queue is empty I need to fetch new elements
	if( this.cache.isEmpty() )
	    // execute the query
	    this.loadChunckOfData();
	
	// the queue contains at least an element already cached, so
	// return it
	return this.cache.remove();
    }




and in the case the cache is empty, the next set of data is loaded from the following method of the DAO:

    public synchronized final List<T> loadChunckOfData(){



	// execute the query to retrieve a new chunk of data
	List<T> fetchedElements = jdbcTemplate.query( getLazyLoadingQuery(), 
		new Object[]{ currentOffset, fetchTreshold }, 
		rowMapper  );


	// if no fetched elements it is time to stop the lazy loading (i.e., reset the offset so that
	// the next loading will start again from the beginning of the table)
	if( fetchedElements == null  || fetchedElements.isEmpty() ){
	    currentOffset = 0;
	}
	else{
	    // if here a chunk of data has been loaded from the database, so I need
	    // to move forward the offset and to refresh each element of the list

	    // increment the offset for the next query
	    currentOffset += fetchTreshold;

	    // create a temporary list
	    List<T> refreshedElements = new LinkedList<T>();

	    // refresh all the loaded elements
	    for( T currentElement : fetchedElements )
		refreshedElements.add( refresh(currentElement) );

	    // switch the lists
	    fetchedElements = refreshedElements;
	}

	// cache the returned chunk of data
	cache.addAll(fetchedElements);

	return fetchedElements;
    }



So, to summarize: the table event listener asks for the next record to the DAO, the DAO looks at its cache, and if the are no more cached (pre-fetched) elements it loads another set.
All the above works, but it is a lot of work to execute in the ui thread, so I'd like to switch to the Job api. The problem is that I don't know how to do and when. In fact, my first idea was to schedule a new job to execute the loadChunkOfData method, the problem is that such method must load elements that are required by the ui, so making the loading async does not allow the gui to show the data. My second thought was to schedule a job to execute the body of the event listener, but such method accesses the ui and so the job thread is not a good choice (right?).
In Swing I would have solved the problem using a swingworker thread to load the data, notifiying with the done method when the loading has finished. So, how can I inroduce the job api within these classes? I'm not a lot of experience about designin crud application like this, so if you have suggestions, links, etc. please advice.

Thanks.
Re: virtual tables, DAOs and the Job API: how to combine? [message #512761 is a reply to message #512343] Sun, 07 February 2010 10:35 Go to previous messageGo to next message
Luca Ferrari is currently offline Luca FerrariFriend
Messages: 159
Registered: November 2009
Senior Member
Nobody has an hint or an example to better understand how to exploit the Job api for loading data from a database?
Re: virtual tables, DAOs and the Job API: how to combine? [message #515762 is a reply to message #512761] Sat, 20 February 2010 15:17 Go to previous message
Luca Ferrari is currently offline Luca FerrariFriend
Messages: 159
Registered: November 2009
Senior Member
I'm trying with the job api but I don't really see how to plug it into the dao loading process. Anybody has a sugestion?
Previous Topic:Cannot detect selection generated by double click in selectionChanged()
Next Topic:selection stores the last element when an exception occurs
Goto Forum:
  


Current Time: Mon Nov 24 20:30:33 GMT 2014

Powered by FUDForum. Page generated in 0.03408 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software