SmartGWT – RestDataSource and PathParam

abril 21, 2017

To integrate a REST web service with SmartGWT DataSource you have client side data integration mechanisms.

I’ve used this to connect an existing REST web service with SmartGWT controls.

Nevertheless, we have a REST web service wich receives a Path Parameter to update an entity.

    @PUT
    @Path("/proveedores/{id}")
    public Response updateProveedor(@PathParam("id")String id, String request){
        Response response = null;
        try{
            SCRequest<Proveedor> scRequest = Jackson.mapper().readValue(request, new TypeReference<SCRequest<Proveedor>>() {
            });
            Proveedor proveedor = Proveedor.guardar(scRequest.getData());
            response = Response.ok(Jackson.mapper().writeValueAsString(new SCResponse(0, proveedor))).build();
        } catch (Exception e){
            String error = "Ocurrio un problema al actualizar el registro del proveedor";
            log.error(error, e);
            response = Response.serverError().entity(error).build();
        }
        return response;
    }

we need to configure the URL of the RestDataSource dynamically like so in the transformRequest method

public class ProveedoresRestDataSource extends RestDataSource {
    private static final Logger log = Logger.getLogger("ProveedoresRestDataSource");

    public static final ProveedoresRestDataSource INSTANCE = new ProveedoresRestDataSource();
    public static final String API_REST_WEB_GWT_COMPRAS_CATALOGOS_PROVEEDORES = "api/rest/web-gwt-compras/catalogos/proveedores";

    public ProveedoresRestDataSource() {
        setDataURL(API_REST_WEB_GWT_COMPRAS_CATALOGOS_PROVEEDORES);
        setDataFormat(DSDataFormat.JSON);

        OperationBinding create = new OperationBinding(DSOperationType.ADD, API_REST_WEB_GWT_COMPRAS_CATALOGOS_PROVEEDORES);
        create.setDataProtocol(DSProtocol.POSTMESSAGE);
        OperationBinding retrieve = new OperationBinding(DSOperationType.FETCH, API_REST_WEB_GWT_COMPRAS_CATALOGOS_PROVEEDORES);
        OperationBinding update = new OperationBinding(DSOperationType.UPDATE, API_REST_WEB_GWT_COMPRAS_CATALOGOS_PROVEEDORES);
        update.setDataProtocol(DSProtocol.POSTMESSAGE);
        DSRequest updateProperties = new DSRequest();
        updateProperties.setHttpMethod("PUT");
        update.setRequestProperties(updateProperties);
        setOperationBindings(create, retrieve, update);

        DataSourceField fldId = new DataSourceTextField("id");
        fldId.setPrimaryKey(true);
...
        setFields(fldId, ...);
    }

    @Override
    protected Object transformRequest(DSRequest dsRequest) {
        if(DSOperationType.UPDATE.equals(dsRequest.getOperationType())){
            String id = JSOHelper.getAttribute(dsRequest.getData(), "id");
            dsRequest.setActionURL(API_REST_WEB_GWT_COMPRAS_CATALOGOS_PROVEEDORES + "/" + id);
        }
        return super.transformRequest(dsRequest);
    }
}

sources:
http://helpdesk.objects.com.au/java/how-do-i-generate-dynamic-urls-with-smartgwt-and-restdatasource
http://www.smartclient.com/smartgwt/javadoc/

Anuncios

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s

A %d blogueros les gusta esto: