Search in sources :

Example 91 with HashMap

use of java.util.HashMap in project camel by apache.

the class BindyKeyValuePairDataFormat method unmarshal.

public Object unmarshal(Exchange exchange, InputStream inputStream) throws Exception {
    BindyKeyValuePairFactory factory = (BindyKeyValuePairFactory) getFactory();
    // List of Pojos
    List<Map<String, Object>> models = new ArrayList<Map<String, Object>>();
    // Pojos of the model
    Map<String, Object> model;
    // Map to hold the model @OneToMany classes while binding
    Map<String, List<Object>> lists = new HashMap<String, List<Object>>();
    InputStreamReader in = new InputStreamReader(inputStream, IOHelper.getCharsetName(exchange));
    // Scanner is used to read big file
    Scanner scanner = new Scanner(in);
    // Retrieve the pair separator defined to split the record
    ObjectHelper.notNull(factory.getPairSeparator(), "The pair separator property of the annotation @Message");
    String separator = factory.getPairSeparator();
    int count = 0;
    try {
        while (scanner.hasNextLine()) {
            // Read the line
            String line = scanner.nextLine().trim();
            if (ObjectHelper.isEmpty(line)) {
                // skip if line is empty
                continue;
            }
            // Increment counter
            count++;
            // Create POJO
            model = factory.factory();
            // Split the message according to the pair separator defined in
            // annotated class @Message
            List<String> result = Arrays.asList(line.split(separator));
            if (result.size() == 0 || result.isEmpty()) {
                throw new java.lang.IllegalArgumentException("No records have been defined in the KVP");
            }
            if (result.size() > 0) {
                // Bind data from message with model classes
                // Counter is used to detect line where error occurs
                factory.bind(result, model, count, lists);
                // Link objects together
                factory.link(model);
                // Add objects graph to the list
                models.add(model);
                LOG.debug("Graph of objects created: {}", model);
            }
        }
        // If this is the case (correspond to an empty stream, ...)
        if (models.size() == 0) {
            throw new java.lang.IllegalArgumentException("No records have been defined in the CSV");
        } else {
            return extractUnmarshalResult(models);
        }
    } finally {
        scanner.close();
        IOHelper.close(in, "in", LOG);
    }
}
Also used : Scanner(java.util.Scanner) InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BindyKeyValuePairFactory(org.apache.camel.dataformat.bindy.BindyKeyValuePairFactory) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 92 with HashMap

use of java.util.HashMap in project camel by apache.

the class BindySimpleKeyValuePairWithoutSectionMarshallDslTest method generateModel.

public List<Map<String, Object>> generateModel() {
    Map<String, Object> modelObjects = new HashMap<String, Object>();
    Order order = new Order();
    order.setAccount("BE.CHM.001");
    order.setClOrdId("CHM0001-01");
    order.setIDSource("4");
    order.setSecurityId("BE0001245678");
    order.setSide("1");
    order.setText("this is a camel - bindy test");
    modelObjects.put(order.getClass().getName(), order);
    models.add(modelObjects);
    return models;
}
Also used : Order(org.apache.camel.dataformat.bindy.model.fix.withoutsection.Order) HashMap(java.util.HashMap)

Example 93 with HashMap

use of java.util.HashMap in project camel by apache.

the class BindySimpleFixedLengthHeaderFooterTest method testMarshallMessageWithIndirectHeaderAndFooterInput.

/**
     * Verifies that header & footer provided via message headers are marshalled successfully
     */
@Test
public void testMarshallMessageWithIndirectHeaderAndFooterInput() throws Exception {
    Order order = new Order();
    order.setOrderNr(10);
    order.setOrderType("BUY");
    order.setClientNr("A9");
    order.setFirstName("Pauline");
    order.setLastName("M");
    order.setAmount(new BigDecimal("2500.45"));
    order.setInstrumentCode("ISIN");
    order.setInstrumentNumber("XD12345678");
    order.setInstrumentType("Share");
    order.setCurrency("USD");
    Calendar calendar = new GregorianCalendar();
    calendar.set(2009, 7, 1, 0, 0, 0);
    order.setOrderDate(calendar.getTime());
    List<Map<String, Object>> input = new ArrayList<Map<String, Object>>();
    Map<String, Object> bodyRow = new HashMap<String, Object>();
    bodyRow.put(Order.class.getName(), order);
    input.add(bodyRow);
    Map<String, Object> headers = new HashMap<String, Object>();
    headers.put(BindyFixedLengthDataFormat.CAMEL_BINDY_FIXED_LENGTH_HEADER, createHeaderRow());
    headers.put(BindyFixedLengthDataFormat.CAMEL_BINDY_FIXED_LENGTH_FOOTER, createFooterRow());
    marshallResult.reset();
    marshallResult.expectedMessageCount(1);
    StringBuffer buff = new StringBuffer();
    buff.append(TEST_HEADER).append(TEST_RECORD).append(TEST_FOOTER);
    marshallResult.expectedBodiesReceived(Arrays.asList(new String[] { buff.toString() }));
    template.sendBodyAndHeaders(URI_DIRECT_MARSHALL, input, headers);
    marshallResult.assertIsSatisfied();
}
Also used : HashMap(java.util.HashMap) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 94 with HashMap

use of java.util.HashMap in project camel by apache.

the class BindySimpleCsvBooleanFalseMarshallTest method generateModel.

public List<Map<String, Object>> generateModel() {
    Map<String, Object> modelObjects = new HashMap<String, Object>();
    BooleanExample example = new BooleanExample();
    example.setName("andrew");
    example.setExist(Boolean.FALSE);
    modelObjects.put(example.getClass().getName(), example);
    models.add(modelObjects);
    return models;
}
Also used : BooleanExample(org.apache.camel.dataformat.bindy.model.simple.bool.BooleanExample) HashMap(java.util.HashMap)

Example 95 with HashMap

use of java.util.HashMap in project camel by apache.

the class BindySimpleCsvBooleanTrueMarshallTest method generateModel.

public List<Map<String, Object>> generateModel() {
    Map<String, Object> modelObjects = new HashMap<String, Object>();
    BooleanExample example = new BooleanExample();
    example.setName("andrew");
    example.setExist(Boolean.TRUE);
    modelObjects.put(example.getClass().getName(), example);
    models.add(modelObjects);
    return models;
}
Also used : BooleanExample(org.apache.camel.dataformat.bindy.model.simple.bool.BooleanExample) HashMap(java.util.HashMap)

Aggregations

HashMap (java.util.HashMap)24090 ArrayList (java.util.ArrayList)5632 Map (java.util.Map)5586 Test (org.junit.Test)5079 List (java.util.List)2721 HashSet (java.util.HashSet)2103 IOException (java.io.IOException)1780 Set (java.util.Set)1465 File (java.io.File)1206 Iterator (java.util.Iterator)1198 LinkedHashMap (java.util.LinkedHashMap)1138 Test (org.testng.annotations.Test)884 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)680 Date (java.util.Date)545 LinkedList (java.util.LinkedList)494 URI (java.net.URI)415 Properties (java.util.Properties)381 InputStream (java.io.InputStream)343 Collection (java.util.Collection)342 TreeMap (java.util.TreeMap)333