View Javadoc
1   package ejava.projects.esales.blimpl;
2   
3   import java.io.InputStream;
4   
5   import javax.xml.bind.JAXBException;
6   import javax.xml.stream.XMLStreamException;
7   
8   import org.slf4j.Logger;
9   import org.slf4j.LoggerFactory;
10  
11  import ejava.projects.esales.dao.AccountDAO;
12  import ejava.projects.esales.dto.ESales;
13  import ejava.projects.esales.xml.ESalesParser;
14  
15  public class ESalesIngestor {
16  	private static final Logger log = LoggerFactory.getLogger(ESalesIngestor.class);
17  	InputStream is;
18  	AccountDAO accountDAO;
19  	ESalesParser parser;
20  	
21  	public void setInputStream(InputStream is) {
22  		this.is = is; 
23  	}
24  	
25  	public void setAccountDAO(AccountDAO accountDAO) {
26  		this.accountDAO = accountDAO;
27  	}
28  	
29  	/**
30  	 * This method will ingest the input data by reading in external DTOs in
31  	 * from the parser, instantiating project business objects, and inserting
32  	 * into database. Note that the XML Schema is organized such that object
33  	 * references are fully resolved. Therefore, there is no specific need
34  	 * to process the addresses as they come in. They can be stored once we
35  	 * get the accounts they are related to.
36  	 * 
37  	 * @throws JAXBException
38  	 * @throws XMLStreamException
39  	 */
40  	public void ingest() throws JAXBException, XMLStreamException {
41  		ESalesParser parser = new ESalesParser(ESales.class, is);
42  		
43  		Object object = parser.getObject(
44  				"address", "account", "auction", "image");
45  		while (object != null) {
46  			if (object instanceof ejava.projects.esales.dto.Account) {
47  				createAccount((ejava.projects.esales.dto.Account)object);
48  			}
49  			object = parser.getObject(
50  					"address", "account", "auction", "image");
51  		}
52  	}
53  	
54  	/**
55  	 * This method is called by the main ingest processing loop. The JAXB/StAX
56  	 * parser will already have the Account populated with Address information.
57  	 * @param accountDTO
58  	 */
59  	private void createAccount(ejava.projects.esales.dto.Account accountDTO) {
60  		ejava.projects.esales.bo.Account accountBO = 
61  			new ejava.projects.esales.bo.Account(accountDTO.getLogin());
62  		accountBO.setFirstName(accountDTO.getFirstName());
63  		for (Object o : accountDTO.getAddress()) {
64  			ejava.projects.esales.dto.Address addressDTO = 
65  				(ejava.projects.esales.dto.Address)o;
66  			ejava.projects.esales.bo.Address addressBO = 
67  				new ejava.projects.esales.bo.Address(
68  						0,
69  						addressDTO.getName(),
70  						addressDTO.getCity());
71  			accountBO.getAddresses().add(addressBO);
72  		}
73  		accountDAO.createAccount(accountBO);
74  		log.debug("created account:" + accountBO);
75  	}
76  }