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.example.etl;
018    
019    import java.util.List;
020    
021    import org.apache.camel.Converter;
022    import org.apache.camel.Exchange;
023    import org.apache.camel.util.CastUtils;
024    import org.apache.commons.logging.Log;
025    import org.apache.commons.logging.LogFactory;
026    import org.springframework.orm.jpa.JpaTemplate;
027    import org.springframework.util.ClassUtils;
028    
029    /**
030     * A Message Transformer of an XML document to a Customer entity bean
031     * 
032     * @version $Revision: 888856 $
033     */
034    // START SNIPPET: example
035    @Converter
036    public class CustomerTransformer {
037    
038        private static final transient Log LOG = LogFactory.getLog(CustomerTransformer.class);
039    
040        /**
041         * A transformation method to convert a person document into a customer
042         * entity
043         * @throws Exception 
044         */
045        @Converter
046        public CustomerEntity toCustomer(PersonDocument doc, Exchange exchange) throws Exception {
047            JpaTemplate template = exchange.getIn().getHeader("CamelJpaTemplate", JpaTemplate.class);
048    
049            
050            String user = doc.getUser();
051            CustomerEntity customer = findCustomerByName(template, user);
052    
053            // let's convert information from the document into the entity bean
054            customer.setUserName(user);
055            customer.setFirstName(doc.getFirstName());
056            customer.setSurname(doc.getLastName());
057            customer.setCity(doc.getCity());
058    
059            LOG.debug("Created object customer: " + customer);
060            return customer;
061        }
062    
063        /**
064         * Finds a customer for the given username
065         */
066        protected CustomerEntity findCustomerByName(JpaTemplate template, String user) throws Exception {
067            List<CustomerEntity> list = CastUtils.cast(template.find("select x from customer"
068                                                                     + " x where x.userName = ?1", user));
069            if (list.isEmpty()) {
070                CustomerEntity answer = new CustomerEntity();
071                answer.setUserName(user);
072                return answer;
073            } else {
074                return list.get(0);
075            }
076        }
077    
078    }
079    // END SNIPPET: example