• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Filter data in Java

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
They gave me pumpkin ice cream. It was not pumpkin pie ice cream. Wiping my tongue on this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic