View Javadoc

1   /**
2    * Copyright 2014 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
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   * @author Kuali Mobility Team (mobility.dev@kuali.org)
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  }