Search in sources :

Example 1 with Table

use of org.qi4j.library.rest.common.table.Table in project qi4j-sdk by Qi4j.

the class TableResponseReader method readResponse.

@Override
public Object readResponse(Response response, Class<?> resultType) throws ResourceException {
    if (response.getEntity().getMediaType().equals(MediaType.APPLICATION_JSON) && Table.class.isAssignableFrom(resultType)) {
        String jsonValue = response.getEntityAsText();
        try {
            JSONObject jsonObject = new JSONObject(jsonValue);
            JSONObject table = jsonObject.getJSONObject("table");
            TableBuilder builder = new TableBuilder(module);
            JSONArray cols = table.getJSONArray("cols");
            for (int i = 0; i < cols.length(); i++) {
                JSONObject col = cols.getJSONObject(i);
                builder.column(col.optString("id"), col.getString("label"), col.getString("type"));
            }
            JSONArray rows = table.getJSONArray("rows");
            for (int i = 0; i < rows.length(); i++) {
                builder.row();
                JSONObject row = rows.getJSONObject(i);
                JSONArray cells = row.getJSONArray("c");
                for (int j = 0; j < cells.length(); j++) {
                    JSONObject cell = cells.getJSONObject(j);
                    Object value = cell.opt("v");
                    String formatted = cell.optString("f");
                    if (cols.getJSONObject(j).getString("type").equals("datetime") && value != null)
                        value = Dates.fromString(value.toString());
                    else if (cols.getJSONObject(j).getString("type").equals("date") && value != null)
                        try {
                            value = new SimpleDateFormat("yyyy-MM-dd").parse(value.toString());
                        } catch (ParseException e) {
                            throw new ResourceException(e);
                        }
                    else if (cols.getJSONObject(j).getString("type").equals("timeofday") && value != null)
                        try {
                            value = new SimpleDateFormat("HH:mm:ss").parse(value.toString());
                        } catch (ParseException e) {
                            throw new ResourceException(e);
                        }
                    builder.cell(value, formatted);
                }
                builder.endRow();
            }
            return builder.newTable();
        } catch (JSONException e) {
            throw new ResourceException(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY, e);
        }
    }
    return null;
}
Also used : Table(org.qi4j.library.rest.common.table.Table) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) ResourceException(org.restlet.resource.ResourceException) ParseException(java.text.ParseException) TableBuilder(org.qi4j.library.rest.common.table.TableBuilder) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 Table (org.qi4j.library.rest.common.table.Table)1 TableBuilder (org.qi4j.library.rest.common.table.TableBuilder)1 ResourceException (org.restlet.resource.ResourceException)1