I am developing a module in which there are 10 filters, on which a user can filter data shown in grid. Lets assume data is of team in a company in which there are 10 members and each person is given a task to do work. All the members of the team add those task in that module and submit it to their superiors telling them that they have done this work, on this date, in that much of time etc.
And their superiors can have a look at these submissions as number of task is more in number they need few filter conditions to filter out data. For example, they might want to see only x-person task or what he has done, or in last 2 days, what task are submitted to them by person-y etc.
All the filters are optional and none is applied when the page load and displays data first time.
As the user add filters, the data gets filtered. But here is the problem - Managing these filters and adding new filters as requirement change has been very trouble some for me. Due to large number of IF Statements.
At present my code something like this
if ((strIdList != null && !strIdList.isEmpty()) && (strTaskDate == null || strTaskDate.isEmpty()) && (strWorkDate == null || strWorkDate.isEmpty()) && (lastModifiedDateTime == null || lastModifiedDateTime.isEmpty())) { query = query + " and " + taskIdQuery + " and " + taskStatusQuery; } if ((strIdList != null && !strIdList.isEmpty()) && (strTaskDate == null || strTaskDate.isEmpty()) && (strWorkDate == null || strWorkDate.isEmpty()) && (lastModifiedDateTime != null && !lastModifiedDateTime.isEmpty())) { query = query + " and " + taskIdQuery + " and " + lastModified + " and " + taskStatusQuery; } if ((strIdList == null || strIdList.isEmpty()) && (strTaskDate == null || strTaskDate.isEmpty()) && (strWorkDate == null || strWorkDate.isEmpty()) && (lastModifiedDateTime != null && !lastModifiedDateTime.isEmpty())) { query = query + " and " + lastModified + " and " + taskStatusQuery; } if (strTaskDate != null && !strTaskDate.isEmpty() && (strIdList == null || strIdList.isEmpty()) && (strWorkDate == null || strWorkDate.isEmpty())) { query = query + " and " + taskDateQuery + " and " + taskStatusQuery; } if (strWorkDate != null && !strWorkDate.isEmpty() && (strTaskDate == null || strTaskDate.isEmpty()) && (strIdList == null || strIdList.isEmpty())) { query = query + " and " + workDateQuery + " and " + taskStatusQuery; } if (strWorkDate != null && !strWorkDate.isEmpty() && strTaskDate != null && !strTaskDate.isEmpty() && strIdList != null && !strIdList.isEmpty()) { query = query + " and " + taskIdQuery + " and " + taskDateQuery + " and " + workDateQuery + " and " + taskStatusQuery; } if (strTaskDate != null && !strTaskDate.isEmpty() && strWorkDate != null && !strWorkDate.isEmpty() && (strIdList == null || strIdList.isEmpty())) { query = query + " and " + taskDateQuery + " and " + workDateQuery + " and " + taskStatusQuery; } if (strTaskDate != null && !strTaskDate.isEmpty() && (strWorkDate == null || strWorkDate.isEmpty()) && strIdList != null && !strIdList.isEmpty()) { query = query + " and " + taskIdQuery + " and " + taskDateQuery + " and " + taskStatusQuery; } if ((strTaskDate == null || strTaskDate.isEmpty()) && strWorkDate != null && !strWorkDate.isEmpty() && strIdList != null && !strIdList.isEmpty()) { query = query + " and " + taskIdQuery + " and " + workDateQuery + " and " + taskStatusQuery; } if ((strTaskDate == null || strTaskDate.isEmpty()) && (strWorkDate == null || strWorkDate.isEmpty()) && (strIdList == null || strIdList.isEmpty()) && (lastModifiedDateTime == null || lastModifiedDateTime.isEmpty())) { query = query + " and " + taskStatusQuery; } } else if (filterParameter == 2) { query = query + " and " + noWorkPerformedQuery; if ((strIdList != null && !strIdList.isEmpty()) && (strTaskDate == null || strTaskDate.isEmpty()) && (lastModifiedDateTime == null || lastModifiedDateTime.isEmpty())) { query = query + " and " + taskIdQuery + " and " + taskStatusQuery; } if ((strIdList != null && !strIdList.isEmpty()) && (strTaskDate == null || strTaskDate.isEmpty()) && (lastModifiedDateTime != null && !lastModifiedDateTime.isEmpty())) { query = query + " and " + taskIdQuery + " and" + lastModified + " and " + taskStatusQuery; } if ((strIdList == null || strIdList.isEmpty()) && (strTaskDate == null || strTaskDate.isEmpty()) && (lastModifiedDateTime != null && !lastModifiedDateTime.isEmpty())) { query = query + " and" + lastModified + " and " + taskStatusQuery; } if ((strIdList == null || strIdList.isEmpty()) && (strTaskDate == null || strTaskDate.isEmpty()) && (lastModifiedDateTime == null || lastModifiedDateTime.isEmpty())) { query = query + " and " + taskStatusQuery; } if (strTaskDate != null && !strTaskDate.isEmpty() && (strIdList == null || strIdList.isEmpty())) { query = query + " and " + taskDateQuery + " and " + taskStatusQuery; } if (strTaskDate != null && !strTaskDate.isEmpty() && strIdList != null && !strIdList.isEmpty()) { query = query + " and " + taskIdQuery + " and " + taskDateQuery + " and " + taskStatusQuery; } And there's more of that... I know this very bad, but don't know how to improve this code. Thought of using switch but it does't help that much.
Why it has so many conditions because for example. Lets say I have two optional filters on name and date, then scenarios
Name Date True True True False False True False False Now you can understand where I am going. So as the number of filters add up these conditions multiple simple maths.
I hope someone can advice me how to handle such scenarios thanks!