View Javadoc
1   /*
2    * Copyright (c) 2002-2020, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.blog.business.rss;
35  
36  import java.util.ArrayList;
37  import java.util.HashMap;
38  import java.util.List;
39  import java.util.Locale;
40  import java.util.Map;
41  
42  import javax.servlet.http.HttpServletRequest;
43  
44  import org.apache.commons.collections.CollectionUtils;
45  import org.apache.commons.lang.StringUtils;
46  
47  import fr.paris.lutece.plugins.blog.business.Blog;
48  import fr.paris.lutece.plugins.blog.business.DocContent;
49  import fr.paris.lutece.plugins.blog.business.portlet.BlogPublication;
50  import fr.paris.lutece.plugins.blog.business.portlet.BlogPublicationHome;
51  import fr.paris.lutece.plugins.blog.service.BlogPlugin;
52  import fr.paris.lutece.plugins.blog.service.BlogService;
53  import fr.paris.lutece.plugins.blog.service.PublishingService;
54  import fr.paris.lutece.portal.business.portlet.Portlet;
55  import fr.paris.lutece.portal.business.rss.FeedResource;
56  import fr.paris.lutece.portal.business.rss.FeedResourceImage;
57  import fr.paris.lutece.portal.business.rss.FeedResourceItem;
58  import fr.paris.lutece.portal.business.rss.IFeedResource;
59  import fr.paris.lutece.portal.business.rss.IFeedResourceImage;
60  import fr.paris.lutece.portal.business.rss.IFeedResourceItem;
61  import fr.paris.lutece.portal.business.rss.ResourceRss;
62  import fr.paris.lutece.portal.service.i18n.I18nService;
63  import fr.paris.lutece.portal.service.plugin.Plugin;
64  import fr.paris.lutece.portal.service.plugin.PluginService;
65  import fr.paris.lutece.portal.service.template.AppTemplateService;
66  import fr.paris.lutece.portal.service.util.AppPropertiesService;
67  import fr.paris.lutece.util.ReferenceList;
68  import fr.paris.lutece.util.html.HtmlTemplate;
69  
70  /**
71   *
72   * BlogResourceRss
73   */
74  public class BlogResourceRss extends ResourceRss
75  {
76      // templates
77      private static final String TEMPLATE_PUSH_RSS_XML_BLOG = "admin/plugins/blog/rss/rss_xml_blogs.html";
78      private static final String TEMPLATE_RSS_BLOG_ITEM_TITLE = "admin/plugins/blog/rss/rss_blogs_item_title.html";
79      private static final String TEMPLATE_TASK_CREATE_CONFIG = "admin/plugins/blog/rss/rss_create_config.html";
80      private static final String TEMPLATE_TASK_MODIFY_CONFIG = "admin/plugins/blog/rss/rss_modify_config.html";
81      private static final String TEMPLATE_IMAGE_RSS = "/images/local/skin/plugins/rss/rss-image.png";
82  
83      // JSPs
84      private static final String JSP_PAGE_BLOG = "/jsp/site/Portal.jsp?page=blog";
85  
86      // Servlets
87      private static final String DOCUMENT_RESOURCE_SERVLET_URL = "/servlet/plugins/blogs/file";
88  
89      // Markers
90      private static final String MARK_RSS_ITEM_TITLE = "item_title";
91      private static final String MARK_RSS_ITEM_DESCRIPTION = "item_description";
92  
93      private static final String MARK_RSS_SITE_URL = "site_url";
94      private static final String MARK_RSS_FILE_LANGUAGE = "file_language";
95      private static final String MARK_PORTLET_LIST = "portlet_list";
96      private static final String MARK_ID_PORTLET = "id_portlet";
97  
98      // Parameters
99      private static final String PARAMETER_ID_BLOG = "id_blog";
100     private static final String PARAMETER_ID_PORTLET = "id_portlet";
101 
102     // Messages
103     private static final String PROPERTY_SITE_LANGUAGE = "rss.language";
104     private static final String PROPERTY_TYPE_IMAGE = "blog.rss.content.type.img";
105     private static final String PROPERTY_WEBAPP_PROD_URL = "lutece.prod.url";
106     private static final String PROPERTY_DESCRIPTION_WIRE = "blog.resource_rss.description_wire";
107     private static final String PROPERTY_TITLE_WIRE = "blog.resource_rss.title_wire";
108 
109     /**
110      * {@inheritDoc}
111      */
112     @Override
113     public boolean contentResourceRss( )
114     {
115 
116         return CollectionUtils.isNotEmpty( BlogPublicationHome.getAllDocPublication( ) );
117     }
118 
119     /**
120      * {@inheritDoc}
121      */
122     @Override
123     public void doSaveConfig( HttpServletRequest request, Locale locale )
124     {
125         Plugin plugin = PluginService.getPlugin( BlogPlugin.PLUGIN_NAME );
126         String stridPortlet = request.getParameter( PARAMETER_ID_PORTLET );
127 
128         BlogResourceRssConfigess/rss/BlogResourceRssConfig.html#BlogResourceRssConfig">BlogResourceRssConfig config = new BlogResourceRssConfig( );
129         config.setIdRss( this.getId( ) );
130         config.setIdPortlet( Integer.parseInt( stridPortlet ) );
131 
132         BlogResourceRssConfigHome.create( config, plugin );
133     }
134 
135     /**
136      * {@inheritDoc}
137      */
138     @Override
139     public void doUpdateConfig( HttpServletRequest request, Locale locale )
140     {
141         Plugin pluginForm = PluginService.getPlugin( BlogPlugin.PLUGIN_NAME );
142         String stridPortlet = request.getParameter( PARAMETER_ID_PORTLET );
143 
144         BlogResourceRssConfigess/rss/BlogResourceRssConfig.html#BlogResourceRssConfig">BlogResourceRssConfig config = new BlogResourceRssConfig( );
145         config.setIdRss( this.getId( ) );
146         config.setIdPortlet( Integer.parseInt( stridPortlet ) );
147 
148         BlogResourceRssConfigHome.update( config, pluginForm );
149     }
150 
151     /**
152      * {@inheritDoc}
153      */
154     @Override
155     public String doValidateConfigForm( HttpServletRequest request, Locale locale )
156     {
157         this.setDescription( I18nService.getLocalizedString( PROPERTY_DESCRIPTION_WIRE, locale ) );
158 
159         return null;
160     }
161 
162     /**
163      * {@inheritDoc}
164      */
165     @Override
166     public String getDisplayCreateConfigForm( HttpServletRequest request, Locale locale )
167     {
168 
169         List<Portlet> portletList = (List<Portlet>) PublishingService.getInstance( ).getBlogsPortlets( );
170 
171         ReferenceList referencelist = new ReferenceList( );
172         HashMap<String, Object> model = new HashMap<>( );
173 
174         for ( Portlet portlet : portletList )
175         {
176             referencelist.addItem( portlet.getId( ), portlet.getName( ) );
177         }
178 
179         model.put( MARK_PORTLET_LIST, referencelist );
180 
181         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_TASK_CREATE_CONFIG, locale, model );
182 
183         return template.getHtml( );
184     }
185 
186     /**
187      * {@inheritDoc}
188      */
189     @Override
190     public String getDisplayModifyConfigForm( HttpServletRequest request, Locale locale )
191     {
192         Plugin plugin = PluginService.getPlugin( BlogPlugin.PLUGIN_NAME );
193         List<Portlet> portletList = (List<Portlet>) PublishingService.getInstance( ).getBlogsPortlets( );
194         BlogResourceRssConfig config = BlogResourceRssConfigHome.findByPrimaryKey( this.getId( ), plugin );
195 
196         ReferenceList referencelist = new ReferenceList( );
197         HashMap<String, Object> model = new HashMap<>( );
198 
199         for ( Portlet portlet : portletList )
200         {
201             referencelist.addItem( portlet.getId( ), portlet.getName( ) );
202         }
203 
204         model.put( MARK_ID_PORTLET, config.getIdPortlet( ) );
205         model.put( MARK_PORTLET_LIST, referencelist );
206 
207         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_TASK_MODIFY_CONFIG, locale, model );
208 
209         return template.getHtml( );
210     }
211 
212     /**
213      * {@inheritDoc}
214      */
215     @Override
216     public IFeedResource getFeed( )
217     {
218         String strRssFileLanguage = AppPropertiesService.getProperty( PROPERTY_SITE_LANGUAGE );
219         Locale locale = new Locale( strRssFileLanguage );
220         Plugin plugin = PluginService.getPlugin( BlogPlugin.PLUGIN_NAME );
221         String strTypeImage = AppPropertiesService.getProperty( PROPERTY_TYPE_IMAGE );
222 
223         BlogResourceRssConfig config = BlogResourceRssConfigHome.findByPrimaryKey( this.getId( ), plugin );
224 
225         String strSiteUrl = AppPropertiesService.getProperty( PROPERTY_WEBAPP_PROD_URL );
226 
227         IFeedResource resource = new FeedResource( );
228         resource.setTitle( I18nService.getLocalizedString( PROPERTY_TITLE_WIRE, locale ) );
229         resource.setLink( strSiteUrl );
230         resource.setLanguage( strRssFileLanguage );
231         resource.setDescription( I18nService.getLocalizedString( PROPERTY_DESCRIPTION_WIRE, locale ) );
232 
233         IFeedResourceImage image = new FeedResourceImage( );
234         image.setTitle( resource.getTitle( ) );
235         image.setLink( resource.getLink( ) );
236         image.setUrl( strSiteUrl + TEMPLATE_IMAGE_RSS );
237 
238         resource.setImage( image );
239 
240         List<BlogPublication> listDocPub = BlogPublicationHome.getDocPublicationByPortlet( config.getIdPortlet( ) );
241 
242         List<IFeedResourceItem> listItems = new ArrayList<>( );
243         for ( BlogPublication dcPub : listDocPub )
244         {
245             Blog blog = BlogService.getInstance( ).loadBlog( dcPub.getIdBlog( ) );
246 
247             IFeedResourceItem item = new FeedResourceItem( );
248 
249             Map<String, Object> model = new HashMap<>( );
250             model.put( MARK_RSS_ITEM_TITLE, blog.getName( ) );
251             model.put( MARK_RSS_ITEM_DESCRIPTION, blog.getDescription( ) );
252             model.put( MARK_RSS_SITE_URL, strSiteUrl );
253             model.put( MARK_RSS_FILE_LANGUAGE, strRssFileLanguage );
254 
255             HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_RSS_BLOG_ITEM_TITLE, locale, model );
256             String strTitle = template.getHtml( );
257 
258             item.setTitle( strTitle );
259             String url = strSiteUrl + JSP_PAGE_BLOG + "&id=" + blog.getId( );
260             if ( StringUtils.isNotEmpty( blog.getUrl( ) ) )
261             {
262                 url = blog.getUrl( );
263             }
264             item.setLink( url );
265             item.setDescription( blog.getDescription( ) );
266             item.setDate( blog.getCreationDate( ) );
267 
268             for ( DocContent dc : blog.getDocContent( ) )
269             {
270                 if ( dc.getContentType( ).getIdContentType( ) == Integer.parseInt( strTypeImage ) )
271                 {
272                     item.setGUID( strSiteUrl + DOCUMENT_RESOURCE_SERVLET_URL + "?id_file=" + dc.getId( ) );
273                     break;
274                 }
275             }
276             listItems.add( item );
277         }
278         resource.setItems( listItems );
279         return resource;
280     }
281 
282     /**
283      * {@inheritDoc}
284      */
285     @Override
286     public void deleteResourceRssConfig( int idResourceRss )
287     {
288         Plugin pluginForm = PluginService.getPlugin( BlogPlugin.PLUGIN_NAME );
289         BlogResourceRssConfigHome.remove( idResourceRss, pluginForm );
290     }
291 
292     /**
293      * {@inheritDoc}
294      */
295     @Override
296     public Map<String, String> getParameterToApply( HttpServletRequest request )
297     {
298         Map<String, String> map = new HashMap<>( );
299 
300         map.put( PARAMETER_ID_BLOG, request.getParameter( PARAMETER_ID_BLOG ) );
301 
302         return map;
303     }
304 
305     /**
306      * {@inheritDoc}
307      */
308     @Override
309     public boolean checkResource( )
310     {
311         Plugin pluginForm = PluginService.getPlugin( BlogPlugin.PLUGIN_NAME );
312         BlogResourceRssConfig config = BlogResourceRssConfigHome.findByPrimaryKey( this.getId( ), pluginForm );
313 
314         return ( config != null );
315     }
316 }