All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.backendless.Geo Maven / Gradle / Ivy

There is a newer version: 3.1.0
Show newest version
/*
 * ********************************************************************************************************************
 *  

* BACKENDLESS.COM CONFIDENTIAL *

* ******************************************************************************************************************** *

* Copyright 2012 BACKENDLESS.COM. All Rights Reserved. *

* NOTICE: All information contained herein is, and remains the property of Backendless.com and its suppliers, * if any. The intellectual and technical concepts contained herein are proprietary to Backendless.com and its * suppliers and may be covered by U.S. and Foreign Patents, patents in process, and are protected by trade secret * or copyright law. Dissemination of this information or reproduction of this material is strictly forbidden * unless prior written permission is obtained from Backendless.com. *

* ******************************************************************************************************************** */ package com.backendless; import com.backendless.async.callback.AsyncCallback; import com.backendless.core.responder.AdaptingResponder; import com.backendless.core.responder.policy.CollectionAdaptingPolicy; import com.backendless.exceptions.BackendlessException; import com.backendless.exceptions.BackendlessFault; import com.backendless.exceptions.ExceptionMessage; import com.backendless.geo.*; import weborb.types.Types; import java.util.Arrays; import java.util.List; import java.util.Map; public final class Geo { private final static String GEO_MANAGER_SERVER_ALIAS = "com.backendless.services.geo.GeoService"; private final static String DEFAULT_CATEGORY_NAME = "Default"; private static final Geo instance = new Geo(); static Geo getInstance() { return instance; } private Geo() { Types.addClientClassMapping( "com.backendless.geo.model.GeoPoint", GeoPoint.class ); Types.addClientClassMapping( "com.backendless.geo.model.GeoCluster", GeoCluster.class ); Types.addClientClassMapping( "com.backendless.geo.model.SearchMatchesResult", SearchMatchesResult.class ); Types.addClientClassMapping( "com.backendless.geo.BackendlessGeoQuery", BackendlessGeoQuery.class ); Types.addClientClassMapping( "com.backendless.geo.model.GeoCategory", GeoCategory.class ); Types.addClientClassMapping( "com.backendless.geo.Units", Units.class ); } public GeoCategory addCategory( String categoryName ) throws BackendlessException { checkCategoryName( categoryName ); return (GeoCategory) Invoker.invokeSync( GEO_MANAGER_SERVER_ALIAS, "addCategory", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), categoryName } ); } public void addCategory( String categoryName, AsyncCallback responder ) { try { checkCategoryName( categoryName ); Invoker.invokeAsync( GEO_MANAGER_SERVER_ALIAS, "addCategory", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), categoryName }, responder ); } catch( Throwable e ) { if( responder != null ) responder.handleFault( new BackendlessFault( e ) ); } } public boolean deleteCategory( String categoryName ) throws BackendlessException { checkCategoryName( categoryName ); return (Boolean) Invoker.invokeSync( GEO_MANAGER_SERVER_ALIAS, "deleteCategory", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), categoryName } ); } public void deleteCategory( String categoryName, AsyncCallback responder ) { try { checkCategoryName( categoryName ); Invoker.invokeAsync( GEO_MANAGER_SERVER_ALIAS, "deleteCategory", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), categoryName }, responder ); } catch( Throwable e ) { if( responder != null ) responder.handleFault( new BackendlessFault( e ) ); } } public GeoPoint savePoint( final double latitude, final double longitude, final Map metadata ) throws BackendlessException { return savePoint( latitude, longitude, null, metadata ); } public GeoPoint savePoint( double latitude, double longitude, List categories, final Map metadata ) throws BackendlessException { return savePoint( new GeoPoint( latitude, longitude, categories, metadata ) ); } public GeoPoint savePoint( GeoPoint geoPoint ) throws BackendlessException { if( geoPoint == null ) throw new IllegalArgumentException( ExceptionMessage.NULL_GEOPOINT ); checkCoordinates( geoPoint.getLatitude(), geoPoint.getLongitude() ); String remoteMethod = geoPoint.getObjectId() == null ? "addPoint" : "updatePoint"; return (GeoPoint) Invoker.invokeSync( GEO_MANAGER_SERVER_ALIAS, remoteMethod, new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), geoPoint } ); } public void savePoint( final double latitude, final double longitude, final Map metadata, AsyncCallback responder ) { savePoint( latitude, longitude, null, metadata, responder ); } public void savePoint( double latitude, double longitude, List categories, Map metadata, AsyncCallback responder ) { savePoint( new GeoPoint( latitude, longitude, categories, metadata ), responder ); } public void savePoint( GeoPoint geoPoint, AsyncCallback responder ) { try { if( geoPoint == null ) throw new IllegalArgumentException( ExceptionMessage.NULL_GEOPOINT ); checkCoordinates( geoPoint.getLatitude(), geoPoint.getLongitude() ); String remoteMethod = geoPoint.getObjectId() == null ? "addPoint" : "updatePoint"; Invoker.invokeAsync( GEO_MANAGER_SERVER_ALIAS, remoteMethod, new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), geoPoint }, responder ); } catch( Throwable e ) { if( responder != null ) responder.handleFault( new BackendlessFault( e ) ); } } public void removePoint( GeoPoint geoPoint ) { Invoker.invokeSync( GEO_MANAGER_SERVER_ALIAS, "removePoint", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), geoPoint.getObjectId() } ); } public void removePoint( GeoPoint geoPoint, AsyncCallback responder ) { Invoker.invokeAsync( GEO_MANAGER_SERVER_ALIAS, "removePoint", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), geoPoint.getObjectId() }, responder ); } public BackendlessCollection getPoints( BackendlessGeoQuery geoQuery ) throws BackendlessException { checkGeoQuery( geoQuery ); CollectionAdaptingPolicy adaptingPolicy = new CollectionAdaptingPolicy(); BackendlessCollection result = (BackendlessCollection) Invoker.invokeSync( GEO_MANAGER_SERVER_ALIAS, "getPoints", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), geoQuery }, new AdaptingResponder( GeoPoint.class, adaptingPolicy ) ); result.setQuery( geoQuery ); result.setType( GeoPoint.class ); if( geoQuery.getDpp() != null && geoQuery.getDpp() > 0 ) { setReferenceToCluster( result ); } return result; } public void getPoints( final BackendlessGeoQuery geoQuery, final AsyncCallback> responder ) { try { checkGeoQuery( geoQuery ); CollectionAdaptingPolicy adaptingPolicy = new CollectionAdaptingPolicy(); Invoker.invokeAsync( GEO_MANAGER_SERVER_ALIAS, "getPoints", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), geoQuery }, new AsyncCallback>() { @Override public void handleResponse( BackendlessCollection response ) { response.setQuery( geoQuery ); response.setType( GeoPoint.class ); if( geoQuery.getDpp() != null && geoQuery.getDpp() > 0 ) { setReferenceToCluster( response ); } if( responder != null ) responder.handleResponse( response ); } @Override public void handleFault( BackendlessFault fault ) { responder.handleFault( fault ); } }, new AdaptingResponder( GeoPoint.class, adaptingPolicy ) ); } catch( Throwable e ) { if( responder != null ) responder.handleFault( new BackendlessFault( e ) ); } } public BackendlessCollection relativeFind( BackendlessGeoQuery geoQuery ) throws BackendlessException { if( geoQuery == null ) throw new IllegalArgumentException( ExceptionMessage.NULL_GEO_QUERY ); if( geoQuery.getRelativeFindMetadata().isEmpty() || geoQuery.getRelativeFindPercentThreshold() == 0 ) throw new IllegalArgumentException( ExceptionMessage.INCONSISTENT_GEO_RELATIVE ); CollectionAdaptingPolicy adaptingPolicy = new CollectionAdaptingPolicy(); BackendlessCollection result = (BackendlessCollection) Invoker.invokeSync( GEO_MANAGER_SERVER_ALIAS, "relativeFind", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), geoQuery }, new AdaptingResponder( SearchMatchesResult.class, adaptingPolicy ) ); result.setQuery( geoQuery ); result.setType( SearchMatchesResult.class ); return result; } public void relativeFind( final BackendlessGeoQuery geoQuery, final AsyncCallback> responder ) { try { if( geoQuery == null ) throw new IllegalArgumentException( ExceptionMessage.NULL_GEO_QUERY ); if( geoQuery.getRelativeFindMetadata().isEmpty() || geoQuery.getRelativeFindPercentThreshold() == 0 ) throw new IllegalArgumentException( ExceptionMessage.INCONSISTENT_GEO_RELATIVE ); CollectionAdaptingPolicy adaptingPolicy = new CollectionAdaptingPolicy(); Invoker.invokeAsync( GEO_MANAGER_SERVER_ALIAS, "relativeFind", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), geoQuery }, new AsyncCallback>() { @Override public void handleResponse( BackendlessCollection response ) { response.setQuery( geoQuery ); response.setType( SearchMatchesResult.class ); if( responder != null ) responder.handleResponse( response ); } @Override public void handleFault( BackendlessFault fault ) { responder.handleFault( fault ); } }, new AdaptingResponder( SearchMatchesResult.class, adaptingPolicy ) ); } catch( Throwable e ) { if( responder != null ) responder.handleFault( new BackendlessFault( e ) ); } } public List getCategories() throws BackendlessException { GeoCategory[] response = Invoker.invokeSync( GEO_MANAGER_SERVER_ALIAS, "getCategories", new Object[] { Backendless.getApplicationId(), Backendless.getVersion() } ); return Arrays.asList( response ); } public void getCategories( final AsyncCallback> responder ) { Invoker.invokeAsync( GEO_MANAGER_SERVER_ALIAS, "getCategories", new Object[] { Backendless.getApplicationId(), Backendless.getVersion() }, new AsyncCallback() { @Override public void handleResponse( GeoCategory[] response ) { if( responder != null ) responder.handleResponse( Arrays.asList( response ) ); } @Override public void handleFault( BackendlessFault fault ) { if( responder != null ) responder.handleFault( fault ); } } ); } public GeoPoint loadMetadata( final GeoPoint geoPoint ) { Map metadata; if( geoPoint instanceof GeoCluster ) { metadata = (Map) Invoker.invokeSync( GEO_MANAGER_SERVER_ALIAS, "loadMetadata", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), geoPoint.getObjectId(), ((GeoCluster) geoPoint).getGeoQuery() } ); } else { metadata = (Map) Invoker.invokeSync( GEO_MANAGER_SERVER_ALIAS, "loadMetadata", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), geoPoint.getObjectId(), null } ); } geoPoint.setMetadata( metadata ); return geoPoint; } public void loadMetadata( final GeoPoint geoPoint, final AsyncCallback responder ) { AsyncCallback> invoker = new AsyncCallback>() { @Override public void handleResponse( Map response ) { geoPoint.setMetadata( response ); if( responder != null ) responder.handleResponse( geoPoint ); } @Override public void handleFault( BackendlessFault fault ) { if( responder != null ) responder.handleFault( fault ); } }; if( geoPoint instanceof GeoCluster ) { Invoker.invokeAsync( GEO_MANAGER_SERVER_ALIAS, "loadMetadata", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), geoPoint.getObjectId(), ((GeoCluster) geoPoint).getGeoQuery() }, invoker ); } else { Invoker.invokeAsync( GEO_MANAGER_SERVER_ALIAS, "loadMetadata", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), geoPoint.getObjectId(), null }, invoker ); } } public BackendlessCollection loadGeoPoints( final GeoCluster geoCluster ) { CollectionAdaptingPolicy adaptingPolicy = new CollectionAdaptingPolicy(); BackendlessCollection result = (BackendlessCollection) Invoker.invokeSync( GEO_MANAGER_SERVER_ALIAS, "loadGeoPoints", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), geoCluster.getObjectId(), geoCluster.getGeoQuery() }, new AdaptingResponder( GeoPoint.class, adaptingPolicy ) ); result.setQuery( geoCluster.getGeoQuery() ); result.setType( GeoPoint.class ); return result; } public void loadGeoPoints( final GeoCluster geoCluster, final AsyncCallback> responder ) { try { CollectionAdaptingPolicy adaptingPolicy = new CollectionAdaptingPolicy(); Invoker.invokeAsync( GEO_MANAGER_SERVER_ALIAS, "loadGeoPoints", new Object[] { Backendless.getApplicationId(), Backendless.getVersion(), geoCluster.getObjectId(), geoCluster.getGeoQuery() }, new AsyncCallback>() { @Override public void handleResponse( BackendlessCollection response ) { response.setQuery( geoCluster.getGeoQuery() ); response.setType( GeoPoint.class ); if( responder != null ) responder.handleResponse( response ); } @Override public void handleFault( BackendlessFault fault ) { responder.handleFault( fault ); } }, new AdaptingResponder( GeoPoint.class, adaptingPolicy ) ); } catch( Throwable e ) { if( responder != null ) responder.handleFault( new BackendlessFault( e ) ); } } private void setReferenceToCluster( BackendlessCollection collection ) { BackendlessGeoQuery geoQuery = new ProtectedBackendlessGeoQuery( (BackendlessGeoQuery) collection.getQuery() ); for( GeoPoint geoPoint : collection.getData() ) { if( geoPoint instanceof GeoCluster ) { ((GeoCluster) geoPoint).setGeoQuery( geoQuery ); } } } private void checkCategoryName( String categoryName ) throws BackendlessException { if( categoryName == null ) throw new IllegalArgumentException( ExceptionMessage.NULL_CATEGORY_NAME ); if( categoryName.equals( "" ) ) throw new IllegalArgumentException( ExceptionMessage.NULL_CATEGORY_NAME ); if( categoryName.equals( DEFAULT_CATEGORY_NAME ) ) throw new IllegalArgumentException( ExceptionMessage.DEFAULT_CATEGORY_NAME ); } private void checkCoordinates( Double latitude, Double longitude ) throws BackendlessException { if( latitude == null && longitude == null ) throw new IllegalArgumentException( ExceptionMessage.NULL_COORDINATES ); if( latitude == null ) throw new IllegalArgumentException( ExceptionMessage.NULL_LATITUDE ); if( longitude == null ) throw new IllegalArgumentException( ExceptionMessage.NULL_LONGITUDE ); if( latitude > 90 || latitude < -90 ) throw new IllegalArgumentException( ExceptionMessage.WRONG_LATITUDE_VALUE ); if( longitude > 180 || longitude < -180 ) throw new IllegalArgumentException( ExceptionMessage.WRONG_LONGITUDE_VALUE ); } private void checkGeoQuery( BackendlessGeoQuery geoQuery ) throws BackendlessException { if( geoQuery == null ) throw new IllegalArgumentException( ExceptionMessage.NULL_GEO_QUERY ); if( geoQuery.getSearchRectangle() != null ) { if( geoQuery.getSearchRectangle().length != 4 ) throw new IllegalArgumentException( ExceptionMessage.WRONG_SEARCH_RECTANGLE_QUERY ); if( geoQuery.getRadius() != null || geoQuery.getLatitude() != null || geoQuery.getLongitude() != null ) throw new IllegalArgumentException( ExceptionMessage.INCONSISTENT_GEO_QUERY ); } else if( geoQuery.getRadius() != null ) { if( geoQuery.getRadius() == null || geoQuery.getRadius() <= 0 ) throw new IllegalArgumentException( ExceptionMessage.WRONG_RADIUS ); if( geoQuery.getLatitude() == null ) throw new IllegalArgumentException( ExceptionMessage.WRONG_LATITUDE_VALUE ); if( geoQuery.getLongitude() == null ) throw new IllegalArgumentException( ExceptionMessage.WRONG_LONGITUDE_VALUE ); checkCoordinates( geoQuery.getLatitude(), geoQuery.getLongitude() ); if( geoQuery.getUnits() == null ) throw new IllegalArgumentException( ExceptionMessage.NULL_UNIT ); } else if( geoQuery.getCategories() == null && geoQuery.getMetadata() == null ) throw new IllegalArgumentException( ExceptionMessage.WRONG_GEO_QUERY ); if( geoQuery.getCategories() != null ) for( String categoryName : geoQuery.getCategories() ) checkCategoryName( categoryName ); if( geoQuery.getOffset() < 0 ) throw new IllegalArgumentException( ExceptionMessage.WRONG_OFFSET ); if( geoQuery.getPageSize() < 0 ) throw new IllegalArgumentException( ExceptionMessage.WRONG_PAGE_SIZE ); if( geoQuery.getDpp() != null ) { if( geoQuery.getDpp() < 0 || (geoQuery.getClusterGridSize() != null && geoQuery.getClusterGridSize() < 0) ) { throw new IllegalArgumentException( ExceptionMessage.WRONG_CLUSTERISATION_QUERY ); } } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy