View Javadoc
1   /**
2    * Copyright 2011-2014 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 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   * @author Joe Swanson <joseswan@umich.edu>
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 	 * @return the dao
108 	 */
109 	public ComputerLabsDao getDao() {
110 		return dao;
111 	}
112 
113 	/**
114 	 * @param dao the dao to set
115 	 */
116 	public void setDao(ComputerLabsDao dao) {
117 		this.dao = dao;
118 	}
119 
120 	/**
121 	 * @return the applicationContext
122 	 */
123 	public ApplicationContext getApplicationContext() {
124 		return applicationContext;
125 	}
126 
127 	/**
128 	 * @param applicationContext the applicationContext to set
129 	 */
130 	public void setApplicationContext(ApplicationContext applicationContext) {
131 		this.applicationContext = applicationContext;
132 	}
133 
134 	/**
135 	 * @return the labUrls
136 	 */
137 	public Map<String, List<String>> getLabUrls() {
138 		return labUrls;
139 	}
140 
141 	/**
142 	 * @param labUrls the labUrls to set
143 	 */
144 	public void setLabUrls(Map<String, List<String>> labUrls) {
145 		this.labUrls = labUrls;
146 	}
147 
148 	/**
149 	 * @return the dataMapper
150 	 */
151 	public DataMapper getDataMapper() {
152 		return dataMapper;
153 	}
154 
155 	/**
156 	 * @param dataMapper the dataMapper to set
157 	 */
158 	public void setDataMapper(DataMapper dataMapper) {
159 		this.dataMapper = dataMapper;
160 	}
161 
162 	/**
163 	 * @return the dataMappingUrl
164 	 */
165 	public String getDataMappingUrl() {
166 		return dataMappingUrl;
167 	}
168 
169 	/**
170 	 * @param dataMappingUrl the dataMappingUrl to set
171 	 */
172 	public void setDataMappingUrl(String dataMappingUrl) {
173 		this.dataMappingUrl = dataMappingUrl;
174 	}
175 }