View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de 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.rss.web.portlet;
35  
36  import fr.paris.lutece.plugins.rss.business.RssFeedHome;
37  import fr.paris.lutece.plugins.rss.business.portlet.RssPortlet;
38  import fr.paris.lutece.plugins.rss.business.portlet.RssPortletHome;
39  import fr.paris.lutece.portal.business.portlet.PortletHome;
40  import fr.paris.lutece.portal.service.message.AdminMessage;
41  import fr.paris.lutece.portal.service.message.AdminMessageService;
42  import fr.paris.lutece.portal.service.template.AppTemplateService;
43  import fr.paris.lutece.portal.service.util.AppLogService;
44  import fr.paris.lutece.portal.web.portlet.PortletJspBean;
45  import fr.paris.lutece.util.ReferenceList;
46  import fr.paris.lutece.util.html.HtmlTemplate;
47  
48  import java.util.HashMap;
49  
50  import javax.servlet.http.HttpServletRequest;
51  
52  
53  /**
54   * This class provides the user interface to manage Rss Portlet features
55   */
56  public class RssPortletJspBean extends PortletJspBean
57  {
58      ///////////////////////////////////////////////////////////////////////////////////
59      // Constants
60  
61      /**
62       * The rights required to use RssPortletJspBean
63       */
64      public static final String RIGHT_MANAGE_ADMIN_SITE = "CORE_ADMIN_SITE";
65  
66      ////////////////////////////////
67      private static final String PARAMETER_PAGE_ID = "page_id";
68      private static final String PARAMETER_PORTLET_ID = "portlet_id";
69      private static final String PARAMETER_PORTLET_TYPE_ID = "portlet_type_id";
70      private static final String PARAMETER_FEED_ID = "feed_id";
71      private static final String COMBO_FEED_LIST = "@combo_feeds@";
72      private static final String MARK_FEED_LIST = "feed_list";
73      private static final String MARK_FEED_ID = "default_feed_id";
74      private static final String MESSAGE_YOU_MUST_CHOOSE_A_FEED = "rss.message.mandatory.feed";
75  
76      //////////////////////////////////////////////////////////////////////////////////
77      //Templates
78      private static final String TEMPLATE_COMBO_FEEDS = "admin/plugins/rss/portlet/combo_feed.html";
79  
80      /**
81       * Returns the properties prefix used for rss portlet and defined in lutece.properties file
82       *
83       * @return the value of the property prefix
84       */
85      public String getPropertiesPrefix(  )
86      {
87          return "portlet.rss";
88      }
89  
90      /**
91       * Returns the Rss Portlet form of creation
92       *
93       * @param request The Http rquest
94       * @return the html code of the rss portlet form
95       */
96      public String getCreate( HttpServletRequest request )
97      {
98          String strPageId = request.getParameter( PARAMETER_PAGE_ID );
99          String strPortletTypeId = request.getParameter( PARAMETER_PORTLET_TYPE_ID );
100         HtmlTemplate template = getCreateTemplate( strPageId, strPortletTypeId );
101         ReferenceList listFeeds = RssFeedHome.getRssFeedsReferenceList(  );
102         String strHtmlCombo = getFeedIndexCombo( listFeeds, "" );
103         template.substitute( COMBO_FEED_LIST, strHtmlCombo );
104 
105         return template.getHtml(  );
106     }
107 
108     /**
109      * Returns the Rss Portlet form for update
110      * @param request The Http request
111      * @return the html code of the rss portlet form
112      */
113     public String getModify( HttpServletRequest request )
114     {
115         String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
116         int nPortletId = Integer.parseInt( strPortletId );
117         RssPortlet/../../../../../fr/paris/lutece/plugins/rss/business/portlet/RssPortlet.html#RssPortlet">RssPortlet portlet = (RssPortlet) PortletHome.findByPrimaryKey( nPortletId );
118         HtmlTemplate template = getModifyTemplate( portlet );
119 
120         // fills the template with specific values
121         ReferenceList listFeeds = RssFeedHome.getRssFeedsReferenceList(  );
122         String strHtmlCombo = getFeedIndexCombo( listFeeds, portlet.getRssFeedId(  ) );
123         template.substitute( COMBO_FEED_LIST, strHtmlCombo );
124 
125         return template.getHtml(  );
126     }
127 
128     /**
129      * Treats the creation form of a new rss portlet
130      *
131      * @param request The Http request
132      * @return The jsp URL which displays the view of the created rss portlet
133      */
134     public String doCreate( HttpServletRequest request )
135     {
136         RssPortletrss/business/portlet/RssPortlet.html#RssPortlet">RssPortlet portlet = new RssPortlet(  );
137 
138         // Get portlet specific attributes
139         String strPageId = request.getParameter( PARAMETER_PAGE_ID );
140         String strFeedId = request.getParameter( PARAMETER_FEED_ID );
141         int nPageId = -1;
142         int nFeedId = -1;
143 
144         try
145         {
146             nPageId = Integer.parseInt( strPageId );
147             nFeedId = Integer.parseInt( strFeedId );
148         }
149         catch ( NumberFormatException ne )
150         {
151             AppLogService.error( ne );
152         }
153 
154         // Get portlet common attributes
155         String strErrorUrl = setPortletCommonData( request, portlet );
156 
157         if ( ( strErrorUrl == null ) && ( nFeedId == -1 ) )
158         {
159             strErrorUrl = AdminMessageService.getMessageUrl( request, MESSAGE_YOU_MUST_CHOOSE_A_FEED,
160                     AdminMessage.TYPE_STOP );
161         }
162 
163         if ( strErrorUrl != null )
164         {
165             return strErrorUrl;
166         }
167 
168         portlet.setPageId( nPageId );
169         portlet.setRssFeedId( strFeedId );
170 
171         // Creates the portlet
172         RssPortletHome.getInstance(  ).create( portlet );
173 
174         // Displays the page with the new Portlet
175         return getPageUrl( nPageId );
176     }
177 
178     /**
179      * Treats the update form of the rss portlet whose identifier is in the http request
180      *
181      * @param request The Http request
182      * @return The jsp URL which displays the view of the updated portlet
183      */
184     public String doModify( HttpServletRequest request )
185     {
186         // Retrieve portlet specific attributes
187         String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
188         String strFeedId = request.getParameter( PARAMETER_FEED_ID );
189         int nPortletId = -1;
190         int nFeedId = -1;
191 
192         try
193         {
194             nPortletId = Integer.parseInt( strPortletId );
195             nFeedId = Integer.parseInt( strFeedId );
196         }
197         catch ( NumberFormatException ne )
198         {
199             AppLogService.error( ne );
200         }
201 
202         RssPortlet/../../../../../fr/paris/lutece/plugins/rss/business/portlet/RssPortlet.html#RssPortlet">RssPortlet portlet = (RssPortlet) PortletHome.findByPrimaryKey( nPortletId );
203 
204         // Retrieve portlet common attributes
205         String strErrorUrl = setPortletCommonData( request, portlet );
206 
207         if ( ( strErrorUrl == null ) && ( nFeedId == -1 ) )
208         {
209             strErrorUrl = AdminMessageService.getMessageUrl( request, MESSAGE_YOU_MUST_CHOOSE_A_FEED,
210                     AdminMessage.TYPE_STOP );
211         }
212 
213         if ( strErrorUrl != null )
214         {
215             return strErrorUrl;
216         }
217 
218         portlet.setRssFeedId( strFeedId );
219 
220         // Updates the portlet
221         portlet.update(  );
222 
223         // Displays the page with the updated portlet
224         return getPageUrl( portlet.getPageId(  ) );
225     }
226 
227     /**
228      * Return the feed listing depending on rights
229      * @param listFeeds list of available rss feeds
230      * @param strDefaultFeedId The id of the feed used in the context
231      * @return The html code of the combo
232      */
233     String getFeedIndexCombo( ReferenceList listFeeds, String strDefaultFeedId )
234     {
235         HashMap<String, Object> model = new HashMap<String, Object>(  );
236         model.put( MARK_FEED_LIST, listFeeds );
237         model.put( MARK_FEED_ID, strDefaultFeedId );
238 
239         HtmlTemplate templateCombo = AppTemplateService.getTemplate( TEMPLATE_COMBO_FEEDS, getLocale(  ), model );
240 
241         return templateCombo.getHtml(  );
242     }
243 }