View Javadoc

1   /**
2    * Copyright 2011-2013 The Kuali Foundation Licensed under the Educational
3    * Community License, Version 2.0 (the "License"); you may not use this file
4    * except in compliance with the License. You may obtain a copy of the License
5    * at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12   * License for the specific language governing permissions and limitations under
13   * the License.
14   */
15  package org.kuali.mobility.computerlabs.dao;
16  
17  import java.io.IOException;
18  import java.net.MalformedURLException;
19  import java.net.URL;
20  import java.util.ArrayList;
21  import java.util.LinkedHashMap;
22  import java.util.List;
23  import java.util.Map;
24  import org.apache.log4j.Logger;
25  import org.kuali.mobility.computerlabs.entity.Lab;
26  import org.kuali.mobility.computerlabs.entity.LabGroup;
27  import org.kuali.mobility.computerlabs.entity.Location;
28  import org.kuali.mobility.util.mapper.DataMapper;
29  import org.springframework.context.ApplicationContext;
30  import org.springframework.context.ApplicationContextAware;
31  
32  /**
33   *
34   * @author Joe Swanson <joseswan@umich.edu>
35   */
36  public class ComputerLabsInitBean implements ApplicationContextAware {
37  
38  	private static Logger LOG = Logger.getLogger(ComputerLabsInitBean.class);
39  	private ApplicationContext applicationContext;
40  	private ComputerLabsDao dao;
41  	private int waitInterval = 300;
42  	private static Thread backgroundThread = null;
43  	private Map<String, List<String>> labUrls;
44  	private DataMapper dataMapper;
45  	private String dataMappingUrl;
46  	private boolean shouldRun = true;
47  
48  	public void init() {
49  		backgroundThread = new Thread(new ComputerLabsInitBean.BackgroundThread());
50  		backgroundThread.setDaemon(true);
51  		backgroundThread.start();
52  	}
53  
54  	public void cleanup() {
55  		LOG.info("Cleaning up computer lab data.");
56  	}
57  
58  	/**
59  	 * @return the dao
60  	 */
61  	public ComputerLabsDao getDao() {
62  		return dao;
63  	}
64  
65  	/**
66  	 * @param dao the dao to set
67  	 */
68  	public void setDao(ComputerLabsDao dao) {
69  		this.dao = dao;
70  	}
71  
72  	/**
73  	 * @return the applicationContext
74  	 */
75  	public ApplicationContext getApplicationContext() {
76  		return applicationContext;
77  	}
78  
79  	/**
80  	 * @param applicationContext the applicationContext to set
81  	 */
82  	public void setApplicationContext(ApplicationContext applicationContext) {
83  		this.applicationContext = applicationContext;
84  	}
85  
86  	/**
87  	 * @return the waitInterval
88  	 */
89  	public int getWaitInterval() {
90  		return waitInterval;
91  	}
92  
93  	/**
94  	 * @param waitInterval the waitInterval to set
95  	 */
96  	public void setWaitInterval(int waitInterval) {
97  		this.waitInterval = waitInterval;
98  	}
99  
100 	/**
101 	 * @return the labUrls
102 	 */
103 	public Map<String, List<String>> getLabUrls() {
104 		return labUrls;
105 	}
106 
107 	/**
108 	 * @param labUrls the labUrls to set
109 	 */
110 	public void setLabUrls(Map<String, List<String>> labUrls) {
111 		this.labUrls = labUrls;
112 	}
113 
114 	/**
115 	 * @return the dataMapper
116 	 */
117 	public DataMapper getDataMapper() {
118 		return dataMapper;
119 	}
120 
121 	/**
122 	 * @param dataMapper the dataMapper to set
123 	 */
124 	public void setDataMapper(DataMapper dataMapper) {
125 		this.dataMapper = dataMapper;
126 	}
127 
128 	/**
129 	 * @return the dataMappingUrl
130 	 */
131 	public String getDataMappingUrl() {
132 		return dataMappingUrl;
133 	}
134 
135 	/**
136 	 * @param dataMappingUrl the dataMappingUrl to set
137 	 */
138 	public void setDataMappingUrl(String dataMappingUrl) {
139 		this.dataMappingUrl = dataMappingUrl;
140 	}
141 
142 	public void loadData() {
143 		List<LabGroup> groups = new ArrayList<LabGroup>();
144 		for (String key : getLabUrls().keySet()) {
145 			LabGroup group = (LabGroup) getApplicationContext().getBean("computerLabGroupBean");
146 			group.setName(key);
147 			LOG.debug("Processing lab group " + key);
148 			for (String groupUrl : getLabUrls().get(key)) {
149 				try {
150 					LOG.debug("SourceUrl: " + groupUrl);
151 					List<Lab> groupLabs = new ArrayList<Lab>();
152 					try {
153 						URL url = new URL(groupUrl);
154 						if (getDataMappingUrl() != null && !"".equals(getDataMappingUrl().trim())) {
155 							groupLabs = getDataMapper().mapData(groupLabs, url, new URL(getDataMappingUrl()));
156 						} else {
157 							groupLabs = getDataMapper().mapData(groupLabs, url, "labMapping.xml");
158 						}
159 					} catch (MalformedURLException mue) {
160 						LOG.error(mue.getLocalizedMessage(), mue);
161 						LOG.debug( "Bad URL, attempting to load data as file in classpath: "+groupUrl);
162 						groupLabs = getDataMapper().mapData(groupLabs, groupUrl, "labMapping.xml");
163 					}
164 					LOG.debug("Loaded " + groupLabs.size() + " labs for group " + group.getName());
165 					Map<String, Location> locations = new LinkedHashMap<String, Location>();
166 					for (Lab lab : groupLabs) {
167 						Location location = null;
168 						if (locations.containsKey(lab.getBuildingCode())) {
169 							location = locations.get(lab.getBuildingCode());
170 						} else {
171 							location = (Location) getApplicationContext().getBean("computerLabLocationBean");
172 							location.setName(lab.getBuildingCode());
173 							locations.put(lab.getBuildingCode(), location);
174 						}
175 						location.addLab(lab);
176 					}
177 					LOG.debug("Sorted into " + locations.size() + " locations.");
178 					group.setLocations(new ArrayList(locations.values()));
179 				} catch (MalformedURLException mue) {
180 					LOG.error(mue.getLocalizedMessage(), mue);
181 				} catch (ClassNotFoundException cnfe) {
182 					LOG.error(cnfe.getLocalizedMessage(), cnfe);
183 				} catch (IOException io) {
184 					LOG.error(io.getLocalizedMessage(), io);
185 				} catch (Exception e) {
186 					LOG.error(e.getLocalizedMessage(), e);
187 				}
188 			}
189 			groups.add(group);
190 		}
191 		LOG.debug("Preparing to set " + groups.size() + " lab groups in dao.");
192 		getDao().setLabGroups(groups);
193 	}
194 
195 	/**
196 	 * @return the shouldRun
197 	 */
198 	public boolean shouldRun() {
199 		return shouldRun;
200 	}
201 
202 	/**
203 	 * @param shouldRun the shouldRun to set
204 	 */
205 	public void setShouldRun(boolean shouldRun) {
206 		this.shouldRun = shouldRun;
207 	}
208 
209 	private class BackgroundThread implements Runnable {
210 
211 		@Override
212 		public void run() {
213 			while (shouldRun()) {
214 				try {
215 					/*
216 					 * Sleeping first for base KME since the test data
217 					 * is self referencing and the URLs won't be valid
218 					 * when the bean is initialized by Spring but will
219 					 * be once the web app is fully up and running.
220 					 */
221 					Thread.sleep(1000 * getWaitInterval());
222 					loadData();
223 					// Loop over campuses
224 				} catch (InterruptedException ie) {
225 					LOG.error(ie.getLocalizedMessage(), ie);
226 				} catch (NullPointerException npe) {
227 					// No idea why we get these yet.
228 					LOG.error(npe.getLocalizedMessage(), npe);
229 				} catch (Exception e) {
230 					LOG.error(e.getLocalizedMessage(), e);
231 				}
232 			}
233 		}
234 	}
235 
236 	/**
237 	 * @return the backgroundThread
238 	 */
239 	public static Thread getBackgroundThread() {
240 		return backgroundThread;
241 	}
242 
243 	/**
244 	 * @param aBackgroundThread the backgroundThread to set
245 	 */
246 	public static void setBackgroundThread(Thread aBackgroundThread) {
247 		backgroundThread = aBackgroundThread;
248 	}
249 }