You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by bp <bh...@gmail.com> on 2013/01/28 22:14:58 UTC

How do I use Java Enum instead of a string into my route in spring xml

Hi,

If you would see my Route pasted below,I am using simple language in when
condition (e.g.<when>
          <simple>${body.entity.subject} == 'Advertising'</simple>).
Instead of above statement I want something like:
          <simple>${body.entity.subject} ==
InquiryTypeEnum.ADVERTISING.nameValue()</simple>).

I don't to achieve above whether simple language is okay or not. I was
trying with javaScript but no luck.

Thank you in advance for the help.

Here is my route:
<route id="other.inquiry.mailer"  >
      <from uri="direct:goToOther"/>
      <unmarshal ref="xstream" />
      <convertBodyTo type="com.ebates.domain.inquiry.Message" />
 	  <transacted ref="required" />
      
      <choice>
        <when>
          <simple>${body.entity.email} != null</simple>
      <setHeader
headerName="X-Email-Type"><simple>${body.emailType}</simple></setHeader>
      <setHeader
headerName="X-autologin"><simple>${body.loginHeader}</simple></setHeader>
      <to uri="bean:debugProcessor"/>
      <choice>
      <when>
          <simple>${body.entity.subject} == 'Advertising'</simple>
        <setHeader
headerName="to"><simple>${properties:cs.inquiry.advertising.emailaddress}</simple></setHeader>
      </when>
      <when>
          <simple>${body.entity.subject} == 'Affiliate Program'</simple>
          <setHeader
headerName="to"><simple>${properties:cs.inquiry.affiliate_program.emailaddress}</simple></setHeader>
      </when>
      <otherwise>
          <setHeader
headerName="to"><simple>${properties:cs.inquiry.default.emailaddress}</simple></setHeader>
      </otherwise>
      </choice>
    <setHeader
headerName="subject"><simple>${body.entity.subject}</simple></setHeader>
    <setHeader
headerName="from"><simple>${body.entity.email}</simple></setHeader>
      <to ref="otherInquiryVelocity" />
      <to uri="direct:toConsumer"/>
     
    </when>
</choice>
 </route>

Here is my Java enum:

public enum InquiryTypeEnum implements Serializable, BaseEnum {
    
    ADVERTISING(2L, "Advertising"),
   
    AFFILIATE_PROGRAM(3L, "Affiliate Program"),
   
    BECOME_AN_EBATES_MERCHANT(6L, "Become an Ebates Merchant"),
  
    CASH_BACK_CHECK(10L, "Cash Back Check"),
 
    private final String name;
    private final Long value;

    private InquiryTypeEnum(final Long v, final String n) {
        this.value = v;
        this.name = n;
    }

    public static InquiryTypeEnum fromValue(final Long v) {
        for (final InquiryTypeEnum c : InquiryTypeEnum.values()) {
            if (c.value.equals(v)) {
                return c;
            }
        }

        return null;
    }

    public static InquiryTypeEnum fromValue(final String n) {
        for (final InquiryTypeEnum c : InquiryTypeEnum.values()) {
            if (c.name.equalsIgnoreCase(n)) {
                return c;
            }
        }

        return null;
    }
}



--
View this message in context: http://camel.465427.n5.nabble.com/How-do-I-use-Java-Enum-instead-of-a-string-into-my-route-in-spring-xml-tp5726445.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: How do I use Java Enum instead of a string into my route in spring xml

Posted by Claus Ibsen <cl...@gmail.com>.
On Tue, Jan 29, 2013 at 10:14 AM, Claus Ibsen <cl...@gmail.com> wrote:
> On Mon, Jan 28, 2013 at 10:14 PM, bp <bh...@gmail.com> wrote:
>> Hi,
>>
>> If you would see my Route pasted below,I am using simple language in when
>> condition (e.g.<when>
>>           <simple>${body.entity.subject} == 'Advertising'</simple>).
>> Instead of above statement I want something like:
>>           <simple>${body.entity.subject} ==
>> InquiryTypeEnum.ADVERTISING.nameValue()</simple>).
>>
>> I don't to achieve above whether simple language is okay or not. I was
>> trying with javaScript but no luck.
>>
>
> You cannot do this with the simple language.
> You can use groovy, mvel, ognl or other strong languages for that.
> http://camel.apache.org/languages
>

I logged a ticket to improve simple language, as others have asked
about this in the past
https://issues.apache.org/jira/browse/CAMEL-6016

Also SpringEL supports this. So we should do the same in Camel.

>
>
>> Thank you in advance for the help.
>>
>> Here is my route:
>> <route id="other.inquiry.mailer"  >
>>       <from uri="direct:goToOther"/>
>>       <unmarshal ref="xstream" />
>>       <convertBodyTo type="com.ebates.domain.inquiry.Message" />
>>           <transacted ref="required" />
>>
>>       <choice>
>>         <when>
>>           <simple>${body.entity.email} != null</simple>
>>       <setHeader
>> headerName="X-Email-Type"><simple>${body.emailType}</simple></setHeader>
>>       <setHeader
>> headerName="X-autologin"><simple>${body.loginHeader}</simple></setHeader>
>>       <to uri="bean:debugProcessor"/>
>>       <choice>
>>       <when>
>>           <simple>${body.entity.subject} == 'Advertising'</simple>
>>         <setHeader
>> headerName="to"><simple>${properties:cs.inquiry.advertising.emailaddress}</simple></setHeader>
>>       </when>
>>       <when>
>>           <simple>${body.entity.subject} == 'Affiliate Program'</simple>
>>           <setHeader
>> headerName="to"><simple>${properties:cs.inquiry.affiliate_program.emailaddress}</simple></setHeader>
>>       </when>
>>       <otherwise>
>>           <setHeader
>> headerName="to"><simple>${properties:cs.inquiry.default.emailaddress}</simple></setHeader>
>>       </otherwise>
>>       </choice>
>>     <setHeader
>> headerName="subject"><simple>${body.entity.subject}</simple></setHeader>
>>     <setHeader
>> headerName="from"><simple>${body.entity.email}</simple></setHeader>
>>       <to ref="otherInquiryVelocity" />
>>       <to uri="direct:toConsumer"/>
>>
>>     </when>
>> </choice>
>>  </route>
>>
>> Here is my Java enum:
>>
>> public enum InquiryTypeEnum implements Serializable, BaseEnum {
>>
>>     ADVERTISING(2L, "Advertising"),
>>
>>     AFFILIATE_PROGRAM(3L, "Affiliate Program"),
>>
>>     BECOME_AN_EBATES_MERCHANT(6L, "Become an Ebates Merchant"),
>>
>>     CASH_BACK_CHECK(10L, "Cash Back Check"),
>>
>>     private final String name;
>>     private final Long value;
>>
>>     private InquiryTypeEnum(final Long v, final String n) {
>>         this.value = v;
>>         this.name = n;
>>     }
>>
>>     public static InquiryTypeEnum fromValue(final Long v) {
>>         for (final InquiryTypeEnum c : InquiryTypeEnum.values()) {
>>             if (c.value.equals(v)) {
>>                 return c;
>>             }
>>         }
>>
>>         return null;
>>     }
>>
>>     public static InquiryTypeEnum fromValue(final String n) {
>>         for (final InquiryTypeEnum c : InquiryTypeEnum.values()) {
>>             if (c.name.equalsIgnoreCase(n)) {
>>                 return c;
>>             }
>>         }
>>
>>         return null;
>>     }
>> }
>>
>>
>>
>> --
>> View this message in context: http://camel.465427.n5.nabble.com/How-do-I-use-Java-Enum-instead-of-a-string-into-my-route-in-spring-xml-tp5726445.html
>> Sent from the Camel - Users mailing list archive at Nabble.com.
>
>
>
> --
> Claus Ibsen
> -----------------
> Red Hat, Inc.
> FuseSource is now part of Red Hat
> Email: cibsen@redhat.com
> Web: http://fusesource.com
> Twitter: davsclaus
> Blog: http://davsclaus.com
> Author of Camel in Action: http://www.manning.com/ibsen



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
FuseSource is now part of Red Hat
Email: cibsen@redhat.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Re: How do I use Java Enum instead of a string into my route in spring xml

Posted by Claus Ibsen <cl...@gmail.com>.
On Mon, Jan 28, 2013 at 10:14 PM, bp <bh...@gmail.com> wrote:
> Hi,
>
> If you would see my Route pasted below,I am using simple language in when
> condition (e.g.<when>
>           <simple>${body.entity.subject} == 'Advertising'</simple>).
> Instead of above statement I want something like:
>           <simple>${body.entity.subject} ==
> InquiryTypeEnum.ADVERTISING.nameValue()</simple>).
>
> I don't to achieve above whether simple language is okay or not. I was
> trying with javaScript but no luck.
>

You cannot do this with the simple language.
You can use groovy, mvel, ognl or other strong languages for that.
http://camel.apache.org/languages



> Thank you in advance for the help.
>
> Here is my route:
> <route id="other.inquiry.mailer"  >
>       <from uri="direct:goToOther"/>
>       <unmarshal ref="xstream" />
>       <convertBodyTo type="com.ebates.domain.inquiry.Message" />
>           <transacted ref="required" />
>
>       <choice>
>         <when>
>           <simple>${body.entity.email} != null</simple>
>       <setHeader
> headerName="X-Email-Type"><simple>${body.emailType}</simple></setHeader>
>       <setHeader
> headerName="X-autologin"><simple>${body.loginHeader}</simple></setHeader>
>       <to uri="bean:debugProcessor"/>
>       <choice>
>       <when>
>           <simple>${body.entity.subject} == 'Advertising'</simple>
>         <setHeader
> headerName="to"><simple>${properties:cs.inquiry.advertising.emailaddress}</simple></setHeader>
>       </when>
>       <when>
>           <simple>${body.entity.subject} == 'Affiliate Program'</simple>
>           <setHeader
> headerName="to"><simple>${properties:cs.inquiry.affiliate_program.emailaddress}</simple></setHeader>
>       </when>
>       <otherwise>
>           <setHeader
> headerName="to"><simple>${properties:cs.inquiry.default.emailaddress}</simple></setHeader>
>       </otherwise>
>       </choice>
>     <setHeader
> headerName="subject"><simple>${body.entity.subject}</simple></setHeader>
>     <setHeader
> headerName="from"><simple>${body.entity.email}</simple></setHeader>
>       <to ref="otherInquiryVelocity" />
>       <to uri="direct:toConsumer"/>
>
>     </when>
> </choice>
>  </route>
>
> Here is my Java enum:
>
> public enum InquiryTypeEnum implements Serializable, BaseEnum {
>
>     ADVERTISING(2L, "Advertising"),
>
>     AFFILIATE_PROGRAM(3L, "Affiliate Program"),
>
>     BECOME_AN_EBATES_MERCHANT(6L, "Become an Ebates Merchant"),
>
>     CASH_BACK_CHECK(10L, "Cash Back Check"),
>
>     private final String name;
>     private final Long value;
>
>     private InquiryTypeEnum(final Long v, final String n) {
>         this.value = v;
>         this.name = n;
>     }
>
>     public static InquiryTypeEnum fromValue(final Long v) {
>         for (final InquiryTypeEnum c : InquiryTypeEnum.values()) {
>             if (c.value.equals(v)) {
>                 return c;
>             }
>         }
>
>         return null;
>     }
>
>     public static InquiryTypeEnum fromValue(final String n) {
>         for (final InquiryTypeEnum c : InquiryTypeEnum.values()) {
>             if (c.name.equalsIgnoreCase(n)) {
>                 return c;
>             }
>         }
>
>         return null;
>     }
> }
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/How-do-I-use-Java-Enum-instead-of-a-string-into-my-route-in-spring-xml-tp5726445.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
FuseSource is now part of Red Hat
Email: cibsen@redhat.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen