Skip to content

Commit

Permalink
Merge pull request #679 from luomus/dont-require-mandatory-columns-wh…
Browse files Browse the repository at this point in the history
…en-deleting-in-excel-import-183507263

Dont require mandatory columns when deleting in excel import 183507263
  • Loading branch information
Blodir authored Nov 25, 2024
2 parents fd823fa + b1fa2a7 commit 7271fe6
Showing 1 changed file with 35 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,6 @@ import { DocumentJobPayload } from '../../../shared/api/DocumentApi';
import { toHtmlSelectElement } from '../../../shared/service/html-element.service';
import {ModalRef, ModalService} from 'projects/laji-ui/src/lib/modal/modal.service';

/*
Check that required columns have a non-empty cell at each row.
Example data:
[
{ "B": "data2", "C": "data3", "D": "data4" },
{ "A": "data1", "B": "data2", "C": "data3", "D": "data4" }
]
Example columnMap:
{ "A": "gatheringEvent.leg[*]", "B": "gatheringEvent.dateBegin",
"C": "gatherings[*].geometry", "D": "gatherings[*].units[*].identifications[*].taxon" }
*/
const checkEarlyValidation = (data: {[key: string]: string}[], columnMap: {[key: string]: string}) => (
data.every(row => {
const entries = Object.entries(row);
const legIsPresent = entries.some(([k,v]) =>
columnMap[k] === 'gatheringEvent.leg[*]');
const placeIsPresent = entries.some(([k,v]) =>
columnMap[k] === 'namedPlaceID'
|| columnMap[k] === 'gatherings[*].namedPlaceID' // nimetty paikka
|| columnMap[k] === 'gatherings[*].geometry' // koordinaatit
|| columnMap[k] === 'gatherings[*].locality' // paikannimet
);
return legIsPresent && placeIsPresent;
})
);

@Component({
selector: 'laji-importer',
templateUrl: './importer.component.html',
Expand Down Expand Up @@ -171,6 +144,35 @@ export class ImporterComponent implements OnInit, OnDestroy {
this.spreadsheetFacade.goToStep(Step.empty);
}

/*
Check that required columns have a non-empty cell at each row.
Example data:
[
{ "B": "data2", "C": "data3", "D": "data4" },
{ "A": "data1", "B": "data2", "C": "data3", "D": "data4" }
]
Example columnMap:
{ "A": "gatheringEvent.leg[*]", "B": "gatheringEvent.dateBegin",
"C": "gatherings[*].geometry", "D": "gatherings[*].units[*].identifications[*].taxon" }
*/
checkEarlyValidation = (data: {[key: string]: string}[], columnMap: {[key: string]: string}) => (
data.every(row => {
const entries = Object.entries(row);
const isDelete = entries.some(([k,v]) =>
columnMap[k] === 'delete' && this.getMappedValue(v, this.fields[columnMap[k]]));
const legIsPresent = entries.some(([k,v]) =>
columnMap[k] === 'gatheringEvent.leg[*]');
const placeIsPresent = entries.some(([k,v]) =>
columnMap[k] === 'namedPlaceID'
|| columnMap[k] === 'gatherings[*].namedPlaceID' // nimetty paikka
|| columnMap[k] === 'gatherings[*].geometry' // koordinaatit
|| columnMap[k] === 'gatherings[*].locality' // paikannimet
);
return (legIsPresent && placeIsPresent) || isDelete;
})
);

onFileChange(event: Event) {
this.fileLoading = true;
this.valid = false;
Expand Down Expand Up @@ -318,7 +320,7 @@ export class ImporterComponent implements OnInit, OnDestroy {
}

colMappingDone(mapping: any) {
if (checkEarlyValidation(this.data!, mapping)) {
if (this.checkEarlyValidation(this.data!, mapping)) {
this.spreadsheetFacade.goToStep(Step.dataMapping);
this.colMap = mapping;
} else {
Expand Down Expand Up @@ -684,7 +686,7 @@ export class ImporterComponent implements OnInit, OnDestroy {
}
const field = fields[mapping[col]];
const value = this.mappingService.getLabel(
this.mappingService.map(this.mappingService.rawValueToArray(row[col], field), field, true),
this.getMappedValue(row[col], field),
field
);
if (!this.importService.hasValue(value)) {
Expand All @@ -698,4 +700,8 @@ export class ImporterComponent implements OnInit, OnDestroy {
});
return result;
}

private getMappedValue(rawValue, field) {
return this.mappingService.map(this.mappingService.rawValueToArray(rawValue, field), field, true);
}
}

0 comments on commit 7271fe6

Please sign in to comment.