View Javadoc

1   package org.kuali.mobility.bus.dao.helper;
2   
3   import java.io.InputStream;
4   import java.util.List;
5   import org.apache.log4j.Logger;
6   
7   import com.thoughtworks.xstream.XStream;
8   
9   public class BusStopNameUtil {
10  
11      public static Logger LOG = Logger.getLogger(BusStopNameUtil.class);
12      private String busStopNameMappingFile;
13      private List<UMBusStopName> busStopNames;
14  
15      public BusStopNameUtil() {
16      }
17  
18      @SuppressWarnings("unchecked")
19      public void loadStopNames() {
20          LOG.debug( "Preparing to load bus stop name mappings from "+getBusStopNameMappingFile() );
21          XStream xstream = new XStream();
22          xstream.processAnnotations(UMBusStopNames.class);
23          xstream.processAnnotations(UMBusStopName.class);
24          InputStream stream = this.getClass().getClassLoader().getResourceAsStream(getBusStopNameMappingFile());
25          UMBusStopNames stopnames = null;
26          try {
27              stopnames = (UMBusStopNames) xstream.fromXML(stream);
28              this.setBusStopNames(stopnames.getStopnames());
29              LOG.debug("Loaded " + getBusStopNames().size() + " bus stopnames ");
30          } catch (Exception e) {
31              LOG.error( e.getLocalizedMessage(), e );
32          }
33      }
34  
35      public String updateBusStopName(String name) {
36          String stopName = new String();
37          if( null != name ) {
38              for (UMBusStopName sn : this.getBusStopNames()) {
39                  LOG.debug( "N1 ["+sn.getName1()+"] N2 ["+sn.getName2()+"]");
40                  if (sn.getName1().equalsIgnoreCase(name)) {
41                      stopName = sn.getName2();
42                      break;
43                  } else {
44                      stopName = name;
45                  }
46              }
47          }
48          else {
49              stopName=name;
50          }
51          return stopName;
52  
53      }
54  
55      public List<UMBusStopName> getBusStopNames() {
56          if ( busStopNames == null || busStopNames.isEmpty() ) {
57              LOG.debug( "Bus Stop name mappings are not loaded.  Attempting to load them." );
58              loadStopNames();
59          }
60          return busStopNames;
61      }
62  
63      public void setBusStopNames(List<UMBusStopName> busStopNames) {
64          this.busStopNames = busStopNames;
65      }
66  
67      /**
68       * @return the busStopNameMappingFile
69       */
70      public String getBusStopNameMappingFile() {
71          return busStopNameMappingFile;
72      }
73  
74      /**
75       * @param busStopNameMappingFile the busStopNameMappingFile to set
76       */
77      public void setBusStopNameMappingFile(String mappingFile) {
78          this.busStopNameMappingFile = mappingFile;
79      }
80  }