use of org.openforis.collect.io.data.csv.CSVDataImportSettings in project collect by openforis.
the class CSVDataImportProcessIntegrationTest method validDeleteExistingEntitiesTest.
@Test
public void validDeleteExistingEntitiesTest() throws Exception {
CollectRecord record = createTestRecord(survey, "10_114");
recordDao.insert(record);
EntityDefinition clusterDefn = survey.getSchema().getRootEntityDefinition("cluster");
EntityDefinition plotDefn = (EntityDefinition) clusterDefn.getChildDefinition("plot");
assertEquals(3, record.getRootEntity().getCount(plotDefn));
CSVDataImportSettings settings = new CSVDataImportSettings();
settings.setDeleteExistingEntities(true);
CSVDataImportProcess process = importCSVFile(VALID_NESTED_ENTITY_TEST_CSV, plotDefn.getId(), true, settings);
ReferenceDataImportStatus<ParsingError> status = process.getStatus();
assertTrue(status.isComplete());
assertTrue(status.getSkippedRows().isEmpty());
assertEquals(3, status.getProcessed());
CollectRecord reloadedRecord = recordDao.load(survey, record.getId(), Step.ENTRY);
assertEquals(2, reloadedRecord.getRootEntity().getCount(plotDefn));
Entity reloadedCluster = reloadedRecord.getRootEntity();
{
Entity plot = reloadedCluster.findChildEntitiesByKeys("plot", "1", "A").get(0);
CodeAttribute landUse = (CodeAttribute) plot.getChild("land_use");
assertEquals("2", landUse.getValue().getCode());
}
{
Entity plot = reloadedCluster.findChildEntitiesByKeys("plot", "2", "B").get(0);
CodeAttribute landUse = (CodeAttribute) plot.getChild("land_use");
assertEquals("3", landUse.getValue().getCode());
}
}
use of org.openforis.collect.io.data.csv.CSVDataImportSettings in project collect by openforis.
the class CSVDataImportJob method buildTasks.
@Override
protected void buildTasks() throws Throwable {
String extension = FilenameUtils.getExtension(input.file.getName());
if (Files.ZIP_FILE_EXTENSION.equalsIgnoreCase(extension)) {
tempInputFilesFolder = Files.createTempDirectory();
ZipFiles.extract(input.file, tempInputFilesFolder);
List<String> fileNames = Files.listFileNamesInFolder(tempInputFilesFolder);
List<String> processedFileNames = new ArrayList<String>(fileNames);
Map<String, EntityDefinition> entityDefinitionsByFileName = new CSVDataExportJob.EntryNameGenerator().generateMultipleEntitesEntryMap(input.survey);
for (Entry<String, EntityDefinition> entry : entityDefinitionsByFileName.entrySet()) {
String fileName = entry.getKey();
File file = new File(tempInputFilesFolder, fileName);
if (file.exists()) {
EntityDefinition entityDef = entry.getValue();
CSVDataImportTask task = createTask(CSVDataImportTask.class);
CSVDataImportSettings settings = input.settings.clone();
if (!entityDef.isRoot()) {
settings.setInsertNewRecords(false);
}
task.input = new CSVDataImportInput(file, input.survey, input.steps, entityDef.getId(), settings);
addTask(task);
processedFileNames.add(fileName);
}
}
if (processedFileNames.size() < fileNames.size()) {
throw new IllegalStateException("Invalid file names found: some of the included files cannot be imported");
}
} else {
CSVDataImportTask task = createTask(CSVDataImportTask.class);
task.input = input;
addTask(task);
}
}
Aggregations