CRM-15343 - CiviCase - Fix corruption of soft FKs (case-type-id)

This is a resubmission of https://github.com/civicrm/civicrm-core/pull/4224 with additional commits.