THIS IS A TEST INSTANCE. ALL YOUR CHANGES WILL BE LOST!!!!
...
Code Block |
---|
@Converter public final class CustomerTransformer { private static final Logger LOG = LoggerFactory.getLogger(CustomerTransformer.class); private CustomerTransformer() { } /** * A transformation method to convert a person document into a customer * entity */ @Converter public static CustomerEntity toCustomer(PersonDocument doc, Exchange exchange) throws Exception { EntityManager entityManager = exchange.getIn().getHeader(JpaConstants.ENTITYMANAGER, EntityManager.class); TransactionTemplate transactionTemplate = exchange.getContext().getRegistry().lookupByNameAndType("transactionTemplate", TransactionTemplate.class); String user = doc.getUser(); CustomerEntity customer = findCustomerByName(transactionTemplate, entityManager, user); // let's convert information from the document into the entity bean customer.setUserName(user); customer.setFirstName(doc.getFirstName()); customer.setSurname(doc.getLastName()); customer.setCity(doc.getCity()); LOG.info("Created object customer: {}", customer); return customer; } /** * Finds a customer for the given username */ private static CustomerEntity findCustomerByName(TransactionTemplate transactionTemplate, final EntityManager entityManager, final String userName) throws Exception { return transactionTemplate.execute(new TransactionCallback<CustomerEntity>() { public CustomerEntity doInTransaction(TransactionStatus status) { entityManager.joinTransaction(); List<CustomerEntity> list = entityManager.createNamedQuery("findCustomerByUsername", CustomerEntity.class).setParameter("userName", userName).getResultList(); CustomerEntity answer; if (list.isEmpty()) { answer = new CustomerEntity(); answer.setUserName(userName); LOG.info("Created a new CustomerEntity {} as no matching persisted entity found.", answer); } else { answer = list.get(0); LOG.info("Found a matching CustomerEntity {} having the userName {}.", answer, userName); } return answer; } }); } } |
...