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.services;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.net.URL;
21  
22  import javax.xml.parsers.DocumentBuilder;
23  import javax.xml.parsers.DocumentBuilderFactory;
24  import javax.xml.parsers.ParserConfigurationException;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  import net.sf.imca.model.AssociationBO;
30  import net.sf.imca.model.PersonBO;
31  import net.sf.imca.model.BoatBO;
32  import net.sf.imca.model.entities.AssociationEntity;
33  
34  import org.w3c.dom.Document;
35  import org.w3c.dom.NodeList;
36  import org.xml.sax.SAXException;
37  
38  /**
39   * Service for importing data into the IMCA.  This is mainly used by
40   * test cases and to migrate data form the XML based site.  It is not 
41   * to be used by the Web Application.
42   *
43   * @author dougculnane
44   */
45  public class ImportData extends Service {
46  
47      public static String WEBSITE_PROTOCAL = "http";
48  
49      public static String WEBSITE_HOST = "www.moth-sailing.org";
50  
51      /**
52       * Apache Commons Logger specific to this class.
53       */
54      private Log log = LogFactory.getLog(ImportData.class);
55  
56      /**
57       * This reads the xml data files for the moth web site and imports the data 
58       * into the database.  It is useful for providing test data and to to 
59       * import the data into the new database.
60       *
61       * @return success
62       */
63      public boolean setUpMasterData() {
64  
65          startTransaction();
66  
67  //        AssociationBO associationWorld = new AssociationBO(em, 
68  //                AssociationBO.IMCA_WORLD_COUNTRY_CODE);
69  //        if (associationWorld.getEntity() == null){
70  //            associationWorld.setEntity(new AssociationEntity());
71  //        }
72  //        associationWorld.getEntity().setIsOfficiallyImca(true);
73  //        em.persist(associationWorld.getEntity());
74  
75  
76  //        NodeList countyList = getNodeListForDataFile("index", "country");
77  //        for (int i = 0; i < countyList.getLength(); i++) {
78  //            new AssociationBO(em, countyList.item(i).getChildNodes());
79  //        }
80  
81          
82  //      NodeList riderList = getNodeListForDataFile("riders", "rider");
83  //      for (int i = 0; i < riderList.getLength(); i++) {
84  //          new PersonBO(em, riderList.item(i).getChildNodes());
85  //      }
86  //      em.flush();
87          
88          
89          // Re-import Boats and Evetns.
90  //      NodeList boatList = getNodeListForDataFile("boats",  "boat");
91  //      for (int i = 0; i < boatList.getLength(); i++) {
92  //          new BoatBO(em, boatList.item(i).getChildNodes());
93  //      }
94  //      em.flush();
95  
96          try {
97              this.endTransaction();
98          } catch (Exception ex) {
99              log.error(ex);
100             return false;
101         }
102         return true;
103     }
104 
105     /**
106      * Small utility for extraction of node tree value from supplied data file 
107      * name.
108      */
109     private NodeList getNodeListForDataFile(String fileName, String dataType){
110 
111         NodeList list = null;
112         try {
113             URL url = new URL(
114                     WEBSITE_PROTOCAL, WEBSITE_HOST, "/" + fileName + ".xml");
115             InputStream is = url.openStream();
116             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
117             DocumentBuilder builder = dbf.newDocumentBuilder();
118             Document document = builder.parse(is);
119             list =  document.getElementsByTagName(dataType);
120         } catch (SAXException e) {
121             log.error("Error reading " + dataType + " data", e);
122         } catch (IOException e) {
123             log.error("Error reading " + dataType + " data", e);
124         } catch (ParserConfigurationException e) {
125             log.error("Error reading " + dataType + " data", e);
126         }
127         return list;
128     }
129 }