Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
CustomerTransformer |
|
| 0.0;0 |
1 | /** |
|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
|
3 | * contributor license agreements. See the NOTICE file distributed with |
|
4 | * this work for additional information regarding copyright ownership. |
|
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
|
6 | * (the "License"); you may not use this file except in compliance with |
|
7 | * the License. You may obtain a copy of the License at |
|
8 | * |
|
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
10 | * |
|
11 | * Unless required by applicable law or agreed to in writing, software |
|
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 | * See the License for the specific language governing permissions and |
|
15 | * limitations under the License. |
|
16 | */ |
|
17 | package org.apache.camel.example.etl; |
|
18 | ||
19 | import java.util.List; |
|
20 | ||
21 | import org.apache.camel.Converter; |
|
22 | import org.apache.commons.logging.Log; |
|
23 | import org.apache.commons.logging.LogFactory; |
|
24 | ||
25 | import org.springframework.orm.jpa.JpaTemplate; |
|
26 | ||
27 | /** |
|
28 | * A Message Transformer of an XML document to a Customer entity bean |
|
29 | * |
|
30 | * @version $Revision: 1.1 $ |
|
31 | */ |
|
32 | // START SNIPPET: example |
|
33 | @Converter |
|
34 | public class CustomerTransformer { |
|
35 | 0 | private static final transient Log LOG = LogFactory.getLog(CustomerTransformer.class); |
36 | private JpaTemplate template; |
|
37 | ||
38 | 0 | public CustomerTransformer(JpaTemplate template) { |
39 | 0 | this.template = template; |
40 | 0 | } |
41 | ||
42 | /** |
|
43 | * A transformation method to convert a person document into a customer |
|
44 | * entity |
|
45 | */ |
|
46 | @Converter |
|
47 | public CustomerEntity toCustomer(PersonDocument doc) { |
|
48 | 0 | String user = doc.getUser(); |
49 | 0 | CustomerEntity customer = findCustomerByName(user); |
50 | ||
51 | // lets convert information from the document into the entity bean |
|
52 | ||
53 | 0 | customer.setFirstName(doc.getFirstName()); |
54 | 0 | customer.setSurname(doc.getLastName()); |
55 | 0 | customer.setCity(doc.getCity()); |
56 | ||
57 | 0 | LOG.debug("Created customer: " + customer); |
58 | 0 | return customer; |
59 | } |
|
60 | ||
61 | /** |
|
62 | * Finds a customer for the given username, or creates and inserts a new one |
|
63 | */ |
|
64 | protected CustomerEntity findCustomerByName(String user) { |
|
65 | 0 | List<CustomerEntity> list = template.find("select x from " + CustomerEntity.class.getName() + " x where x.userName = ?1", user); |
66 | 0 | if (list.isEmpty()) { |
67 | 0 | CustomerEntity answer = new CustomerEntity(); |
68 | 0 | answer.setUserName(user); |
69 | 0 | template.persist(answer); |
70 | 0 | return answer; |
71 | } else { |
|
72 | 0 | return list.get(0); |
73 | } |
|
74 | } |
|
75 | } |
|
76 | // END SNIPPET: example |