1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.kuali.mobility.computerlabs.dao;
16
17 import org.apache.log4j.Logger;
18 import org.kuali.mobility.computerlabs.entity.Lab;
19 import org.kuali.mobility.computerlabs.entity.LabGroup;
20 import org.kuali.mobility.computerlabs.entity.Location;
21 import org.kuali.mobility.shared.InitBean;
22 import org.kuali.mobility.util.mapper.DataMapper;
23 import org.springframework.context.ApplicationContext;
24 import org.springframework.context.ApplicationContextAware;
25
26 import javax.annotation.Resource;
27 import java.io.IOException;
28 import java.net.MalformedURLException;
29 import java.net.URL;
30 import java.util.ArrayList;
31 import java.util.LinkedHashMap;
32 import java.util.List;
33 import java.util.Map;
34
35
36
37
38
39 public class ComputerLabsInitBean implements InitBean, ApplicationContextAware {
40
41 private static Logger LOG = Logger.getLogger(ComputerLabsInitBean.class);
42 private ApplicationContext applicationContext;
43 @Resource(name="computerLabDao")
44 private ComputerLabsDao dao;
45
46 private Map<String, List<String>> labUrls;
47
48 @Resource(name="dataMapper")
49 private DataMapper dataMapper;
50
51 private String dataMappingUrl;
52
53 public void loadData() {
54 List<LabGroup> groups = new ArrayList<LabGroup>();
55 for (String key : getLabUrls().keySet()) {
56 LabGroup group = (LabGroup) getApplicationContext().getBean("computerLabGroupBean");
57 group.setName(key);
58 LOG.debug("Processing lab group " + key);
59 for (String groupUrl : getLabUrls().get(key)) {
60 try {
61 LOG.debug("SourceUrl: " + groupUrl);
62 List<Lab> groupLabs = new ArrayList<Lab>();
63 try {
64 URL url = new URL(groupUrl);
65 if (getDataMappingUrl() != null && !"".equals(getDataMappingUrl().trim())) {
66 groupLabs = getDataMapper().mapData(groupLabs, url, new URL(getDataMappingUrl()));
67 } else {
68 groupLabs = getDataMapper().mapData(groupLabs, url, "labMapping.xml");
69 }
70 } catch (MalformedURLException mue) {
71 LOG.error(mue.getLocalizedMessage(), mue);
72 LOG.debug( "Bad URL, attempting to load data as file in classpath: "+groupUrl);
73 groupLabs = getDataMapper().mapData(groupLabs, groupUrl, "labMapping.xml");
74 }
75 LOG.debug("Loaded " + groupLabs.size() + " labs for group " + group.getName());
76 Map<String, Location> locations = new LinkedHashMap<String, Location>();
77 for (Lab lab : groupLabs) {
78 Location location = null;
79 if (locations.containsKey(lab.getBuildingCode())) {
80 location = locations.get(lab.getBuildingCode());
81 } else {
82 location = (Location) getApplicationContext().getBean("computerLabLocationBean");
83 location.setName(lab.getBuildingCode());
84 locations.put(lab.getBuildingCode(), location);
85 }
86 location.addLab(lab);
87 }
88 LOG.debug("Sorted into " + locations.size() + " locations.");
89 group.setLocations(new ArrayList(locations.values()));
90 } catch (MalformedURLException mue) {
91 LOG.error(mue.getLocalizedMessage(), mue);
92 } catch (ClassNotFoundException cnfe) {
93 LOG.error(cnfe.getLocalizedMessage(), cnfe);
94 } catch (IOException io) {
95 LOG.error(io.getLocalizedMessage(), io);
96 } catch (Exception e) {
97 LOG.error(e.getLocalizedMessage(), e);
98 }
99 }
100 groups.add(group);
101 }
102 LOG.debug("Preparing to set " + groups.size() + " lab groups in dao.");
103 getDao().setLabGroups(groups);
104 }
105
106
107
108
109 public ComputerLabsDao getDao() {
110 return dao;
111 }
112
113
114
115
116 public void setDao(ComputerLabsDao dao) {
117 this.dao = dao;
118 }
119
120
121
122
123 public ApplicationContext getApplicationContext() {
124 return applicationContext;
125 }
126
127
128
129
130 public void setApplicationContext(ApplicationContext applicationContext) {
131 this.applicationContext = applicationContext;
132 }
133
134
135
136
137 public Map<String, List<String>> getLabUrls() {
138 return labUrls;
139 }
140
141
142
143
144 public void setLabUrls(Map<String, List<String>> labUrls) {
145 this.labUrls = labUrls;
146 }
147
148
149
150
151 public DataMapper getDataMapper() {
152 return dataMapper;
153 }
154
155
156
157
158 public void setDataMapper(DataMapper dataMapper) {
159 this.dataMapper = dataMapper;
160 }
161
162
163
164
165 public String getDataMappingUrl() {
166 return dataMappingUrl;
167 }
168
169
170
171
172 public void setDataMappingUrl(String dataMappingUrl) {
173 this.dataMappingUrl = dataMappingUrl;
174 }
175 }