Hi all (sorry in advance for the long post),

I have been tweaking the camel-example-etl project to what I require for my
project. My requirements are very similar. I need to parse an xml document
and then insert the parsed entities as records into the database.

The structure of my xml is different to the example. Here is what my xml
looks like.

<WEB-ITM-EXT-REC>
   <ACTVTY-CODE>A</ACTVTY-CODE>
   <WEB-ITM-EXT-DATA>
     <ITEM-NO>93501250080</ITEM-NO>
     <PROMOTION-DETAILS>
       <PROMOTION-ID></PROMOTION-ID>
       <PROMOTION-START-DATE>0</PROMOTION-START-DATE>
       <PROMOTION-END-DATE>0</PROMOTION-END-DATE>
       <PROMOTION-PRICE>.00</PROMOTION-PRICE>
     </PROMOTION-DETAILS>
     <NORMAL-SELL-PRICES>
       <SELL-PRICE-EFFECTIVE-DATE-1>0</SELL-PRICE-EFFECTIVE-DATE-1>
       <SELL-PRICE-1>.00</SELL-PRICE-1>
       <SELL-PRICE-EFFECTIVE-DATE-2>0</SELL-PRICE-EFFECTIVE-DATE-2>
       <SELL-PRICE-2>.00</SELL-PRICE-2>
       <SELL-PRICE-EFFECTIVE-DATE-3>0</SELL-PRICE-EFFECTIVE-DATE-3>
       <SELL-PRICE-3>189.95</SELL-PRICE-3>
     </NORMAL-SELL-PRICES>
     <OUT-OF-STOCK-IND>Y</OUT-OF-STOCK-IND>
   </WEB-ITM-EXT-DATA>
 </WEB-ITM-EXT-REC>

So far I have succesfully managed to get jaxb to parse my xml using four
beans (hooray!).

1. ItemDocuments (holds a list of ItemDocument)
2. ItemDocument
3. PromotionDocument
4. SellPriceDocument

So after parsing the document I end up with a single ItemDocuments class
holding an ArrayList of ItemDocument which in turn has a PromotionDocument
and a SellPriceDocument.

Now I need to insert my List of ItemDocument into the database as records. I
have three entity beans (ItemEntity, PromotionEntity, SellPriceEntity). 

I am confused about how to change the Type Converter to do this for me. In
the original camel-example-etl project there was not a collection of parsed
records, it was only parsing 1 record at a time and only using 1 entity.

So I have a few questions...

1. How do I re-write my route below to convert to the three separate entity
beans? i.e. One parsed record for me should lead to three db inserts into my
three entities.

public class ItemEtl extends SpringRouteBuilder {
    public void configure() throws Exception {
        System.err.println("CONVERT STARTED");          
        from("file:src/data?noop=true")         
            .convertBodyTo(ItemDocuments.class)          
            .to("jpa:org.apache.camel.example.etl.ItemEntity");
        System.err.println("CONVERT FINISHED");     
    }
}

2. How do I write my Type Converter to handle my ItemDocuments and my list
of ItemDocument. In t he original example there is just a single record not
a list of them. I tried something like below but very unsure about it. 

@Converter
public class ItemsTransformer {
    private static final transient Logger LOG =
LoggerFactory.getLogger(ItemsTransformer.class);
    @Converter
    public List<ItemEntity> toItem(ItemDocuments docs, Exchange exchange)
throws Exception {
        JpaTemplate template =
exchange.getIn().getHeader("CamelJpaTemplate", JpaTemplate.class);
        List<ItemEntity> itemEntities = new ArrayList<ItemEntity>();
        List<ItemDocument> itemDocumentList = docs.getItemDocumentList();
        for (ItemDocument doc : itemDocumentList) {
            
            String itemNo = doc.getItemNo();           
            ItemEntity item = findItemByItemNumber(template, itemNo);
            
            item.setItemNo(itemNo);
            item.setClassNo(item.getClassNo());
            item.setDescription(item.getDescription());
            item.setGstCode(item.getGstCode()); 
            LOG.debug("Created object item: " + itemNo);
            itemEntities.add(item);
        }
        return itemEntities;
    }
    protected ItemEntity findItemByItemNumber(JpaTemplate template, String
itemNo) throws Exception {
        List<ItemEntity> list = CastUtils.cast(template.find("select x from
item"
                                                                 + " x where
x.item_no = ?1", itemNo));
        if (list.isEmpty()) {
            ItemEntity answer = new ItemEntity();
            answer.setItemNo(itemNo);
            return answer;
        } else {
            return list.get(0);
        }
    }
}

Of course it does not work. My error is ...

No type converter available to convert from type:
org.apache.camel.example.etl.ItemDocuments to the required type:
org.apache.camel.example.etl.ItemEntity with value
org.apache.camel.example.etl.ItemDocuments@535a4838

I would appreciate any help that anyone could give me. 

thank you



--
View this message in context: 
http://camel.465427.n5.nabble.com/How-to-write-a-type-converter-tp5741592.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Reply via email to