'Collect any dataset inputs from incoming. Returns a mapping from
parameter name to Dataset instance for each tool parameter that is
of the DataToolParameter type.'

def _collect_input_datasets(self, tool, param_values, trans, history, current_user_roles=None):

    if (current_user_roles is None):
        current_user_roles = trans.get_current_user_roles()
    input_datasets = odict()
    def visitor(input, value, prefix, parent=None, **kwargs):
        def process_dataset(data, formats=None):
            if ((not data) or isinstance(data, RuntimeValue)):
                return None
            if (formats is None):
                formats = input.formats
            if (not data.datatype.matches_any(formats)):
                trans.sa_session.refresh(data)
                (target_ext, converted_dataset) = data.find_conversion_destination(formats)
                if target_ext:
                    if converted_dataset:
                        data = converted_dataset
                    else:
                        data = data.get_converted_dataset(trans, target_ext, target_context=parent, history=history)
            if (not trans.app.security_agent.can_access_dataset(current_user_roles, data.dataset)):
                raise Exception(('User    does    not    have    permission    to    use    a    dataset    (%s)    provided    for    input.' % data.id))
            return data
        if isinstance(input, DataToolParameter):
            if isinstance(value, list):
                for (i, v) in enumerate(value):
                    processed_dataset = process_dataset(v)
                    if (i == 0):
                        input_datasets[(prefix + input.name)] = processed_dataset
                    input_datasets[((prefix + input.name) + str((i + 1)))] = processed_dataset
                    conversions = []
                    for (conversion_name, conversion_extensions, conversion_datatypes) in input.conversions:
                        new_data = process_dataset(input_datasets[((prefix + input.name) + str((i + 1)))], conversion_datatypes)
                        if ((not new_data) or new_data.datatype.matches_any(conversion_datatypes)):
                            input_datasets[((prefix + conversion_name) + str((i + 1)))] = new_data
                            conversions.append((conversion_name, new_data))
                        else:
                            raise Exception(('A    path    for    explicit    datatype    conversion    has    not    been    found:    %s    --/-->    %s' % (input_datasets[((prefix + input.name) + str((i + 1)))].extension, conversion_extensions)))
                    if parent:
                        parent[input.name][i] = input_datasets[((prefix + input.name) + str((i + 1)))]
                        for (conversion_name, conversion_data) in conversions:
                            parent[conversion_name][i] = conversion_data.id
                    else:
                        param_values[input.name][i] = input_datasets[((prefix + input.name) + str((i + 1)))]
                        for (conversion_name, conversion_data) in conversions:
                            param_values[conversion_name][i] = conversion_data.id
            else:
                input_datasets[(prefix + input.name)] = process_dataset(value)
                conversions = []
                for (conversion_name, conversion_extensions, conversion_datatypes) in input.conversions:
                    new_data = process_dataset(input_datasets[(prefix + input.name)], conversion_datatypes)
                    if ((not new_data) or new_data.datatype.matches_any(conversion_datatypes)):
                        input_datasets[(prefix + conversion_name)] = new_data
                        conversions.append((conversion_name, new_data))
                    else:
                        raise Exception(('A    path    for    explicit    datatype    conversion    has    not    been    found:    %s    --/-->    %s' % (input_datasets[(prefix + input.name)].extension, conversion_extensions)))
                target_dict = parent
                if (not target_dict):
                    target_dict = param_values
                target_dict[input.name] = input_datasets[(prefix + input.name)]
                for (conversion_name, conversion_data) in conversions:
                    target_dict[conversion_name] = conversion_data.id
        elif isinstance(input, DataCollectionToolParameter):
            if (not value):
                return
            dataset_instances = []
            if hasattr(value, 'child_collection'):
                dataset_instances = value.child_collection.dataset_instances
            else:
                dataset_instances = value.collection.dataset_instances
            for (i, v) in enumerate(dataset_instances):
                data = v
                if (not trans.app.security_agent.can_access_dataset(current_user_roles, data.dataset)):
                    raise Exception(('User    does    not    have    permission    to    use    a    dataset    (%s)    provided    for    input.' % data.id))
                input_datasets[((prefix + input.name) + str((i + 1)))] = data
    tool.visit_inputs(param_values, visitor)
    return input_datasets
