View Javadoc

1   /**
2    * Copyright 2011 The Kuali Foundation Licensed under the Educational Community
3    * License, Version 2.0 (the "License"); you may not use this file except in
4    * compliance with the License. You may obtain a copy of the License at
5    *
6    * http://www.osedu.org/licenses/ECL-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11   * License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  package org.kuali.mobility.news.dao;
15  
16  import java.io.IOException;
17  import java.net.MalformedURLException;
18  import java.net.URL;
19  import java.util.ArrayList;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.apache.commons.collections.CollectionUtils;
25  import org.apache.log4j.Logger;
26  import org.kuali.mobility.news.entity.NewsSource;
27  import org.kuali.mobility.news.util.NewsSourcePredicate;
28  import org.kuali.mobility.util.mapper.DataMapper;
29  import org.springframework.context.ApplicationContext;
30  
31  public class NewsDaoImpl implements NewsDao {
32  
33      public static Logger LOG = Logger.getLogger(NewsDaoImpl.class);
34      private ApplicationContext applicationContext;
35      private NewsCache cache;
36      private DataMapper mapper;
37      private String newsSourceFile;
38      private String newsSourceUrl;
39      private String newsMappingFile;
40      private String newsMappingUrl;
41  
42      public List<? extends NewsSource> findNewsSources(final Long parentId, final Boolean isActive) {
43          initData();
44          return (List<? extends NewsSource>) CollectionUtils.select(getCache().getNewsSources().values(), new NewsSourcePredicate(parentId, isActive));
45      }
46  
47      @Override
48      public List<? extends NewsSource> findAllActiveNewsSources() {
49          initData();
50          return (List<? extends NewsSource>) CollectionUtils.select(getCache().getNewsSources().values(), new NewsSourcePredicate(null, new Boolean(true)));
51      }
52  
53      public List<? extends NewsSource> findAllActiveNewsSources(final Long parentId) {
54          initData();
55          return (List<? extends NewsSource>) CollectionUtils.select((List<NewsSource>) getCache().getNewsSources(), new NewsSourcePredicate(parentId, new Boolean(true)));
56      }
57  
58      @Override
59      public List<? extends NewsSource> findAllNewsSources() {
60          initData();
61          return (List<? extends NewsSource>) (new ArrayList<NewsSource>(getCache().getNewsSources().values()));
62      }
63  
64      public List<? extends NewsSource> findAllNewsSources(final Long parentId) {
65          initData();
66          return (List<? extends NewsSource>) CollectionUtils.select((List<NewsSource>) getCache().getNewsSources(), new NewsSourcePredicate(parentId, null));
67      }
68  
69      @Override
70      public NewsSource lookup(Long id) {
71          initData();
72          NewsSource source = null;
73          source = getCache().getNewsSources().get(id);
74          return source;
75      }
76  
77      @Override
78      public NewsSource save(NewsSource newsSource) {
79          // TODO Auto-generated method stub
80          return null;
81      }
82  
83      @Override
84      public NewsSource delete(NewsSource newsSource) {
85          // TODO Auto-generated method stub
86          return null;
87      }
88  
89      @SuppressWarnings("unchecked")
90      private void initData() {
91          Map<Long, NewsSource> source = getCache().getNewsSources();
92          if (source == null || source.isEmpty()) {
93              source = new HashMap<Long, NewsSource>();
94              List<NewsSource> sources = new ArrayList<NewsSource>();
95  
96              boolean isNewsSourceUrlAvailable = (getNewsSourceUrl() != null ? true : false);
97              boolean isNewsMappingUrlAvailable = (getNewsMappingUrl() != null ? true : false);
98  
99              try {
100                 if (isNewsSourceUrlAvailable) {
101                     if (isNewsMappingUrlAvailable) {
102                         sources = (List<NewsSource>) mapper.mapData(source,
103                                 new URL(getNewsSourceUrl()),
104                                 new URL(getNewsMappingUrl()));
105                     } else {
106                         sources = (List<NewsSource>) mapper.mapData(source,
107                                 new URL(getNewsSourceUrl()),
108                                 getNewsMappingFile());
109 
110                     }
111                 } else {
112                     if (isNewsMappingUrlAvailable) {
113                         // not supported in mapper.mapData
114                         LOG.error("DataMapper does NOT support this case!");
115                         return;
116                     } else {
117                         sources = (List<NewsSource>) mapper.mapData(source,
118                                 getNewsSourceFile(), getNewsMappingFile());
119                     }
120 
121                 }
122 
123                 int i = 0;
124                 for (NewsSource s : sources) {
125                     s.setActive(true);
126                     if (s.isActive()) {
127                         getCache().updateSource(s);
128                     }
129                     if (s.getId() == null) {
130                         s.setId(new Long(i));
131                     }
132                     source.put(s.getId(), s);
133                     i++;
134                 }
135 
136                 for (NewsSource s : source.values()) {
137                     if (null != s.getParentId() && s.getParentId().intValue() > 0) {
138                         NewsSource parent = source.get(s.getParentId());
139                         parent.addChild(s);
140                     }
141                 }
142 
143                 getCache().setNewsSources(source);
144 
145             } catch (MalformedURLException e) {
146                 LOG.error(e.getMessage());
147                 // e.printStackTrace();
148             } catch (ClassNotFoundException e) {
149                 LOG.error(e.getMessage());
150                 // e.printStackTrace();
151             } catch (IOException e) {
152                 LOG.error(e.getMessage());
153                 // e.printStackTrace();
154             }
155 
156         }
157     }
158 
159     public ApplicationContext getApplicationContext() {
160         return applicationContext;
161     }
162 
163     public void setApplicationContext(ApplicationContext applicationContext) {
164         this.applicationContext = applicationContext;
165     }
166 
167     public NewsCache getCache() {
168         return cache;
169     }
170 
171     public void setCache(NewsCache cache) {
172         this.cache = cache;
173     }
174 
175     public DataMapper getMapper() {
176         return mapper;
177     }
178 
179     public void setMapper(DataMapper mapper) {
180         this.mapper = mapper;
181     }
182 
183     public String getNewsSourceFile() {
184         return newsSourceFile;
185     }
186 
187     public void setNewsSourceFile(String newsSourceFile) {
188         this.newsSourceFile = newsSourceFile;
189     }
190 
191     public String getNewsSourceUrl() {
192         return newsSourceUrl;
193     }
194 
195     public void setNewsSourceUrl(String newsSourceUrl) {
196         this.newsSourceUrl = newsSourceUrl;
197     }
198 
199     public String getNewsMappingFile() {
200         return newsMappingFile;
201     }
202 
203     public void setNewsMappingFile(String newsMappingFile) {
204         this.newsMappingFile = newsMappingFile;
205     }
206 
207     public String getNewsMappingUrl() {
208         return newsMappingUrl;
209     }
210 
211     public void setNewsMappingUrl(String newsMappingUrl) {
212         this.newsMappingUrl = newsMappingUrl;
213     }
214 }