View Javadoc

1   /**
2    * Copyright 2011-2012 The Kuali Foundation Licensed under the Educational
3    * Community License, Version 2.0 (the "License"); you may not use this file
4    * except in compliance with the License. You may obtain a copy of the License
5    * at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12   * License for the specific language governing permissions and limitations under
13   * the License.
14   */
15  package org.kuali.mobility.news.service;
16  
17  import java.io.UnsupportedEncodingException;
18  import java.net.URLDecoder;
19  import java.util.ArrayList;
20  import java.util.List;
21  import javax.ws.rs.GET;
22  import javax.ws.rs.Path;
23  import javax.ws.rs.PathParam;
24  import javax.ws.rs.QueryParam;
25  import org.apache.commons.collections.CollectionUtils;
26  
27  import org.kuali.mobility.news.dao.NewsCache;
28  import org.kuali.mobility.news.dao.NewsDao;
29  import org.kuali.mobility.news.entity.NewsArticle;
30  import org.kuali.mobility.news.entity.NewsArticleImpl;
31  import org.kuali.mobility.news.entity.NewsSource;
32  import org.kuali.mobility.news.entity.NewsSourceImpl;
33  import org.kuali.mobility.news.service.util.NewsArticleTransform;
34  import org.kuali.mobility.news.service.util.NewsSourceTransform;
35  import org.springframework.context.ApplicationContext;
36  import org.springframework.context.ApplicationContextAware;
37  import org.springframework.web.bind.annotation.RequestParam;
38  
39  /**
40   * Service for actually doing the work of interacting with the nedws entity
41   * objects
42   *
43   * @author Kuali Mobility Team (mobility.dev@kuali.org)
44   * @see org.kuali.mobility.news.service.NewsService
45   */
46  public class NewsServiceImpl implements NewsService, ApplicationContextAware {
47  
48      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(NewsServiceImpl.class);
49      private ApplicationContext applicationContext;
50      private NewsDao dao;
51      private NewsCache cache;
52  
53      @Override
54      @GET
55      @Path("/sources/getAll")
56      public List<NewsSourceImpl> getAllNewsSources() {
57          LOG.debug("Called getAllNewsSources web service.");
58          List<NewsSourceImpl> sources = new ArrayList<NewsSourceImpl>();
59          CollectionUtils.collect(getDao().findAllNewsSources(), new NewsSourceTransform(), sources);
60          return sources;
61      }
62  
63      @Override
64      @GET
65      @Path("/sources/getAllActive")
66      public List<NewsSourceImpl> getAllActiveNewsSources() {
67          LOG.debug("Called getAllActiveNewsSources web service.");
68          List<NewsSourceImpl> sources = new ArrayList<NewsSourceImpl>();
69          CollectionUtils.collect(getDao().findAllActiveNewsSources(), new NewsSourceTransform(), sources);
70          return sources;
71      }
72  
73      @Override
74      @GET
75      @Path("/sources/getAllActive/{parentId}")
76      public List<NewsSourceImpl> getAllActiveNewsSources(@PathParam("parentId") final Long parentId) {
77          LOG.debug("Called getAllActiveNewsSources web service.");
78          List<NewsSourceImpl> sources = new ArrayList<NewsSourceImpl>();
79          CollectionUtils.collect(getDao().findAllActiveNewsSources(parentId), new NewsSourceTransform(), sources);
80          return sources;
81      }
82  
83      @Override
84      @GET
85      @Path("/sources/getChildNewsSource/{parentId}")
86      public List<NewsSourceImpl> getNewsSources(@PathParam("parentId") final Long parentId, @QueryParam("active") final Boolean isActive) {
87          List<NewsSourceImpl> sources = new ArrayList<NewsSourceImpl>();
88          CollectionUtils.collect(getDao().findNewsSources(parentId, isActive), new NewsSourceTransform(), sources);
89          return sources;
90      }
91  
92      @Override
93      @GET
94      @Path("/sources/getNewsSource/{sourceId}")
95      public NewsSourceImpl getNewsSourceById(@PathParam("sourceId") final Long id) {
96          LOG.debug("Called getNewsSourceById web service.");
97          NewsSourceTransform transform = new NewsSourceTransform();
98          return transform.transform(getDao().lookup(id));
99      }
100 
101     @Override
102     @GET
103     @Path("/source/{sourceId}/article/{articleId}")
104     public NewsArticleImpl getNewsArticle(@PathParam("articleId") final String articleId, @PathParam("sourceId") final long sourceId) {
105         NewsArticleImpl foundArticle = null;
106         LOG.debug("Looking for article id " + articleId);
107         NewsSource source = getNewsSourceById(sourceId);
108         NewsArticleTransform transform = new NewsArticleTransform();
109         for (NewsArticle article : source.getArticles()) {
110             LOG.debug("Comparing with: " + article.getArticleId());
111             String id;
112             try {
113                 id = URLDecoder.decode(article.getArticleId(), "UTF-8");
114 //				if (articleId.equals(id)) {
115 //					return article.copy();
116 //				}
117                 if (articleId.equalsIgnoreCase(id) || articleId.equalsIgnoreCase(article.getArticleId())) {
118                     foundArticle = transform.transform(article);
119                     break;
120                 }
121             } catch (UnsupportedEncodingException e) {
122                 LOG.error(e.getLocalizedMessage());
123             }
124         }
125         return foundArticle;
126     }
127 
128     @Override
129     @GET
130     @Path("/articles")
131     public NewsArticleImpl getNewsArticle(@QueryParam("articleId") final String articleId) {
132         NewsArticleImpl foundArticle = null;
133         LOG.debug("Looking for article id " + articleId);
134         NewsArticleTransform transform = new NewsArticleTransform();
135         for (NewsSource source : getDao().findAllActiveNewsSources()) {
136             if (source.getArticles() != null && source.getArticles().size() > 0) {
137                 for (NewsArticle article : source.getArticles()) {
138                     LOG.debug("Comparing with: " + article.getArticleId());
139                     String id;
140                     try {
141                         id = URLDecoder.decode(article.getArticleId(), "UTF-8");
142                         if (articleId.equalsIgnoreCase(id) || articleId.equalsIgnoreCase(article.getArticleId())) {
143                             foundArticle = transform.transform(article);
144                             break;
145                         }
146                     } catch (UnsupportedEncodingException e) {
147                         LOG.error(e.getLocalizedMessage());
148                     }
149                 }
150             }
151         }
152         return foundArticle;
153     }
154 
155     public ApplicationContext getApplicationContext() {
156         return applicationContext;
157     }
158 
159     public void setApplicationContext(ApplicationContext applicationContext) {
160         this.applicationContext = applicationContext;
161     }
162 
163     public NewsDao getDao() {
164         return dao;
165     }
166 
167     public void setDao(NewsDao dao) {
168         this.dao = dao;
169     }
170 
171     public NewsCache getCache() {
172         return cache;
173     }
174 
175     public void setCache(NewsCache cache) {
176         this.cache = cache;
177     }
178 }