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.service;
35  
36  import java.util.HashMap;
37  import java.util.List;
38  import java.util.Map;
39  
40  import javax.servlet.http.HttpServletRequest;
41  import javax.ws.rs.core.UriBuilder;
42  
43  import org.apache.commons.lang.StringUtils;
44  
45  import fr.paris.lutece.plugins.directory.modules.gismap.business.DirectoryGismapSourceQuery;
46  import fr.paris.lutece.plugins.directory.modules.gismap.utils.GismapDirectoryUtils;
47  import fr.paris.lutece.plugins.gismap.business.MapParameter;
48  import fr.paris.lutece.plugins.gismap.business.View;
49  import fr.paris.lutece.plugins.gismap.business.ViewHome;
50  import fr.paris.lutece.portal.web.l10n.LocaleService;
51  import fr.paris.lutece.portal.service.i18n.I18nService;
52  import fr.paris.lutece.portal.service.template.AppTemplateService;
53  import fr.paris.lutece.portal.service.util.AppPathService;
54  import fr.paris.lutece.portal.service.util.AppPropertiesService;
55  import fr.paris.lutece.util.html.HtmlTemplate;
56  
57  import java.util.Locale;
58  
59  /**
60   *
61   * GismapService
62   *
63   */
64  public class GismapDirectoryService
65  {
66  	private static final String PARAM_ID_DIRECTORY = "idDirectory";
67  
68  	public static final String   GISMAP_DEFAULT_VIEW_PROPERTIES = "gismap.mainmap.defaultview";
69  
70  	// Markers
71  	public static final String   GISMAP_VIEW_INIT               = "gismap.view.init";
72  	private static final String  PARAMETER_MAP_PARAMETER        = "map_parameter";
73  	private static final String  PARAMETER_ADD_PARAMETER        = "add_parameter";
74  	private static final String  PARAMETER_DEFAULT_VIEW         = "default_view";
75  
76  	// Templates
77  	private static GismapDirectoryService _singleton                     = new GismapDirectoryService( );
78  
79  	// Constant
80  	public static final String   GISMAP_URL_REST                = "rest/directory-gismap/listRecord";
81  	public static final String   PARAM_VIEW_URLGEOJSON1          = "UrlGeoJSON1";
82  	public static final String   PARAM_VIEW_GEOJSON1            = "GeoJSON1";
83  	public static final String   PARAM_VIEW_THEMATICSIMPLE1     = "ThematicSimple1";
84  	public static final String   PARAM_VIEW_POPUP1              = "Popup1";
85  	public static final String   PARAM_VIEW_URLGEOJSON          = "UrlGeoJSON";
86  	public static final String   PARAM_VIEW_GEOJSON            = "GeoJSON";
87  	public static final String   PARAM_VIEW_THEMATICSIMPLE     = "ThematicSimple";
88  	public static final String   PARAM_VIEW_POPUP              = "Popup";
89  	public static final String   PARAM_VIEW_SHOWLINK            = "Popup_ShowLink";
90  	public static final String   PARAM_ID_GEOLOCATION_ENTRY     = "idGeolocationEntry";
91  	public static final String	 PARAM_GEOJSONINDEX 			= "geoJsonIndex";
92  	public static final String 	 PARAM_VIEW 					= "view";
93  
94  	//MESSAGES
95      private static final String UNAVAILABILITY_MESSAGE = "module.directory.gismap.message.portlet.unavailable.view.misconfiguration";
96  
97  
98  	/**
99  	 * Initialize the GISMAP service
100 	 *
101 	 */
102 	public void init( )
103 	{
104 		// TODO
105 	}
106 
107 	/**
108 	 * Returns the instance of the singleton
109 	 *
110 	 * @return The instance of the singleton
111 	 */
112 	public static GismapDirectoryService getInstance( )
113 	{
114 		return _singleton;
115 	}
116 
117 	
118 	public String getMapTemplateWithDirectoryGismapSources( HttpServletRequest request, String viewId, List<DirectoryGismapSourceQuery> listGeojsonSources )
119 	{
120 		Map<String, Object> model = new HashMap<>( );
121 
122 		if ( StringUtils.isEmpty( viewId ) )
123 		{
124 			return "<span>" + I18nService.getLocalizedString( UNAVAILABILITY_MESSAGE, Locale.FRENCH ) + "</span>";
125 		}
126 
127 		String strDefaultView = AppPropertiesService.getProperty( GISMAP_DEFAULT_VIEW_PROPERTIES , "0" );
128 
129 		View view = ViewHome.findByPrimaryKey( Integer.parseInt( viewId ) );
130 
131 		MapParameter tmp = view.getMapParameter( );
132 		
133 		String strWSUrl = AppPathService.getBaseUrl( request ).concat( GISMAP_URL_REST );
134 
135 		for ( DirectoryGismapSourceQuery geojsonSource : listGeojsonSources)
136 		{
137 			String strIndex = String.valueOf( geojsonSource.getGeoJsonIndex( ) );
138 			String strIdDirectory = String.valueOf( geojsonSource.getIdDirectory( ) );
139 			String strIdEntryGeolocation = String.valueOf( geojsonSource.getIdGeolocationEntry( ) );
140 			String strView = StringUtils.isEmpty( geojsonSource.getView( ) ) ? strDefaultView : geojsonSource.getView( );
141 			
142 			String strWSUrlWithParams = UriBuilder.fromUri(strWSUrl)
143 					.queryParam( PARAM_ID_GEOLOCATION_ENTRY, strIdEntryGeolocation )
144 					.queryParam( PARAM_ID_DIRECTORY, strIdDirectory )
145 					.queryParam( PARAM_GEOJSONINDEX,  strIndex )
146 					.queryParam( PARAM_VIEW, strView )
147 							.build( ).toString( );
148 
149 			tmp.setParameters( PARAM_VIEW_URLGEOJSON.concat( strIndex ), "'" + strWSUrlWithParams+ "'" );
150 		}
151 		
152 		view.setMapParameter( tmp );
153 
154 		model.put( PARAMETER_MAP_PARAMETER, view.getMapParameter( ) );
155 		model.put( PARAMETER_ADD_PARAMETER, view.getAddressParam( ) );
156 		model.put( PARAMETER_DEFAULT_VIEW, strDefaultView );
157 
158 		Locale locale = ( request == null ) ? LocaleService.getDefault( ) : request.getLocale( );
159 		HtmlTemplate templateList = AppTemplateService.getTemplate( view.getMapTemplateFile( ), locale, model );
160 
161 		return templateList.getHtml( );
162 	}
163 
164 }