View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.lang3.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_RSS_BLOG_ITEM_TITLE = "admin/plugins/blog/rss/rss_blogs_item_title.html";
78      private static final String TEMPLATE_TASK_CREATE_CONFIG = "admin/plugins/blog/rss/rss_create_config.html";
79      private static final String TEMPLATE_TASK_MODIFY_CONFIG = "admin/plugins/blog/rss/rss_modify_config.html";
80      private static final String TEMPLATE_IMAGE_RSS = "/images/local/skin/plugins/rss/rss-image.png";
81  
82      // JSPs
83      private static final String JSP_PAGE_BLOG = "/jsp/site/Portal.jsp?page=blog";
84  
85      // Servlets
86      private static final String DOCUMENT_RESOURCE_SERVLET_URL = "/servlet/plugins/blogs/file";
87  
88      // Markers
89      private static final String MARK_RSS_ITEM_TITLE = "item_title";
90      private static final String MARK_RSS_ITEM_DESCRIPTION = "item_description";
91  
92      private static final String MARK_RSS_SITE_URL = "site_url";
93      private static final String MARK_RSS_FILE_LANGUAGE = "file_language";
94      private static final String MARK_PORTLET_LIST = "portlet_list";
95      private static final String MARK_ID_PORTLET = "id_portlet";
96  
97      // Parameters
98      private static final String PARAMETER_ID_BLOG = "id_blog";
99      private static final String PARAMETER_ID_PORTLET = "id_portlet";
100 
101     // Messages
102     private static final String PROPERTY_SITE_LANGUAGE = "rss.language";
103     private static final String PROPERTY_TYPE_IMAGE = "blog.rss.content.type.img";
104     private static final String PROPERTY_WEBAPP_PROD_URL = "lutece.prod.url";
105     private static final String PROPERTY_DESCRIPTION_WIRE = "blog.resource_rss.description_wire";
106     private static final String PROPERTY_TITLE_WIRE = "blog.resource_rss.title_wire";
107 
108     /**
109      * {@inheritDoc}
110      */
111     @Override
112     public boolean contentResourceRss( )
113     {
114 
115         return CollectionUtils.isNotEmpty( BlogPublicationHome.getAllDocPublication( ) );
116     }
117 
118     /**
119      * {@inheritDoc}
120      */
121     @Override
122     public void doSaveConfig( HttpServletRequest request, Locale locale )
123     {
124         Plugin plugin = PluginService.getPlugin( BlogPlugin.PLUGIN_NAME );
125         String stridPortlet = request.getParameter( PARAMETER_ID_PORTLET );
126 
127         BlogResourceRssConfigess/rss/BlogResourceRssConfig.html#BlogResourceRssConfig">BlogResourceRssConfig config = new BlogResourceRssConfig( );
128         config.setIdRss( this.getId( ) );
129         config.setIdPortlet( Integer.parseInt( stridPortlet ) );
130 
131         BlogResourceRssConfigHome.create( config, plugin );
132     }
133 
134     /**
135      * {@inheritDoc}
136      */
137     @Override
138     public void doUpdateConfig( HttpServletRequest request, Locale locale )
139     {
140         Plugin pluginForm = PluginService.getPlugin( BlogPlugin.PLUGIN_NAME );
141         String stridPortlet = request.getParameter( PARAMETER_ID_PORTLET );
142 
143         BlogResourceRssConfigess/rss/BlogResourceRssConfig.html#BlogResourceRssConfig">BlogResourceRssConfig config = new BlogResourceRssConfig( );
144         config.setIdRss( this.getId( ) );
145         config.setIdPortlet( Integer.parseInt( stridPortlet ) );
146 
147         BlogResourceRssConfigHome.update( config, pluginForm );
148     }
149 
150     /**
151      * {@inheritDoc}
152      */
153     @Override
154     public String doValidateConfigForm( HttpServletRequest request, Locale locale )
155     {
156         this.setDescription( I18nService.getLocalizedString( PROPERTY_DESCRIPTION_WIRE, locale ) );
157 
158         return null;
159     }
160 
161     /**
162      * {@inheritDoc}
163      */
164     @Override
165     public String getDisplayCreateConfigForm( HttpServletRequest request, Locale locale )
166     {
167 
168         List<Portlet> portletList = (List<Portlet>) PublishingService.getInstance( ).getBlogsPortlets( );
169 
170         ReferenceList referencelist = new ReferenceList( );
171         HashMap<String, Object> model = new HashMap<>( );
172 
173         for ( Portlet portlet : portletList )
174         {
175             referencelist.addItem( portlet.getId( ), portlet.getName( ) );
176         }
177 
178         model.put( MARK_PORTLET_LIST, referencelist );
179 
180         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_TASK_CREATE_CONFIG, locale, model );
181 
182         return template.getHtml( );
183     }
184 
185     /**
186      * {@inheritDoc}
187      */
188     @Override
189     public String getDisplayModifyConfigForm( HttpServletRequest request, Locale locale )
190     {
191         Plugin plugin = PluginService.getPlugin( BlogPlugin.PLUGIN_NAME );
192         List<Portlet> portletList = (List<Portlet>) PublishingService.getInstance( ).getBlogsPortlets( );
193         BlogResourceRssConfig config = BlogResourceRssConfigHome.findByPrimaryKey( this.getId( ), plugin );
194 
195         ReferenceList referencelist = new ReferenceList( );
196         HashMap<String, Object> model = new HashMap<>( );
197 
198         for ( Portlet portlet : portletList )
199         {
200             referencelist.addItem( portlet.getId( ), portlet.getName( ) );
201         }
202 
203         model.put( MARK_ID_PORTLET, config.getIdPortlet( ) );
204         model.put( MARK_PORTLET_LIST, referencelist );
205 
206         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_TASK_MODIFY_CONFIG, locale, model );
207 
208         return template.getHtml( );
209     }
210 
211     /**
212      * {@inheritDoc}
213      */
214     @Override
215     public IFeedResource getFeed( )
216     {
217         String strRssFileLanguage = AppPropertiesService.getProperty( PROPERTY_SITE_LANGUAGE );
218         Locale locale = new Locale( strRssFileLanguage );
219         Plugin plugin = PluginService.getPlugin( BlogPlugin.PLUGIN_NAME );
220         String strTypeImage = AppPropertiesService.getProperty( PROPERTY_TYPE_IMAGE );
221 
222         BlogResourceRssConfig config = BlogResourceRssConfigHome.findByPrimaryKey( this.getId( ), plugin );
223 
224         String strSiteUrl = AppPropertiesService.getProperty( PROPERTY_WEBAPP_PROD_URL );
225 
226         IFeedResource resource = new FeedResource( );
227         resource.setTitle( I18nService.getLocalizedString( PROPERTY_TITLE_WIRE, locale ) );
228         resource.setLink( strSiteUrl );
229         resource.setLanguage( strRssFileLanguage );
230         resource.setDescription( I18nService.getLocalizedString( PROPERTY_DESCRIPTION_WIRE, locale ) );
231 
232         IFeedResourceImage image = new FeedResourceImage( );
233         image.setTitle( resource.getTitle( ) );
234         image.setLink( resource.getLink( ) );
235         image.setUrl( strSiteUrl + TEMPLATE_IMAGE_RSS );
236 
237         resource.setImage( image );
238 
239         List<BlogPublication> listDocPub = BlogPublicationHome.getDocPublicationByPortlet( config.getIdPortlet( ) );
240 
241         List<IFeedResourceItem> listItems = new ArrayList<>( );
242         for ( BlogPublication dcPub : listDocPub )
243         {
244             Blog blog = BlogService.getInstance( ).loadBlog( dcPub.getIdBlog( ) );
245 
246             IFeedResourceItem item = new FeedResourceItem( );
247 
248             Map<String, Object> model = new HashMap<>( );
249             model.put( MARK_RSS_ITEM_TITLE, blog.getName( ) );
250             model.put( MARK_RSS_ITEM_DESCRIPTION, blog.getDescription( ) );
251             model.put( MARK_RSS_SITE_URL, strSiteUrl );
252             model.put( MARK_RSS_FILE_LANGUAGE, strRssFileLanguage );
253 
254             HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_RSS_BLOG_ITEM_TITLE, locale, model );
255             String strTitle = template.getHtml( );
256 
257             item.setTitle( strTitle );
258             String url = strSiteUrl + JSP_PAGE_BLOG + "&id=" + blog.getId( );
259             if ( StringUtils.isNotEmpty( blog.getUrl( ) ) )
260             {
261                 url = blog.getUrl( );
262             }
263             item.setLink( url );
264             item.setDescription( blog.getDescription( ) );
265             item.setDate( blog.getCreationDate( ) );
266 
267             for ( DocContent dc : blog.getDocContent( ) )
268             {
269                 if ( dc.getContentType( ).getIdContentType( ) == Integer.parseInt( strTypeImage ) )
270                 {
271                     item.setGUID( strSiteUrl + DOCUMENT_RESOURCE_SERVLET_URL + "?id_file=" + dc.getId( ) );
272                     break;
273                 }
274             }
275             listItems.add( item );
276         }
277         resource.setItems( listItems );
278         return resource;
279     }
280 
281     /**
282      * {@inheritDoc}
283      */
284     @Override
285     public void deleteResourceRssConfig( int idResourceRss )
286     {
287         Plugin pluginForm = PluginService.getPlugin( BlogPlugin.PLUGIN_NAME );
288         BlogResourceRssConfigHome.remove( idResourceRss, pluginForm );
289     }
290 
291     /**
292      * {@inheritDoc}
293      */
294     @Override
295     public Map<String, String> getParameterToApply( HttpServletRequest request )
296     {
297         Map<String, String> map = new HashMap<>( );
298 
299         map.put( PARAMETER_ID_BLOG, request.getParameter( PARAMETER_ID_BLOG ) );
300 
301         return map;
302     }
303 
304     /**
305      * {@inheritDoc}
306      */
307     @Override
308     public boolean checkResource( )
309     {
310         Plugin pluginForm = PluginService.getPlugin( BlogPlugin.PLUGIN_NAME );
311         BlogResourceRssConfig config = BlogResourceRssConfigHome.findByPrimaryKey( this.getId( ), pluginForm );
312 
313         return ( config != null );
314     }
315 }