0

Precursor

I'd like to apologize at the start of this for the length of this. I've been working 55+ hour weeks for 3+ months so I want to do my best to explain the entire problem to the best of my ability. Also please be aware that I am working with Maven on my company's framework so I may not have the ability to implement every possible solution, but I can request permission to add a library if there are no security risks in the code. I will still upvote valid solutions even if I cannot implement them.

Possible Solutions?

  • Spanning Tree Algorithms
  • Dynamic UI building libraries
  • Tree traversals?
  • Graph approaches?
  • Not sure if JSR 303 Validators could handle this at the class-level without being overkill & overly confusing code (in case future devs ever come on the project)

Issue & Background

I need to build a dynamic @ViewScoped webpage with around 40 Boolean fields with some normal input fields that render along the way, but are not relevant unless a solution builds the UI in Java. A user will need to answer one question at a time which will render the next question based on their answer - one question at a time. There are at least 17 complete/valid paths/branches they can take in the fully constructed flowchart (i.e. the user has to answer every question along that workflow path with specific boolean values - every path has a single endpoint - many being the same on different paths - one of my primary issues).

I need to build the UI so that only the next question on a path renders which means I not only need to know what path they are currently on (both when they're filling the form out the first time and subsequent visits), but I need to avoid factoring in questions on the wrong path in the event that the user changes a previous answer - like if they answer 10 questions and then go back and change question 4; otherwise a question could meet the conditions to render from the old path despite the user changing to a new one.

Ideas About Some Approaches & Algorithms

If I'm doing a bad job explaining the problem to everyone (I've been staring at these 6 flowcharts so long that I have every single path memorized... it's taking a toll) - just imagine a spanning tree with all Boolean nodes or a flowchart with the same.

  • I can't use minimum spanning tree algorithms because the back-end entities, or POJO's if I took such an approach, would inherently retain the value until the page is saved (app-specific logic which is explained below), regardless of whether the fields render.

  • There's a lot of questions so I can't use a MinSpanTree algorithm because the user could complete a smaller path/set of questions, change their mind, and move onto a longer path which consequently invalidating the shorter path. There are no restrictions on their ability to change previous questions at any time so long as the question is rendered.

  • A maximum spanning tree approach wouldn't work with the current setup because values from unused questions are retained if a path changes. I would also need to know which max tree is valid if multiple pass.

  • Other tree traversal algorithms?

  • I know I can validate that a path is completed through a brute force iteration-type approach of checking every single question with a (properly ordered) List or Map in my next examples.

  • I could map the values to something like an Integer representing the question number (Map<Integer, Boolean>), or more appropriately an enum representing the question (Map<MyQuestionEnum, Boolean>).

  • I could even a DTO to hold the information so I can avoid the performance of a Map and simply use the DTO's to set the actual entity's values pre-persist (using BeanUtils, etc.).

Then I can confirm that all values match a predetermined path, but these approaches can require a bit of ugly code for something that I can't imagine doesn't have a better approach. I could technically have a giant 30-40 if/else or switch statement that simply maps an Enum to handle this logic, but why unless you've exhausted all other reasonable approaches?...

General Page Logic

  1. User saves their selections as draft or to finalize/validate it.

  2. When the user saves the page, all fields that are not on the path they've completed will be reset to null.

  • I'd like to avoid setting previous questions on other paths to null while the user fills out the form because you wouldn't want to fill 10 questions, accidentally click the 4th one in the progression, and wipe out all of that information you previously had.

These are very large forms, so it is unlikely that a user will fill everything out at once and it is unlikely that a user will change answers so many times that this becomes an issue, but we don't restrict that so we must account for it.

Current Solution

My current solution involves resetting specific node values when other nodes are clicked if the change would cause the questions following the cleared node to have no chance of rendering, but I'd prefer not to do this if possible. I must avoid duplicate id errors and cannot use a different id for the same field in a different location. (So JSTL or another approach depending on page layout). Then on save I literally traverse every possible path to see if they've completed one and that I wipe out all other fields. This leads to many nested if/else statements.

I already have the UI built with panels that render under certain conditions, and a save method that clears values based on each answer, but there are so many permutations that it leaves uncertainty as to whether there are any bugs/unintended ways a field could render and/or not reset under the right conditions when saving the entity.

It should be noted that I would like to avoid using additional database tables due to the performance of DAO calls and simply having extra table(s), but I'll hear out any ideas so if you have one involving DB work, please still share.

blau
  • 43
  • 4

0 Answers0