split handling of alerts into several functions and use switch instead of dynamic_casts

This is somewhat similar to #1652, but rebased on top of my work.