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;
17  
18  import java.util.HashSet;
19  
20  import javax.persistence.EntityManager;
21  
22  import org.w3c.dom.Node;
23  import org.w3c.dom.NodeList;
24  
25  import net.sf.imca.model.entities.BoatDAO;
26  import net.sf.imca.model.entities.BoatEntity;
27  import net.sf.imca.model.entities.DesignDAO;
28  import net.sf.imca.model.entities.DesignEntity;
29  import net.sf.imca.model.entities.SailNumberDAO;
30  import net.sf.imca.model.entities.SailNumberEntity;
31  import net.sf.imca.model.entities.EquipmentSupplierDAO;
32  import net.sf.imca.model.entities.EquipmentSupplierEntity;
33  
34  /**
35   * Business Object representing the properties, persistence and functionality of
36   * the IMCA domain object.
37   *
38   * @author dougculnane
39   */
40  public class BoatBO {
41  
42      /**
43       * Entity for persisting this Business Object.
44       */
45      private BoatEntity entity;
46  
47      /**
48       * Get the persistence entity.
49       *
50       * @return The persistence entity for this Business Object.
51       */
52      public BoatEntity getEntity() {
53          return entity;
54      }
55  
56      /**
57       * Set the persistence entity.
58       *
59       * @param entity
60       *            The new persistence entity for this Business Object.
61       */
62      public void setEntity(BoatEntity entity) {
63          this.entity = entity;
64      }
65  
66      public BoatBO(EntityManager em, NodeList nodeList) {
67  
68          BoatDAO boatDAO = new BoatDAO();
69          DesignDAO designDAO = new DesignDAO();
70          SailNumberDAO sailNumberDAO = new SailNumberDAO();
71  
72          boolean doVersions = false;
73  
74          setEntity(new BoatEntity());
75  
76          for (int i = 0; i < nodeList.getLength(); i++) {
77              Node node = nodeList.item(i);
78              if ("name".equals(node.getNodeName())) {
79                  if (node.getChildNodes() != null) {
80                      if (node.getChildNodes().item(0) != null) {
81                          String boatName = node.getChildNodes().item(0)
82                                  .getNodeValue();
83                          if (boatName != null) {
84                              entity.setName(boatName);
85                          }
86                      }
87                  }
88              } else if ("id".equals(node.getNodeName())) {
89                  String sailNumber = node.getChildNodes().item(0).getNodeValue();
90                  SailNumberEntity sailNum = sailNumberDAO.findSailNumber(em,
91                          sailNumber);
92                  if (sailNum == null) {
93                      sailNum = new SailNumberEntity();
94                      sailNum.setSailNumber(sailNumber);
95                      em.persist(sailNum);
96                  }
97                  if (getEntity().getSailNumbers() == null) {
98                      HashSet<SailNumberEntity> sailNumbers = new HashSet<SailNumberEntity>();
99                      getEntity().setSailNumbers(sailNumbers);
100                 }
101                 getEntity().getSailNumbers().add(sailNum);
102             } else if ("design".equals(node.getNodeName())) {
103                 String designName = node.getChildNodes().item(0).getNodeValue();
104                 DesignEntity design = designDAO.findDesign(em, designName);
105                 if (design == null) {
106                     design = new DesignEntity();
107                     design.setName(designName);
108                     em.persist(design);
109                 }
110                 getEntity().setDesign(design);
111             } else if ("icfPlaqueNo".equals(node.getNodeName())) {
112                 Integer icfPlaqueNo = new Integer(node.getChildNodes().item(0)
113                         .getNodeValue());
114                 getEntity().setIcfPlaqueNo(icfPlaqueNo.intValue());
115             } else if ("yearbuilt".equals(node.getNodeName())) {
116                 Integer yearbuilt = new Integer(node.getChildNodes().item(0)
117                         .getNodeValue());
118                 getEntity().setYearBuilt(yearbuilt.intValue());
119             } else if ("versions".equals(node.getNodeName())) {
120                 doVersions = true;
121                 NodeList versionsNodes = nodeList.item(i).getChildNodes();
122                 for (int j = 0; j < versionsNodes.getLength(); j++) {
123                     if ("version".equals(versionsNodes.item(j).getNodeName())) {
124                         NodeList versionNodes = versionsNodes.item(j)
125                                 .getChildNodes();
126                         // TODO: version extraction.
127                         String xmlId = "";
128                         for (int k = 0; k < versionNodes.getLength(); k++) {
129                             if ("id".equals(versionNodes.item(j).getNodeName())) {
130                                 // xmlId = 
131                             }
132                            
133                         }
134                         
135                     }
136                 }
137             }
138         }
139 
140         for (int i = 0; i < nodeList.getLength(); i++) {
141             if ("id".equals(nodeList.item(i).getNodeName())) {
142                 replaceSailNumber(em, nodeList.item(i).getChildNodes().item(0)
143                         .getNodeValue());
144             } else if ("builder".equals(nodeList.item(i).getNodeName())) {
145                 replaceBuilder(em, nodeList.item(i).getChildNodes().item(0)
146                         .getNodeValue());
147             }
148         }
149         boatDAO.replace(em, getEntity());
150     }
151 
152     public void replaceSailNumber(EntityManager em, String sailNumber) {
153         SailNumberDAO sailNumDAO = new SailNumberDAO();
154         SailNumberEntity sailNumberEntity = sailNumDAO.findSailNumber(em,
155                 sailNumber);
156         if (!getEntity().getSailNumbers().contains(sailNumberEntity)) {
157             getEntity().getSailNumbers().add(sailNumberEntity);
158         }
159         em.persist(sailNumberEntity);
160     }
161 
162     public void replaceBuilder(EntityManager em, String builderName) {
163         EquipmentSupplierDAO equipSupDAO = new EquipmentSupplierDAO();
164         EquipmentSupplierEntity builder = equipSupDAO.findEquipmentSupplier(em,
165                 builderName);
166         if (builder == null) {
167             builder = new EquipmentSupplierEntity();
168             builder.setName(builderName);
169         }
170         em.persist(builder);
171         getEntity().setBuilder(builder);
172     }
173 }