'Returns a list of dictionaries, each representing the widgets that define a sample on a form.
The widgets are populated from kwd based on the set of samples received.  The set of samples
corresponds to a request.samples list, but if the user checked specific check boxes on the form,
those samples that were not checked will have None objects in the list of samples.  In this case,
the corresponding sample_widget is populated from the db rather than kwd.'

def __get_sample_widgets(self, trans, request, samples, **kwd):

    params = util.Params(kwd)
    sample_operation = params.get('sample_operation', 'none')
    sample_widgets = []
    if (sample_operation != 'none'):
        library_id = util.restore_text(params.get('sample_operation_library_id', 'none'))
        folder_id = util.restore_text(params.get('sample_operation_folder_id', 'none'))
    if (not request):
        return sample_widgets
    libraries = trans.app.security_agent.get_accessible_libraries(trans, request.user)
    for (index, sample) in enumerate(samples):
        if (sample is None):
            sample = request.samples[index]
            sample_id = sample.id
            name = sample.name
            bar_code = sample.bar_code
            library = sample.library
            folder = sample.folder
            history = sample.history
            workflow = sample.workflow
            field_values = sample.values.content
            if (not history):
                history_id = 'none'
            else:
                history_id = history.id
            if (not workflow):
                workflow_id = 'none'
            else:
                workflow_id = workflow.id
            workflow_dict = sample.workflow
        else:
            sample_id = None
            name = util.restore_text(params.get(('sample_%i_name' % index), sample.name))
            bar_code = util.restore_text(params.get(('sample_%i_bar_code' % index), sample.bar_code))
            library_id = util.restore_text(params.get(('sample_%i_library_id' % index), ''))
            if ((not library_id) and sample.library):
                library_id = trans.security.encode_id(sample.library.id)
            folder_id = util.restore_text(params.get(('sample_%i_folder_id' % index), ''))
            if ((not folder_id) and sample.folder):
                folder_id = trans.security.encode_id(sample.folder.id)
            (library, folder) = self.__get_library_and_folder(trans, library_id, folder_id)
            history_id = util.restore_text(params.get(('sample_%i_history_id' % index), ''))
            if ((not history_id) and sample.history):
                history_id = trans.security.encode_id(sample.history.id)
            history = self.__get_history(trans, history_id)
            wf_tag = ('sample_%i_workflow_id' % index)
            workflow_id = util.restore_text(params.get(wf_tag, ''))
            if ((not workflow_id) and sample.workflow):
                workflow_id = trans.security.encode_id(sample.workflow['id'])
                workflow_dict = sample.workflow
                workflow = self.__get_workflow(trans, workflow_id)
            else:
                workflow_dict = None
                workflow = self.__get_workflow(trans, workflow_id)
                if workflow:
                    workflow_dict = {'id': workflow.id, 'name': workflow.name, 'mappings': {}}
                    for (k, v) in kwd.iteritems():
                        kwd_tag = ('%s_' % wf_tag)
                        if k.startswith(kwd_tag):
                            workflow_dict['mappings'][int(k[len(kwd_tag):])] = {'ds_tag': v}
            field_values = {}
            for (field_index, field) in enumerate(request.type.sample_form.fields):
                field_name = field['name']
                input_value = params.get(('sample_%i_field_%i' % (index, field_index)), sample.values.content[field_name])
                if (field['type'] == CheckboxField.__name__):
                    field_value = CheckboxField.is_checked(input_value)
                else:
                    field_value = util.restore_text(input_value)
                field_values[field_name] = field_value
        (library_select_field, folder_select_field) = self.__build_library_and_folder_select_fields(trans=trans, user=request.user, sample_index=index, libraries=libraries, sample=sample, library_id=library_id, folder_id=folder_id, **kwd)
        history_select_field = self.__build_history_select_field(trans=trans, user=request.user, sample_index=index, sample=sample, history_id=history_id, **kwd)
        workflow_select_field = self.__build_workflow_select_field(trans=trans, user=request.user, request=request, sample_index=index, sample=sample, workflow_dict=workflow_dict, history_id=history_id, **kwd)
        sample_widgets.append(dict(id=sample_id, name=name, bar_code=bar_code, library=library, folder=folder, history=history, workflow=workflow, workflow_dict=workflow_dict, field_values=field_values, library_select_field=library_select_field, folder_select_field=folder_select_field, history_select_field=history_select_field, workflow_select_field=workflow_select_field))
    index = len(samples)
    while True:
        name = util.restore_text(params.get(('sample_%i_name' % index), ''))
        if (not name):
            break
        bar_code = util.restore_text(params.get(('sample_%i_bar_code' % index), ''))
        library_id = util.restore_text(params.get(('sample_%i_library_id' % index), ''))
        folder_id = util.restore_text(params.get(('sample_%i_folder_id' % index), ''))
        (library, folder) = self.__get_library_and_folder(trans, library_id, folder_id)
        history_id = util.restore_text(params.get(('sample_%i_history_id' % index), ''))
        if ((not history_id) and sample.history):
            history_id = trans.security.encode_id(sample.history.id)
        history = self.__get_history(trans, history_id)
        wf_tag = ('sample_%i_workflow_id' % index)
        workflow_id = util.restore_text(params.get(wf_tag, ''))
        if ((not workflow_id) and sample.workflow):
            workflow_id = trans.security.encode_id(sample.workflow['id'])
            workflow_dict = sample.workflow
            workflow = self.__get_workflow(trans, workflow_id)
        else:
            workflow_dict = None
            workflow = self.__get_workflow(trans, workflow_id)
            if workflow:
                workflow_dict = {'id': workflow.id, 'name': workflow.name, 'mappings': {}}
                for (k, v) in kwd.iteritems():
                    kwd_tag = ('%s_' % wf_tag)
                    if k.startswith(kwd_tag):
                        workflow_dict['mappings'][int(k[len(kwd_tag):])] = {'ds_tag': v}
        field_values = {}
        for (field_index, field) in enumerate(request.type.sample_form.fields):
            field_name = field['name']
            input_value = params.get(('sample_%i_field_%i' % (index, field_index)), '')
            if (field['type'] == CheckboxField.__name__):
                field_value = CheckboxField.is_checked(input_value)
            else:
                field_value = util.restore_text(input_value)
            field_values[field_name] = field_value
        (library_select_field, folder_select_field) = self.__build_library_and_folder_select_fields(trans=trans, user=request.user, sample_index=index, libraries=libraries, sample=None, library_id=library_id, folder_id=folder_id, **kwd)
        history_select_field = self.__build_history_select_field(trans=trans, user=request.user, sample_index=index, sample=None, history_id=history_id, **kwd)
        workflow_select_field = self.__build_workflow_select_field(trans=trans, user=request.user, request=request, sample_index=index, sample=None, workflow_dict=workflow_dict, history_id=history_id, **kwd)
        sample_widgets.append(dict(id=None, name=name, bar_code=bar_code, library=library, folder=folder, field_values=field_values, history=history, workflow=workflow, workflow_dict=workflow_dict, history_select_field=history_select_field, workflow_select_field=workflow_select_field, library_select_field=library_select_field, folder_select_field=folder_select_field))
        index += 1
    return sample_widgets
