View Javadoc
1   /*
2    * JBoss, Home of Professional Open Source
3    * Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
4    * contributors by the @authors tag. See the copyright.txt in the
5    * distribution for a full listing of individual contributors.
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * http://www.apache.org/licenses/LICENSE-2.0
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.jboss.as.quickstarts.cmt.ejb;
18  
19  import java.rmi.RemoteException;
20  import java.util.List;
21  
22  import javax.ejb.EJBException;
23  import javax.ejb.Stateless;
24  import javax.ejb.TransactionAttribute;
25  import javax.ejb.TransactionAttributeType;
26  import javax.inject.Inject;
27  import javax.jms.JMSException;
28  import javax.naming.NamingException;
29  import javax.persistence.EntityManager;
30  import javax.persistence.PersistenceContext;
31  import javax.transaction.HeuristicMixedException;
32  import javax.transaction.HeuristicRollbackException;
33  import javax.transaction.NotSupportedException;
34  import javax.transaction.RollbackException;
35  import javax.transaction.SystemException;
36  
37  import org.jboss.as.quickstarts.cmt.model.Customer;
38  
39  @Stateless
40  public class CustomerManagerEJB {
41  
42      @PersistenceContext
43      private EntityManager entityManager;
44  
45      @Inject
46      private LogMessageManagerEJB logMessageManager;
47  
48      @Inject
49      private InvoiceManagerEJB invoiceManager;
50  
51      @TransactionAttribute(TransactionAttributeType.REQUIRED)
52      public void createCustomer(String name) throws RemoteException, JMSException {
53          logMessageManager.logCreateCustomer(name);
54  
55          Customer c1 = new Customer();
56          c1.setName(name);
57          entityManager.persist(c1);
58  
59          invoiceManager.createInvoice(name);
60  
61          // It could be done before all the 'storing' but this is just to show that
62          // the invoice is not delivered when we cause an EJBException
63          // after the fact but before the transaction is committed.
64          if (!nameIsValid(name)) {
65              throw new EJBException("Invalid name: customer names should only contain letters & '-'");
66          }
67      }
68  
69      static boolean nameIsValid(String name) {
70          return name.matches("[\\p{L}-]+");
71      }
72  
73      /**
74       * List all the customers.
75       * 
76       * @return
77       * @throws NamingException
78       * @throws NotSupportedException
79       * @throws SystemException
80       * @throws SecurityException
81       * @throws IllegalStateException
82       * @throws RollbackException
83       * @throws HeuristicMixedException
84       * @throws HeuristicRollbackException
85       */
86      @TransactionAttribute(TransactionAttributeType.NEVER)
87      @SuppressWarnings("unchecked")
88      public List<Customer> listCustomers() {
89          return entityManager.createQuery("select c from Customer c").getResultList();
90      }
91  }