1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
67
68 @Controller( controllerJsp = "ManageMapTemplates.jsp", controllerPath = "jsp/admin/plugins/carto/", right = "CARTO_MANAGEMENT" )
69 public class MapTemplateJspBean extends AbstractManageCartoJspBean<Integer, MapTemplate>
70 {
71
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
77 private static final String PARAMETER_ID_MAPTEMPLATE = "id";
78
79
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
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
92 private static final String MESSAGE_CONFIRM_REMOVE_MAPTEMPLATE = "carto.message.confirmRemoveMapTemplate";
93
94
95 private static final String VALIDATION_ATTRIBUTES_PREFIX = "carto.model.entity.maptemplate.attribute.";
96
97
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
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
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
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
118 private MapTemplate _maptemplate;
119 private List<Integer> _listIdMapTemplates;
120
121
122
123
124
125
126
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
145
146
147
148
149 @Override
150 List<MapTemplate> getItemsFromIds( List<Integer> listIds )
151 {
152 List<MapTemplate> listMapTemplate = MapTemplateHome.getMapTemplatesListByIds( listIds );
153
154
155 return listMapTemplate.stream( ).sorted( Comparator.comparingInt( notif -> listIds.indexOf( notif.getId( ) ) ) ).collect( Collectors.toList( ) );
156 }
157
158
159
160
161 public void resetListId( )
162 {
163 _listIdMapTemplates = new ArrayList<>( );
164 }
165
166
167
168
169
170
171
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
188
189
190
191 public ReferenceList getMapProvidersRefList( )
192 {
193 ReferenceList refList = new ReferenceList( );
194
195 refList.addItem( StringUtils.EMPTY, StringUtils.EMPTY );
196
197
198
199
200
201 return refList;
202 }
203
204
205
206
207
208
209
210
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
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
237
238
239
240
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
256
257
258
259
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
282
283
284
285
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
308
309
310
311
312
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
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 }