1

I have a working form with multiple pages using Spring and Hibernate and need to report the progress of the fields completed thus far by the user (percentage of fields complete). Here are the options that I explored:

Option A) Check each property on the model for a value. If it's not null, increment a counter, then divide by the total number of properties checked.

Option B) Use reflection on the model object and call each getter. If it's not null, increment a counter, then divide by the total number of properties checked to get the completion percentage. I am thinking something along the lines of this post.

I know that Option B) will be more expensive to do and generally avoid reflection, but it may be easier to maintain when properties are added/removed.

Are there other options I'm missing? Maybe some type of bean utility where I can specifically exclude properties to check? (Or some clever use of BeanUtils?)

Community
  • 1
  • 1
riddle_me_this
  • 8,575
  • 10
  • 55
  • 80

2 Answers2

1

Neither will work. The fact that a field is not null doesn't mean that the user loaded it (consider default values). Besides that, you're creating tight coupling between the data model and the presentation layer (what if you don't want to show some fields on the screen?).

In my opinion you should send a list of the fields you want to be loaded by the page to the browser, and manage that information there, as the user loads (or deletes) field values. Once the user commits the changes, you'll have the number of modified fields.

Andres
  • 10,561
  • 4
  • 45
  • 63
  • If I understand your response correctly, I would still need to query the db or check a bean for the committed fields, right? It becomes the same (or a similar) question. – riddle_me_this Jun 25 '14 at 20:17
  • No, you should keep track of the modifications, on the layer that makes the modification. That is, the browser. – Andres Jun 25 '14 at 20:56
0

I'm not familiar with the system you are using, but in general there is another option:

On the textbox "enter" event, update a counter. Assuming you know the total amount of fields needing completed, you can use this counter and that known value to determine the percentage.

One problem that can arise from this is that the user presses enter to go to the next one or tabs through it (setting off the event either way) and then comes back to the previous field (by doing something like SHIFT+Tab or clicking on it) and then when they click off it, it updates the counter falsely.

So in light of that problem, I instead suggest keeping a map of values. Say each textbox, when you edit it, puts a boolean ("true", for example; the exact value doesn't matter, as long as it's there and not null or false) into a map under the textbox's name. Then you can count how many keys are in the map, and check that against the known total value.

Something like this:

Map<String, boolean> map = new Map<String, boolean>();

//In the textbox textchanged event:
if (textbox.text != null &&  ! textbox.text.equals("")){
    map.put(textbox.name, true);
}
else{
    //if the user puts something in and then deletes it,
    //remove this textbox from the map
    map.remove(textbox.name);
}

//In the field checker:
if (map.getEntrySet().size() >= numberOfFields){
   //the number of fields is good
}

Obviously some of this is pseudo code (such as "textbox.text"), but the idea is there. I use map.getEntrySet(), which I think I spelled correctly, but really any of the map methods that return a list representation of the contents will do, since you're just getting the size of the list and don't really need to see its contents.

shieldgenerator7
  • 1,507
  • 17
  • 22