View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.myapps.web.portlet;
35  
36  import fr.paris.lutece.plugins.myapps.business.portlet.MyAppsPortlet;
37  import fr.paris.lutece.plugins.myapps.service.MyAppsManager;
38  import fr.paris.lutece.plugins.myapps.service.MyAppsProvider;
39  import fr.paris.lutece.plugins.myapps.service.portlet.MyAppsPortletService;
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.security.LuteceUser;
43  import fr.paris.lutece.portal.service.security.SecurityService;
44  import fr.paris.lutece.portal.service.security.UserNotSignedException;
45  import fr.paris.lutece.portal.service.util.AppPathService;
46  import fr.paris.lutece.portal.web.constants.Parameters;
47  import fr.paris.lutece.portal.web.portlet.PortletJspBean;
48  import fr.paris.lutece.util.html.HtmlTemplate;
49  
50  import org.apache.commons.lang.StringUtils;
51  
52  import javax.servlet.http.HttpServletRequest;
53  
54  
55  /**
56   *
57   * MyAppsPortletJspBean
58   *
59   */
60  public class MyAppsPortletJspBean extends PortletJspBean
61  {
62      private static final String PARAMETER_PLUGIN_NAME = "plugin_name";
63      private static final String PARAMETER_MYAPP_ID = "myapp_id";
64      private static final String MESSAGE_NOT_NUMERIC = "myapps.message.notNumeric";
65      private static final String MESSAGE_ERROR = "message.error";
66  
67      /**
68       * Returns portlet user application's creation form
69       *
70       * @param request request
71       * @return Html form
72       */
73      public String getCreate( HttpServletRequest request )
74      {
75          String strPageId = request.getParameter( Parameters.PAGE_ID );
76          String strPortletTypeId = request.getParameter( Parameters.PORTLET_TYPE_ID );
77          HtmlTemplate template = getCreateTemplate( strPageId, strPortletTypeId );
78  
79          return template.getHtml(  );
80      }
81  
82      /**
83       * Do create a {@link MyAppsPortlet}
84       *
85       * @param request {@link HttpServletRequest}
86       * @return The Jsp management URL of the process result
87       */
88      public String doCreate( HttpServletRequest request )
89      {
90          String strUrl = StringUtils.EMPTY;
91          String strPageId = request.getParameter( Parameters.PAGE_ID );
92  
93          if ( StringUtils.isNotBlank( strPageId ) && StringUtils.isNumeric( strPageId ) )
94          {
95              MyAppsPortlet portlet = new MyAppsPortlet(  );
96              String strErrorUrl = setPortletCommonData( request, portlet );
97  
98              if ( StringUtils.isBlank( strErrorUrl ) )
99              {
100                 int nIdPage = Integer.parseInt( strPageId );
101                 portlet.setPageId( nIdPage );
102                 MyAppsPortletService.getInstance(  ).create( portlet );
103 
104                 //Displays the page with the new Portlet
105                 strUrl = getPageUrl( nIdPage );
106             }
107             else
108             {
109                 strUrl = strErrorUrl;
110             }
111         }
112         else
113         {
114             strUrl = AdminMessageService.getMessageUrl( request, MESSAGE_NOT_NUMERIC, AdminMessage.TYPE_ERROR );
115         }
116 
117         return strUrl;
118     }
119 
120     /**
121      * Returns portlet user application's modification form
122      *
123      * @param request request
124      * @return Html form
125      */
126     public String getModify( HttpServletRequest request )
127     {
128         String strHtml = StringUtils.EMPTY;
129         String strPortletId = request.getParameter( Parameters.PORTLET_ID );
130 
131         if ( StringUtils.isNotBlank( strPortletId ) && StringUtils.isNumeric( strPortletId ) )
132         {
133             int nPortletId = Integer.parseInt( strPortletId );
134             MyAppsPortlet portlet = MyAppsPortletService.getInstance(  ).findByPrimaryKey( nPortletId );
135 
136             if ( portlet != null )
137             {
138                 HtmlTemplate template = getModifyTemplate( portlet );
139                 strHtml = template.getHtml(  );
140             }
141             else
142             {
143                 strHtml = AdminMessageService.getMessageUrl( request, MESSAGE_ERROR, AdminMessage.TYPE_ERROR );
144             }
145         }
146         else
147         {
148             strHtml = AdminMessageService.getMessageUrl( request, MESSAGE_NOT_NUMERIC, AdminMessage.TYPE_ERROR );
149         }
150 
151         return strHtml;
152     }
153 
154     /**
155      * Process portlet's modification
156      *
157      * @param request request
158      * @return The Jsp management URL of the process result
159      */
160     public String doModify( HttpServletRequest request )
161     {
162         String strUrl = StringUtils.EMPTY;
163         String strPortletId = request.getParameter( Parameters.PAGE_ID );
164 
165         if ( StringUtils.isNotBlank( strPortletId ) && StringUtils.isNumeric( strPortletId ) )
166         {
167             int nPortletId = Integer.parseInt( strPortletId );
168             MyAppsPortlet portlet = MyAppsPortletService.getInstance(  ).findByPrimaryKey( nPortletId );
169 
170             if ( portlet != null )
171             {
172                 String strErrorUrl = setPortletCommonData( request, portlet );
173 
174                 if ( StringUtils.isBlank( strErrorUrl ) )
175                 {
176                     int nIdPage = Integer.parseInt( strPortletId );
177                     portlet.setPageId( nIdPage );
178                     MyAppsPortletService.getInstance(  ).update( portlet );
179 
180                     //Displays the page with the new Portlet
181                     strUrl = getPageUrl( portlet.getPageId(  ) );
182                 }
183                 else
184                 {
185                     strUrl = strErrorUrl;
186                 }
187             }
188             else
189             {
190                 strUrl = AdminMessageService.getMessageUrl( request, MESSAGE_ERROR, AdminMessage.TYPE_ERROR );
191             }
192         }
193         else
194         {
195             strUrl = AdminMessageService.getMessageUrl( request, MESSAGE_NOT_NUMERIC, AdminMessage.TYPE_ERROR );
196         }
197 
198         return strUrl;
199     }
200 
201     /**
202      * Returns application url with parameters
203      *
204      * @param request request
205      * @return strLink application url
206      * @throws UserNotSignedException access denied if the user is not connected
207      */
208     public String doOpenMyApp( HttpServletRequest request )
209         throws UserNotSignedException
210     {
211         String strUrl = AppPathService.getBaseUrl( request );
212         LuteceUser user = SecurityService.getInstance(  ).getRemoteUser( request );
213 
214         if ( user != null )
215         {
216             String strPluginName = request.getParameter( PARAMETER_PLUGIN_NAME );
217             String strMyAppId = request.getParameter( PARAMETER_MYAPP_ID );
218 
219             if ( StringUtils.isNotBlank( strPluginName ) && StringUtils.isNotBlank( strMyAppId ) &&
220                     StringUtils.isNumeric( strMyAppId ) )
221             {
222                 int nMyAppId = Integer.parseInt( strMyAppId );
223                 MyAppsProvider provider = MyAppsManager.getInstance(  ).getProvider( strPluginName );
224 
225                 if ( provider != null )
226                 {
227                     strUrl = provider.getUrlOpenMyApps( nMyAppId, user );
228                 }
229             }
230         }
231         else
232         {
233             throw new UserNotSignedException(  );
234         }
235 
236         return strUrl;
237     }
238 }