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

com.gwtplatform.dispatch.rpc.client.RpcDispatchAsync Maven / Gradle / Ivy

There is a newer version: 1.6
Show newest version
/**
 * Copyright 2013 ArcBees 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 com.gwtplatform.dispatch.rpc.client;

import javax.inject.Inject;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.gwtplatform.dispatch.rpc.shared.Action;
import com.gwtplatform.dispatch.rpc.shared.DispatchAsync;
import com.gwtplatform.dispatch.rpc.shared.DispatchServiceAsync;
import com.gwtplatform.dispatch.rpc.shared.Result;
import com.gwtplatform.dispatch.shared.DispatchRequest;

/**
 * This class is the default implementation of {@link com.gwtplatform.dispatch.rpc.shared.DispatchAsync}, which is
 * essentially the client-side access to the {@link com.gwtplatform.dispatch.rpc.server.Dispatch} class on the
 * server-side.
 */
public class RpcDispatchAsync implements DispatchAsync {
    private final DispatchServiceAsync realService;
    private final RpcDispatchCallFactory rpcDispatchCallFactory;
    private final String baseUrl;

    @Inject
    RpcDispatchAsync(RpcDispatchCallFactory rpcDispatchCallFactory,
                     DispatchServiceAsync dispatchService) {
        this.realService = dispatchService;
        this.rpcDispatchCallFactory = rpcDispatchCallFactory;

        String entryPointUrl = ((ServiceDefTarget) realService).getServiceEntryPoint();
        if (entryPointUrl == null) {
            this.baseUrl = "";
        } else {
            this.baseUrl = entryPointUrl;
        }
    }

    @Override
    public , R extends Result> DispatchRequest execute(A action, AsyncCallback callback) {
        prepareExecute(action);

        RpcDispatchExecuteCall call = rpcDispatchCallFactory.create(action, callback);
        return call.execute();
    }

    @Override
    public , R extends Result> DispatchRequest undo(A action, R result,
                                                                        AsyncCallback callback) {
        prepareUndo(action);

        RpcDispatchUndoCall call = rpcDispatchCallFactory.create(action, result, callback);
        return call.execute();
    }

    protected , R extends Result> void prepareExecute(A action) {
        prepareService((ServiceDefTarget) realService, baseUrl, action.getServiceName());
    }

    protected , R extends Result> void prepareUndo(A action) {
        prepareService((ServiceDefTarget) realService, baseUrl, action.getServiceName());
    }

    protected void prepareService(ServiceDefTarget service, String moduleUrl, String relativeServiceUrl) {
        service.setServiceEntryPoint(moduleUrl + relativeServiceUrl);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy