Commit e0cb637a authored by pierre.tremouilhac's avatar pierre.tremouilhac
Browse files

Merge branch '258-report-sample-export-accept-uistate-params' into 'development'

Resolve "report sample export accept uistate params"

Closes #258

See merge request ComPlat/chemotion_ELN!456
parents 1bf66b94 9effefce
......@@ -64,13 +64,28 @@ module Chemotion
end
params do
requires :type, type: String
requires :checkedIds, type: Array
requires :uncheckedIds, type: Array
requires :checkedAll, type: Boolean
requires :currentCollection, type: Integer
requires :removedColumns, type: Array[String]
requires :columns, type: Array[String]
requires :exportType, type: Integer
requires :uiState, type: Hash do
requires :sample, type: Hash do
requires :checkedIds, type: Array
requires :uncheckedIds, type: Array
requires :checkedAll, type: Boolean
end
requires :reaction, type: Hash do
requires :checkedIds, type: Array
requires :uncheckedIds, type: Array
requires :checkedAll, type: Boolean
end
requires :wellplate, type: Hash do
requires :checkedIds, type: Array
requires :uncheckedIds, type: Array
requires :checkedAll, type: Boolean
end
requires :currentCollection, type: Integer
requires :isSync, type: Boolean
end
requires :columns, type: Array
end
post :export_samples_from_selections do
env['api.format'] = :binary
......@@ -85,34 +100,32 @@ module Chemotion
fileType = '.sdf'
export = Reporter::SdfExport.new
end
fileName = params[:type].capitalize + '_' + time_now + fileType
fileName = 'sample_export_' + time_now + fileType
fileURI = URI.escape(fileName)
header 'Content-Disposition', "attachment; filename=\"#{fileURI}\""
# header 'Content-Disposition', "attachment; filename*=UTF-8''#{fileURI}"
type = params[:type]
checkedIds = params[:checkedIds]
uncheckedIds = params[:uncheckedIds]
checkedAll = params[:checkedAll]
currentCollection = params[:currentCollection]
removed_field = params[:removedColumns]
elements = selected_elements(
type, checkedAll, checkedIds, uncheckedIds, currentCollection
)
samples = if type == 'sample'
elements.includes([:molecule,:molecule_name])
elsif type == 'reaction'
elements.map { |r|
r.starting_materials + r.reactants + r.products
}.flatten
elsif type == 'wellplate'
elements.map { |wellplate|
wellplate.wells.map(&:sample).flatten
}.flatten
end
samples.each { |sample| export.add_sample(sample) }
currColl = params[:uiState][:isSync] ? 0 : params[:uiState][:currentCollection]
removed_field = params[:columns]
[:sample, :reaction, :wellplate].each do |type|
next unless ( p_t = params[:uiState][type])
elements = selected_elements(
type.to_s, p_t[:checkedAll], p_t[:checkedIds],
p_t[:uncheckedIds], currColl
)
samples = case type.to_s
when 'sample'
elements.includes([:molecule,:molecule_name])
when 'reaction'
elements.map { |r|
r.starting_materials + r.reactants + r.products
}.flatten
when 'wellplate'
elements.map { |wellplate|
wellplate.wells.map(&:sample).flatten
}.flatten
end
samples.each { |sample| export.add_sample(sample) }
end
export.generate_file(excluded_field, included_field, removed_field)
end
......
......@@ -122,29 +122,33 @@ export default class ModalExport extends React.Component {
}
}
const exportSelections = (uiState, userState, removedColumns, e) => {
const { currentCollection, sample, reaction, wellplate } = uiState;
const { currentType } = userState;
const { checkedIds, uncheckedIds, checkedAll } = sample
const exportSelections = (uiState, userState, columns, e) => {
ReportsFetcher.createDownloadFile({
type: currentType,
exportType: e,
checkedIds: checkedIds.toArray(),
uncheckedIds: uncheckedIds.toArray(),
checkedAll: checkedAll,
currentCollection: currentCollection.id,
removedColumns: removedColumns
uiState: filterUIState(uiState),
columns: columns
});
}
const selectedStringfy = (input, currentCollection, removedColumns, e) => {
const { checkedIds, uncheckedIds, checkedAll } = input;
return "&exportType=" + e +
"&checkedIds=" + checkedIds.toArray() +
"&uncheckedIds=" + uncheckedIds.toArray() +
"&checkedAll=" + checkedAll +
"&currentCollection=" + currentCollection.id +
"&removedColumns=" + removedColumns
const filterUIState = (uiState) =>{
const { currentCollection, sample, reaction, wellplate, isSync } = uiState;
return {
sample: {
checkedIds: sample.checkedIds.toArray(),
uncheckedIds: sample.uncheckedIds.toArray(),
checkedAll: sample.checkedAll,
},
reaction: {
checkedIds: reaction.checkedIds.toArray(),
uncheckedIds: reaction.uncheckedIds.toArray(),
checkedAll: reaction.checkedAll,
},
wellplate: {
checkedIds: wellplate.checkedIds.toArray(),
uncheckedIds: wellplate.uncheckedIds.toArray(),
checkedAll: wellplate.checkedAll,
},
currentCollection: currentCollection.id,
isSync: isSync,
}
}
......@@ -3,15 +3,15 @@ class Reporter::ExcelExport
DEFAULT_ROW_HEIGHT = 20
def initialize
@@sample_list = Array.new
@sample_list = Array.new
end
def add_sample(sample)
@@sample_list << sample
@sample_list << sample
end
def generate_file(default_excluded_field, default_included_field, removed_field = [])
return -1 if @@sample_list.empty? || @@sample_list.first == nil
return -1 if @sample_list.empty? || @sample_list.first == nil
header = process_header(default_excluded_field, default_included_field, removed_field)
return -1 if header.empty?
p = Axlsx::Package.new
......@@ -23,7 +23,7 @@ class Reporter::ExcelExport
files = [] # do not let Tempfile object to be garbage collected
need_images = header.index("Image")
@@sample_list.compact.each_with_index do |sample, row|
@sample_list.compact.each_with_index do |sample, row|
data_hash = []
start = 0
......@@ -50,7 +50,7 @@ class Reporter::ExcelExport
end
def process_header(default_excluded_field, default_included_field, removed_field)
header = @@sample_list.first.attribute_names
header = @sample_list.first.attribute_names
# Exclude field
header.delete_if { |x| default_excluded_field.include?(x) }
header = header.reject { |x| x.empty? }
......
class Reporter::SdfExport
def initialize
@@sample_list = Array.new
@sample_list = Array.new
end
def add_sample(sample)
@@sample_list << sample
@sample_list << sample
end
def generate_file(default_excluded_field, default_included_field, removed_field = [])
return -1 if @@sample_list.empty? || @@sample_list.first == nil
return -1 if @sample_list.empty? || @sample_list.first == nil
mdf_string = ""
# We dont want to export Sample description
default_excluded_field += ["description", "image", "molfile"]
......@@ -16,13 +16,13 @@ class Reporter::SdfExport
x.slice!("molecule.")
x
}
included_field = @@sample_list.first.attribute_names
included_field = @sample_list.first.attribute_names
# Exclude field
included_field.delete_if { |x| default_excluded_field.include?(x) }
included_field = included_field.reject { |x| x.empty? }
included_field = included_field.uniq - removed_field
@@sample_list.compact.each_with_index do |sample, index|
@sample_list.compact.each_with_index do |sample, index|
sample_mdf = sample.molfile.split(/#|END/).first + "END\n"
(default_included_field - removed_field).each do |field|
......
......@@ -56,13 +56,27 @@ describe Chemotion::ReportAPI do
before do
params = {
type: 'sample',
exportType: 1,
checkedIds: [sample_1.id],
uncheckedIds: [],
checkedAll: false,
currentCollection: c.id,
removedColumns: %w(
uiState: {
sample: {
checkedIds: [sample_1.id],
uncheckedIds: [],
checkedAll: false,
},
reaction: {
checkedIds: [],
uncheckedIds: [],
checkedAll: false,
},
wellplate: {
checkedIds: [],
uncheckedIds: [],
checkedAll: false,
},
currentCollection: c.id,
isSync: false
},
columns: %w(
target_amount_value target_amount_unit
created_at updated_at molfile
)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment