aspose file tools*
The moose likes Java in General and the fly likes Filter data in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Filter data in Java" Watch "Filter data in Java" New topic
Author

Filter data in Java

Anirudha Joshi
Greenhorn

Joined: Apr 08, 2005
Posts: 22
am developing a screen on which user can see various details of an Employee. Details includes fields like Name, Date Of birth, Salary etc.
Server side code retrieves data for an employee using different data sources. Some fields are retrieved from the database using JDBC while other are retrieved from external system using API calls.

Now, application also allows end-user to configure different VIEW filters and end user can apply them while viewing the data. For ex: User can setup a filter as "Show Employee where firstname like 'A' and DOB > '2/2/2000'.

Issue is, how do I filter the dataset?

If user sets a filter on the attribute which is getting retrieved from JDBC, I can append this filter in the SQL string as WHERE clause (for ex: where emp.first_name like 'A%'). So no issues for SQL fields.

But how do I apply filter criteria which is set on the non SQL attributes?

One of the solution which I tried to implement is converting final dataset into XML and then applying filter using XPATH. But as I understand XPATH can be used only for String and numeric values. It can not be used for dates and filter criteria like "WHERE DEPTNAME IN A, B, C, D).

Any ideas?
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
This is a perfect MVC application: Model View Control. I would first create individual adapters for the SQL versus API call data collectors. Then create one builder class that assembles a single data model using the adapters. The filtering can be handled by a separate view class. The control is represented by the controls on your "screen" and the class that handles events.

The single data model could be a list of rows where each row has a list of cells. The view would then filter the individual rows and columns based on criteria dictated by the controller, which is an extension of your "screen" controls.

I think you will find this easy to build, easy to maintain, easy to test. You will also discover that you are using a set of well-known battle-tested design patterns.
Anirudha Joshi
Greenhorn

Joined: Apr 08, 2005
Posts: 22
But the issues is what approach should I take to filter the collection. How do I filter the dataset? What code should I write to achieve this?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
A generalized solution would be pretty daunting ... you'd have to evaluate operators like = != > <= IN NOT AND OR and so on, and convert the argumentsin the query string to the proper type to do the compares. Actually sounds like a lot of fun if you really like reflection.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
Stan, it's surprisingly straight forward even for the simple jobs. IMO that's a common misconception that MVC is too elaborate but in fact there's just a small handful of well organized classes and it scales massively.

You're suggesting reflection versus of well understood pattern that's part of best practices, based in part on the perceived fun of that activity. That's a valid objective, but IMO the standard approach is better.

Anirudha, in terms of filtering, you have multipe input sources but you need to filter them all uniformly. That's why you do it in the view. One model and several views or one parametric view. You need a a List of Row objects where each Row is a list of Cell objects. What complicates your situation is having multiple inputs. Also, you did not indicate how much data you are talking abou and that's going to impact your design significantly.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I was ignoring the larger design and just thinking about the mechanics of essentially duplicating the "where" clause of SQL against a collection of objects. As with most cute ideas in Java, it's probably been done.
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
Duplicating the "where" on his other inputs definitely could be problematic. The reason the post-query filtering is a good idea in his case is that he's got multiple inputs and also he has a screen to control the filters. If there is a ton of data returned without a WHERE, it would be smart to have the SQL filter its part and the other inputs filter theirs. That would argue against my original suggestion. Need more specifics on his app.
[ August 21, 2005: Message edited by: Rick O'Shay ]
Anirudha Joshi
Greenhorn

Joined: Apr 08, 2005
Posts: 22
I am doing the filteration in the Model as this is used by multiple views. Data is retrieved in two steps. First from the database input source and then from the specific APIs.
Filter set on the attributes which are getting retrieved from the database is not a problem, as I convert the filter criteria before firing SQL query and append it in the base query. This works good for me as data is filtered at the database level and once retrieved my code just wraps it in the final dataset. Issue comes when filter is set on the attributes which are getting retrieved from the APIs. I am not sure what code should I write tp apply the filter. Do I need to add the resultste into collection and then loop through it and apply filter or do I need to use XPATH.

Stan, u talked about using reflection. I couldnt understand it. Can you please elaborate your idea with an example.
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
You would can use xpath expressions to select portions of XML documents, usually with an XSL transformation, so if your input is XML that's fine.

However, notice that when you say you filter within the model you are saying you've combined the view and the model. That's what a view is, essentially, a filter of data model. Moreover, you're not really doing that be cause you are filtering at the SQL level. Usually that's a good thing, but it depends on the data.

If you were to build an unfiltered data structure (the model) you could use a separate view class to filter the data using a single strategy: looping over the data set (presumably not a large one). That's one problem instead of two problems or three problems if you add another data source.

There's no right or wrong, it depends on your application.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Filter data in Java
 
Similar Threads
findByCriteria
Filter data in Java
Design query in jasper report (ireport)
Disadvantage of Java Business Developmet over Stored prcedures
Closing cursor in a stored procedure when invoked from a callable statement