Hi,
I am trying to implement my custom rest based datasource:
It works but compared to the provided RestDataSource, when tracking RPC requests on the Developer Console, some information is missing. For example Operation column has always "RPC" value regardless if it is "fetch" or "update". No Component info at all. There is also no DSRequest tab (only RPCRequest and Raw tabs). Obviously I missed to initialize something. Could you give me some hints here ?
Thanks,
MichalG
I am trying to implement my custom rest based datasource:
Code:
public abstract class CustomRestDataSource extends DataSource { public CustomRestDataSource () { setDataProtocol(DSProtocol.CLIENTCUSTOM); setDataFormat(DSDataFormat.XML); //? setDataTransport(RPCTransport.XMLHTTPREQUEST); setClientOnly(false); } @Override protected Object transformRequest (final DSRequest request) { final String requestId = request.getRequestId(); final DSResponse response = new DSResponse(); response.setAttribute ("clientContext", request.getAttributeAsObject ("clientContext")); response.setStatus(DSResponse.STATUS_SUCCESS); DSRequest params = new DSRequest(); params.setDataSource(this.getID()); params.setAttribute("requestId", requestId); if (request.getOperationType() != null) params.setOperationType(request.getOperationType()); if (request.getOperationId() != null) params.setOperationId(request.getOperationId()); if (request.getStartRow() != null) params.setStartRow(request.getStartRow()); if (request.getEndRow() != null) params.setEndRow(request.getEndRow()); if (request.getSortBy() != null) params.setSortBy(request.getSortBy()); if (request.getTextMatchStyle() != null) params.setTextMatchStyle(request.getTextMatchStyle()); if (request.getComponentId() != null) params.setComponentId(request.getComponentId()); DataSource ds = new DataSource(); DataSourceField dataField = new DataSourceField(); dataField.setName("data"); dataField.setMultiple(true); dataField.setTypeAsDataSource(this); DataSourceField oldValuesField = new DataSourceField(); oldValuesField.setName("oldValues"); oldValuesField.setTypeAsDataSource(this); ds.setFields(dataField, oldValuesField); ds.setTagName("request"); if (request.getData() != null) params.setData(request.getData()); if (request.getOldValues() != null) params.setOldValues(request.getOldValues()); String serializedData = ds.xmlSerialize(params.getJsObj()); RPCRequest rpcRequest = new RPCRequest(); rpcRequest.setData(serializedData); rpcRequest.setActionURL(getDataURL()); rpcRequest.setUseSimpleHttp(true); //? rpcRequest.setTransport(RPCTransport.XMLHTTPREQUEST); //? rpcRequest.setSendNoQueue(true); //? rpcRequest.setWillHandleError(false); RPCManager.sendRequest(rpcRequest, new RPCCallback() { public void execute(RPCResponse rpcResponse, Object rawData, RPCRequest rpcRequest) { _transformResponse(response, request, rawData); processResponse(requestId, response); } }); return serializedData; } protected void _transformResponse(DSResponse response, DSRequest request, Object data) { if (response.getStatus() < 0 || data==null) return; response.setStatus(parseStatus(XMLTools.selectString(data, "//status"))); if (response.getStatus() < 0) { response.setAttribute("data", XMLTools.selectString(data, "//data")); return; } else if (response.getStatus() == DSResponse.STATUS_VALIDATION_ERROR) { response.setErrors(XMLTools.toJS(XMLTools.selectNodes(data, "//errors"))); return; } else { Record[] records = recordsFromXML(XMLTools.selectNodes(data, "//data/*")); if (records != null) { response.setData(records); } String totalRowsString = XMLTools.selectString(data, "//totalRows"); if (totalRowsString != null) { response.setTotalRows(Integer.valueOf(totalRowsString)); } String startRowString = XMLTools.selectString(data, "//startRow"); if (startRowString != null) { response.setStartRow(Integer.valueOf(startRowString)); } String endRowString = XMLTools.selectString(data, "//endRow"); if (endRowString != null) { response.setEndRow(Integer.valueOf(endRowString)); } } return; } private static int parseStatus(String statusString) { int status = DSResponse.STATUS_FAILURE; if (statusString == null) return status; try { status = Integer.parseInt(statusString); } catch (Exception e) { if (statusString.equals("STATUS_FAILURE")) { status = DSResponse.STATUS_FAILURE; } else if (statusString.equals("STATUS_LOGIN_INCORRECT")) { status = DSResponse.STATUS_LOGIN_INCORRECT; } else if (statusString.equals("STATUS_LOGIN_REQUIRED")) { status = DSResponse.STATUS_LOGIN_REQUIRED; } else if (statusString.equals("STATUS_LOGIN_SUCCESS")) { status = DSResponse.STATUS_LOGIN_SUCCESS; } else if (statusString.equals("STATUS_MAX_LOGIN_ATTEMPTS_EXCEEDED")) { status = DSResponse.STATUS_MAX_LOGIN_ATTEMPTS_EXCEEDED; } else if (statusString.equals("STATUS_SERVER_TIMEOUT")) { status = DSResponse.STATUS_SERVER_TIMEOUT; } else if (statusString.equals("STATUS_SUCCESS")) { status = DSResponse.STATUS_SUCCESS; } else if (statusString.equals("STATUS_TRANSPORT_ERROR")) { status = DSResponse.STATUS_TRANSPORT_ERROR; } else if (statusString.equals("STATUS_VALIDATION_ERROR")) { status = DSResponse.STATUS_VALIDATION_ERROR; } } return status; } }
Thanks,
MichalG