001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.component.flatpack;
018    
019    import java.util.ArrayList;
020    import java.util.HashMap;
021    import java.util.List;
022    import java.util.Map;
023    
024    import net.sf.flatpack.DataSet;
025    import org.apache.camel.Converter;
026    
027    /**
028     * @version $Revision: 16256 $
029     */
030    @Converter
031    public final class FlatpackConverter {
032    
033        private FlatpackConverter() {
034            // helper class
035        }
036    
037        @Converter
038        public static Map<String, Object> toMap(DataSet dataSet) {
039            Map<String, Object> map = new HashMap<String, Object>();
040            putValues(map, dataSet);
041            return map;
042        }
043    
044        @Converter
045        public static List<Map<String, Object>> toList(DataSet dataSet) {
046            List<Map<String, Object>> answer = new ArrayList<Map<String, Object>>();
047            dataSet.goTop();
048    
049            while (dataSet.next()) {
050                Map<String, Object> map = new HashMap<String, Object>();
051                putValues(map, dataSet);
052                answer.add(map);
053            }
054    
055            return answer;
056        }
057    
058        /**
059         * Puts the values of the dataset into the map
060         */
061        private static void putValues(Map<String, Object> map, DataSet dataSet) {
062            boolean header = dataSet.isRecordID(FlatpackComponent.HEADER_ID);
063            boolean trailer = dataSet.isRecordID(FlatpackComponent.TRAILER_ID);
064    
065            // the columns can vary depending on header, body or trailer
066            String[] columns;
067            if (header) {
068                columns = dataSet.getColumns(FlatpackComponent.HEADER_ID);
069            } else if (trailer) {
070                columns = dataSet.getColumns(FlatpackComponent.TRAILER_ID);
071            } else {
072                columns = dataSet.getColumns();
073            }
074    
075            for (String column : columns) {
076                String value = dataSet.getString(column);
077                map.put(column, value);
078            }
079        }
080    }