View Javadoc
1   /*
2    * Copyright (c) 2002-2014, 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.directory.modules.gismap.web.portlet;
35  
36  import java.util.ArrayList;
37  import java.util.HashMap;
38  import java.util.List;
39  import javax.servlet.http.HttpServletRequest;
40  
41  import org.apache.commons.lang.StringUtils;
42  
43  import fr.paris.lutece.plugins.directory.business.DirectoryFilter;
44  import fr.paris.lutece.plugins.directory.business.DirectoryHome;
45  import fr.paris.lutece.plugins.directory.business.EntryFilter;
46  import fr.paris.lutece.plugins.directory.business.EntryHome;
47  import fr.paris.lutece.plugins.directory.business.EntryTypeGeolocation;
48  import fr.paris.lutece.plugins.directory.business.Field;
49  import fr.paris.lutece.plugins.directory.business.FieldHome;
50  import fr.paris.lutece.plugins.directory.business.IEntry;
51  import fr.paris.lutece.plugins.directory.business.Directory;
52  import fr.paris.lutece.plugins.directory.modules.gismap.business.DirectoryGismapSourceQuery;
53  import fr.paris.lutece.plugins.directory.modules.gismap.business.portlet.GismapDirectoryPortlet;
54  import fr.paris.lutece.plugins.directory.modules.gismap.business.portlet.GismapDirectoryPortletHome;
55  import fr.paris.lutece.plugins.directory.modules.gismap.service.GismapProvider;
56  import fr.paris.lutece.plugins.directory.service.DirectoryPlugin;
57  import fr.paris.lutece.plugins.directory.utils.DirectoryUtils;
58  import fr.paris.lutece.plugins.gismap.web.GismapJspBean;
59  import fr.paris.lutece.portal.business.portlet.PortletHome;
60  import fr.paris.lutece.portal.service.message.AdminMessage;
61  import fr.paris.lutece.portal.service.message.AdminMessageService;
62  import fr.paris.lutece.portal.service.plugin.Plugin;
63  import fr.paris.lutece.portal.service.plugin.PluginService;
64  import fr.paris.lutece.portal.service.util.AppLogService;
65  import fr.paris.lutece.portal.service.util.AppPropertiesService;
66  import fr.paris.lutece.portal.web.portlet.PortletJspBean;
67  import fr.paris.lutece.util.ReferenceList;
68  import fr.paris.lutece.util.html.HtmlTemplate;
69  
70  /**
71   * This class provides the user interface to manage view Portlet
72   */
73  public class GismapDirectoryPortletJspBean extends PortletJspBean
74  {
75      // //////////////////////////////////////////////////////////////////////////
76      // Constants
77      /**
78       * Right to manage gismap
79       */
80      public static final String  RIGHT_MANAGE_GISMAP                 = GismapJspBean.RIGHT_MANAGE_GISMAP;
81  
82      private static final long   serialVersionUID                    = -2619049973871862337L;
83      private static final String MARK_DIRECTORY_LIST                 = "directory_list";
84      private static final String MARK_DIRECTORY_ID                 = "directory_id";
85      private static final String PARAMETER_ID_DIRECTORY              = "id_directory";
86      private static final String MESSAGE_YOU_MUST_CHOOSE_A_DIRECTORY = "module.directory.gismap.message.mandatory.directory";
87      private static final String PARAMETER_CLOSE_PAGE				= "close";
88  
89     
90      private static final String JSP_ADMIN_SITE = "../../../../site/AdminSite.jsp";
91  
92  	private static final String TYPE_GEOLOC_CLASSNAME = EntryTypeGeolocation.class.getName( );
93  
94  	private static final Object GISMAPPROVIDER_CLASSNAME = GismapProvider.class.getName( );
95  
96  	private static final String MESSAGE_ERROR_PLUGIN_DIRECTORY_DISABLED = "module.directory.gismap.message.plugin.directory.disabled";
97  
98  	private static final String MESSAGE_DIRECTORY_GEOLOCATION_MISCONFIG = "module.directory.gismap.message.directory.geolocation_misconfiguration";
99  
100 	private static final String MESSAGE_YOU_MUST_ENTER_VIEW = "module.directory.gismap.message.mandatory.view";
101 	
102 	private static final String MARK_MAP_VIEW_ID = "map_view_id";
103 
104 	private static final String MARK_MAP_SOURCE_LIST = "map_source_list";
105 	private static final String MARK_MAX_LAYER_NUMBER = "maxLayerNumber";
106 
107 	private static final String PARAMETER_MAP_VIEW_ID = "map_view_id";
108 	private static final String PARAMETER_GEOJSON = "GeoJSON";
109 
110 
111 
112     // //////////////////////////////////////////////////////////////////////////
113     // Class attributes
114     
115     private static Plugin _directoryPlugin = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
116     private static boolean _bDirectoryAvailable = PluginService.isPluginEnable( DirectoryPlugin.PLUGIN_NAME  );
117 
118     /**
119      * Returns the Download portlet creation view
120      *
121      * @param request The HTTP request
122      * @return The HTML view
123      */
124     @Override
125     public String getCreate( HttpServletRequest request )
126     {
127     	if (!_bDirectoryAvailable)
128     	{
129     		return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_PLUGIN_DIRECTORY_DISABLED,
130                     AdminMessage.TYPE_STOP );
131     	}
132         HashMap<String, Object> model = new HashMap<>( );
133         String strIdPage = request.getParameter( PARAMETER_PAGE_ID );
134         String strIdPortletType = request.getParameter( PARAMETER_PORTLET_TYPE_ID );
135         		
136         model.put( MARK_DIRECTORY_LIST, GetDirectoriesWithGeolocation( ) );
137         model.put( MARK_MAP_VIEW_ID, StringUtils.EMPTY );
138         
139         String strMaxLayerGeojson = AppPropertiesService.getProperty( GismapDirectoryPortlet.MAX_LAYER_GEOJSON_PROPERTY, "1" );
140         int nMaxLayerGeojson = Integer.parseInt( strMaxLayerGeojson );        
141         
142         model.put( MARK_MAX_LAYER_NUMBER, nMaxLayerGeojson);
143         for (int i =0; i<=nMaxLayerGeojson; i++ )
144         {  
145         	model.put( PARAMETER_GEOJSON + String.valueOf( i ), "0" );
146         }  
147         
148         HtmlTemplate template = getCreateTemplate( strIdPage, strIdPortletType, model );
149 
150         return template.getHtml( );
151     }
152 
153 
154 	/**
155      * Returns the Download portlet modification view
156      *
157      * @param request The Http request
158      * @return The HTML view
159      */
160     @Override
161     public String getModify( HttpServletRequest request )
162     {
163     	if (!_bDirectoryAvailable)
164     	{
165     		return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_PLUGIN_DIRECTORY_DISABLED,
166                     AdminMessage.TYPE_STOP );
167     	}
168         HashMap<String, Object> model = new HashMap<>( );
169         String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
170         int nPortletId = -1;
171         int nViewId = 0;
172 
173         try
174         {
175             nPortletId = Integer.parseInt( strPortletId );
176         } catch ( NumberFormatException ne )
177         {
178             AppLogService.error( ne );
179         }
180 
181         GismapDirectoryPortlet portlet = ( GismapDirectoryPortlet ) PortletHome.findByPrimaryKey( nPortletId );
182         nViewId = portlet.getView( );
183         List<DirectoryGismapSourceQuery> listPortletSources = portlet.getListMapSource( );
184         
185         model.put( MARK_DIRECTORY_LIST, GetDirectoriesWithGeolocation( ) );
186         model.put( MARK_MAP_VIEW_ID, nViewId);
187         
188         String strMaxLayerGeojson = AppPropertiesService.getProperty( GismapDirectoryPortlet.MAX_LAYER_GEOJSON_PROPERTY, "1" );
189         int nMaxLayerGeojson = Integer.parseInt( strMaxLayerGeojson );        
190         
191         model.put( MARK_MAX_LAYER_NUMBER, nMaxLayerGeojson);
192         boolean bfound;
193         
194         for (int i =1; i<=nMaxLayerGeojson; i++ )
195         {  
196         	bfound = false;
197         	
198         	for ( DirectoryGismapSourceQuery portletSource : listPortletSources)
199         	{
200         		if ( portletSource.getGeoJsonIndex( ) == i)
201         		{
202         			bfound = true;
203         			String strMapSourceKey = String.valueOf( portletSource.getIdDirectory( ) )
204         					.concat( "-" ).concat( String.valueOf( portletSource.getIdGeolocationEntry( ) ) );
205         			model.put( PARAMETER_GEOJSON + String.valueOf( i ), strMapSourceKey );
206         			
207         		}
208         	}
209         	if ( !bfound )
210         	{
211         		model.put( PARAMETER_GEOJSON + String.valueOf( i ), "0" );
212         	}        		        	
213         }  
214         
215 
216         HtmlTemplate template = getModifyTemplate( portlet, model );
217 
218         return template.getHtml( );
219     }
220 
221     private void removeMapSourceFromPortlet(int nPortletId, String directoryMapSourceToRemove) {
222 		// TODO Auto-generated method stub
223 		
224 	}
225 
226 
227 	private void addMapSourceToPortlet(int nPortletId, List<DirectoryGismapSourceQuery> listMapSources) {
228 		// TODO Auto-generated method stub
229 		
230 	}
231 	
232 
233 
234 
235 	/**
236      * Process portlet's creation
237      *
238      * @param request The Http request
239      * @return The Jsp management URL of the process result
240      */
241     @Override
242     public String doCreate( HttpServletRequest request )
243     {
244         GismapDirectoryPortlet portlet = new GismapDirectoryPortlet( );
245         String strPageId = request.getParameter( PARAMETER_PAGE_ID );        
246         
247         //String strView = AppPropertiesService.getProperty( GismapDirectoryPortlet.GISMAP_DEFAULT_VIEW_PROPERTIES );
248         String strView = request.getParameter( PARAMETER_MAP_VIEW_ID );
249         
250         int nPageId = -1;
251         int nView = -1;
252         List<DirectoryGismapSourceQuery> listDirectoryGismapSource = new ArrayList<DirectoryGismapSourceQuery>( );
253 
254         // get portlet common attributes
255         String strErrorUrl = setPortletCommonData( request, portlet );
256 
257         
258         if ( ( strErrorUrl == null ) && ( StringUtils.isEmpty( strView ) ) )
259         {
260             strErrorUrl = AdminMessageService.getMessageUrl( request, MESSAGE_YOU_MUST_ENTER_VIEW, AdminMessage.TYPE_STOP );
261         }
262 
263         if ( strErrorUrl != null )
264         {
265             return strErrorUrl;
266         }
267 
268         try
269         {
270             nPageId = Integer.parseInt( strPageId );
271             nView = Integer.parseInt( strView );
272         } catch ( NumberFormatException ne )
273         {
274             AppLogService.error( ne );
275         }
276         
277         /*
278         if (!CheckGeolocationParams( nDirectoryId ) )
279         {
280         	return AdminMessageService.getMessageUrl( request, MESSAGE_DIRECTORY_GEOLOCATION_MISCONFIG, AdminMessage.TYPE_STOP );
281         }
282         */
283         
284         portlet.setPageId( nPageId );
285         //portlet.setDirectoryId( nDirectoryId );
286         portlet.setView(nView);
287 
288         
289         String strMaxLayerGeojson = AppPropertiesService.getProperty( GismapDirectoryPortlet.MAX_LAYER_GEOJSON_PROPERTY, "1" );
290         int nMaxLayerGeojson = Integer.parseInt( strMaxLayerGeojson );
291         
292         for (int i =0; i<=nMaxLayerGeojson; i++ )
293         {  
294         	String directoryMapSourceToAdd = request.getParameter( PARAMETER_GEOJSON + String.valueOf( i ) );
295 	        if ( StringUtils.isNotEmpty( directoryMapSourceToAdd ) && !directoryMapSourceToAdd.equals( "0" ) )
296 	        {
297 	        	try
298 	        	{
299 	        	String strDirectoryId = directoryMapSourceToAdd.split("-")[0];
300 	        	int nDirectoryId = Integer.parseInt( strDirectoryId );
301 	        	String strGeolocationEntryId = directoryMapSourceToAdd.split("-")[1];
302 	        	int nGeolocationEntryId = Integer.parseInt( strGeolocationEntryId );
303 	        	
304 	        	DirectoryGismapSourceQuery directoryGismapSource = new DirectoryGismapSourceQuery( );
305 	        	directoryGismapSource.setIdDirectory( nDirectoryId );
306 	        	directoryGismapSource.setIdGeolocationEntry( nGeolocationEntryId );
307 	        	directoryGismapSource.setGeoJsonIndex( i );
308 	        	
309 	        	listDirectoryGismapSource.add( directoryGismapSource );
310 	        	}
311 	        	catch (NumberFormatException e)
312 	        	{
313 	        		AppLogService.error("An error occured while reading geojson"+i+" source parameters in GismapDirectoryPortlet creation.", e);
314 	        	}
315 	        }
316         }
317         
318         portlet.setListMapSource( listDirectoryGismapSource );
319         
320         // Creating portlet
321         GismapDirectoryPortletHome.getInstance( ).create( portlet );
322 
323 
324         // Displays the page with the new Portlet
325         return getPageUrl( nPageId );
326     }
327 
328     /**
329      * Process portlet's modification
330      *
331      * @param request The http request
332      * @return Management's Url
333      */
334     @Override
335     public String doModify( HttpServletRequest request )
336     {
337         // recovers portlet attributes
338         String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
339         int nPortletId = -1;
340 
341         try
342         {
343             nPortletId = Integer.parseInt( strPortletId );
344         } catch ( NumberFormatException ne )
345         {
346             AppLogService.error( ne );
347         }
348 
349         GismapDirectoryPortlet portlet = ( GismapDirectoryPortlet ) PortletHome.findByPrimaryKey( nPortletId );
350         
351         String strPageId = request.getParameter( PARAMETER_PAGE_ID );        
352         
353         //String strView = AppPropertiesService.getProperty( GismapDirectoryPortlet.GISMAP_DEFAULT_VIEW_PROPERTIES );
354         String strView = request.getParameter( PARAMETER_MAP_VIEW_ID );
355         
356         int nPageId = -1;
357         int nView = -1;
358         List<DirectoryGismapSourceQuery> listDirectoryGismapSource = new ArrayList<DirectoryGismapSourceQuery>( );
359 
360         // get portlet common attributes
361         String strErrorUrl = setPortletCommonData( request, portlet );
362 
363         
364         if ( ( strErrorUrl == null ) && ( StringUtils.isEmpty( strView ) ) )
365         {
366             strErrorUrl = AdminMessageService.getMessageUrl( request, MESSAGE_YOU_MUST_ENTER_VIEW, AdminMessage.TYPE_STOP );
367         }
368 
369         if ( strErrorUrl != null )
370         {
371             return strErrorUrl;
372         }
373 
374         try
375         {
376             nPageId = Integer.parseInt( strPageId );
377             nView = Integer.parseInt( strView );
378         } catch ( NumberFormatException ne )
379         {
380             AppLogService.error( ne );
381         }
382         
383         /*
384         if (!CheckGeolocationParams( nDirectoryId ) )
385         {
386         	return AdminMessageService.getMessageUrl( request, MESSAGE_DIRECTORY_GEOLOCATION_MISCONFIG, AdminMessage.TYPE_STOP );
387         }
388         */
389         
390         portlet.setPageId( nPageId );
391         portlet.setView(nView);
392 
393         
394         String strMaxLayerGeojson = AppPropertiesService.getProperty( GismapDirectoryPortlet.MAX_LAYER_GEOJSON_PROPERTY, "1" );
395         int nMaxLayerGeojson = Integer.parseInt( strMaxLayerGeojson );
396         
397         for (int i =0; i<=nMaxLayerGeojson; i++ )
398         {  
399         	String directoryMapSourceToAdd = request.getParameter( PARAMETER_GEOJSON + String.valueOf( i ) );
400 	        if ( StringUtils.isNotEmpty( directoryMapSourceToAdd ) && !directoryMapSourceToAdd.equals( "0" ) )
401 	        {
402 	        	try
403 	        	{
404 	        	String strDirectoryId = directoryMapSourceToAdd.split("-")[0];
405 	        	int nDirectoryId = Integer.parseInt( strDirectoryId );
406 	        	String strGeolocationEntryId = directoryMapSourceToAdd.split("-")[1];
407 	        	int nGeolocationEntryId = Integer.parseInt( strGeolocationEntryId );
408 	        	
409 	        	DirectoryGismapSourceQuery directoryGismapSource = new DirectoryGismapSourceQuery( );
410 	        	directoryGismapSource.setIdDirectory( nDirectoryId );
411 	        	directoryGismapSource.setIdGeolocationEntry( nGeolocationEntryId );
412 	        	directoryGismapSource.setGeoJsonIndex( i );
413 	        	
414 	        	listDirectoryGismapSource.add( directoryGismapSource );
415 	        	}
416 	        	catch (NumberFormatException e)
417 	        	{
418 	        		AppLogService.error("An error occured while reading geojson"+i+" source parameters in GismapDirectoryPortlet creation.", e);
419 	        	}
420 	        }
421         }
422         
423         portlet.setListMapSource( listDirectoryGismapSource );
424         
425         // updates the portlet
426         portlet.update( );
427 
428     	// displays the page withe the potlet updated
429     	return getPageUrl( portlet.getPageId( ) );
430 
431     }
432     
433     
434 	/**
435      * Check for missing View configuration inside Geolocation fields of the provided directory identifier.
436      * Return true if check passed
437      * 
438      * @param nDirectoryId
439      *            Directory identifier
440      * @return True if view configuration is OK for this directory
441      */
442     private boolean CheckGeolocationParams(int nDirectoryId) {
443 
444     	boolean bViewsConfigured = true;
445     	
446     	EntryFilter filterEntry = new EntryFilter( );
447     	filterEntry.setIsComment( EntryFilter.FILTER_FALSE );
448     	filterEntry.setIsGroup( EntryFilter.FILTER_FALSE );
449     	filterEntry.setIdDirectory( nDirectoryId );
450         List<IEntry> listEntry = EntryHome.getEntryList( filterEntry, _directoryPlugin );
451     	
452         for (IEntry entry : listEntry)
453         {
454         	if (entry.getEntryType( ).getClassName().equals(  TYPE_GEOLOC_CLASSNAME ) )
455         	{
456 
457         		 List<Field> fieldList = FieldHome.getFieldListByIdEntry( entry.getIdEntry(  ),
458                          DirectoryUtils.getPlugin(  ) );
459 
460                  for ( Field field : fieldList )
461                  {
462                      if ( ( field != null ) && ( field.getTitle(  ) != null ) &&
463                              ( field.getTitle(  ).equals(EntryTypeGeolocation.CONSTANT_VIEW_NUMBER_GES ) ) )
464                      {
465                          if ( StringUtils.isEmpty(field.getValue(  ) ) )
466                         		 {
467                         	 return false;
468                         		 };
469                      }
470                  }
471 
472         	}
473         }    	
474 		return bViewsConfigured;
475 	}
476 
477 
478 	/**
479      * Gets the page URL with relative path
480      * 
481      * @param nIdPage
482      *            Page ID
483      * @return The page URL
484      */
485     @Override
486     protected String getPageUrl( int nIdPage )
487     {
488         return JSP_ADMIN_SITE + "?" + PARAMETER_PAGE_ID + "=" + nIdPage;
489     }
490     
491     /**
492      * Fetch all Enabled Directories containing geolocation entries with gismap as MapProvider
493      * and return them as a referenceList
494      * 
495      * @return The Directory reference List
496      */
497     private ReferenceList GetDirectoriesWithGeolocation( )
498     {
499     	ReferenceList refDirectory = new ReferenceList( );
500     	refDirectory.addItem( "0", StringUtils.EMPTY );
501     	
502     	DirectoryFilter filter = new DirectoryFilter( );
503     	filter.setIsDisabled( DirectoryFilter.FILTER_TRUE );    	
504         List<Directory> directoryList = DirectoryHome.getDirectoryList(filter, PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME ) );
505         
506     	EntryFilter filterEntry = new EntryFilter( );
507     	filterEntry.setIsComment( EntryFilter.FILTER_FALSE );
508     	filterEntry.setIsGroup( EntryFilter.FILTER_FALSE );
509         
510     	
511         for (Directory directory : directoryList)
512         {
513       	
514         	filterEntry.setIdDirectory( directory.getIdDirectory( )  );
515             List<IEntry> listEntry = EntryHome.getEntryList( filterEntry, _directoryPlugin );
516         	
517             for (IEntry entry : listEntry)
518             {
519             	if (entry.getEntryType( ).getClassName().equals( TYPE_GEOLOC_CLASSNAME ) )
520             	{
521 
522             		if (entry.getMapProvider( )!= null && entry.getMapProvider( ).getClass( ).getName( ).equals( GISMAPPROVIDER_CLASSNAME)  )
523             		{
524                 		refDirectory.addItem( String.valueOf( directory.getIdDirectory( ) ).concat( "-" ).concat( String.valueOf( entry.getIdEntry( ) ) )
525                 				, directory.getTitle( ).concat( " - " ).concat( entry.getTitle( ) ) );
526                 		break;
527             		}
528 
529             	}
530             }
531         	
532         }
533     	
534 		return refDirectory;
535 	}
536     
537 }