Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 64 additions & 41 deletions src/controllers/darwincore-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const {
Subfamilia,
Autor,
Coletor,
ColetorComplementar,
Variedade,
Subespecie,
ColecaoAnexa,
Expand Down Expand Up @@ -47,6 +48,21 @@ function obtemNomeArquivoCsv() {
return `hcf_${data}.csv`;
}

function csvEscape(valor) {
if (valor === null || valor === undefined) {
return '';
}
const str = String(valor);
if (str.includes(',') || str.includes('"') || str.includes('\n') || str.includes('\r')) {
return `"${str.replace(/"/g, '""')}"`;
}
return str;
}

function csvLinha(campos) {
return campos.map(csvEscape).join(',');
}

const obterModeloDarwinCoreLotes = async (limit, offset, request, response) => {
const entidadeTombo = await Tombo.findAll({
limit,
Expand Down Expand Up @@ -87,6 +103,7 @@ const obterModeloDarwinCoreLotes = async (limit, offset, request, response) => {
'relevo_id',
'solo_id',
'coletor_id',
'cidade_id',
],
include: [
{
Expand Down Expand Up @@ -177,6 +194,10 @@ const obterModeloDarwinCoreLotes = async (limit, offset, request, response) => {
{
model: Coletor,
},
{
model: ColetorComplementar,
as: 'coletor_complementar',
},
{
model: Genero,
},
Expand Down Expand Up @@ -210,8 +231,7 @@ const obterModeloDarwinCoreLotes = async (limit, offset, request, response) => {
const paisCodigo = tombo?.cidade?.estado?.paise?.sigla ?? '';
const paranaNome = tombo?.cidade?.estado?.nome ?? '';
const cidadeNome = tombo?.cidade?.nome ?? '';
const vegetacao
= tombo.locais_coletum && tombo.locais_coletum.vegetacao ? tombo.locais_coletum.vegetacao.nome : '';
const vegetacao = tombo.vegetaco ? tombo.vegetaco.nome : '';
const familiaNome = tombo.familia ? tombo.familia.nome : '';
const generoNome = tombo.genero ? tombo.genero.nome : '';
const especieNome = tombo.especy ? tombo.especy.nome : '';
Expand All @@ -223,17 +243,20 @@ const obterModeloDarwinCoreLotes = async (limit, offset, request, response) => {
if (tombo.variedade) {
menorNomeCientifico = tombo.variedade.nome;
}
if (tombo.especy && tombo.especy.autore) {
autores = tombo.especy.autore.nome;
if (tombo.especy && tombo.especy.autor) {
autores = tombo.especy.autor.nome;
}
if (tombo.sub_especy && tombo.sub_especy.autore) {
autores += `| ${tombo.sub_especy.autore.nome}`;
if (tombo.sub_especy && tombo.sub_especy.autor) {
autores += `| ${tombo.sub_especy.autor.nome}`;
}
if (tombo.variedade && tombo.variedade.autore) {
autores += `| ${tombo.variedade.autore.nome}`;
if (tombo.variedade && tombo.variedade.autor) {
autores += `| ${tombo.variedade.autor.nome}`;
}
if (tombo.coletores && tombo.coletores.length > 0) {
coletores = tombo.coletores.map(coletor => padronizarNomeDarwincore(coletor.nome)).join(' | ');
if (tombo.coletore) {
coletores = padronizarNomeDarwincore(tombo.coletore.nome);
if (tombo.coletor_complementar?.complementares) {
coletores += ` | ${tombo.coletor_complementar.complementares}`;
}
}
if (tombo.data_coleta_ano) {
dataColeta = tombo.data_coleta_ano;
Expand Down Expand Up @@ -281,41 +304,43 @@ const obterModeloDarwinCoreLotes = async (limit, offset, request, response) => {
tombo.tombos_fotos.forEach(foto => {
const dataAtualizacao = format(tombo.updated_at, 'yyyy-MM-dd');

let linha = [
`PreservedSpecimen\tColecao\tpt\t${dataAtualizacao}\t02.032.297/0005-26\t`,
'UTFPR\tHerbario da Universidade Tecnologica Federal do Parana – Campus Campo Mourao – HCF\t',
`${license}\tUTFPR\t{"barcode":${foto.codigo_barra}}\tBr:UTFPR:HCF:${tombo.hcf.toString()}`,
`${foto.id.toString()}\t${tombo.hcf.toString()}${foto.id.toString()}\t${coletores}\t`,
`${tombo.numero_coleta}\t\t${tombo.observacao}\t${dataColeta}\t`,
`${tombo.data_coleta_ano}\t${tombo.data_coleta_mes}\t${tombo.data_coleta_dia}\t`,
`${vegetacao}\t${'América do Sul'}\t${paisNome}\t${paisCodigo}\t${paranaNome}\t`,
`${cidadeNome}\t${tombo.altitude}\t${tombo.altitude}\t\t\t${tombo.latitude}\t`,
`${tombo.longitude}\t${'WGS84'}\t${'GPS'}\t${'Plantae'}\t${familiaNome}\t${generoNome}\t`,
`${especieNome}\t${menorNomeCientifico}\t${tombo.nome_cientifico}\t${autores}\t${tombo.taxon}`,
`\t${tombo.nomes_populares}\t\t${nomeTipo}\t${nomeIdentificador}\t${dataIdentificacao}`,
`\t${identificationQualifier}`,
].join('');
const campos = [
'PreservedSpecimen', 'Colecao', 'pt', dataAtualizacao, '02.032.297/0005-26',
'UTFPR', 'Herbario da Universidade Tecnologica Federal do Parana – Campus Campo Mourao – HCF',
license, 'UTFPR', `{"barcode":"${foto.codigo_barra}"}`, `Br:UTFPR:HCF:${tombo.hcf}`,
tombo.hcf, coletores,
tombo.numero_coleta, '', tombo.observacao, dataColeta,
tombo.data_coleta_ano, tombo.data_coleta_mes, tombo.data_coleta_dia,
vegetacao, 'América do Sul', paisNome, paisCodigo, paranaNome,
cidadeNome, tombo.altitude, tombo.altitude, '', '', tombo.latitude,
tombo.longitude, 'WGS84', 'GPS', 'Plantae', familiaNome, generoNome,
especieNome, menorNomeCientifico, tombo.nome_cientifico, autores, tombo.taxon,
tombo.nomes_populares, '', nomeTipo, nomeIdentificador, dataIdentificacao,
identificationQualifier,
];
let linha = csvLinha(campos);
linha = linha.replace(/(null|undefined)/g, '');

linhasProcessadas.push(`${linha.replace(/[\r\n]/g, '')}\n`);
});
} else {
const dataAtualizacao = format(tombo.updated_at, 'yyyy-MM-dd');

let linha = [
`PreservedSpecimen\tColecao\tpt\t${dataAtualizacao}\t02.032.297/0005-26\t`,
'UTFPR\tHerbario da Universidade Tecnologica Federal do Parana – Campus Campo Mourao – HCF\t',
`${license}\tUTFPR\t{"barcode":}\tBr:UTFPR:HCF:${tombo.hcf.toString()}`,
`\t${tombo.hcf.toString()}\t${coletores}\t`,
`${tombo.numero_coleta}\t\t${tombo.observacao}\t${dataColeta}\t`,
`${tombo.data_coleta_ano}\t${tombo.data_coleta_mes}\t${tombo.data_coleta_dia}\t`,
`${vegetacao}\t${'América do Sul'}\t${paisNome}\t${paisCodigo}\t${paranaNome}\t`,
`${cidadeNome}\t${tombo.altitude}\t${tombo.altitude}\t\t\t${tombo.latitude}\t`,
`${tombo.longitude}\t${'WGS84'}\t${'GPS'}\t${'Plantae'}\t${familiaNome}\t${generoNome}\t`,
`${especieNome}\t${menorNomeCientifico}\t${tombo.nome_cientifico}\t${autores}\t${tombo.taxon}`,
`\t${tombo.nomes_populares}\t\t${nomeTipo}\t${nomeIdentificador}\t${dataIdentificacao}`,
`\t${identificationQualifier}`,
].join('');
const campos = [
'PreservedSpecimen', 'Colecao', 'pt', dataAtualizacao, '02.032.297/0005-26',
'UTFPR', 'Herbario da Universidade Tecnologica Federal do Parana – Campus Campo Mourao – HCF',
license, 'UTFPR', '{"barcode":""}', `Br:UTFPR:HCF:${tombo.hcf}`,
tombo.hcf, coletores,
tombo.numero_coleta, '', tombo.observacao, dataColeta,
tombo.data_coleta_ano, tombo.data_coleta_mes, tombo.data_coleta_dia,
vegetacao, 'América do Sul', paisNome, paisCodigo, paranaNome,
cidadeNome, tombo.altitude, tombo.altitude, '', '', tombo.latitude,
tombo.longitude, 'WGS84', 'GPS', 'Plantae', familiaNome, generoNome,
especieNome, menorNomeCientifico, tombo.nome_cientifico, autores, tombo.taxon,
tombo.nomes_populares, '', nomeTipo, nomeIdentificador, dataIdentificacao,
identificationQualifier,
];
let linha = csvLinha(campos);

linha = linha.replace(/null|undefined/g, '');

Expand All @@ -330,9 +355,7 @@ export const obterModeloDarwinCore = async (request, response, next) => {

const limit = request.query.limit > 1000 ? 1000 : request.query.limit || 1000;

const quantidadeTombos = await Tombo.count(
{ distinct: true },
);
const quantidadeTombos = await Tombo.count({ col: 'hcf' });

const cabecalho = colunasComoLinhaUnica();

Expand Down
13 changes: 10 additions & 3 deletions src/controllers/fichas-tombos-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,16 @@ export default function fichaTomboController(request, response, next) {
const dataTombo = new Date(tombo.data_tombo);
const romanoDataTombo = (`${dataTombo.getDate()}/${romanos[dataTombo.getMonth()]}/${dataTombo.getFullYear()}`);

const identificador = tombo.identificadores?.[0]?.nome
&& tombo.identificadores?.[0]?.nome.toLowerCase() !== 'não-identificado'
? tombo.identificadores?.[0]?.nome
const identificador = tombo.identificadores && tombo.identificadores.length > 0
? tombo.identificadores
.filter(ident => ident.nome && ident.nome.toLowerCase() !== 'não-identificado')
.sort((a, b) => {
const ordemA = a.tombos_identificadores?.ordem || 0;
const ordemB = b.tombos_identificadores?.ordem || 0;
return ordemA - ordemB;
})
.map(ident => ident.nome)
.join('; ')
: '';

const romanoDataIdentificacao = formataDataIdentificacao(
Expand Down
14 changes: 8 additions & 6 deletions src/controllers/splinker-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const obterModeloSPlinkerLotes = async (limit, offset, request, response) => {
'data_identificacao_ano',
'data_coleta_dia',
'observacao',
'descricao',
'nomes_populares',
'numero_coleta',
'updated_at',
Expand Down Expand Up @@ -138,7 +139,8 @@ const obterModeloSPlinkerLotes = async (limit, offset, request, response) => {

tombos.forEach(tombo => {
const kingdom = tombo.familia?.reino?.nome || '\t';
const family = tombo.familia?.nome || '\t';
const familyName = tombo.familia?.nome;
const family = (familyName && familyName.toLowerCase() !== 'indeterminada') ? familyName : '\t';
const genus = tombo.genero?.nome || '\t';
const species = tombo.especy?.nome || '\t';
const scientificNameAuthor = tombo.especy?.autor?.nome || '\t';
Expand All @@ -156,7 +158,7 @@ const obterModeloSPlinkerLotes = async (limit, offset, request, response) => {
const collectorName = tombo.coletore?.nome || '\t';
const collectorNumber = tombo.numero_coleta || '\t';
const country = tombo.locais_coletum?.cidade?.estado?.paise?.nome || '\t';
const stateOrProvince = tombo.locais_coletum?.cidade?.estado?.sigla || '\t';
const stateOrProvince = tombo.locais_coletum?.cidade?.estado?.sigla?.trim() || '\t';
const city = tombo.locais_coletum?.cidade?.nome || '\t';
const locality = tombo.locais_coletum?.descricao || '\t';
const latitude = tombo.latitude
Expand All @@ -165,7 +167,7 @@ const obterModeloSPlinkerLotes = async (limit, offset, request, response) => {
const longitude = tombo.longitude
? converteDecimalParaGrausMinutosSegundos(tombo.longitude, true, true)
: '\t';
const elevation = tombo.altitude + ' m' || '\t';
const elevation = tombo.altitude ? `${tombo.altitude} m` : '\t';
const identificationDate = [
tombo.data_identificacao_ano,
tombo.data_identificacao_mes?.toString().padStart(2, '0'),
Expand All @@ -174,11 +176,11 @@ const obterModeloSPlinkerLotes = async (limit, offset, request, response) => {
.filter(Boolean)
.join('-');
const identifierName = tombo.identificadores
? tombo.identificadores.map(i => i.nome).join(', ')
? tombo.identificadores.map(i => i.nome).join(';')
: '\t';
const notes = tombo.tombos_fotos?.length > 0
? `${tombo.tombos_fotos.map(foto => `[BARCODE=${foto.codigo_barra}]`).join(' , ')} ${tombo.observacao || '\t'}`
: tombo.observacao || '\t';
? `${tombo.tombos_fotos.map(foto => `[BARCODE=${foto.codigo_barra}]`).join(' , ')} ${tombo.descricao || '\t'}`
: tombo.descricao || '\t';

const linha = [
kingdom,
Expand Down
81 changes: 45 additions & 36 deletions src/controllers/tombos-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -681,61 +681,72 @@ export const desativar = (request, response, next) => {
export const listagem = (request, response, next) => {
const { pagina, limite, offset } = request.paginacao;
const {
nome_cientifico: nomeCientifico, hcf, tipo, nome_popular: nomePopular, situacao,
nome_cientifico: nomeCientifico,
hcf,
tipo,
nome_popular: nomePopular,
situacao,
codigo_barra_foto,
} = request.query;

let where = {
rascunho: false,
};

if (nomeCientifico) {
where = {
...where,
nome_cientifico: { [Op.iLike]: `%${nomeCientifico}%` },
};
where.nome_cientifico = { [Op.iLike]: `%${nomeCientifico}%` };
}

if (hcf) {
where = {
...where,
hcf,
};
where.hcf = hcf;
}

if (tipo) {
where = {
...where,
tipo_id: tipo,
};
where.tipo_id = tipo;
}

if (nomePopular) {
where = {
...where,
nomes_populares: { [Op.iLike]: `%${nomePopular}%` },
};
where.nomes_populares = { [Op.iLike]: `%${nomePopular}%` };
}

if (situacao) {
where = {
...where,
situacao,
};
where.situacao = situacao;
}

let include = [
{
model: Coletor,
attributes: ['id', 'nome'],
required: false,
},
];

if (codigo_barra_foto) {
include.push({
model: TomboFoto,
where: {
codigo_barra: codigo_barra_foto,
},
attributes: ['id', 'codigo_barra', 'tombo_hcf'],
required: true,
});
}

let retorno = { // eslint-disable-line
let retorno = {
metadados: {
total: 0,
pagina,
limite,
},
tombos: [],
};

Promise.resolve()
.then(() => Tombo.count({ where }))
.then(() => Tombo.count({
where,
include: codigo_barra_foto ? include : [],
distinct: true,
}))
.then(total => {
retorno.metadados.total = total;
})
.then(() => Tombo.findAndCountAll({
.then(() => Tombo.findAll({
attributes: [
'hcf',
'nomes_populares',
Expand All @@ -744,21 +755,19 @@ export const listagem = (request, response, next) => {
'data_coleta_mes',
'data_coleta_ano',
'created_at',
'coletor_id',
'tipo_id',
],
include: {
model: Coletor,
attributes: ['id', 'nome'],
required: false,
},
include,
where,
subQuery: false,
order: [['hcf', 'DESC']],
limit: limite,
offset,
}))
.then(listaTombos => {
retorno.tombos = listaTombos.rows;
response.status(codigos.LISTAGEM)
.json(retorno);
retorno.tombos = listaTombos;
response.status(codigos.LISTAGEM).json(retorno);
})
.catch(next);
};
Expand Down
2 changes: 1 addition & 1 deletion src/resources/darwincore/cabecalho.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ const colunas = [
export default colunas;

export function colunasComoLinhaUnica() {
return `${colunas.join('\t')}\n`;
return `${colunas.join(',')}\n`;
}
Loading