Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2014 JBoss Inc Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.dashbuilder.dataset.client;
 
 import java.util.List;
 import java.util.Map;
 
 import  org.dashbuilder.dataset.DataSet;
 import  org.dashbuilder.dataset.DataSetLookup;
 import  org.dashbuilder.dataset.DataSetMetadata;
 import  org.dashbuilder.dataset.DataSetLookupService;
 import  org.dashbuilder.dataset.events.DataSetBackendRegisteredEvent;
 import  org.dashbuilder.dataset.events.DataSetModifiedEvent;
 import  org.uberfire.workbench.events.NotificationEvent;
 
 import static org.uberfire.commons.validation.PortablePreconditions.checkNotNull;
 import static org.uberfire.workbench.events.NotificationEvent.NotificationType.*;

Proxy interface to the list of available DataSetManager implementations. It hides to client widgets where the data sets are stored and how they are fetched and processed.
 
 public class DataSetLookupClient {
 
     public static DataSetLookupClient get() {
         IOCBeanDef<DataSetLookupClientbeanDef = beans.iterator().next();
         return beanDef.getInstance();
     }
 
     @Inject
 
     @Inject
     private Event<NotificationEvent> notification;
 
     @Inject
     private Event<DataSetModifiedEvent> dataSetModifiedEvent;

    
The service caller used to lookup data sets from the backend.
 
     private Caller<DataSetLookupService> dataSetLookupService = null;

    
A cache of DataSetMetadata instances
 
     private Map<DataSetLookup,DataSetMetadata> remoteMetadataMap = new HashMap<DataSetLookup,DataSetMetadata>();

    
If enabled then remote data set can be pushed to clients.
 
     private boolean pushRemoteDataSetEnabled = true;

    
Maximum size (in kbytes) a data set may have in order to be pushed to clients.
 
     private int pushRemoteDataSetMaxSize = 1024;

    
It holds a set of data set push requests in progress.
 
 
     public boolean isPushRemoteDataSetEnabled() {
         return ;
     }

    
Enable/disable the ability to push remote data sets from server.
 
     public void setPushRemoteDataSetEnabled(boolean pushRemoteDataSetEnabled) {
         this. = pushRemoteDataSetEnabled;
    }
    public int getPushRemoteDataSetMaxSize() {
        return ;
    }

    
Set the maximum size (in kbytes) a data set may have in order to be pushed to clients.
    public void setPushRemoteDataSetMaxSize(int pushRemoteDataSetMaxSize) {
        this. = pushRemoteDataSetMaxSize;
    }

    
The DataSetLookupService instance is disabled by default. Those client modules interested in activate the data set backend should set an instance.
    public void setLookupService(Caller<DataSetLookupService> dataSetLookupService) {
        this. = dataSetLookupService;
    }

    
Fetch the metadata instance for the specified data set.

Parameters:
request The data set lookup request
Throws:
Exception It there is an unexpected error trying to execute the lookup request.
    public void fetchMetadata(final DataSetLookup requestfinal DataSetMetadataCallback listenerthrows Exception {
        DataSetMetadata metadata = .lookupDataSetMetadata(request);
        if (metadata != null) {
            listener.callback(metadata);
        }
        else if ( != null) {
            if (.containsKey(request)) {
                listener.callback(.get(request));
            } else {
                .call(
                    new RemoteCallback<DataSetMetadata>() {
                    public void callback(DataSetMetadata result) {
                        if (result == nulllistener.notFound();
                        else {
                            .put(requestresult);
                            listener.callback(result);
                        }
                    }}).lookupDataSetMetadata(request);
            }
        }
        else {
            listener.notFound();
        }
    }

    
Process the specified data set lookup request.

Parameters:
request The data set lookup request
Throws:
Exception It there is an unexpected error trying to execute the lookup request.
    public void lookupDataSet(final DataSetLookup requestfinal DataSetReadyCallback listenerthrows Exception {
        // Look always into the client data set manager.
        if (.getDataSet(request.getDataSetUUID()) != null) {
            DataSet dataSet = .lookupDataSet(request);
            listener.callback(dataSet);
        }
        // If the data set is not in client, then look up remotely (only if the remote access is available).
        else if ( != null) {
            // First of all, get the target data set estimated size.
            final DataSetLookup lookupSourceDataSet = new DataSetLookup(request.getDataSetUUID());
            fetchMetadata(lookupSourceDataSetnew DataSetMetadataCallback() {
                public void callback(DataSetMetadata metatada) {
                    // Push smalls data sets to client.
                    if ( && metatada.getEstimatedSize() < ) {
                        // Check if a push is already in progress.
                        // (This is necessary in order to avoid repeating multiple push requests over the same data set).
                        DataSetPushHandler pushHandler = .get(request.getDataSetUUID());
                        if (pushHandler == null) {
                            // Create a push handler.
                            pushHandler = new DataSetPushHandler(metatada);
                            // Send the lookup request to the server...
                            _lookupDataSet(lookupSourceDataSetpushHandler);
                        }
                        // Register the lookup request into the current handler.
                        pushHandler.registerLookup(requestlistener);
                    }
                    // Lookup the remote data set otherwise.
                    else {
                        _lookupDataSet(requestlistener);
                    }
                }
                // Data set metadata not found
                public void notFound() {
                    listener.notFound();
                }
            });
        }
        // Data set not found on client.
        else {
            listener.notFound();
        }
    }
    private void _lookupDataSet(DataSetLookup requestfinal DataSetReadyCallback listener) {
        try {
            .call(
                new RemoteCallback<DataSet>() {
                    public void callback(DataSet result) {
                        if (result == nulllistener.notFound();
                        else listener.callback(result);
                    }}).lookupDataSet(request);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    private class DataSetPushHandler implements DataSetReadyCallback {
        private DataSetMetadata dataSetMetadata = null;
        private DataSetPushHandler(DataSetMetadata metadata) {
            this. = metadata;
            .put(.getUUID(), this);
            .fire(
                    new NotificationEvent( ..dsLookupClient_loadingFromServer( Integer.toStringmetadata.getEstimatedSize() ) ), INFO)
            );
        }
        public void registerLookup(DataSetLookup lookupDataSetReadyCallback listener) {
            .add(new DataSetLookupListenerPair(lookuplistener));
        }
        public void callback(DataSet dataSet) {
            .remove(.getUUID());
            .registerDataSet(dataSet);
            .fire(new NotificationEvent( ..dsLookupClient_successfullyLoaded(), SUCCESS));
            for (DataSetLookupListenerPair pair : ) {
                DataSet result = .lookupDataSet(pair.lookup);
                pair.listener.callback(result);
            }
        }
        public void notFound() {
            .remove(.getUUID());
            .fire(new NotificationEvent(..dsLookupClient_dsNotFoundInServer(), ERROR));
            for (DataSetLookupListenerPair pair : ) {
                pair.listener.notFound();
            }
        }
    }
    private class DataSetLookupListenerPair {
        DataSetLookup lookup;
        private DataSetLookupListenerPair(DataSetLookup lookupDataSetReadyCallback listener) {
            this. = lookup;
            this. = listener;
        }
    }
    // Catch backend events
    private void onDataSetRegistered(@Observes DataSetBackendRegisteredEvent event) {
        // Remove stale data. This will force next lookup requests to push a refreshed data set.
        checkNotNull("event"event);
        String uuid = event.getDataSetMetadata().getUUID();
        DataSet clientDataSet = .removeDataSet(uuid);
        // If the data set existed on client then an out of date event is fired.
        if (clientDataSet != null) {
            .fire(new DataSetModifiedEvent(clientDataSet.getMetadata()));
        }
    }
New to GrepCode? Check out our FAQ X