View Javadoc
1   package ejava.projects.edmv.blimpl;
2   
3   import info.ejava.projects.edmv._1.Dmv;
4   
5   import java.io.InputStream;
6   import java.util.HashMap;
7   import java.util.Map;
8   
9   import javax.xml.bind.JAXBException;
10  import javax.xml.stream.XMLStreamException;
11  
12  import org.slf4j.Logger;
13  import org.slf4j.LoggerFactory;
14  
15  import ejava.projects.edmv.bl.EDmvException;
16  import ejava.projects.edmv.dao.DAOException;
17  import ejava.projects.edmv.dao.PersonDAO;
18  import ejava.projects.edmv.dao.VehicleDAO;
19  import ejava.projects.edmv.xml.EDmvParser;
20  
21  /**
22   * This class provides a _sparse_ example implementation of how one can use
23   * the parser to ingest the information from the XML file to populate the
24   * database.
25   */
26  public class EDmvIngestor {
27  	private static final Logger log = LoggerFactory.getLogger(EDmvIngestor.class);
28  	InputStream is;
29  	PersonDAO personDAO;
30  	VehicleDAO vehicleDAO;
31  	EDmvParser parser;
32  	Map<String, ejava.projects.edmv.bo.Person> dto2bo = 
33  	    new HashMap<String, ejava.projects.edmv.bo.Person>();
34  	
35  	public void setInputStream(InputStream is) {
36  		this.is = is; 
37  	}
38  	
39  	public void setPersonDAO(PersonDAO personDAO) {
40  		this.personDAO = personDAO;
41  	}
42  	
43      public void setVehicleDAO(VehicleDAO vehicleDAO) {
44          this.vehicleDAO = vehicleDAO;
45      }
46  
47      /**
48  	 * This method will ingest the input data by reading in external DTOs in
49  	 * from the parser, instantiating project business objects, and inserting
50  	 * into database. Note that the XML Schema is organized such that object
51  	 * references are fully resolved. Therefore, there is no specific need
52  	 * to process the addresses as they come in. They can be stored once we
53  	 * get the accounts they are related to.
54  	 * 
55  	 * @throws JAXBException
56  	 * @throws XMLStreamException
57       * @throws DAOException 
58  	 */
59  	public void ingest() 
60  	    throws JAXBException, XMLStreamException, Exception {
61  		EDmvParser parser = new EDmvParser(Dmv.class, is);
62  		
63  		Object object = parser.getObject(
64  		        "Person", "VehicleRegistration");
65  		while (object != null) {
66  			if (object instanceof gov.ojp.it.jxdm._3_0.Person) {
67  				createPerson((gov.ojp.it.jxdm._3_0.Person)object);
68  			}
69              if (object instanceof gov.ojp.it.jxdm._3_0.VehicleRegistration) {
70                  createRegistration((gov.ojp.it.jxdm._3_0.VehicleRegistration)object);
71              }
72  			object = parser.getObject(
73  			        "Person", "VehicleRegistration");
74  		}
75  	}
76  	
77  	/**
78  	 * This method is called by the main ingest processing loop. The JAXB/StAX
79  	 * parser will already have the Person populated with internal property
80  	 * information.
81  	 * @param personDTO
82  	 * @throws DAOException 
83  	 */
84  	private void createPerson(gov.ojp.it.jxdm._3_0.Person personDTO) 
85  	    throws DAOException {
86  	    ejava.projects.edmv.bo.Person personBO =
87  	        new ejava.projects.edmv.bo.Person();
88  	    personBO.setLastName(
89  	            personDTO.getPersonName().getPersonSurName().getValue());
90      		
91     		personDAO.createPerson(personBO);
92     		log.debug("created person:" + personBO);
93     		
94     		//map DTO id to BO object for follow-on ownership processing
95     		dto2bo.put(personDTO.getId(), personBO);
96  	}
97  
98      /**
99       * This method is called by the main ingest processing loop. The JAXB/StAX
100      * parser will already have the VehicleRegistration populated with internal 
101      * property information. The Person information for owners should also
102      * already exist.
103      * 
104      * @param registrationDTO
105      * @throws DAOException 
106      */
107     private void createRegistration(
108             gov.ojp.it.jxdm._3_0.VehicleRegistration registrationDTO) 
109         throws Exception {
110         ejava.projects.edmv.bo.VehicleRegistration registrationBO =
111             new ejava.projects.edmv.bo.VehicleRegistration();
112         registrationBO.setVin(
113                 registrationDTO.getVehicle().getVehicleID().getID().getValue());
114         
115         //add the m-m owners
116         for (gov.ojp.it.jxdm._3_0.ReferenceType ref: 
117                 registrationDTO.getVehicle().getPropertyOwnerPerson()) {
118             gov.ojp.it.jxdm._3_0.Person ownerDTO =
119                 (gov.ojp.it.jxdm._3_0.Person)ref.getRef();
120             ejava.projects.edmv.bo.Person ownerBO = 
121                 dto2bo.get(ownerDTO.getId());
122             if (ownerBO == null) {
123                 throw new EDmvException("owner not found:" + ownerDTO.getId()); 
124             }
125             
126             registrationBO.getOwners().add(ownerBO);
127         }
128             
129         vehicleDAO.createRegistration(registrationBO);
130         log.debug("created registration:" + registrationBO);
131     }
132 }