Search in sources :

Example 16 with CellProcessor

use of org.supercsv.cellprocessor.ift.CellProcessor in project apex-malhar by apache.

the class CellProcessorBuilder method getIntegerCellProcessor.

/**
 * Method to get cellprocessor for Integer with constraints. These constraints
 * are evaluated against the Integer field for which this cellprocessor is
 * defined.
 *
 * @param constraints
 *          map of constraints applicable to Integer
 * @return CellProcessor
 */
private static CellProcessor getIntegerCellProcessor(Map<String, Object> constraints) {
    Boolean required = constraints.get(DelimitedSchema.REQUIRED) == null ? null : Boolean.parseBoolean((String) constraints.get(DelimitedSchema.REQUIRED));
    Integer equals = constraints.get(DelimitedSchema.EQUALS) == null ? null : Integer.parseInt((String) constraints.get(DelimitedSchema.EQUALS));
    Integer minValue = constraints.get(DelimitedSchema.MIN_VALUE) == null ? null : Integer.parseInt((String) constraints.get(DelimitedSchema.MIN_VALUE));
    Integer maxValue = constraints.get(DelimitedSchema.MAX_VALUE) == null ? null : Integer.parseInt((String) constraints.get(DelimitedSchema.MAX_VALUE));
    CellProcessor cellProcessor = null;
    if (equals != null) {
        cellProcessor = new Equals(equals);
        cellProcessor = addParseInt(cellProcessor);
    } else if (minValue != null || maxValue != null) {
        cellProcessor = addIntMinMax(minValue, maxValue);
    } else {
        cellProcessor = addParseInt(null);
    }
    if (required == null || !required) {
        cellProcessor = addOptional(cellProcessor);
    }
    return cellProcessor;
}
Also used : Equals(org.supercsv.cellprocessor.constraint.Equals) DoubleCellProcessor(org.supercsv.cellprocessor.ift.DoubleCellProcessor) CellProcessor(org.supercsv.cellprocessor.ift.CellProcessor) LongCellProcessor(org.supercsv.cellprocessor.ift.LongCellProcessor)

Example 17 with CellProcessor

use of org.supercsv.cellprocessor.ift.CellProcessor in project apex-malhar by apache.

the class CsvParser method getProcessor.

/**
 * Returns array of cellprocessors, one for each field
 */
private CellProcessor[] getProcessor(List<Field> fields) {
    CellProcessor[] processor = new CellProcessor[fields.size()];
    int fieldCount = 0;
    for (Field field : fields) {
        processor[fieldCount++] = CellProcessorBuilder.getCellProcessor(field.getType(), field.getConstraints());
    }
    return processor;
}
Also used : Field(org.apache.apex.malhar.contrib.parser.DelimitedSchema.Field) CellProcessor(org.supercsv.cellprocessor.ift.CellProcessor)

Example 18 with CellProcessor

use of org.supercsv.cellprocessor.ift.CellProcessor in project mots by motech-implementations.

the class InchargeService method processInchageCsv.

/**
 *.
 * Processes CSV file which contains Incharge list and returns list of errors
 * @param inchargeCsvFile CSV file with Incharge list
 * @return map with row numbers as keys and errors as values.
 * @throws IOException in case of file issues
 */
@SuppressWarnings("PMD.CyclomaticComplexity")
@PreAuthorize(RoleNames.HAS_UPLOAD_CSV_ROLE)
public Map<Integer, String> processInchageCsv(MultipartFile inchargeCsvFile, Boolean selected) throws IOException {
    ICsvMapReader csvMapReader;
    csvMapReader = new CsvMapReader(new InputStreamReader(inchargeCsvFile.getInputStream()), CsvPreference.STANDARD_PREFERENCE);
    final String[] header = csvMapReader.getHeader(true);
    final CellProcessor[] processors = getProcessors();
    Map<String, Object> csvRow;
    Set<String> phoneNumberSet = new HashSet<>();
    Map<Integer, String> errorMap = new HashMap<>();
    while ((csvRow = csvMapReader.read(header, processors)) != null) {
        LOGGER.debug(String.format("lineNo=%s, rowNo=%s, chw=%s", csvMapReader.getLineNumber(), csvMapReader.getRowNumber(), csvRow));
        String facilityId = Objects.toString(csvRow.get("FACILITY_ID"), null);
        Optional<Facility> existingFacility = facilityRepository.findByFacilityId(facilityId);
        if (!existingFacility.isPresent()) {
            errorMap.put(csvMapReader.getLineNumber(), "Facility with this Facility Id does not exist");
            continue;
        }
        String phoneNumber = Objects.toString(csvRow.get("PHU in-charge number"), null);
        if (phoneNumberSet.contains(phoneNumber)) {
            errorMap.put(csvMapReader.getLineNumber(), "Phone number is duplicated in CSV");
            continue;
        }
        if (phoneNumber != null) {
            phoneNumberSet.add(phoneNumber);
        }
        String name = Objects.toString(csvRow.get("PHU in-charge name"), null);
        if (StringUtils.isBlank(name)) {
            errorMap.put(csvMapReader.getLineNumber(), "Incharge name is empty");
            continue;
        }
        String[] nameParts = name.split("([. ])");
        List<String> names = Arrays.stream(nameParts).map(part -> part.length() == 1 ? part + "." : part).collect(Collectors.toList());
        if (names.size() < MIN_NAME_PARTS_NUMBER) {
            errorMap.put(csvMapReader.getLineNumber(), "Incharge second name is empty");
            continue;
        }
        Facility facility = existingFacility.get();
        Optional<Incharge> existingIncharge = inchargeRepository.findByFacilityId(facility.getId());
        String otherName = null;
        if (names.size() > MIN_NAME_PARTS_NUMBER) {
            otherName = StringUtils.join(names.subList(1, names.size() - 1), " ");
        }
        String firstName = names.get(0);
        String secondName = names.get(names.size() - 1);
        if (existingIncharge.isPresent()) {
            Incharge incharge = existingIncharge.get();
            incharge.setFirstName(firstName);
            incharge.setSecondName(secondName);
            incharge.setOtherName(otherName);
            incharge.setPhoneNumber(phoneNumber);
            if (selected) {
                incharge.setSelected(true);
            }
            inchargeRepository.save(incharge);
            continue;
        }
        inchargeRepository.save(new Incharge(firstName, secondName, otherName, phoneNumber, null, facility, selected));
    }
    return errorMap;
}
Also used : ICsvMapReader(org.supercsv.io.ICsvMapReader) Arrays(java.util.Arrays) StringUtils(org.apache.commons.lang.StringUtils) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) RoleNames(org.motechproject.mots.domain.security.UserPermission.RoleNames) CellProcessor(org.supercsv.cellprocessor.ift.CellProcessor) Facility(org.motechproject.mots.domain.Facility) CsvPreference(org.supercsv.prefs.CsvPreference) FacilityRepository(org.motechproject.mots.repository.FacilityRepository) HashSet(java.util.HashSet) Logger(org.apache.log4j.Logger) InchargeRepository(org.motechproject.mots.repository.InchargeRepository) Incharge(org.motechproject.mots.domain.Incharge) Service(org.springframework.stereotype.Service) Map(java.util.Map) EntityNotFoundException(org.motechproject.mots.exception.EntityNotFoundException) Pageable(org.springframework.data.domain.Pageable) Set(java.util.Set) IOException(java.io.IOException) UUID(java.util.UUID) Page(org.springframework.data.domain.Page) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) CsvMapReader(org.supercsv.io.CsvMapReader) Objects(java.util.Objects) List(java.util.List) Optional(java.util.Optional) MultipartFile(org.springframework.web.multipart.MultipartFile) Incharge(org.motechproject.mots.domain.Incharge) InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) ICsvMapReader(org.supercsv.io.ICsvMapReader) CsvMapReader(org.supercsv.io.CsvMapReader) CellProcessor(org.supercsv.cellprocessor.ift.CellProcessor) Facility(org.motechproject.mots.domain.Facility) ICsvMapReader(org.supercsv.io.ICsvMapReader) HashSet(java.util.HashSet) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 19 with CellProcessor

use of org.supercsv.cellprocessor.ift.CellProcessor in project jprime by bgjug.

the class CSVService method writeSubmissions.

private void writeSubmissions(List<Submission> submissions, ICsvMapWriter mapWriter) throws IOException {
    CellProcessor[] processors = new CellProcessor[] { null, null, null, null, null, null, null, null };
    Map<String, Object> submissionRow;
    mapWriter.writeHeader(submissionHeader);
    for (Submission submission : submissions) {
        submissionRow = new HashMap<String, Object>();
        submissionRow.put(submissionHeader[0], submission.getTitle());
        submissionRow.put(submissionHeader[1], submission.getDescription());
        submissionRow.put(submissionHeader[2], submission.getLevel());
        submissionRow.put(submissionHeader[3], submission.getType());
        submissionRow.put(submissionHeader[4], submission.getSpeaker().getFirstName());
        submissionRow.put(submissionHeader[5], submission.getSpeaker().getBio());
        if (submission.getCoSpeaker() != null) {
            submissionRow.put(submissionHeader[6], submission.getCoSpeaker().getFirstName());
            submissionRow.put(submissionHeader[7], submission.getCoSpeaker().getBio());
        }
        mapWriter.write(submissionRow, submissionHeader, processors);
    }
}
Also used : Submission(site.model.Submission) CellProcessor(org.supercsv.cellprocessor.ift.CellProcessor)

Aggregations

CellProcessor (org.supercsv.cellprocessor.ift.CellProcessor)19 DoubleCellProcessor (org.supercsv.cellprocessor.ift.DoubleCellProcessor)7 LongCellProcessor (org.supercsv.cellprocessor.ift.LongCellProcessor)7 InputStreamReader (java.io.InputStreamReader)6 IOException (java.io.IOException)5 Equals (org.supercsv.cellprocessor.constraint.Equals)5 CsvMapReader (org.supercsv.io.CsvMapReader)4 ICsvMapReader (org.supercsv.io.ICsvMapReader)4 CsvPreference (org.supercsv.prefs.CsvPreference)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Field (org.apache.apex.malhar.contrib.parser.DelimitedSchema.Field)3 Optional (org.supercsv.cellprocessor.Optional)3 HashSet (java.util.HashSet)2 List (java.util.List)2 Map (java.util.Map)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 NotNull (org.supercsv.cellprocessor.constraint.NotNull)2 CommentStartsWith (org.supercsv.comment.CommentStartsWith)2 AmazonEC2ClientBuilder (com.amazonaws.services.ec2.AmazonEC2ClientBuilder)1