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.tagcloud.web.portlet;
35  
36  import fr.paris.lutece.plugins.tagcloud.business.TagHome;
37  import fr.paris.lutece.plugins.tagcloud.business.portlet.TagCloudPortlet;
38  import fr.paris.lutece.plugins.tagcloud.business.portlet.TagCloudPortletHome;
39  import fr.paris.lutece.portal.business.portlet.PortletHome;
40  import fr.paris.lutece.portal.service.plugin.Plugin;
41  import fr.paris.lutece.portal.service.plugin.PluginService;
42  import fr.paris.lutece.portal.service.template.AppTemplateService;
43  import fr.paris.lutece.portal.web.portlet.PortletJspBean;
44  import fr.paris.lutece.util.ReferenceList;
45  import fr.paris.lutece.util.html.HtmlTemplate;
46  
47  import java.util.HashMap;
48  
49  import javax.servlet.http.HttpServletRequest;
50  
51  
52  /**
53   * This class provides the user interface to manage tagcloud Portlet features
54   */
55  public class TagCloudPortletJspBean extends PortletJspBean
56  {
57      ///////////////////////////////////////////////////////////////////////////////////
58      // Constants
59  
60      /**
61       * The rights required to use TagCloudPortletJspBean
62       */
63      public static final String RIGHT_MANAGE_ADMIN_SITE = "CORE_ADMIN_SITE";
64  
65      ////////////////////////////////
66      private static final String PARAMETER_PAGE_ID = "page_id";
67      private static final String PARAMETER_PORTLET_ID = "portlet_id";
68      private static final String PARAMETER_PORTLET_TYPE_ID = "portlet_type_id";
69      private static final String PARAMETER_TAGCLOUD_ID = "tagcloud_id";
70      private static final String COMBO_CLOUD_LIST = "@combo_clouds@";
71      private static final String MARK_TAGCLOUD_LIST = "tagcloud_list";
72      private static final String MARK_TAGCLOUD_ID = "default_cloud_id";
73      private static final String PROPERTY_PLUGIN_NAME = "tagcloud";
74  
75      //////////////////////////////////////////////////////////////////////////////////
76      //Templates
77      private static final String TEMPLATE_COMBO_CLOUDS = "admin/plugins/tagcloud/portlet/combo_tagcloud.html";
78  
79      /**
80       * Returns the properties prefix used for tagcloud portlet and defined in lutece.properties file
81       *
82       * @return the value of the property prefix
83       */
84      public String getPropertiesPrefix(  )
85      {
86          return "portlet.tagcloud";
87      }
88  
89      /**
90       * Returns the TagCloud Portlet form of creation
91       *
92       * @param request The Http rquest
93       * @return the html code of the tagcloud portlet form
94       */
95      public String getCreate( HttpServletRequest request )
96      {
97          // Use the id in the request to load the portlet
98          String strPageId = request.getParameter( PARAMETER_PAGE_ID );
99          String strPortletTypeId = request.getParameter( PARAMETER_PORTLET_TYPE_ID );
100         HtmlTemplate template = getCreateTemplate( strPageId, strPortletTypeId );
101 
102         //List of clouds present
103         Plugin plugin = PluginService.getPlugin( PROPERTY_PLUGIN_NAME );
104         ReferenceList listClouds = TagHome.getAllTagClouds( plugin );
105         String strHtmlCombo = getCloudsCombo( listClouds, "" );
106         template.substitute( COMBO_CLOUD_LIST, strHtmlCombo );
107 
108         return template.getHtml(  );
109     }
110 
111     /**
112      * Returns the TagCloud Portlet form for update
113      * @param request The Http request
114      * @return the html code of the tagcloud portlet form
115      */
116     public String getModify( HttpServletRequest request )
117     {
118         String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
119         int nPortletId = Integer.parseInt( strPortletId );
120         TagCloudPortlet./../../../fr/paris/lutece/plugins/tagcloud/business/portlet/TagCloudPortlet.html#TagCloudPortlet">TagCloudPortlet portlet = (TagCloudPortlet) PortletHome.findByPrimaryKey( nPortletId );
121         HtmlTemplate template = getModifyTemplate( portlet );
122 
123         // Get the plugin for the portlet
124         Plugin plugin = PluginService.getPlugin( portlet.getPluginName(  ) );
125 
126         // fills the template with specific values
127         ReferenceList listClouds = TagHome.getAllTagClouds( plugin );
128         String strHtmlCombo = getCloudsCombo( listClouds, "" + portlet.getIdCloud(  ) );
129         template.substitute( COMBO_CLOUD_LIST, strHtmlCombo );
130 
131         return template.getHtml(  );
132     }
133 
134     /**
135      * Treats the creation form of a new tagcloud portlet
136      * @param request The Http request
137      * @return The jsp URL which displays the view of the created tagcloud portlet
138      */
139     public String doCreate( HttpServletRequest request )
140     {
141         TagCloudPortletoud/business/portlet/TagCloudPortlet.html#TagCloudPortlet">TagCloudPortlet portlet = new TagCloudPortlet(  );
142 
143         String strIdPage = request.getParameter( PARAMETER_PAGE_ID );
144         int nIdPage = Integer.parseInt( strIdPage );
145 
146         //gets the identifier of the parent page
147         int nCloudId = Integer.parseInt( request.getParameter( PARAMETER_TAGCLOUD_ID ) );
148 
149         // get portlet common attributes
150         String strErrorUrl = setPortletCommonData( request, portlet );
151 
152         if ( strErrorUrl != null )
153         {
154             return strErrorUrl;
155         }
156 
157         portlet.setPageId( nIdPage );
158 
159         //gets the specific parameters
160         portlet.setIdCloud( nCloudId );
161 
162         //Portlet creation
163         TagCloudPortletHome.getInstance(  ).create( portlet );
164 
165         //Displays the page with the new Portlet
166         return getPageUrl( nIdPage );
167     }
168 
169     /**
170      * Treats the update form of the tagcloud portlet whose identifier is in the http request
171      *
172      * @param request The Http request
173      * @return The jsp URL which displays the view of the updated portlet
174      */
175     public String doModify( HttpServletRequest request )
176     {
177         // fetches portlet attributes
178         String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
179         int nPortletId = Integer.parseInt( strPortletId );
180         TagCloudPortlet./../../../fr/paris/lutece/plugins/tagcloud/business/portlet/TagCloudPortlet.html#TagCloudPortlet">TagCloudPortlet portlet = (TagCloudPortlet) PortletHome.findByPrimaryKey( nPortletId );
181 
182         // retrieve portlet common attributes
183         String strErrorUrl = setPortletCommonData( request, portlet );
184 
185         if ( strErrorUrl != null )
186         {
187             return strErrorUrl;
188         }
189 
190         // fetches portlet specific attributes
191         String strCloudId = request.getParameter( PARAMETER_TAGCLOUD_ID );
192         portlet.setIdCloud( Integer.parseInt( strCloudId ) );
193 
194         // updates the portlet
195         portlet.update(  );
196 
197         int nCloudId = Integer.parseInt( request.getParameter( PARAMETER_TAGCLOUD_ID ) );
198         TagCloudPortletHome.storeCloud( nPortletId, nCloudId );
199 
200         // displays the page with the updated portlet
201         return getPageUrl( portlet.getPageId(  ) );
202     }
203 
204     /**
205      * Constructs the html combo
206      * @param listClouds List of tagClouds present
207      * @param strDefaultCloudId The default tagcloud
208      * @return A String representation of the combo
209      */
210     public String getCloudsCombo( ReferenceList listClouds, String strDefaultCloudId )
211     {
212         HashMap model = new HashMap(  );
213         model.put( MARK_TAGCLOUD_LIST, listClouds );
214         model.put( MARK_TAGCLOUD_ID, strDefaultCloudId );
215 
216         HtmlTemplate templateCombo = AppTemplateService.getTemplate( TEMPLATE_COMBO_CLOUDS, getLocale(  ), model );
217 
218         return templateCombo.getHtml(  );
219     }
220 }