Search in sources :

Example 21 with CsvMapper

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvMapper in project xm-ms-entity by xm-online.

the class EntityToCsvConverterUtils method toCsv.

/**
 * Writes entities to csv file.
 * @param o the object which serialize to csv
 * @param clazz the class from which csv schema based would be
 * @return byte array of csv file
 */
public static byte[] toCsv(Object o, Class clazz) {
    if (o == null) {
        log.warn("Passed empty object for serialize, therefore return empty byte array which represents csv file");
        return new byte[0];
    }
    CsvMapper mapper = createDefaultCsvMapper();
    ObjectWriter csvWriter = mapper.writer(clazz == null ? CsvSchema.emptySchema() : createCsvSchemaBasedOnClass(mapper, clazz));
    try {
        return csvWriter.writeValueAsBytes(o);
    } catch (JsonProcessingException e) {
        throw new IllegalStateException("Exception while writing data to csv file", e);
    }
}
Also used : CsvMapper(com.fasterxml.jackson.dataformat.csv.CsvMapper) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 22 with CsvMapper

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvMapper in project perun by CESNET.

the class ExtSourceCSV method csvParsing.

/**
 * Parse CSV file into list of our standard "subject" (aka candidates)
 *
 * @param query query to check CSV file content against
 * @param maxResults limit results to X row or 0 for unlimited
 * @return List of Maps representing subjects for synchronization (perun_attr/constant = value).
 * @throws InternalErrorException When implementation fails
 * @throws IOException When reading CSV file fails
 */
private List<Map<String, String>> csvParsing(String query, int maxResults) throws IOException {
    List<Map<String, String>> subjects = new ArrayList<>();
    Map<String, String> attributeMapping = getCsvMapping();
    File csvFile = new File(file);
    CsvMapper mapper = new CsvMapper();
    // use first row as header; otherwise defaults are fine
    CsvSchema schema = CsvSchema.emptySchema().withHeader();
    MappingIterator<Map<String, String>> it = mapper.readerFor(Map.class).with(schema).readValues(csvFile);
    while (it.hasNext()) {
        Map<String, String> rowAsMap = it.next();
        if (compareRowToQuery(rowAsMap, query)) {
            Map<String, String> singleSubject = new HashMap<>();
            // translate CSV column names to perun attribute URNs
            for (String key : rowAsMap.keySet()) {
                singleSubject.put(attributeMapping.get(key), rowAsMap.get(key));
            }
            subjects.add(singleSubject);
            // break if we required limited response
            if (maxResults > 0) {
                if (subjects.size() >= maxResults) {
                    break;
                }
            }
        }
    }
    return subjects;
}
Also used : CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema) HashMap(java.util.HashMap) CsvMapper(com.fasterxml.jackson.dataformat.csv.CsvMapper) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File)

Example 23 with CsvMapper

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvMapper in project perun by CESNET.

the class MembersManagerBlImpl method createSponsoredMembersFromCSV.

@Override
public List<Map<String, String>> createSponsoredMembersFromCSV(PerunSession sess, Vo vo, String namespace, List<String> data, String header, User sponsor, LocalDate validityTo, boolean sendActivationLink, String url, Validation validation, List<Group> groups) {
    List<Map<String, String>> totalResult = new ArrayList<>();
    Set<Member> createdMembers = new HashSet<>();
    List<String> dataWithHeader = new ArrayList<>();
    dataWithHeader.add(header);
    dataWithHeader.addAll(data);
    MappingIterator<Map<String, String>> dataIterator;
    try {
        byte[] bytes = String.join("\n", dataWithHeader).getBytes(StandardCharsets.UTF_8);
        dataIterator = new CsvMapper().readerFor(Map.class).with(CsvSchema.emptySchema().withHeader().withColumnSeparator(';')).readValues(bytes);
    } catch (IOException e) {
        log.error("Failed to parse received CSV data.", e);
        throw new InternalErrorException("Failed to parse received CSV data.", e);
    }
    int processedCounter = 0;
    while (dataIterator.hasNext()) {
        Map<String, String> singleRow = dataIterator.next();
        // async validation must be performed at the end, not directly during member creation
        Validation localValidation = (Objects.equals(Validation.ASYNC, validation)) ? Validation.NONE : validation;
        Map<String, Object> originalResult = createSingleSponsoredMemberFromCSV(sess, vo, namespace, singleRow, sponsor, validityTo, sendActivationLink, url, localValidation, groups);
        // convert result to expected "type" for outer API
        Map<String, String> newResult = new HashMap<>();
        if (OK.equals(originalResult.get(STATUS))) {
            newResult.put(STATUS, (String) originalResult.get(STATUS));
            newResult.put(LOGIN, (String) originalResult.get(LOGIN));
            newResult.put(PASSWORD, (String) originalResult.get(PASSWORD));
            createdMembers.add((Member) originalResult.get(MEMBER));
        } else {
            // error when creating
            newResult.put(STATUS, (String) originalResult.get(STATUS));
        }
        newResult.put(NAME, data.get(processedCounter++));
        totalResult.add(newResult);
    }
    // perform async validation if necessary
    if (Objects.equals(Validation.ASYNC, validation)) {
        for (Member member : createdMembers) {
            getPerunBl().getMembersManagerBl().validateMemberAsync(sess, member);
        }
    }
    return totalResult;
}
Also used : Validation(cz.metacentrum.perun.core.api.Validation) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) CsvMapper(com.fasterxml.jackson.dataformat.csv.CsvMapper) ArrayList(java.util.ArrayList) IOException(java.io.IOException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) RichMember(cz.metacentrum.perun.core.api.RichMember) Member(cz.metacentrum.perun.core.api.Member) HashSet(java.util.HashSet)

Example 24 with CsvMapper

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvMapper in project dhis2-core by dhis2.

the class JacksonCsvUtils method toCsv.

/**
 * Writes the given response to the given output stream as CSV using
 * {@link CsvMapper}. The schema is inferred from the given type using
 * {@CsvSchema}. A header line is included.
 *
 * @param value the value to write.
 * @param out the {@link OutputStream} to write to.
 * @throws IOException if the write operation fails.
 */
public static void toCsv(Object value, Class<?> type, OutputStream out) throws IOException {
    CsvMapper csvMapper = JacksonObjectMapperConfig.csvMapper;
    CsvSchema schema = csvMapper.schemaFor(type).withHeader();
    ObjectWriter writer = csvMapper.writer(schema);
    writer.writeValue(out, value);
}
Also used : CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema) CsvMapper(com.fasterxml.jackson.dataformat.csv.CsvMapper) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter)

Example 25 with CsvMapper

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvMapper in project dhis2-core by dhis2.

the class AbstractFullReadOnlyController method getObjectListCsv.

@GetMapping(produces = "application/csv")
public void getObjectListCsv(@RequestParam Map<String, String> rpParameters, OrderParams orderParams, @CurrentUser User currentUser, @RequestParam(defaultValue = ",") char separator, @RequestParam(defaultValue = "false") boolean skipHeader, HttpServletResponse response) throws IOException {
    List<Order> orders = orderParams.getOrders(getSchema());
    List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
    List<String> filters = Lists.newArrayList(contextService.getParameterValues("filter"));
    WebOptions options = new WebOptions(rpParameters);
    WebMetadata metadata = new WebMetadata();
    if (fields.isEmpty()) {
        fields.addAll(Preset.defaultPreset().getFields());
    }
    // only support metadata
    if (!getSchema().isMetadata()) {
        throw new HttpClientErrorException(HttpStatus.NOT_FOUND);
    }
    if (!aclService.canRead(currentUser, getEntityClass())) {
        throw new ReadAccessDeniedException("You don't have the proper permissions to read objects of this type.");
    }
    List<T> entities = getEntityList(metadata, options, filters, orders);
    CsvSchema schema;
    CsvSchema.Builder schemaBuilder = CsvSchema.builder();
    List<Property> properties = new ArrayList<>();
    for (String field : fields) {
        // then the group[id] part is simply ignored.
        for (String splitField : field.split(",")) {
            Property property = getSchema().getProperty(splitField);
            if (property == null || !property.isSimple()) {
                continue;
            }
            schemaBuilder.addColumn(property.getName());
            properties.add(property);
        }
    }
    schema = schemaBuilder.build().withColumnSeparator(separator);
    if (!skipHeader) {
        schema = schema.withHeader();
    }
    CsvMapper csvMapper = new CsvMapper();
    csvMapper.configure(JsonGenerator.Feature.IGNORE_UNKNOWN, true);
    List<Map<String, Object>> csvObjects = entities.stream().map(e -> {
        Map<String, Object> map = new HashMap<>();
        for (Property property : properties) {
            Object value = ReflectionUtils.invokeMethod(e, property.getGetterMethod());
            map.put(property.getName(), value);
        }
        return map;
    }).collect(toList());
    csvMapper.writer(schema).writeValue(response.getWriter(), csvObjects);
    response.flushBuffer();
}
Also used : Order(org.hisp.dhis.query.Order) PathVariable(org.springframework.web.bind.annotation.PathVariable) Order(org.hisp.dhis.query.Order) RequestParam(org.springframework.web.bind.annotation.RequestParam) ReflectionUtils(org.hisp.dhis.system.util.ReflectionUtils) UserContext(org.hisp.dhis.common.UserContext) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) InclusionStrategy(org.hisp.dhis.node.config.InclusionStrategy) Pagination(org.hisp.dhis.query.Pagination) UserSettingKey(org.hisp.dhis.user.UserSettingKey) Autowired(org.springframework.beans.factory.annotation.Autowired) CurrentUser(org.hisp.dhis.user.CurrentUser) PaginationUtils(org.hisp.dhis.webapi.utils.PaginationUtils) NodeUtils(org.hisp.dhis.node.NodeUtils) UserSettingService(org.hisp.dhis.user.UserSettingService) Locale(java.util.Locale) Optional(com.google.common.base.Optional) Map(java.util.Map) Preset(org.hisp.dhis.node.Preset) Query(org.hisp.dhis.query.Query) ContextService(org.hisp.dhis.webapi.service.ContextService) LinkService(org.hisp.dhis.webapi.service.LinkService) FieldFilterService(org.hisp.dhis.fieldfilter.FieldFilterService) CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema) CacheControl.noCache(org.springframework.http.CacheControl.noCache) QueryService(org.hisp.dhis.query.QueryService) Property(org.hisp.dhis.schema.Property) Defaults(org.hisp.dhis.fieldfilter.Defaults) SimpleNode(org.hisp.dhis.node.types.SimpleNode) List(java.util.List) Include(org.hisp.dhis.node.config.InclusionStrategy.Include) ComplexNode(org.hisp.dhis.node.types.ComplexNode) AttributeService(org.hisp.dhis.attribute.AttributeService) FieldFilterParams(org.hisp.dhis.fieldfilter.FieldFilterParams) AclService(org.hisp.dhis.security.acl.AclService) Schema(org.hisp.dhis.schema.Schema) RootNode(org.hisp.dhis.node.types.RootNode) Joiner(com.google.common.base.Joiner) DhisApiVersion(org.hisp.dhis.common.DhisApiVersion) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) WebMessageUtils.notFound(org.hisp.dhis.dxf2.webmessage.WebMessageUtils.notFound) CollectionNode(org.hisp.dhis.node.types.CollectionNode) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) HashMap(java.util.HashMap) ApiVersion(org.hisp.dhis.webapi.mvc.annotation.ApiVersion) ArrayList(java.util.ArrayList) Enums(com.google.common.base.Enums) HttpServletRequest(javax.servlet.http.HttpServletRequest) Lists(com.google.common.collect.Lists) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) WebMetadata(org.hisp.dhis.webapi.webdomain.WebMetadata) User(org.hisp.dhis.user.User) GetMapping(org.springframework.web.bind.annotation.GetMapping) QueryParserException(org.hisp.dhis.query.QueryParserException) ReadAccessDeniedException(org.hisp.dhis.hibernate.exception.ReadAccessDeniedException) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) ContextUtils(org.hisp.dhis.webapi.utils.ContextUtils) Node(org.hisp.dhis.node.Node) Pager(org.hisp.dhis.common.Pager) CsvMapper(com.fasterxml.jackson.dataformat.csv.CsvMapper) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) HttpStatus(org.springframework.http.HttpStatus) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) Collectors.toList(java.util.stream.Collectors.toList) OrderParams(org.hisp.dhis.dxf2.common.OrderParams) CurrentUserService(org.hisp.dhis.user.CurrentUserService) TranslateParams(org.hisp.dhis.dxf2.common.TranslateParams) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) CsvMapper(com.fasterxml.jackson.dataformat.csv.CsvMapper) ArrayList(java.util.ArrayList) ReadAccessDeniedException(org.hisp.dhis.hibernate.exception.ReadAccessDeniedException) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) WebMetadata(org.hisp.dhis.webapi.webdomain.WebMetadata) CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Property(org.hisp.dhis.schema.Property) Map(java.util.Map) HashMap(java.util.HashMap) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

CsvMapper (com.fasterxml.jackson.dataformat.csv.CsvMapper)21 CsvSchema (com.fasterxml.jackson.dataformat.csv.CsvSchema)15 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)7 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)5 InputStream (java.io.InputStream)5 List (java.util.List)5 CsvMapper (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvMapper)5 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)4 File (java.io.File)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Converter (org.apache.flink.formats.common.Converter)4 CsvSchema (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.dataformat.csv.CsvSchema)4 FileUploadException (uk.ac.ebi.spot.goci.curation.exception.FileUploadException)4 OutputStream (java.io.OutputStream)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 MappingIterator (com.fasterxml.jackson.databind.MappingIterator)2 JetException (com.hazelcast.jet.JetException)2 FileOutputStream (java.io.FileOutputStream)2