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