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.Component;
37  import fr.paris.lutece.plugins.appstore.business.ComponentHome;
38  import fr.paris.lutece.plugins.appstore.service.ComponentInfoService;
39  import fr.paris.lutece.portal.service.message.AdminMessage;
40  import fr.paris.lutece.portal.service.message.AdminMessageService;
41  import fr.paris.lutece.portal.service.template.AppTemplateService;
42  import fr.paris.lutece.portal.service.util.AppPropertiesService;
43  import fr.paris.lutece.portal.web.constants.Messages;
44  import fr.paris.lutece.portal.web.util.LocalizedPaginator;
45  import fr.paris.lutece.util.html.HtmlTemplate;
46  import fr.paris.lutece.util.html.Paginator;
47  import fr.paris.lutece.util.url.UrlItem;
48  
49  import java.util.HashMap;
50  import java.util.List;
51  import java.util.Map;
52  
53  import javax.servlet.http.HttpServletRequest;
54  
55  /**
56   * Component JSP Bean
57   */
58  public class ComponentJspBean extends AppStoreJspBean
59  {
60      private static final String TEMPLATE_MANAGE_COMPONENTS = "/admin/plugins/appstore/manage_component.html";
61      private static final String TEMPLATE_CREATE_COMPONENT = "/admin/plugins/appstore/create_component.html";
62      private static final String TEMPLATE_MODIFY_COMPONENT = "/admin/plugins/appstore/modify_component.html";
63      private static final String PARAMETER_COMPONENT_ID_COMPONENT = "component_id_component";
64      private static final String PARAMETER_COMPONENT_GROUP_ID = "component_group_id";
65      private static final String PARAMETER_COMPONENT_TITLE = "component_title";
66      private static final String PARAMETER_COMPONENT_DESCRIPTION = "component_description";
67      private static final String PARAMETER_COMPONENT_ARTIFACT_ID = "component_artifact_id";
68      private static final String PARAMETER_COMPONENT_VERSION = "component_version";
69      private static final String PARAMETER_COMPONENT_COMPONENT_TYPE = "component_component_type";
70      private static final String PROPERTY_PAGE_TITLE_MANAGE_COMPONENTS = "appstore.manage_components.pageTitle";
71      private static final String PROPERTY_PAGE_TITLE_MODIFY_COMPONENT = "appstore.modify_component.pageTitle";
72      private static final String PROPERTY_PAGE_TITLE_CREATE_COMPONENT = "appstore.create_component.pageTitle";
73      private static final String MARK_COMPONENT_LIST = "component_list";
74      private static final String MARK_COMPONENT = "component";
75      private static final String JSP_DO_REMOVE_COMPONENT = "jsp/admin/plugins/appstore/DoRemoveComponent.jsp";
76      private static final String JSP_MANAGE_COMPONENTS = "jsp/admin/plugins/appstore/ManageComponents.jsp";
77      private static final String JSP_REDIRECT_TO_MANAGE_COMPONENTS = "ManageComponents.jsp";
78      private static final String PROPERTY_DEFAULT_LIST_COMPONENT_PER_PAGE = "appstore.listComponents.itemsPerPage";
79      private static final String MESSAGE_CONFIRM_REMOVE_COMPONENT = "appstore.message.confirmRemoveComponent";
80  
81      /**
82       * Returns the list of component
83       *
84       * @param request
85       *            The Http request
86       * @return the components list
87       */
88      public String getManageComponents( HttpServletRequest request )
89      {
90          setPageTitleProperty( PROPERTY_PAGE_TITLE_MANAGE_COMPONENTS );
91  
92          _strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
93          _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_LIST_COMPONENT_PER_PAGE, 50 );
94          _nItemsPerPage = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, _nDefaultItemsPerPage );
95  
96          UrlItem url = new UrlItem( JSP_MANAGE_COMPONENTS );
97          String strUrl = url.getUrl( );
98          List<Component> listComponents = (List<Component>) ComponentHome.getComponentsList( );
99  
100         // PAGINATOR
101         LocalizedPaginator paginator = new LocalizedPaginator( listComponents, _nItemsPerPage, strUrl, PARAMETER_PAGE_INDEX, _strCurrentPageIndex, getLocale( ) );
102 
103         Map<String, Object> model = new HashMap<String, Object>( );
104 
105         model.put( MARK_NB_ITEMS_PER_PAGE, "" + _nItemsPerPage );
106         model.put( MARK_PAGINATOR, paginator );
107 
108         model.put( MARK_COMPONENT_LIST, paginator.getPageItems( ) );
109 
110         HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_MANAGE_COMPONENTS, getLocale( ), model );
111 
112         return getAdminPage( templateList.getHtml( ) );
113     }
114 
115     /**
116      * Returns the form to create a component
117      *
118      * @param request
119      *            The Http request
120      * @return the html code of the component form
121      */
122     public String getCreateComponent( HttpServletRequest request )
123     {
124         setPageTitleProperty( PROPERTY_PAGE_TITLE_CREATE_COMPONENT );
125 
126         Map<String, Object> model = new HashMap<String, Object>( );
127 
128         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_COMPONENT, getLocale( ), model );
129 
130         return getAdminPage( template.getHtml( ) );
131     }
132 
133     /**
134      * Process the data capture form of a new component
135      *
136      * @param request
137      *            The Http Request
138      * @return The Jsp URL of the process result
139      */
140     public String doCreateComponent( HttpServletRequest request )
141     {
142         Component component = new Component( );
143 
144         if ( request.getParameter( PARAMETER_COMPONENT_TITLE ).equals( "" ) )
145         {
146             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
147         }
148 
149         if ( request.getParameter( PARAMETER_COMPONENT_DESCRIPTION ).equals( "" ) )
150         {
151             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
152         }
153 
154         if ( request.getParameter( PARAMETER_COMPONENT_ARTIFACT_ID ).equals( "" ) )
155         {
156             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
157         }
158 
159         component.setTitle( request.getParameter( PARAMETER_COMPONENT_TITLE ) );
160         component.setDescription( request.getParameter( PARAMETER_COMPONENT_DESCRIPTION ) );
161         component.setArtifactId( request.getParameter( PARAMETER_COMPONENT_ARTIFACT_ID ) );
162         component.setGroupId( request.getParameter( PARAMETER_COMPONENT_GROUP_ID ) );
163         component.setComponentType( request.getParameter( PARAMETER_COMPONENT_COMPONENT_TYPE ) );
164         ComponentInfoService.setReleaseVersion( component );
165 
166         ComponentHome.create( component );
167 
168         return JSP_REDIRECT_TO_MANAGE_COMPONENTS;
169     }
170 
171     /**
172      * Manages the removal form of a component whose identifier is in the http request
173      *
174      * @param request
175      *            The Http request
176      * @return the html code to confirm
177      */
178     public String getConfirmRemoveComponent( HttpServletRequest request )
179     {
180         int nId = Integer.parseInt( request.getParameter( PARAMETER_COMPONENT_ID_COMPONENT ) );
181         UrlItem url = new UrlItem( JSP_DO_REMOVE_COMPONENT );
182         url.addParameter( PARAMETER_COMPONENT_ID_COMPONENT, nId );
183 
184         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_COMPONENT, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION );
185     }
186 
187     /**
188      * Handles the removal form of a component
189      *
190      * @param request
191      *            The Http request
192      * @return the jsp URL to display the form to manage components
193      */
194     public String doRemoveComponent( HttpServletRequest request )
195     {
196         int nId = Integer.parseInt( request.getParameter( PARAMETER_COMPONENT_ID_COMPONENT ) );
197         ComponentHome.remove( nId );
198 
199         return JSP_REDIRECT_TO_MANAGE_COMPONENTS;
200     }
201 
202     /**
203      * Returns the form to update info about a component
204      *
205      * @param request
206      *            The Http request
207      * @return The HTML form to update info
208      */
209     public String getModifyComponent( HttpServletRequest request )
210     {
211         setPageTitleProperty( PROPERTY_PAGE_TITLE_MODIFY_COMPONENT );
212 
213         int nId = Integer.parseInt( request.getParameter( PARAMETER_COMPONENT_ID_COMPONENT ) );
214         Component component = ComponentHome.findByPrimaryKey( nId );
215 
216         Map<String, Object> model = new HashMap<String, Object>( );
217         model.put( MARK_COMPONENT, component );
218 
219         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_COMPONENT, getLocale( ), model );
220 
221         return getAdminPage( template.getHtml( ) );
222     }
223 
224     /**
225      * Process the change form of a component
226      *
227      * @param request
228      *            The Http request
229      * @return The Jsp URL of the process result
230      */
231     public String doModifyComponent( HttpServletRequest request )
232     {
233         int nId = Integer.parseInt( request.getParameter( PARAMETER_COMPONENT_ID_COMPONENT ) );
234         Component component = ComponentHome.findByPrimaryKey( nId );
235 
236         if ( request.getParameter( PARAMETER_COMPONENT_TITLE ).equals( "" ) )
237         {
238             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
239         }
240 
241         if ( request.getParameter( PARAMETER_COMPONENT_DESCRIPTION ).equals( "" ) )
242         {
243             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
244         }
245 
246         if ( request.getParameter( PARAMETER_COMPONENT_ARTIFACT_ID ).equals( "" ) )
247         {
248             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
249         }
250 
251         component.setTitle( request.getParameter( PARAMETER_COMPONENT_TITLE ) );
252         component.setDescription( request.getParameter( PARAMETER_COMPONENT_DESCRIPTION ) );
253         component.setArtifactId( request.getParameter( PARAMETER_COMPONENT_ARTIFACT_ID ) );
254         component.setGroupId( request.getParameter( PARAMETER_COMPONENT_GROUP_ID ) );
255         component.setComponentType( request.getParameter( PARAMETER_COMPONENT_COMPONENT_TYPE ) );
256         ComponentInfoService.setReleaseVersion( component );
257         ComponentHome.update( component );
258 
259         return JSP_REDIRECT_TO_MANAGE_COMPONENTS;
260     }
261 }