'Returns the history state based on the states of the HDAs it contains.'

def serialize_history_state(self, history, key, **context):

    states = model.Dataset.states
    state = states.ERROR
    hda_state_counts = self.serialize_state_counts(history, 'counts', exclude_deleted=True, **context)
    num_hdas = sum(hda_state_counts.values())
    if (num_hdas == 0):
        state = states.NEW
    elif ((hda_state_counts[states.RUNNING] > 0) or (hda_state_counts[states.SETTING_METADATA] > 0) or (hda_state_counts[states.UPLOAD] > 0)):
        state = states.RUNNING
    elif ((hda_state_counts[states.QUEUED] > 0) or (hda_state_counts[states.NEW] > 0)):
        state = states.QUEUED
    elif ((hda_state_counts[states.ERROR] > 0) or (hda_state_counts[states.FAILED_METADATA] > 0)):
        state = states.ERROR
    elif (hda_state_counts[states.OK] == num_hdas):
        state = states.OK
    return state
