Refactor task/flow flattening

- Instead of having to pass context data between the
  recursive function calls, create an type/object to hold
  this context data, which also contains the flattening
  functionality itself.
- Allow the flattening functions to be more easily
  extended by having a selection function that matches
  item type to flattening function.
- Add pre/post flattening functions (which can be
  extended/inherited to do different activites as
  needed).

Change-Id: I2f2087097f5934e02d9d775eea709673b63f01b1
Merge "Refactor task/flow flattening"