View Javadoc
1   /*
2    * Copyright (c) 2002-2023, 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  
35  package fr.paris.lutece.plugins.carto.web;
36  
37  import fr.paris.lutece.portal.service.message.AdminMessage;
38  import fr.paris.lutece.portal.service.message.AdminMessageService;
39  import fr.paris.lutece.portal.service.security.SecurityTokenService;
40  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
41  import fr.paris.lutece.portal.service.util.AppException;
42  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
43  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
44  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
45  import fr.paris.lutece.util.url.UrlItem;
46  import fr.paris.lutece.util.ReferenceList;
47  import fr.paris.lutece.util.html.AbstractPaginator;
48  
49  import java.util.Comparator;
50  import java.util.ArrayList;
51  import java.util.List;
52  import java.util.Map;
53  import java.util.Optional;
54  import java.util.stream.Collectors;
55  import javax.servlet.http.HttpServletRequest;
56  
57  import org.apache.commons.lang3.StringUtils;
58  
59  import fr.paris.lutece.plugins.carto.business.BasemapHome;
60  import fr.paris.lutece.plugins.carto.business.DataLayer;
61  import fr.paris.lutece.plugins.carto.business.DataLayerMapTemplateHome;
62  import fr.paris.lutece.plugins.carto.business.MapTemplate;
63  import fr.paris.lutece.plugins.carto.business.MapTemplateHome;
64  
65  /**
66   * This class provides the user interface to manage MapTemplate features ( manage, create, modify, remove )
67   */
68  @Controller( controllerJsp = "ManageMapTemplates.jsp", controllerPath = "jsp/admin/plugins/carto/", right = "CARTO_MANAGEMENT" )
69  public class MapTemplateJspBean extends AbstractManageCartoJspBean<Integer, MapTemplate>
70  {
71      // Templates
72      private static final String TEMPLATE_MANAGE_MAPTEMPLATES = "/admin/plugins/carto/manage_maptemplates.html";
73      private static final String TEMPLATE_CREATE_MAPTEMPLATE = "/admin/plugins/carto/create_maptemplate.html";
74      private static final String TEMPLATE_MODIFY_MAPTEMPLATE = "/admin/plugins/carto/modify_maptemplate.html";
75  
76      // Parameters
77      private static final String PARAMETER_ID_MAPTEMPLATE = "id";
78  
79      // Properties for page titles
80      private static final String PROPERTY_PAGE_TITLE_MANAGE_MAPTEMPLATES = "carto.manage_maptemplates.pageTitle";
81      private static final String PROPERTY_PAGE_TITLE_MODIFY_MAPTEMPLATE = "carto.modify_maptemplate.pageTitle";
82      private static final String PROPERTY_PAGE_TITLE_CREATE_MAPTEMPLATE = "carto.create_maptemplate.pageTitle";
83  
84      // Markers
85      private static final String MARK_MAPTEMPLATE_LIST = "maptemplate_list";
86      private static final String MARK_MAPTEMPLATE = "maptemplate";
87      private static final String MARK_MAP_PROVIDER_LIST = "mapprovider_list";
88  
89      private static final String JSP_MANAGE_MAPTEMPLATES = "jsp/admin/plugins/carto/ManageMapTemplates.jsp";
90  
91      // Properties
92      private static final String MESSAGE_CONFIRM_REMOVE_MAPTEMPLATE = "carto.message.confirmRemoveMapTemplate";
93  
94      // Validations
95      private static final String VALIDATION_ATTRIBUTES_PREFIX = "carto.model.entity.maptemplate.attribute.";
96  
97      // Views
98      private static final String VIEW_MANAGE_MAPTEMPLATES = "manageMapTemplates";
99      private static final String VIEW_CREATE_MAPTEMPLATE = "createMapTemplate";
100     private static final String VIEW_MODIFY_MAPTEMPLATE = "modifyMapTemplate";
101 
102     // Actions
103     private static final String ACTION_CREATE_MAPTEMPLATE = "createMapTemplate";
104     private static final String ACTION_MODIFY_MAPTEMPLATE = "modifyMapTemplate";
105     private static final String ACTION_REMOVE_MAPTEMPLATE = "removeMapTemplate";
106     private static final String ACTION_CONFIRM_REMOVE_MAPTEMPLATE = "confirmRemoveMapTemplate";
107 
108     // Infos
109     private static final String INFO_MAPTEMPLATE_CREATED = "carto.info.maptemplate.created";
110     private static final String INFO_MAPTEMPLATE_UPDATED = "carto.info.maptemplate.updated";
111     private static final String INFO_MAPTEMPLATE_REMOVED = "carto.info.maptemplate.removed";
112 
113     // Errors
114     private static final String ERROR_RESOURCE_NOT_FOUND = "Resource not found";
115     private static final String ERROR_MAP_REMOVED = "carto.manage_maptemplate.MapIsPresent";
116 
117     // Session variable to store working values
118     private MapTemplate _maptemplate;
119     private List<Integer> _listIdMapTemplates;
120 
121     /**
122      * Build the Manage View
123      * 
124      * @param request
125      *            The HTTP request
126      * @return The page
127      */
128     @View( value = VIEW_MANAGE_MAPTEMPLATES, defaultView = true )
129     public String getManageMapTemplates( HttpServletRequest request )
130     {
131         _maptemplate = null;
132 
133         if ( request.getParameter( AbstractPaginator.PARAMETER_PAGE_INDEX ) == null || _listIdMapTemplates.isEmpty( ) )
134         {
135             _listIdMapTemplates = MapTemplateHome.getIdMapTemplatesList( );
136         }
137 
138         Map<String, Object> model = getPaginatedListModel( request, MARK_MAPTEMPLATE_LIST, _listIdMapTemplates, JSP_MANAGE_MAPTEMPLATES );
139 
140         return getPage( PROPERTY_PAGE_TITLE_MANAGE_MAPTEMPLATES, TEMPLATE_MANAGE_MAPTEMPLATES, model );
141     }
142 
143     /**
144      * Get Items from Ids list
145      * 
146      * @param listIds
147      * @return the populated list of items corresponding to the id List
148      */
149     @Override
150     List<MapTemplate> getItemsFromIds( List<Integer> listIds )
151     {
152         List<MapTemplate> listMapTemplate = MapTemplateHome.getMapTemplatesListByIds( listIds );
153 
154         // keep original order
155         return listMapTemplate.stream( ).sorted( Comparator.comparingInt( notif -> listIds.indexOf( notif.getId( ) ) ) ).collect( Collectors.toList( ) );
156     }
157 
158     /**
159      * reset the _listIdMapTemplates list
160      */
161     public void resetListId( )
162     {
163         _listIdMapTemplates = new ArrayList<>( );
164     }
165 
166     /**
167      * Returns the form to create a maptemplate
168      *
169      * @param request
170      *            The Http request
171      * @return the html code of the maptemplate form
172      */
173     @View( VIEW_CREATE_MAPTEMPLATE )
174     public String getCreateMapTemplate( HttpServletRequest request )
175     {
176         _maptemplate = ( _maptemplate != null ) ? _maptemplate : new MapTemplate( );
177 
178         Map<String, Object> model = getModel( );
179         model.put( MARK_MAPTEMPLATE, _maptemplate );
180         model.put( MARK_MAP_PROVIDER_LIST, BasemapHome.getBasemapsReferenceList( ) );
181         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, ACTION_CREATE_MAPTEMPLATE ) );
182 
183         return getPage( PROPERTY_PAGE_TITLE_CREATE_MAPTEMPLATE, TEMPLATE_CREATE_MAPTEMPLATE, model );
184     }
185 
186     /**
187      * Builds the {@link ReferenceList} of all available map providers
188      * 
189      * @return the {@link ReferenceList}
190      */
191     public ReferenceList getMapProvidersRefList( )
192     {
193         ReferenceList refList = new ReferenceList( );
194 
195         refList.addItem( StringUtils.EMPTY, StringUtils.EMPTY );
196 
197         /*
198          * for ( IMapProvider mapProvider : MapProviderManager.getMapProvidersList( ) ) { refList.add( mapProvider.toRefItem( ) ); }
199          */
200 
201         return refList;
202     }
203 
204     /**
205      * Process the data capture form of a new maptemplate
206      *
207      * @param request
208      *            The Http Request
209      * @return The Jsp URL of the process result
210      * @throws AccessDeniedException
211      */
212     @Action( ACTION_CREATE_MAPTEMPLATE )
213     public String doCreateMapTemplate( HttpServletRequest request ) throws AccessDeniedException
214     {
215         populate( _maptemplate, request, getLocale( ) );
216 
217         if ( !SecurityTokenService.getInstance( ).validate( request, ACTION_CREATE_MAPTEMPLATE ) )
218         {
219             throw new AccessDeniedException( "Invalid security token" );
220         }
221 
222         // Check constraints
223         if ( !validateBean( _maptemplate, VALIDATION_ATTRIBUTES_PREFIX ) )
224         {
225             return redirectView( request, VIEW_CREATE_MAPTEMPLATE );
226         }
227 
228         MapTemplateHome.create( _maptemplate );
229         addInfo( INFO_MAPTEMPLATE_CREATED, getLocale( ) );
230         resetListId( );
231 
232         return redirectView( request, VIEW_MANAGE_MAPTEMPLATES );
233     }
234 
235     /**
236      * Manages the removal form of a maptemplate whose identifier is in the http request
237      *
238      * @param request
239      *            The Http request
240      * @return the html code to confirm
241      */
242     @Action( ACTION_CONFIRM_REMOVE_MAPTEMPLATE )
243     public String getConfirmRemoveMapTemplate( HttpServletRequest request )
244     {
245         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_MAPTEMPLATE ) );
246         UrlItem url = new UrlItem( getActionUrl( ACTION_REMOVE_MAPTEMPLATE ) );
247         url.addParameter( PARAMETER_ID_MAPTEMPLATE, nId );
248 
249         String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_MAPTEMPLATE, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION );
250 
251         return redirect( request, strMessageUrl );
252     }
253 
254     /**
255      * Handles the removal form of a maptemplate
256      *
257      * @param request
258      *            The Http request
259      * @return the jsp URL to display the form to manage maptemplates
260      */
261     @Action( ACTION_REMOVE_MAPTEMPLATE )
262     public String doRemoveMapTemplate( HttpServletRequest request )
263     {
264         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_MAPTEMPLATE ) );
265 
266         List<DataLayer> lstDataLayer = DataLayerMapTemplateHome.getDataLayerListByMapTemplateId( nId );
267         if ( !lstDataLayer.isEmpty( ) )
268         {
269             addError( ERROR_MAP_REMOVED, getLocale( ) );
270             return redirectView( request, VIEW_MANAGE_MAPTEMPLATES );
271         }
272 
273         MapTemplateHome.remove( nId );
274         addInfo( INFO_MAPTEMPLATE_REMOVED, getLocale( ) );
275         resetListId( );
276 
277         return redirectView( request, VIEW_MANAGE_MAPTEMPLATES );
278     }
279 
280     /**
281      * Returns the form to update info about a maptemplate
282      *
283      * @param request
284      *            The Http request
285      * @return The HTML form to update info
286      */
287     @View( VIEW_MODIFY_MAPTEMPLATE )
288     public String getModifyMapTemplate( HttpServletRequest request )
289     {
290         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_MAPTEMPLATE ) );
291 
292         if ( _maptemplate == null || ( _maptemplate.getId( ) != nId ) )
293         {
294             Optional<MapTemplate> optMapTemplate = MapTemplateHome.findByPrimaryKey( nId );
295             _maptemplate = optMapTemplate.orElseThrow( ( ) -> new AppException( ERROR_RESOURCE_NOT_FOUND ) );
296         }
297 
298         Map<String, Object> model = getModel( );
299         model.put( MARK_MAPTEMPLATE, _maptemplate );
300         model.put( MARK_MAP_PROVIDER_LIST, BasemapHome.getBasemapsReferenceList( ) );
301         model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, ACTION_MODIFY_MAPTEMPLATE ) );
302 
303         return getPage( PROPERTY_PAGE_TITLE_MODIFY_MAPTEMPLATE, TEMPLATE_MODIFY_MAPTEMPLATE, model );
304     }
305 
306     /**
307      * Process the change form of a maptemplate
308      *
309      * @param request
310      *            The Http request
311      * @return The Jsp URL of the process result
312      * @throws AccessDeniedException
313      */
314     @Action( ACTION_MODIFY_MAPTEMPLATE )
315     public String doModifyMapTemplate( HttpServletRequest request ) throws AccessDeniedException
316     {
317         populate( _maptemplate, request, getLocale( ) );
318 
319         if ( !SecurityTokenService.getInstance( ).validate( request, ACTION_MODIFY_MAPTEMPLATE ) )
320         {
321             throw new AccessDeniedException( "Invalid security token" );
322         }
323 
324         // Check constraints
325         if ( !validateBean( _maptemplate, VALIDATION_ATTRIBUTES_PREFIX ) )
326         {
327             return redirect( request, VIEW_MODIFY_MAPTEMPLATE, PARAMETER_ID_MAPTEMPLATE, _maptemplate.getId( ) );
328         }
329 
330         MapTemplateHome.update( _maptemplate );
331         addInfo( INFO_MAPTEMPLATE_UPDATED, getLocale( ) );
332         resetListId( );
333 
334         return redirectView( request, VIEW_MANAGE_MAPTEMPLATES );
335     }
336 }