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.quickstart.cdi.veto;
18  
19  import java.util.logging.Logger;
20  
21  import javax.enterprise.inject.Produces;
22  import javax.inject.Inject;
23  import javax.persistence.EntityManager;
24  import javax.transaction.HeuristicMixedException;
25  import javax.transaction.HeuristicRollbackException;
26  import javax.transaction.NotSupportedException;
27  import javax.transaction.RollbackException;
28  import javax.transaction.SystemException;
29  import javax.transaction.UserTransaction;
30  
31  import org.jboss.as.quickstart.cdi.veto.model.Car;
32  
33  /**
34   * Producer for the {@link Car} entity.
35   */
36  public class CarManager {
37      @Inject
38      private EntityManager em;
39  
40      @Inject
41      private UserTransaction utx;
42  
43      private Long id;
44  
45      private Logger log = Logger.getLogger(CarManager.class.getSimpleName());
46  
47      public void setId(Long id) {
48          this.id = id;
49      }
50  
51      /**
52       * Returns either a new instance or a managed instance of {@link Car}.
53       * The produced entity should be dependent scoped to avoid incompatible proxies between
54       * JPA and CDI.
55       */
56      @Produces
57      public Car getCar() {
58          if (id == null) {
59              log.info("Returning new instance of Car");
60              return new Car();
61          }
62          log.info("Finding instance of Car with id " + id);
63          return em.find(Car.class, id);
64      }
65  
66      public void save(Car car) {
67          try {
68              utx.begin();
69              em.joinTransaction();
70              em.persist(car);
71              utx.commit();
72          } catch (NotSupportedException e) {
73              log.severe("Transaction Error: " + e.getMessage());
74          } catch (SystemException e) {
75              log.severe("Transaction Error: " + e.getMessage());
76          } catch (HeuristicRollbackException e) {
77              log.severe("Transaction Error: " + e.getMessage());
78          } catch (RollbackException e) {
79              log.severe("Transaction Error: " + e.getMessage());
80          } catch (HeuristicMixedException e) {
81              log.severe("Transaction Error: " + e.getMessage());
82          }
83      }
84  }