Search in sources :

Example 1 with CollectEarthGridTemplateGenerator

use of org.openforis.collect.io.metadata.collectearth.CollectEarthGridTemplateGenerator in project collect by openforis.

the class SurveyFileVM method validateFileContent.

private boolean validateFileContent(Binder binder) {
    String typeName = getFormFieldValue(binder, SurveyFileFormObject.TYPE_FIELD_NAME);
    SurveyFileType type = SurveyFileType.valueOf(typeName);
    switch(type) {
        case COLLECT_EARTH_GRID:
            CollectEarthGridTemplateGenerator templateGenerator = new CollectEarthGridTemplateGenerator();
            CSVFileValidationResult headersValidationResult = templateGenerator.validate(uploadedFile, survey, new ValidationParameters());
            if (headersValidationResult.isSuccessful()) {
                return true;
            } else {
                switch(headersValidationResult.getErrorType()) {
                    case INVALID_FILE_TYPE:
                        MessageUtil.showWarning("survey.file.error.invalid_file_type", "CSV (Comma Separated Values)");
                        // don't block the user
                        return true;
                    case INVALID_HEADERS:
                        MessageUtil.showWarning("survey.file.type.collect_earth_grid.error.invalid_file_structure", new Object[] { headersValidationResult.getExpectedHeaders().toString(), headersValidationResult.getFoundHeaders().toString() });
                        // don't block the user
                        return true;
                    case INVALID_NUMBER_OF_PLOTS_WARNING:
                        MessageUtil.showWarning("survey.file.error.warning_csv_size", CollectEarthGridTemplateGenerator.CSV_LENGTH_WARNING + "");
                        // don't block the user
                        return true;
                    case INVALID_NUMBER_OF_PLOTS_TOO_LARGE:
                        MessageUtil.showWarning("survey.file.error.error_csv_size", CollectEarthGridTemplateGenerator.CSV_LENGTH_ERROR + "");
                        // block the user , a file so large would make the CEP file unusable!
                        return false;
                    default:
                        return true;
                }
            }
        default:
            return true;
    }
}
Also used : SurveyFileType(org.openforis.collect.model.SurveyFile.SurveyFileType) ValidationParameters(org.openforis.collect.manager.validation.SurveyValidator.ValidationParameters) CSVFileValidationResult(org.openforis.collect.io.metadata.collectearth.CSVFileValidationResult) CollectEarthGridTemplateGenerator(org.openforis.collect.io.metadata.collectearth.CollectEarthGridTemplateGenerator)

Example 2 with CollectEarthGridTemplateGenerator

use of org.openforis.collect.io.metadata.collectearth.CollectEarthGridTemplateGenerator in project collect by openforis.

the class SurveyValidator method validateCollectEarthGridFile.

private List<SurveyValidationResult> validateCollectEarthGridFile(CollectSurvey survey, SurveyFile surveyFile, ValidationParameters validationParameters) {
    List<SurveyValidationResult> results = new ArrayList<SurveyValidator.SurveyValidationResult>();
    byte[] fileContent = surveyManager.loadSurveyFileContent(surveyFile);
    ByteArrayInputStream is = new ByteArrayInputStream(fileContent);
    File file = OpenForisIOUtils.copyToTempFile(is);
    CSVFileValidationResult fileValidationResult = new CollectEarthGridTemplateGenerator().validate(file, survey, validationParameters);
    if (!fileValidationResult.isSuccessful()) {
        SurveyValidationResult validationResult = null;
        switch(fileValidationResult.getErrorType()) {
            case INVALID_FILE_TYPE:
                validationResult = new SurveyValidationResult(Flag.WARNING, String.format(SURVEY_FILE_PATH_FORMAT, surveyFile.getFilename()), "survey.file.error.invalid_file_type", "CSV (Comma Separated Values)");
                break;
            case INVALID_HEADERS:
                validationResult = new SurveyValidationResult(Flag.WARNING, String.format(SURVEY_FILE_PATH_FORMAT, surveyFile.getFilename()), "survey.file.type.collect_earth_grid.error.invalid_file_structure", fileValidationResult.getExpectedHeaders().toString(), fileValidationResult.getFoundHeaders().toString());
                break;
            case INVALID_VALUES_IN_CSV:
                validationResult = new SurveyValidationResult(Flag.WARNING, String.format(SURVEY_FILE_PATH_FORMAT, surveyFile.getFilename()), "survey.file.error.invalid_content", getRowValidationMessages(fileValidationResult.getRowValidations()));
                break;
            case INVALID_NUMBER_OF_COLUMNS:
                validationResult = new SurveyValidationResult(Flag.WARNING, String.format(SURVEY_FILE_PATH_FORMAT, surveyFile.getFilename()), "survey.file.type.collect_earth_grid.error.invalid_file_structure", fileValidationResult.getExpectedHeaders().toString(), fileValidationResult.getFoundHeaders().toString());
                break;
            case INVALID_NUMBER_OF_PLOTS_TOO_LARGE:
                validationResult = new SurveyValidationResult(Flag.WARNING, String.format(SURVEY_FILE_PATH_FORMAT, surveyFile.getFilename()), "survey.file.error.error_csv_size", fileValidationResult.getNumberOfRows().toString());
                break;
            case INVALID_NUMBER_OF_PLOTS_WARNING:
                validationResult = new SurveyValidationResult(Flag.WARNING, String.format(SURVEY_FILE_PATH_FORMAT, surveyFile.getFilename()), "survey.file.error.warning_csv_size", fileValidationResult.getNumberOfRows().toString());
                break;
            default:
        }
        if (validationResult != null)
            results.add(validationResult);
    }
    return results;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) SurveyFile(org.openforis.collect.model.SurveyFile) File(java.io.File) CSVFileValidationResult(org.openforis.collect.io.metadata.collectearth.CSVFileValidationResult) CollectEarthGridTemplateGenerator(org.openforis.collect.io.metadata.collectearth.CollectEarthGridTemplateGenerator)

Example 3 with CollectEarthGridTemplateGenerator

use of org.openforis.collect.io.metadata.collectearth.CollectEarthGridTemplateGenerator in project collect by openforis.

the class SurveyEditVM method exportCEGridTemplate.

@Command
public void exportCEGridTemplate() throws IOException {
    File templateFile = new CollectEarthGridTemplateGenerator().generateTemplateCSVFile(survey);
    String fileName = String.format("%s_grid_template_%s.csv", survey.getName(), Dates.formatDateTime(new Date()));
    Filedownload.save(new FileInputStream(templateFile), MediaTypes.CSV_CONTENT_TYPE, fileName);
}
Also used : File(java.io.File) Date(java.util.Date) FileInputStream(java.io.FileInputStream) CollectEarthGridTemplateGenerator(org.openforis.collect.io.metadata.collectearth.CollectEarthGridTemplateGenerator) GlobalCommand(org.zkoss.bind.annotation.GlobalCommand) Command(org.zkoss.bind.annotation.Command)

Example 4 with CollectEarthGridTemplateGenerator

use of org.openforis.collect.io.metadata.collectearth.CollectEarthGridTemplateGenerator in project collect by openforis.

the class SurveyFileVM method downloadExampleFile.

@Command
public void downloadExampleFile(@BindingParam("fileType") String fileType) throws IOException {
    switch(SurveyFileType.valueOf(fileType)) {
        case COLLECT_EARTH_GRID:
            File templateFile = new CollectEarthGridTemplateGenerator().generateTemplateCSVFile(survey);
            String fileName = String.format("%s_grid_template_%s.csv", survey.getName(), Dates.formatDateTime(new Date()));
            Filedownload.save(new FileInputStream(templateFile), MediaTypes.CSV_CONTENT_TYPE, fileName);
            break;
        default:
    }
}
Also used : SurveyFile(org.openforis.collect.model.SurveyFile) File(java.io.File) Date(java.util.Date) FileInputStream(java.io.FileInputStream) CollectEarthGridTemplateGenerator(org.openforis.collect.io.metadata.collectearth.CollectEarthGridTemplateGenerator) Command(org.zkoss.bind.annotation.Command)

Aggregations

CollectEarthGridTemplateGenerator (org.openforis.collect.io.metadata.collectearth.CollectEarthGridTemplateGenerator)4 File (java.io.File)3 FileInputStream (java.io.FileInputStream)2 Date (java.util.Date)2 CSVFileValidationResult (org.openforis.collect.io.metadata.collectearth.CSVFileValidationResult)2 SurveyFile (org.openforis.collect.model.SurveyFile)2 Command (org.zkoss.bind.annotation.Command)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ArrayList (java.util.ArrayList)1 ValidationParameters (org.openforis.collect.manager.validation.SurveyValidator.ValidationParameters)1 SurveyFileType (org.openforis.collect.model.SurveyFile.SurveyFileType)1 GlobalCommand (org.zkoss.bind.annotation.GlobalCommand)1