Search in sources :

Example 1 with CSVDataImportSettings

use of org.openforis.collect.io.data.csv.CSVDataImportSettings in project collect by openforis.

the class CSVDataImportService method start.

@Secured("ROLE_ADMIN")
public DataImportStatusProxy start(String tempFileName, int parentEntityId, Step step, boolean transactional, boolean validateRecords, boolean insertNewRecords, String newRecordVersionName, boolean deleteExistingEntities) throws DataImportExeption {
    if (importJob == null || !importJob.isRunning()) {
        File importFile = new File(tempFileName);
        SessionState sessionState = sessionManager.getSessionState();
        CollectSurvey survey = sessionState.getActiveSurvey();
        if (transactional) {
            importJob = jobManager.createJob(TransactionalCSVDataImportJob.class);
        } else {
            importJob = jobManager.createJob(CSVDataImportJob.BEAN_NAME, CSVDataImportJob.class);
        }
        CSVDataImportSettings settings = new CSVDataImportSettings();
        settings.setRecordValidationEnabled(validateRecords);
        settings.setInsertNewRecords(insertNewRecords);
        settings.setNewRecordVersionName(newRecordVersionName);
        settings.setDeleteExistingEntities(deleteExistingEntities);
        Step[] steps = step == null ? Step.values() : new Step[] { step };
        CSVDataImportInput input = new CSVDataImportInput(importFile, survey, steps, parentEntityId, settings);
        importJob.setInput(input);
        jobManager.start(importJob);
    }
    return getStatus();
}
Also used : TransactionalCSVDataImportJob(org.openforis.collect.io.data.TransactionalCSVDataImportJob) CSVDataImportJob(org.openforis.collect.io.data.CSVDataImportJob) SessionState(org.openforis.collect.web.session.SessionState) CSVDataImportInput(org.openforis.collect.io.data.CSVDataImportJob.CSVDataImportInput) Step(org.openforis.collect.model.CollectRecord.Step) CollectSurvey(org.openforis.collect.model.CollectSurvey) TransactionalCSVDataImportJob(org.openforis.collect.io.data.TransactionalCSVDataImportJob) CSVDataImportSettings(org.openforis.collect.io.data.csv.CSVDataImportSettings) File(java.io.File) Secured(org.springframework.security.access.annotation.Secured)

Example 2 with CSVDataImportSettings

use of org.openforis.collect.io.data.csv.CSVDataImportSettings in project collect by openforis.

the class RecordController method startCsvDataImportJob.

@RequestMapping(value = "survey/{surveyId}/data/csvimport/records", method = POST, consumes = MULTIPART_FORM_DATA_VALUE)
@ResponseBody
public JobView startCsvDataImportJob(@PathVariable("surveyId") int surveyId, @RequestParam("file") MultipartFile multipartFile, @RequestParam String rootEntityName, @RequestParam String importType, @RequestParam String steps, @RequestParam(required = false) Integer entityDefinitionId, @RequestParam(required = false) boolean validateRecords, @RequestParam(required = false) boolean deleteEntitiesBeforeImport, @RequestParam(required = false) String newRecordVersionName) throws IOException {
    File file = Files.writeToTempFile(multipartFile.getInputStream(), multipartFile.getOriginalFilename(), "ofc_csv_data_import");
    CollectSurvey survey = surveyManager.getById(surveyId);
    CSVDataImportJob job = jobManager.createJob(TransactionalCSVDataImportJob.class);
    CSVDataImportSettings settings = new CSVDataImportSettings();
    settings.setDeleteExistingEntities(deleteEntitiesBeforeImport);
    settings.setRecordValidationEnabled(validateRecords);
    settings.setInsertNewRecords("newRecords".equals(importType));
    settings.setNewRecordVersionName(newRecordVersionName);
    CSVDataImportInput input = new CSVDataImportInput(file, survey, fromStepNames(steps), entityDefinitionId, settings);
    job.setInput(input);
    jobManager.start(job);
    this.csvDataImportJob = job;
    return new JobView(job);
}
Also used : TransactionalCSVDataImportJob(org.openforis.collect.io.data.TransactionalCSVDataImportJob) CSVDataImportJob(org.openforis.collect.io.data.CSVDataImportJob) JobView(org.openforis.collect.web.controller.CollectJobController.JobView) CSVDataImportInput(org.openforis.collect.io.data.CSVDataImportJob.CSVDataImportInput) CollectSurvey(org.openforis.collect.model.CollectSurvey) CSVDataImportSettings(org.openforis.collect.io.data.csv.CSVDataImportSettings) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 3 with CSVDataImportSettings

use of org.openforis.collect.io.data.csv.CSVDataImportSettings in project collect by openforis.

the class CSVDataImportJobIntegrationTest method importCSVFile.

public CSVDataImportJob importCSVFile(String fileName, int parentEntityDefinitionId, boolean transactional, boolean insertNewRecords, String newRecordVersionName, boolean createAncestorEntities) throws Exception {
    CSVDataImportSettings settings = new CSVDataImportSettings();
    settings.setInsertNewRecords(insertNewRecords);
    settings.setNewRecordVersionName(newRecordVersionName);
    settings.setCreateAncestorEntities(createAncestorEntities);
    return importCSVFile(fileName, parentEntityDefinitionId, transactional, settings);
}
Also used : CSVDataImportSettings(org.openforis.collect.io.data.csv.CSVDataImportSettings)

Example 4 with CSVDataImportSettings

use of org.openforis.collect.io.data.csv.CSVDataImportSettings in project collect by openforis.

the class CSVDataImportJobIntegrationTest 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);
    CSVDataImportJob process = importCSVFile(VALID_NESTED_ENTITY_TEST_CSV, plotDefn.getId(), true, settings);
    assertTrue(process.isCompleted());
    assertTrue(process.getParsingErrors().isEmpty());
    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) CodeAttribute(org.openforis.idm.model.CodeAttribute) CSVDataImportSettings(org.openforis.collect.io.data.csv.CSVDataImportSettings) CollectIntegrationTest(org.openforis.collect.CollectIntegrationTest) Test(org.junit.Test)

Example 5 with CSVDataImportSettings

use of org.openforis.collect.io.data.csv.CSVDataImportSettings in project collect by openforis.

the class CSVDataImportProcessIntegrationTest method importCSVFile.

public CSVDataImportProcess importCSVFile(String fileName, int parentEntityDefinitionId, boolean transactional, boolean insertNewRecords, String newRecordVersionName, boolean createAncestorEntities) throws Exception {
    CSVDataImportSettings settings = new CSVDataImportSettings();
    settings.setInsertNewRecords(insertNewRecords);
    settings.setNewRecordVersionName(newRecordVersionName);
    settings.setCreateAncestorEntities(createAncestorEntities);
    return importCSVFile(fileName, parentEntityDefinitionId, transactional, settings);
}
Also used : CSVDataImportSettings(org.openforis.collect.io.data.csv.CSVDataImportSettings)

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