Search in sources :

Example 6 with CSVDataImportSettings

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());
    }
}
Also used : CollectRecord(org.openforis.collect.model.CollectRecord) EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) Entity(org.openforis.idm.model.Entity) ParsingError(org.openforis.collect.io.metadata.parsing.ParsingError) CodeAttribute(org.openforis.idm.model.CodeAttribute) CSVDataImportSettings(org.openforis.collect.io.data.csv.CSVDataImportSettings) CollectIntegrationTest(org.openforis.collect.CollectIntegrationTest) Test(org.junit.Test)

Example 7 with CSVDataImportSettings

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);
    }
}
Also used : ArrayList(java.util.ArrayList) CSVDataImportSettings(org.openforis.collect.io.data.csv.CSVDataImportSettings) EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) File(java.io.File)

Aggregations

CSVDataImportSettings (org.openforis.collect.io.data.csv.CSVDataImportSettings)7 File (java.io.File)3 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)3 Test (org.junit.Test)2 CollectIntegrationTest (org.openforis.collect.CollectIntegrationTest)2 CSVDataImportJob (org.openforis.collect.io.data.CSVDataImportJob)2 CSVDataImportInput (org.openforis.collect.io.data.CSVDataImportJob.CSVDataImportInput)2 TransactionalCSVDataImportJob (org.openforis.collect.io.data.TransactionalCSVDataImportJob)2 CollectRecord (org.openforis.collect.model.CollectRecord)2 CollectSurvey (org.openforis.collect.model.CollectSurvey)2 CodeAttribute (org.openforis.idm.model.CodeAttribute)2 Entity (org.openforis.idm.model.Entity)2 ArrayList (java.util.ArrayList)1 ParsingError (org.openforis.collect.io.metadata.parsing.ParsingError)1 Step (org.openforis.collect.model.CollectRecord.Step)1 JobView (org.openforis.collect.web.controller.CollectJobController.JobView)1 SessionState (org.openforis.collect.web.session.SessionState)1 Secured (org.springframework.security.access.annotation.Secured)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1