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 */
017package org.apache.camel.model.dataformat;
018
019import java.util.Locale;
020
021import javax.xml.bind.annotation.XmlAccessType;
022import javax.xml.bind.annotation.XmlAccessorType;
023import javax.xml.bind.annotation.XmlAttribute;
024import javax.xml.bind.annotation.XmlRootElement;
025import javax.xml.bind.annotation.XmlTransient;
026
027import org.apache.camel.model.DataFormatDefinition;
028import org.apache.camel.spi.Metadata;
029
030/**
031 * Marshal and unmarshal Java beans from and to flat payloads (such as CSV,
032 * delimited, fixed length formats, or FIX messages).
033 */
034@Metadata(firstVersion = "2.0.0", label = "dataformat,transformation,csv", title = "Bindy")
035@XmlRootElement(name = "bindy")
036@XmlAccessorType(XmlAccessType.FIELD)
037public class BindyDataFormat extends DataFormatDefinition {
038    @XmlAttribute(required = true)
039    @Metadata(required = true, javaType = "org.apache.camel.model.dataformat.BindyType", enums = "Csv,Fixed,KeyValue")
040    private String type;
041    @XmlAttribute
042    private String classType;
043    @XmlAttribute
044    private String locale;
045    @XmlAttribute
046    @Metadata(javaType = "java.lang.Boolean", defaultValue = "true")
047    private String unwrapSingleInstance;
048    @XmlAttribute
049    @Metadata(javaType = "java.lang.Boolean", defaultValue = "false")
050    private String allowEmptyStream;
051    @XmlTransient
052    private Class<?> clazz;
053
054    public BindyDataFormat() {
055        super("bindy");
056    }
057
058    public String getType() {
059        return type;
060    }
061
062    /**
063     * Whether to use Csv, Fixed, or KeyValue.
064     */
065    public void setType(String type) {
066        this.type = type;
067    }
068
069    public String getClassTypeAsString() {
070        return classType;
071    }
072
073    @Override
074    public String getDataFormatName() {
075        if ("Csv".equals(type)) {
076            return "bindy-csv";
077        } else if ("Fixed".equals(type)) {
078            return "bindy-fixed";
079        } else {
080            return "bindy-kvp";
081        }
082    }
083
084    /**
085     * Name of model class to use.
086     */
087    public void setClassTypeAsString(String classType) {
088        this.classType = classType;
089    }
090
091    /**
092     * Name of model class to use.
093     */
094    public void setClassType(String classType) {
095        setClassTypeAsString(classType);
096    }
097
098    /**
099     * Name of model class to use.
100     */
101    public void setClassType(Class<?> classType) {
102        this.clazz = classType;
103    }
104
105    public Class<?> getClassType() {
106        return clazz;
107    }
108
109    public String getLocale() {
110        return locale;
111    }
112
113    /**
114     * To configure a default locale to use, such as <tt>us</tt> for united
115     * states.
116     * <p/>
117     * To use the JVM platform default locale then use the name <tt>default</tt>
118     */
119    public void setLocale(String locale) {
120        this.locale = locale;
121    }
122
123    public String getUnwrapSingleInstance() {
124        return unwrapSingleInstance;
125    }
126
127    /**
128     * When unmarshalling should a single instance be unwrapped and returned
129     * instead of wrapped in a <tt>java.util.List</tt>.
130     */
131    public void setUnwrapSingleInstance(String unwrapSingleInstance) {
132        this.unwrapSingleInstance = unwrapSingleInstance;
133    }
134
135    public String getAllowEmptyStream() {
136        return allowEmptyStream;
137    }
138
139    /**
140   * Whether to allow empty streams in the unmarshal process. If true, no
141   * exception will be thrown when a body without records is provided.
142   */
143    public void setAllowEmptyStream(String allowEmptyStream) {
144        this.allowEmptyStream = allowEmptyStream;
145    }
146
147    //
148    // Fluent builder api
149    //
150
151    public BindyDataFormat csv() {
152        return type(BindyType.Csv);
153    }
154
155    public BindyDataFormat fixed() {
156        return type(BindyType.Fixed);
157    }
158
159    public BindyDataFormat keyValue() {
160        return type(BindyType.KeyValue);
161    }
162
163    public BindyDataFormat type(BindyType type) {
164        return type(type.name());
165    }
166
167    public BindyDataFormat type(String type) {
168        this.type = type;
169        return this;
170    }
171
172    public BindyDataFormat classType(Class<?> classType) {
173        this.clazz = classType;
174        return this;
175    }
176
177    public BindyDataFormat classType(String classType) {
178        this.classType = classType;
179        return this;
180    }
181
182    public BindyDataFormat locale(Locale locale) {
183        return locale(locale.getCountry().isEmpty()
184                ? locale.getLanguage() : locale.getLanguage() + "-" + locale.getCountry());
185    }
186
187    public BindyDataFormat locale(String locale) {
188        this.locale = locale;
189        return this;
190    }
191
192    public BindyDataFormat unwrapSingleInstance(boolean unwrapSingleInstance) {
193        return unwrapSingleInstance(Boolean.toString(unwrapSingleInstance));
194    }
195
196    public BindyDataFormat unwrapSingleInstance(String unwrapSingleInstance) {
197        this.unwrapSingleInstance = unwrapSingleInstance;
198        return this;
199    }
200
201    public BindyDataFormat allowEmptyStream(boolean allowEmptyStream) {
202        return allowEmptyStream(Boolean.toString(allowEmptyStream));
203    }
204
205    public BindyDataFormat allowEmptyStream(String allowEmptyStream) {
206        this.allowEmptyStream = allowEmptyStream;
207        return this;
208    }
209
210
211}