1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.kuali.mobility.dining.dao;
16
17 import org.apache.log4j.Logger;
18 import org.kuali.mobility.dining.entity.DiningHall;
19 import org.kuali.mobility.shared.InitBean;
20
21 import javax.annotation.Resource;
22 import javax.xml.bind.JAXBContext;
23 import javax.xml.bind.JAXBException;
24 import javax.xml.bind.Unmarshaller;
25 import java.io.InputStream;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Map;
29
30
31
32
33 public class DiningInitBean implements InitBean {
34
35 private static final Logger LOG = Logger.getLogger(DiningInitBean.class);
36
37 @Resource(name="diningDao")
38 private DiningDao dao;
39
40 private Map<String, List<String>> diningUrls;
41 private boolean loadFromFile;
42
43 public void loadData() {
44 List<DiningHall> diningHalls = new ArrayList<DiningHall>();
45
46 if( isLoadFromFile() ) {
47 for( String key : getDiningUrls().keySet() ) {
48 DiningHall diningHall = null;
49 try {
50 JAXBContext jc = JAXBContext.newInstance(DiningHall.class);
51 Unmarshaller um = jc.createUnmarshaller();
52 InputStream in = this.getClass().getResourceAsStream( getDiningUrls().get(key).get(0));
53 diningHall = (DiningHall) um.unmarshal(in);
54
55 } catch (JAXBException jbe) {
56 LOG.error(jbe.getLocalizedMessage(), jbe);
57 }
58 diningHalls.add(diningHall);
59 }
60 }
61 getDao().setDiningHalls(diningHalls);
62 }
63
64 public DiningDao getDao() {
65 return dao;
66 }
67
68 public void setDao(DiningDao dao) {
69 this.dao = dao;
70 }
71
72 public Map<String, List<String>> getDiningUrls() {
73 return diningUrls;
74 }
75
76 public void setDiningUrls(Map<String, List<String>> diningUrls) {
77 this.diningUrls = diningUrls;
78 }
79
80 public boolean isLoadFromFile() {
81 return loadFromFile;
82 }
83
84 public void setLoadFromFile(boolean loadFromFile) {
85 this.loadFromFile = loadFromFile;
86 }
87 }