View Javadoc

1   /*
2    * Copyright 2007 The International Moth Class Association (IMCA)
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.imca.model.entities;
17  
18  import java.util.Collection;
19  import java.util.Iterator;
20  import java.util.List;
21  import javax.persistence.EntityManager;
22  import javax.persistence.Query;
23  
24  import net.sf.imca.services.ReflectionUtil;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  public class BoatDAO {
30  
31      /**
32       * Apache Commons Logger specific to this class.
33       */
34      private Log log = LogFactory.getLog(BoatDAO.class);
35  
36      @SuppressWarnings("unchecked")
37      public BoatEntity findBoat(EntityManager em, final BoatEntity boat) {
38  
39          BoatEntity boatInDB = em.find(BoatEntity.class, boat.getId());
40          if (boatInDB == null) {
41              if (boat.getIcfPlaqueNo() != -1) {
42                  Query query = em.createNamedQuery("findBoatByIsafId");
43                  query.setParameter("icfPlaqueNo", boat.getIcfPlaqueNo());
44                  List<BoatEntity> list = query.getResultList();
45                  if (list.size() != 0) {
46                      boatInDB = list.get(0);
47                  }
48  
49              } else if (boat.getSailNumbers() != null) {
50  
51                  Query query = em.createNamedQuery("findSailNumberBySailNumber");
52  
53                  Iterator<SailNumberEntity> iterator = boat.getSailNumbers()
54                          .iterator();
55                  while (iterator.hasNext()) {
56                      String sailNumber = iterator.next().getSailNumber();
57                      query.setParameter("sailNumber", sailNumber);
58  
59                      List list = query.getResultList();
60                      if (list.size() != 0) {
61                          Collection<BoatEntity> boats = ((SailNumberEntity) list
62                                  .get(0)).getBoats();
63                          if (boats != null) {
64                              if (boats.size() == 1) {
65                                  boatInDB = boats.iterator().next();
66                              } else if (boats.size() > 1) {
67                                  log.warn("Multiple boats with sail number: "
68                                          + sailNumber);
69                              }
70                          }
71                      }
72                  }
73              }
74          }
75          return boatInDB;
76      }
77  
78      public void replace(EntityManager em, BoatEntity boat) {
79  
80          BoatEntity boatInDB = findBoat(em, boat);
81          if (boatInDB == null) {
82              em.persist(boat);
83          } else {
84              try {
85                  ReflectionUtil.replaceDBObject(boatInDB, boat);
86                  em.persist(boatInDB);
87              } catch (Exception e) {
88                  log.error("Reflection Error: " + e);
89                  em.persist(boat);
90              }
91          }
92      }
93  
94  }