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