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.slf4j.Logger;
18  import org.slf4j.LoggerFactory;
19  import org.kuali.mobility.dining.entity.DiningHall;
20  import org.kuali.mobility.dining.entity.DiningHallGroup;
21  import org.kuali.mobility.shared.InitBean;
22  
23  import javax.annotation.Resource;
24  import javax.xml.bind.JAXBContext;
25  import javax.xml.bind.JAXBException;
26  import javax.xml.bind.Unmarshaller;
27  import java.io.InputStream;
28  import java.util.ArrayList;
29  import java.util.List;
30  import java.util.Map;
31  
32  /**
33   * @author Kuali Mobility Team (mobility.dev@kuali.org)
34   */
35  public class DiningInitBean implements InitBean {
36  
37      private static final Logger LOG = LoggerFactory.getLogger(DiningInitBean.class);
38  
39      @Resource(name="diningDao")
40      private DiningDao dao;
41  
42      private Map<String, List<String>> diningUrls;
43      private boolean loadFromFile;
44  
45      public void loadData() {
46          List<DiningHallGroup> diningHallGroups = new ArrayList<DiningHallGroup>();
47          DiningHall diningHall = null;
48          DiningHallGroup diningHallGroup = new DiningHallGroup();
49          if( isLoadFromFile() ) {
50              for( String key : getDiningUrls().keySet() ) {
51  
52                  try {
53                      JAXBContext jc = JAXBContext.newInstance(DiningHallGroup.class);
54                      Unmarshaller um = jc.createUnmarshaller();
55                      InputStream in = this.getClass().getResourceAsStream( getDiningUrls().get(key).get(0));
56                      diningHallGroup = (DiningHallGroup) um.unmarshal(in);
57  
58                  } catch (JAXBException jbe) {
59                      LOG.error(jbe.getLocalizedMessage(), jbe);
60                  }
61                  diningHallGroups.add(diningHallGroup);
62              }
63          }
64          getDao().setDiningHallGroups(diningHallGroups);
65      }
66  
67      public DiningDao getDao() {
68          return dao;
69      }
70  
71      public void setDao(DiningDao dao) {
72          this.dao = dao;
73      }
74  
75      public Map<String, List<String>> getDiningUrls() {
76          return diningUrls;
77      }
78  
79      public void setDiningUrls(Map<String, List<String>> diningUrls) {
80          this.diningUrls = diningUrls;
81      }
82  
83      public boolean isLoadFromFile() {
84          return loadFromFile;
85      }
86  
87      public void setLoadFromFile(boolean loadFromFile) {
88          this.loadFromFile = loadFromFile;
89      }
90  }