package org.gcube.portlets.user.geoportaldataviewer.client; import java.util.Iterator; import org.gcube.application.geoportalcommon.shared.GeoNaDataViewerProfile; import org.gcube.application.geoportalcommon.shared.GeoNaItemRef; import org.gcube.application.geoportalcommon.shared.LayerItem; import org.gcube.application.geoportalcommon.shared.products.ConcessioneDV; import org.gcube.application.geoportalcommon.shared.products.model.RecordDV; import org.gcube.portlets.user.geoportaldataviewer.client.events.AddedLayerToMapEvent; import org.gcube.portlets.user.geoportaldataviewer.client.events.AddedLayerToMapEventHandler; import org.gcube.portlets.user.geoportaldataviewer.client.events.ClosedViewDetailsEvent; import org.gcube.portlets.user.geoportaldataviewer.client.events.ClosedViewDetailsEventHandler; import org.gcube.portlets.user.geoportaldataviewer.client.events.QueryDataEvent; import org.gcube.portlets.user.geoportaldataviewer.client.events.ShowDetailsEvent; import org.gcube.portlets.user.geoportaldataviewer.client.events.ShowDetailsEventHandler; import org.gcube.portlets.user.geoportaldataviewer.client.events.ZoomOutOverMinimumEvent; import org.gcube.portlets.user.geoportaldataviewer.client.events.ZoomOutOverMinimumEventHandler; import org.gcube.portlets.user.geoportaldataviewer.client.gis.MapUtils; import org.gcube.portlets.user.geoportaldataviewer.client.ui.GeonaDataViewMainPanel; import org.gcube.portlets.user.geoportaldataviewer.shared.gis.GeoQuery; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.core.client.GWT; import com.google.gwt.core.client.Scheduler; import com.google.gwt.core.client.Scheduler.ScheduledCommand; import com.google.gwt.core.client.ScriptInjector; import com.google.gwt.event.logical.shared.ResizeEvent; import com.google.gwt.event.logical.shared.ResizeHandler; import com.google.gwt.event.shared.HandlerManager; import com.google.gwt.user.client.Timer; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.rpc.AsyncCallback; import com.google.gwt.user.client.ui.RootPanel; import ol.Coordinate; /** * Entry point classes define onModuleLoad(). * * @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it) * * Oct 27, 2020 */ public class GeoportalDataViewer implements EntryPoint { /** The Constant APP_DIV. */ public final static String APP_DIV = "geoportal-data-viewer"; /** The ol map. */ //private OpenLayerOSM olMap = null; /** The main panel. */ private GeonaDataViewMainPanel mainPanel; /** The param wms request. */ private String paramWmsRequest; /** The param UUID. */ //private String paramUUID; private String paramGeonaItemType; /** The param layer title. */ private String paramLayerTitle; private HandlerManager applicationBus = new HandlerManager(""); /** The layer manager. */ private LayerManager layerManager = new LayerManager(applicationBus); private String paramGeonaItemID; private GeoNaDataViewerProfile geonaDataViewerProfile; private OLMapManager olMapMng = null; int attempt = 0; //https://data.d4science.org/gis-viewer-app/?wmsrequest=https%3A%2F%2Fgeona-proto.d4science.org%2Fgeoserver%2Fconcessioni_conf%2Fwms%3Fservice%3DWMS%26version%3D1.1.0%26request%3DGetMap%26layers%3Dconcessioni_conf%3Acentroids_concessioni%26styles%3D%26bbox%3D8.476%2C39.179%2C17.391%2C45.772%26width%3D768%26height%3D567%26srs%3DEPSG%3A4326%26format%3Dapplication%2Fopenlayers&zoom=6¢ermap=12.45%2C42.98 /** * This is the entry point method. */ public void onModuleLoad() { mainPanel = new GeonaDataViewMainPanel(applicationBus, getClientHeight()); RootPanel.get(APP_DIV).add(mainPanel); Scheduler.get().scheduleDeferred(new ScheduledCommand() { @Override public void execute() { olMapMng = new OLMapManager(mainPanel.getMapPanel().getElement().getId(), layerManager.getLayerManagerBus()); layerManager.setOlMap(olMapMng.getOLOSMMap()); mainPanel.setMap(olMapMng.getOLOSMMap()); updateSize(); ScriptInjector.fromUrl("//cdnjs.cloudflare.com/ajax/libs/nanogallery2/3.0.5/jquery.nanogallery2.min.js") .setWindow(ScriptInjector.TOP_WINDOW).inject(); } }); Window.addResizeHandler(new ResizeHandler() { @Override public void onResize(ResizeEvent event) { updateSize(); } }); paramWmsRequest = Window.Location.getParameter(GeoportalDataViewerConstants.GET_WMS_PARAMETER); paramGeonaItemType = Window.Location.getParameter(GeoportalDataViewerConstants.GET_GEONA_ITEM_TYPE); // paramUUID = // Window.Location.getParameter(GeoportalDataViewerConstants.GET_UUID_PARAMETER); paramGeonaItemID = Window.Location.getParameter(GeoportalDataViewerConstants.GET_GEONA_ITEM_ID); paramLayerTitle = Window.Location.getParameter(GeoportalDataViewerConstants.GET_LAYER_TITLE); GWT.log(GeoportalDataViewerConstants.GET_WMS_PARAMETER + " = " + paramWmsRequest); GWT.log(GeoportalDataViewerConstants.GET_GEONA_ITEM_TYPE + " = " + paramGeonaItemType); GWT.log(GeoportalDataViewerConstants.GET_GEONA_ITEM_ID + " = " + paramGeonaItemID); // GWT.log(GeoportalDataViewerConstants.GET_UUID_PARAMETER + " = " + paramUUID); GWT.log(GeoportalDataViewerConstants.GET_LAYER_TITLE + " = " + paramLayerTitle); // VALIDATING THE LONG FORMAT FOR GID // try { // if (paramGeonaItemID != null) // Long.parseLong(paramGeonaItemID); // } catch (Exception e) { // Window.alert("Bad format for parameter " + GeoportalDataViewerConstants.GET_GEONA_ITEM_ID // + ". It must be a Long"); // paramGeonaItemID = null; // } Scheduler.get().scheduleDeferred(new ScheduledCommand() { @Override public void execute() { GeoportalDataViewerServiceAsync.Util.getInstance() .getGeoNaDataViewProfile(new AsyncCallback() { @Override public void onFailure(Throwable caught) { Window.alert(caught.getMessage()); } @Override public void onSuccess(GeoNaDataViewerProfile profile) { geonaDataViewerProfile = profile; GWT.log("Profile: " + geonaDataViewerProfile); Iterator it; String theItemType = paramGeonaItemType; if (theItemType == null) { it = geonaDataViewerProfile.getMapLayers().keySet().iterator(); it.hasNext(); theItemType = it.next(); } LayerItem layerItem = geonaDataViewerProfile.getMapLayers().get(theItemType); if (layerItem == null) { Window.alert("Not detected any layer with type: " + theItemType); return; } GWT.log("The layerItem is: " + layerItem); layerManager.addLayer(theItemType, null, null, layerItem.getWmsLink(), false, false, null, true); if (paramGeonaItemID != null) { if (paramGeonaItemType == null) { Window.alert( "Missing parameter " + GeoportalDataViewerConstants.GET_GEONA_ITEM_TYPE + " (GeoNa Data Type) where to search the item id: " + paramGeonaItemID); return; } GeoNaItemRef gir = new GeoNaItemRef(paramGeonaItemID, paramGeonaItemType); applicationBus.fireEvent(new ShowDetailsEvent(gir, null, null)); } } }); } }); bindEvents(); } /** * Update window size. */ public void updateSize() { int rootHeight = getClientHeight(); int height = rootHeight; mainPanel.setPanelsHeight(height); GWT.log("Set height: "+height); } public int getClientHeight() { RootPanel principalDiv = RootPanel.get(APP_DIV); int topBorder = principalDiv.getAbsoluteTop(); int rootHeight = Window.getClientHeight() - topBorder; return rootHeight; } /** * Bind events. */ private void bindEvents() { applicationBus.addHandler(ShowDetailsEvent.TYPE, new ShowDetailsEventHandler() { @Override public void onShowDetails(ShowDetailsEvent showDetailsEvent) { GWT.log("Fired event: "+showDetailsEvent); String geonaMongoId = showDetailsEvent.getGeonaMongoID(); if(geonaMongoId==null) { Window.alert("Item Id not found"); return; } //TODO boolean found = false; GWT.log("Product with id: "+geonaMongoId+" found? "+found); if(!found) { if(showDetailsEvent.getGeonaItemType().equalsIgnoreCase("Concessione")) { GeoportalDataViewerServiceAsync.Util.getInstance().getConcessioneForId(geonaMongoId, new AsyncCallback() { @Override public void onFailure(Throwable caught) { Window.alert(caught.getMessage()); mainPanel.hidePanelDetails(); } @Override public void onSuccess(ConcessioneDV concessioneDV) { GWT.log("Showing: "+concessioneDV); mainPanel.showAsDetails(concessioneDV, showDetailsEvent.getGeonaItemRef()); } }); } } } }); applicationBus.addHandler(AddedLayerToMapEvent.TYPE, new AddedLayerToMapEventHandler() { @Override public void onLayerRendered(AddedLayerToMapEvent addedLayerToMapEvent) { GWT.log("Fired AddedLayerToMapEvent "+addedLayerToMapEvent.getLayerItem()); //GeoportalDataViewerConstants.print("Fired AddedLayerToMapEvent "+addedLayerToMapEvent.getLayerItem()); try { attempt = 0; if(paramGeonaItemID!=null) { //waiting for record retrieved by service before calling the WFS final int MAX_RETRY = 7; Timer timer = new com.google.gwt.user.client.Timer() { @Override public void run() { attempt++; GWT.log("waiting got record tenative: "+attempt); RecordDV record = mainPanel.getDisplyedRecord(); if(record!=null) { this.cancel(); GWT.log("cancelled timer"); performWFSQueryOnCentroid(record); } if(attempt>MAX_RETRY) { GWT.log("MAX_RETRY reached, cancelled timer"); this.cancel(); } } }; timer.scheduleRepeating(1000); } }catch (Exception e) { // TODO: handle exception } } }); applicationBus.addHandler(ZoomOutOverMinimumEvent.TYPE, new ZoomOutOverMinimumEventHandler() { @Override public void onZoomOut(ZoomOutOverMinimumEvent zoomOutEvent) { if(mainPanel.getDisplyedRecord()==null && !olMapMng.isQueryPointActive()) { olMapMng.hidePopInfo(); } } }); applicationBus.addHandler(ClosedViewDetailsEvent.TYPE, new ClosedViewDetailsEventHandler() { @Override public void onClosed(ClosedViewDetailsEvent closedViewDetailsEvent) { olMapMng.hidePopInfo(); } }); } private void performWFSQueryOnCentroid(RecordDV record) { if(record!=null) { GWT.log("record instanceof ConcessioneDV: " +(record instanceof ConcessioneDV) +" with mongo item id: "+record.getItemId()); //GeoportalDataViewerConstants.print("record instanceof ConcessioneDV: " +(record instanceof ConcessioneDV)); if(record instanceof ConcessioneDV) { ConcessioneDV concessioneDV = (ConcessioneDV) record; Double x = concessioneDV.getCentroidLong(); Double y = concessioneDV.getCentroidLat(); GWT.log("X: "+x +", Y:"+y); if(x!=null && y!=null) { Coordinate transfCoord = MapUtils.transformCoordiante(new Coordinate(x, y), GeoportalDataViewerConstants.EPSG_4326, GeoportalDataViewerConstants.EPSG_3857); GeoQuery select = olMapMng.toDataPointQuery(transfCoord); GWT.log("GeoQuery: "+select); //GeoportalDataViewerConstants.print("fireEvent QueryDataEvent"); layerManager.getLayerManagerBus().fireEvent(new QueryDataEvent(select, transfCoord, record.getItemId(), true)); }else { GeoportalDataViewerConstants.printJs("I cannot select the point one or both coordiantes are null. X: "+x +", Y:"+y); } } } } }