package ca.corefacility.bioinformatics.irida.ria.web.components.datatables.export;

import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.MessageSource;
import org.springframework.data.domain.Page;

import ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin;
import ca.corefacility.bioinformatics.irida.model.sample.Sample;
import ca.corefacility.bioinformatics.irida.ria.web.components.datatables.models.ProjectSampleModel;

import com.github.dandelion.datatables.core.export.HtmlTableBuilder;
import com.github.dandelion.datatables.core.html.HtmlTable;

/**
 * Export project samples table.
 */
public class ProjectSamplesTableExport extends TableExport {

	public ProjectSamplesTableExport(String exportFormat, String fileName, MessageSource messageSource, Locale locale) throws ExportFormatException {
		super(exportFormat, fileName, messageSource, locale);
	}

	/**
	 * Generate a {@link HtmlTable} from the currently displayed {@link Sample}s
	 *
	 * @param page
	 * 		{@link Page} of {@link ProjectSampleJoin} that are displayed.
	 * @param request
	 * 		the current {@link HttpServletRequest}
	 *
	 * @return {@link HtmlTable}
	 */
	public HtmlTable generateHtmlTable(Page<ProjectSampleJoin> page, HttpServletRequest request) {

		List<ProjectSampleModel> samples = page.getContent().stream().map(j -> new ProjectSampleModel(j, null))
				.collect(Collectors.toList());

		HtmlTableBuilder.ColumnStep steps = new HtmlTableBuilder<ProjectSampleModel>()
				.newBuilder("samples", samples, request, exportConf);

		for (String attr : ProjectSampleModel.attributes) {
			steps.column().fillWithProperty(attr).title(messageSource.getMessage("sample." + attr, new Object[]{}, locale));
		}

		return steps.column().fillWith("").title("").build();
	}
}

@@ -225,44 +225,160 @@
 rg.s
-pringframework.context.MessageSource
+lf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0Aimport org.springframework.context.MessageSource;%0Aimport org.springframework.context.NoSuchMessageException
 ;%0Aim
@@ -899,16 +899,112 @@
 Export %7B
+%0A%09private static final Logger logger = LoggerFactory.getLogger(ProjectSamplesTableExport.class);
 %0A%0A%09publi
@@ -1918,52 +1918,50 @@
 %0A%09%09%09
-steps.column().fillWithProperty(attr).
+String title = attr;%0A%09%09%09try %7B%0A%09%09%09%09
 title
-(
+ = 
 mess
@@ -2011,16 +2011,17 @@
 Object%5B%5D
+ 
 %7B%7D, loca
@@ -2023,16 +2023,233 @@
  locale)
+;%0A%09%09%09%7D catch (NoSuchMessageException e) %7B%0A%09%09%09%09logger.debug(%22No internationalization string found for samples table export for attribute: %22 + attr);%0A%09%09%09%7D%0A%09%09%09steps.column()%0A%09%09%09%09%09.fillWithProperty(attr)%0A%09%09%09%09%09.title(title
 );%0A%09%09%7D%0A%0A

Fix export from project samples