View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.portal.web.portlet;
35  
36  import fr.paris.lutece.portal.business.portlet.AliasPortlet;
37  import fr.paris.lutece.portal.business.portlet.AliasPortletHome;
38  import fr.paris.lutece.portal.business.portlet.Portlet;
39  import fr.paris.lutece.portal.business.portlet.PortletHome;
40  import fr.paris.lutece.portal.service.i18n.I18nService;
41  import fr.paris.lutece.portal.service.message.AdminMessage;
42  import fr.paris.lutece.portal.service.message.AdminMessageService;
43  import fr.paris.lutece.portal.web.constants.Messages;
44  import fr.paris.lutece.util.ReferenceList;
45  import fr.paris.lutece.util.html.HtmlTemplate;
46  
47  import java.util.HashMap;
48  import java.util.Map;
49  
50  import javax.servlet.http.HttpServletRequest;
51  
52  /**
53   * This class provides the user interface to manage Alias Portlet
54   */
55  public class AliasPortletJspBean extends PortletJspBean
56  {
57      // //////////////////////////////////////////////////////////////////////////
58      // Constants
59  
60      // Right
61      public static final String RIGHT_MANAGE_ADMIN_SITE = "CORE_ADMIN_SITE";
62      private static final long serialVersionUID = 1894295808070813451L;
63      private static final String PARAM_PORTLET_NAME = "portlet_name";
64      private static final String PARAM_ORDER = "order";
65      private static final String PARAM_COLUMN = "column";
66      private static final String PARAM_ALIAS_ID = "alias_id";
67      private static final String PARAM_ACCEPT_ALIAS = "accept_alias";
68      private static final String MARK_ALIAS_PORTLETS_LIST = "alias_portlets_list";
69      private static final String MARK_ALIAS_PORTLET = "alias_portlet";
70      private static final String LABEL_ALIAS_PORTLET_NAME = "portal.site.portlet_alias.portlet.name.label";
71  
72      /**
73       * Process portlet's creation
74       *
75       * @param request
76       *            The Http request
77       * @return The management url
78       */
79      @Override
80      public String doCreate( HttpServletRequest request )
81      {
82          AliasPortletrtlet/AliasPortlet.html#AliasPortlet">AliasPortlet aliasPortlet = new AliasPortlet( );
83          String strAliasId = request.getParameter( PARAM_ALIAS_ID );
84  
85          // if no portlet has the accept alias field true
86          if ( strAliasId == null )
87          {
88              return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
89          }
90  
91          // Gets the parameters of the alias portlet posted in the request
92          String strName = request.getParameter( PARAM_PORTLET_NAME );
93  
94          // mandatory field
95          if ( ( strName == null ) || strName.trim( ).equals( "" ) )
96          {
97              return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
98          }
99  
100         String strOrder = request.getParameter( PARAM_ORDER );
101         int nOrder = Integer.parseInt( strOrder );
102         String strColumn = request.getParameter( PARAM_COLUMN );
103         int nColumn = Integer.parseInt( strColumn );
104         String strAcceptAlias = request.getParameter( PARAM_ACCEPT_ALIAS );
105         int nAcceptAlias = Integer.parseInt( strAcceptAlias );
106         aliasPortlet.setName( strName );
107         aliasPortlet.setOrder( nOrder );
108         aliasPortlet.setColumn( nColumn );
109         aliasPortlet.setAcceptAlias( nAcceptAlias );
110 
111         String strPageId = request.getParameter( PARAMETER_PAGE_ID );
112         int nPageId = Integer.parseInt( strPageId );
113         int nAliasId = Integer.parseInt( strAliasId );
114         aliasPortlet.setPageId( nPageId );
115         aliasPortlet.setAliasId( nAliasId );
116 
117         // gets the style of the parent portlet
118         Portlet portlet = PortletHome.findByPrimaryKey( nAliasId );
119         aliasPortlet.setStyleId( portlet.getStyleId( ) );
120 
121         // creates the alias portlet
122         AliasPortletHome.getInstance( ).create( aliasPortlet );
123 
124         // Displays the page with the new portlet
125         return getPageUrl( nPageId );
126     }
127 
128     /**
129      * Process portlet's modification
130      *
131      * @param request
132      *            The http request
133      * @return Management's Url
134      */
135     @Override
136     public String doModify( HttpServletRequest request )
137     {
138         // recovers portlet attributes
139         String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
140         int nPortletId = Integer.parseInt( strPortletId );
141         AliasPortlet./../../../fr/paris/lutece/portal/business/portlet/AliasPortlet.html#AliasPortlet">AliasPortlet portlet = (AliasPortlet) PortletHome.findByPrimaryKey( nPortletId );
142 
143         // Gets the parameters of the alias portlet posted in the request
144         String strName = request.getParameter( PARAM_PORTLET_NAME );
145         String strOrder = request.getParameter( PARAM_ORDER );
146         int nOrder = Integer.parseInt( strOrder );
147         String strColumn = request.getParameter( PARAM_COLUMN );
148         int nColumn = Integer.parseInt( strColumn );
149 
150         // mandatory field
151         if ( ( strName == null ) || strName.trim( ).equals( "" ) )
152         {
153             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
154         }
155 
156         portlet.setName( strName );
157         portlet.setOrder( nOrder );
158         portlet.setColumn( nColumn );
159 
160         String strIdAlias = request.getParameter( PARAM_ALIAS_ID );
161         int nIdAlias = Integer.parseInt( strIdAlias );
162         portlet.setAliasId( nIdAlias );
163 
164         Portlet rub = PortletHome.findByPrimaryKey( nIdAlias );
165         portlet.setStyleId( rub.getStyleId( ) );
166 
167         // updates the portlet
168         portlet.update( );
169 
170         // Displays the page with the portlet updated
171         return getPageUrl( portlet.getPageId( ) );
172     }
173 
174     /**
175      * Returns the Alias portlet creation form
176      *
177      * @param request
178      *            The http request
179      * @return The HTML form
180      */
181     @Override
182     public String getCreate( HttpServletRequest request )
183     {
184         String strIdPage = request.getParameter( PARAMETER_PAGE_ID );
185         String strIdPortletType = request.getParameter( PARAMETER_PORTLET_TYPE_ID );
186         Map<String, Object> model = new HashMap<>( );
187         model.put( MARK_ALIAS_PORTLETS_LIST, buildAliasPortletList( ) );
188 
189         HtmlTemplate template = getCreateTemplate( strIdPage, strIdPortletType, model );
190 
191         return template.getHtml( );
192     }
193 
194     /**
195      * Returns the Alias portlet modification form
196      *
197      * @param request
198      *            The Http request
199      * @return The HTML form
200      */
201     @Override
202     public String getModify( HttpServletRequest request )
203     {
204         String strIdPortlet = request.getParameter( PARAMETER_PORTLET_ID );
205         int idPortlet = Integer.parseInt( strIdPortlet );
206         Portlet portlet = PortletHome.findByPrimaryKey( idPortlet );
207         AliasPortlet../../fr/paris/lutece/portal/business/portlet/AliasPortlet.html#AliasPortlet">AliasPortlet aliasPortlet = (AliasPortlet) portlet;
208         Map<String, Object> model = new HashMap<>( );
209         model.put( MARK_ALIAS_PORTLETS_LIST, buildAliasPortletList( ) );
210         model.put( MARK_ALIAS_PORTLET, aliasPortlet.getAliasId( ) );
211 
212         HtmlTemplate template = getModifyTemplate( portlet, model );
213 
214         return template.getHtml( );
215     }
216 
217     /**
218      * Returns the Reference List of Portlets accepting Alias.
219      *
220      * @return The Reference List
221      */
222     private ReferenceList buildAliasPortletList( )
223     {
224 
225         ReferenceListList.html#ReferenceList">ReferenceList refList = new ReferenceList( );
226 
227         for ( Portlet portlet : AliasPortletHome.getAcceptAliasPortletList( ) )
228         {
229             refList.addItem( portlet.getId( ), I18nService.getLocalizedString( LABEL_ALIAS_PORTLET_NAME, new String [ ] {
230                     String.valueOf( portlet.getPageId( ) ), portlet.getName( )
231             }, getLocale( ) ) );
232         }
233         return refList;
234     }
235 }