View Javadoc
1   /*
2    * Copyright (c) 2002-2018, 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.appstore.web;
35  
36  import fr.paris.lutece.plugins.appstore.business.Application;
37  import fr.paris.lutece.plugins.appstore.business.ApplicationHome;
38  import fr.paris.lutece.plugins.appstore.business.CategoryHome;
39  import fr.paris.lutece.plugins.appstore.business.ComponentHome;
40  import fr.paris.lutece.plugins.appstore.business.IconHome;
41  import fr.paris.lutece.portal.service.i18n.I18nService;
42  import fr.paris.lutece.portal.service.message.AdminMessage;
43  import fr.paris.lutece.portal.service.message.AdminMessageService;
44  import fr.paris.lutece.portal.service.template.AppTemplateService;
45  import fr.paris.lutece.portal.service.util.AppPropertiesService;
46  import fr.paris.lutece.portal.web.constants.Messages;
47  import fr.paris.lutece.portal.web.util.LocalizedPaginator;
48  import fr.paris.lutece.util.ReferenceList;
49  import fr.paris.lutece.util.html.HtmlTemplate;
50  import fr.paris.lutece.util.html.Paginator;
51  import fr.paris.lutece.util.url.UrlItem;
52  
53  import java.util.HashMap;
54  import java.util.List;
55  import java.util.Map;
56  
57  import javax.servlet.http.HttpServletRequest;
58  
59  /**
60   * This class provides the user interface to manage Category, Application, Component features ( manage, create, modify, remove )
61   */
62  public class ApplicationJspBean extends AppStoreJspBean
63  {
64      // //////////////////////////////////////////////////////////////////////////
65      // Constants
66  
67      // Parameters
68      private static final String PARAMETER_ID_APPLICATION = "application_id_application";
69      private static final String PARAMETER_APPLICATION_TITLE = "application_title";
70      private static final String PARAMETER_APPLICATION_DESCRIPTION = "application_description";
71      private static final String PARAMETER_APPLICATION_ID_CATEGORY = "application_id_category";
72      private static final String PARAMETER_APPLICATION_APPLICATION_ORDER = "application_application_order";
73      private static final String PARAMETER_APPLICATION_ID_ICON = "application_id_icon";
74      private static final String PARAMETER_APPLICATION_POM_URL = "application_pom_url";
75      private static final String PARAMETER_APPLICATION_WEBAPP_URL = "application_webapp_url";
76      private static final String PARAMETER_APPLICATION_SQL_SCRIPT_URL = "application_sql_script_url";
77      private static final String PARAMETER_APPLICATION_PRESENTATION = "application_presentation";
78      private static final String PARAMETER_APPLICATION_INSTALLATION = "application_installation";
79      private static final String PARAMETER_APPLICATION_ARTIFACT_ID = "application_artifact_id";
80      private static final String PARAMETER_APPLICATION_VERSION = "application_version";
81      private static final String PARAMETER_APPLICATION_PUBLISH_STATUS = "application_publish_status";
82      private static final String PARAMETER_ID_COMPONENT = "id_component";
83  
84      // templates
85      private static final String TEMPLATE_MANAGE_APPLICATIONS = "/admin/plugins/appstore/manage_application.html";
86      private static final String TEMPLATE_CREATE_APPLICATION = "/admin/plugins/appstore/create_application.html";
87      private static final String TEMPLATE_MODIFY_APPLICATION = "/admin/plugins/appstore/modify_application.html";
88  
89      // Properties for page titles
90      private static final String PROPERTY_PAGE_TITLE_MANAGE_APPLICATIONS = "appstore.manage_applications.pageTitle";
91      private static final String PROPERTY_PAGE_TITLE_MODIFY_APPLICATION = "appstore.modify_application.pageTitle";
92      private static final String PROPERTY_PAGE_TITLE_CREATE_APPLICATION = "appstore.create_application.pageTitle";
93  
94      // Markers
95      private static final String MARK_APPLICATION_LIST = "application_list";
96      private static final String MARK_APPLICATION = "application";
97      private static final String MARK_CATEGORIES_LIST = "categories_list";
98      private static final String MARK_COMPONENTS_LIST = "components_list";
99      private static final String MARK_APP_COMPONENTS_LIST = "app_components_list";
100     private static final String MARK_PUBLISH_STATUS_LIST = "publish_status_list";
101     private static final String MARK_ICONS_LIST = "icons_list";
102 
103     // Jsp Definition
104     private static final String JSP_DO_REMOVE_APPLICATION = "jsp/admin/plugins/appstore/DoRemoveApplication.jsp";
105     private static final String JSP_MANAGE_APPLICATIONS = "jsp/admin/plugins/appstore/ManageApplications.jsp";
106     private static final String JSP_REDIRECT_TO_MANAGE_APPLICATIONS = "ManageApplications.jsp";
107 
108     // Properties
109     private static final String PROPERTY_DEFAULT_LIST_APPLICATION_PER_PAGE = "appstore.listApplications.itemsPerPage";
110 
111     // Messages
112     private static final String MESSAGE_CONFIRM_REMOVE_APPLICATION = "appstore.message.confirmRemoveApplication";
113     private static final String PROPERTY_NOT_PUBLISHED = "appstore.label.notPublished";
114     private static final String PROPERTY_PUBLISHED = "appstore.label.published";
115 
116     /**
117      * Returns the list of application
118      *
119      * @param request
120      *            The Http request
121      * @return the applications list
122      */
123     public String getManageApplications( HttpServletRequest request )
124     {
125         setPageTitleProperty( PROPERTY_PAGE_TITLE_MANAGE_APPLICATIONS );
126 
127         _strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
128         _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_LIST_APPLICATION_PER_PAGE, 50 );
129         _nItemsPerPage = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, _nDefaultItemsPerPage );
130 
131         UrlItem url = new UrlItem( JSP_MANAGE_APPLICATIONS );
132         String strUrl = url.getUrl( );
133         List<Application> listApplications = (List<Application>) ApplicationHome.getApplicationsList( );
134 
135         // PAGINATOR
136         LocalizedPaginator paginator = new LocalizedPaginator( listApplications, _nItemsPerPage, strUrl, PARAMETER_PAGE_INDEX, _strCurrentPageIndex,
137                 getLocale( ) );
138 
139         Map<String, Object> model = new HashMap<String, Object>( );
140 
141         model.put( MARK_NB_ITEMS_PER_PAGE, "" + _nItemsPerPage );
142         model.put( MARK_PAGINATOR, paginator );
143 
144         model.put( MARK_APPLICATION_LIST, paginator.getPageItems( ) );
145 
146         HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_MANAGE_APPLICATIONS, getLocale( ), model );
147 
148         return getAdminPage( templateList.getHtml( ) );
149     }
150 
151     /**
152      * Returns the form to create a application
153      *
154      * @param request
155      *            The Http request
156      * @return the html code of the application form
157      */
158     public String getCreateApplication( HttpServletRequest request )
159     {
160         setPageTitleProperty( PROPERTY_PAGE_TITLE_CREATE_APPLICATION );
161 
162         Map<String, Object> model = new HashMap<String, Object>( );
163         model.put( MARK_CATEGORIES_LIST, CategoryHome.getCategories( ) );
164         model.put( MARK_PUBLISH_STATUS_LIST, getPublishStatusList( request ) );
165         model.put( MARK_ICONS_LIST, IconHome.getListIcons( getPlugin( ) ) );
166 
167         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_APPLICATION, getLocale( ), model );
168 
169         return getAdminPage( template.getHtml( ) );
170     }
171 
172     /**
173      * Process the data capture form of a new application
174      *
175      * @param request
176      *            The Http Request
177      * @return The Jsp URL of the process result
178      */
179     public String doCreateApplication( HttpServletRequest request )
180     {
181         Application application = new Application( );
182 
183         if ( request.getParameter( PARAMETER_APPLICATION_TITLE ).equals( "" ) )
184         {
185             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
186         }
187 
188         if ( request.getParameter( PARAMETER_APPLICATION_DESCRIPTION ).equals( "" ) )
189         {
190             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
191         }
192 
193         if ( request.getParameter( PARAMETER_APPLICATION_ID_CATEGORY ).equals( "" ) )
194         {
195             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
196         }
197 
198         if ( request.getParameter( PARAMETER_APPLICATION_APPLICATION_ORDER ).equals( "" ) )
199         {
200             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
201         }
202 
203         if ( request.getParameter( PARAMETER_APPLICATION_ID_ICON ).equals( "" ) )
204         {
205             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
206         }
207 
208         if ( request.getParameter( PARAMETER_APPLICATION_WEBAPP_URL ).equals( "" ) )
209         {
210             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
211         }
212 
213         if ( request.getParameter( PARAMETER_APPLICATION_SQL_SCRIPT_URL ).equals( "" ) )
214         {
215             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
216         }
217 
218         application.setTitle( request.getParameter( PARAMETER_APPLICATION_TITLE ) );
219         application.setDescription( request.getParameter( PARAMETER_APPLICATION_DESCRIPTION ) );
220 
221         int nIdCategory = Integer.parseInt( request.getParameter( PARAMETER_APPLICATION_ID_CATEGORY ) );
222         application.setIdCategory( nIdCategory );
223 
224         int nApplicationOrder = Integer.parseInt( request.getParameter( PARAMETER_APPLICATION_APPLICATION_ORDER ) );
225         application.setOrder( nApplicationOrder );
226 
227         int nIdIcon = Integer.parseInt( request.getParameter( PARAMETER_APPLICATION_ID_ICON ) );
228         application.setIdIcon( nIdIcon );
229 
230         int nPublishStatus = Integer.parseInt( request.getParameter( PARAMETER_APPLICATION_PUBLISH_STATUS ) );
231 
232         application.setWebappUrl( request.getParameter( PARAMETER_APPLICATION_WEBAPP_URL ) );
233         application.setSqlScriptUrl( request.getParameter( PARAMETER_APPLICATION_SQL_SCRIPT_URL ) );
234         application.setPresentation( request.getParameter( PARAMETER_APPLICATION_PRESENTATION ) );
235         application.setInstallation( request.getParameter( PARAMETER_APPLICATION_INSTALLATION ) );
236         application.setArtifactId( request.getParameter( PARAMETER_APPLICATION_ARTIFACT_ID ) );
237         application.setVersion( request.getParameter( PARAMETER_APPLICATION_VERSION ) );
238         application.setPublishStatus( nPublishStatus );
239 
240         ApplicationHome.create( application );
241 
242         return JSP_REDIRECT_TO_MANAGE_APPLICATIONS;
243     }
244 
245     /**
246      * Manages the removal form of a application whose identifier is in the http request
247      *
248      * @param request
249      *            The Http request
250      * @return the html code to confirm
251      */
252     public String getConfirmRemoveApplication( HttpServletRequest request )
253     {
254         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_APPLICATION ) );
255         UrlItem url = new UrlItem( JSP_DO_REMOVE_APPLICATION );
256         url.addParameter( PARAMETER_ID_APPLICATION, nId );
257 
258         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_APPLICATION, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION );
259     }
260 
261     /**
262      * Handles the removal form of a application
263      *
264      * @param request
265      *            The Http request
266      * @return the jsp URL to display the form to manage applications
267      */
268     public String doRemoveApplication( HttpServletRequest request )
269     {
270         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_APPLICATION ) );
271         ApplicationHome.remove( nId );
272 
273         return JSP_REDIRECT_TO_MANAGE_APPLICATIONS;
274     }
275 
276     /**
277      * Returns the form to update info about a application
278      *
279      * @param request
280      *            The Http request
281      * @return The HTML form to update info
282      */
283     public String getModifyApplication( HttpServletRequest request )
284     {
285         setPageTitleProperty( PROPERTY_PAGE_TITLE_MODIFY_APPLICATION );
286 
287         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_APPLICATION ) );
288         Application application = ApplicationHome.findByPrimaryKey( nId );
289 
290         Map<String, Object> model = new HashMap<String, Object>( );
291         model.put( MARK_CATEGORIES_LIST, CategoryHome.getCategories( ) );
292         model.put( MARK_APPLICATION, application );
293         model.put( MARK_COMPONENTS_LIST, ComponentHome.getComponentsList( ) );
294         model.put( MARK_APP_COMPONENTS_LIST, ComponentHome.findByApplication( nId ) );
295         model.put( MARK_PUBLISH_STATUS_LIST, getPublishStatusList( request ) );
296         model.put( MARK_ICONS_LIST, IconHome.getListIcons( getPlugin( ) ) );
297 
298         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_APPLICATION, getLocale( ), model );
299 
300         return getAdminPage( template.getHtml( ) );
301     }
302 
303     /**
304      * Process the change form of a application
305      *
306      * @param request
307      *            The Http request
308      * @return The Jsp URL of the process result
309      */
310     public String doModifyApplication( HttpServletRequest request )
311     {
312         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_APPLICATION ) );
313         Application application = ApplicationHome.findByPrimaryKey( nId );
314 
315         if ( request.getParameter( PARAMETER_ID_APPLICATION ).equals( "" ) )
316         {
317             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
318         }
319 
320         int nIdApplication = Integer.parseInt( request.getParameter( PARAMETER_ID_APPLICATION ) );
321         application.setId( nIdApplication );
322 
323         if ( request.getParameter( PARAMETER_APPLICATION_TITLE ).equals( "" ) )
324         {
325             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
326         }
327 
328         application.setTitle( request.getParameter( PARAMETER_APPLICATION_TITLE ) );
329 
330         if ( request.getParameter( PARAMETER_APPLICATION_DESCRIPTION ).equals( "" ) )
331         {
332             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
333         }
334 
335         application.setDescription( request.getParameter( PARAMETER_APPLICATION_DESCRIPTION ) );
336 
337         if ( request.getParameter( PARAMETER_APPLICATION_ID_CATEGORY ).equals( "" ) )
338         {
339             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
340         }
341 
342         int nIdCategory = Integer.parseInt( request.getParameter( PARAMETER_APPLICATION_ID_CATEGORY ) );
343         application.setIdCategory( nIdCategory );
344 
345         if ( request.getParameter( PARAMETER_APPLICATION_APPLICATION_ORDER ).equals( "" ) )
346         {
347             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
348         }
349 
350         int nApplicationOrder = Integer.parseInt( request.getParameter( PARAMETER_APPLICATION_APPLICATION_ORDER ) );
351         application.setOrder( nApplicationOrder );
352 
353         if ( request.getParameter( PARAMETER_APPLICATION_ID_ICON ).equals( "" ) )
354         {
355             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
356         }
357 
358         int nIdIcon = Integer.parseInt( request.getParameter( PARAMETER_APPLICATION_ID_ICON ) );
359         application.setIdIcon( nIdIcon );
360 
361         if ( request.getParameter( PARAMETER_APPLICATION_WEBAPP_URL ).equals( "" ) )
362         {
363             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
364         }
365 
366         application.setWebappUrl( request.getParameter( PARAMETER_APPLICATION_WEBAPP_URL ) );
367 
368         if ( request.getParameter( PARAMETER_APPLICATION_SQL_SCRIPT_URL ).equals( "" ) )
369         {
370             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
371         }
372 
373         application.setSqlScriptUrl( request.getParameter( PARAMETER_APPLICATION_SQL_SCRIPT_URL ) );
374         application.setPresentation( request.getParameter( PARAMETER_APPLICATION_PRESENTATION ) );
375         application.setInstallation( request.getParameter( PARAMETER_APPLICATION_INSTALLATION ) );
376         application.setArtifactId( request.getParameter( PARAMETER_APPLICATION_ARTIFACT_ID ) );
377         application.setVersion( request.getParameter( PARAMETER_APPLICATION_VERSION ) );
378 
379         int nPublishStatus = Integer.parseInt( request.getParameter( PARAMETER_APPLICATION_PUBLISH_STATUS ) );
380         application.setPublishStatus( nPublishStatus );
381         ApplicationHome.update( application );
382 
383         return JSP_REDIRECT_TO_MANAGE_APPLICATIONS;
384     }
385 
386     public String doModifyComponentsList( HttpServletRequest request )
387     {
388         String [ ] ids = request.getParameterValues( PARAMETER_ID_COMPONENT );
389         String strApplicationId = request.getParameter( PARAMETER_ID_APPLICATION );
390         int nApplicationId = Integer.parseInt( strApplicationId );
391 
392         ApplicationHome.clearComponentsList( nApplicationId );
393 
394         for ( int i = 0; i < ids.length; i++ )
395         {
396             int nIdComponent = Integer.parseInt( ids [i] );
397             ApplicationHome.addComponent( nApplicationId, nIdComponent );
398         }
399 
400         return JSP_REDIRECT_TO_MANAGE_APPLICATIONS;
401     }
402 
403     private ReferenceList getPublishStatusList( HttpServletRequest request )
404     {
405         ReferenceList list = new ReferenceList( );
406         list.addItem( Application.NOT_PUBLISHED, I18nService.getLocalizedString( PROPERTY_NOT_PUBLISHED, request.getLocale( ) ) );
407         list.addItem( Application.PUBLISHED, I18nService.getLocalizedString( PROPERTY_PUBLISHED, request.getLocale( ) ) );
408 
409         return list;
410     }
411 }