org.broadleafcommerce.openadmin.client.datasource.GwtRpcDataSource Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of broadleaf-open-admin-platform Show documentation
Show all versions of broadleaf-open-admin-platform Show documentation
BroadleafCommerce Open Admin Platform
/*
* Copyright 2008-2012 the original author or authors.
*
* 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.broadleafcommerce.openadmin.client.datasource;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.types.DSProtocol;
/**
* Data source with ability to communicate with server by GWT RPC.
* SmartClient natively supports data protocol "clientCustom". This protocol
* means that communication with server should be implemented in
* transformRequest (DSRequest request)
method. Here is a few
* things to note on transformRequest
implementation:
*
* DSResponse
object has to be created and
* processResponse (requestId, response)
must be called to finish
* data request. requestId
should be taken from original
* DSRequest.getRequestId ()
.
* - "clientContext" attribute from
DSRequest
should be copied to
* DSResponse
.
* - In case of failure
DSResponse
should contain at least "status"
* attribute with error code (<0).
* - In case of success
DSResponse
should contain at least "data"
* attribute with operation type specific data:
*
* - FETCH -
ListGridRecord[]
retrieved records.
* - ADD -
ListGridRecord[]
with single added record.
* Operation is called on every newly added record.
* - UPDATE -
ListGridRecord[]
with single updated record.
* Operation is called on every updated record.
* - REMOVE -
ListGridRecord[]
with single removed record.
* Operation is called on every removed record.
*
*
*
*
* @author Aleksandras Novikovas
* @author System Tier
* @version 1.0
*/
public abstract class GwtRpcDataSource
extends DataSource {
/**
* Creates new data source which communicates with server by GWT RPC.
* It is normal server side SmartClient data source with data protocol
* set to DSProtocol.CLIENTCUSTOM
("clientCustom" - natively
* supported by SmartClient but should be added to smartGWT) and with data
* format DSDataFormat.CUSTOM
.
*/
public GwtRpcDataSource (String name) {
super(name);
setDataProtocol (DSProtocol.CLIENTCUSTOM);
setDataFormat (DSDataFormat.CUSTOM);
setClientOnly (false);
}
/**
* Executes request to server.
*
* @param request DSRequest
being processed.
* @return Object
data from original request.
*/
@Override
protected Object transformRequest (DSRequest request) {
// use simple HTTP request without SmartGWT-specific request encoding
request.setUseSimpleHttp(true);
String requestId = request.getRequestId ();
DSResponse response = new DSResponse ();
response.setAttribute ("clientContext", request.getAttributeAsObject ("clientContext"));
// assume success
response.setStatus (0);
switch (request.getOperationType ()) {
case FETCH:
executeFetch (requestId, request, response);
break;
case ADD:
executeAdd (requestId, request, response);
break;
case UPDATE:
executeUpdate (requestId, request, response);
break;
case REMOVE:
executeRemove (requestId, request, response);
break;
default:
// operation not implemented
break;
}
return request.getData ();
}
/**
* Executed on FETCH
operation. processResponse (requestId, response)
* should be called when operation completes (either successful or failure).
*
* @param requestId String
extracted from DSRequest.getRequestId ()
.
* @param request DSRequest
being processed.
* @param response DSResponse
. setData (list)
should be called on
* successful execution of this method. setStatus (<0)
should be called
* on failure.
*/
protected abstract void executeFetch (String requestId, DSRequest request, DSResponse response);
/**
* Executed on ADD
operation. processResponse (requestId, response)
* should be called when operation completes (either successful or failure).
*
* @param requestId String
extracted from DSRequest.getRequestId ()
.
* @param request DSRequest
being processed. request.getData ()
* contains record should be added.
* @param response DSResponse
. setData (list)
should be called on
* successful execution of this method. Array should contain single element representing
* added row. setStatus (<0)
should be called on failure.
*/
protected abstract void executeAdd (String requestId, DSRequest request, DSResponse response);
/**
* Executed on UPDATE
operation. processResponse (requestId, response)
* should be called when operation completes (either successful or failure).
*
* @param requestId String
extracted from DSRequest.getRequestId ()
.
* @param request DSRequest
being processed. request.getData ()
* contains record should be updated.
* @param response DSResponse
. setData (list)
should be called on
* successful execution of this method. Array should contain single element representing
* updated row. setStatus (<0)
should be called on failure.
*/
protected abstract void executeUpdate (String requestId, DSRequest request, DSResponse response);
/**
* Executed on REMOVE
operation. processResponse (requestId, response)
* should be called when operation completes (either successful or failure).
*
* @param requestId String
extracted from DSRequest.getRequestId ()
.
* @param request DSRequest
being processed. request.getData ()
* contains record should be removed.
* @param response DSResponse
. setData (list)
should be called on
* successful execution of this method. Array should contain single element representing
* removed row. setStatus (<0)
should be called on failure.
*/
protected abstract void executeRemove (String requestId, DSRequest request, DSResponse response);
}