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
23
24
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
49
50
51
52
53
54
55
56
57
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
79
80
81
82
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
95 dto2bo.put(personDTO.getId(), personBO);
96 }
97
98
99
100
101
102
103
104
105
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
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 }