{
MediaType type = getVariant( response.getRequest(), ENGLISH, supportedMediaTypes ).getMediaType();
if( MediaType.APPLICATION_JSON.equals( type ) )
{
response.setEntity( new WriterRepresentation( MediaType.APPLICATION_JSON )
{
@Override
public void write( Writer writer )
throws IOException
{
try
{
JSONWriter json = new JSONWriter( writer );
Table tableValue = (Table) result;
// Parse parameters
String tqx = response.getRequest().getResourceRef().getQueryAsForm().getFirstValue( "tqx" );
String reqId = null;
if( tqx != null )
{
String[] params = tqx.split( ";" );
for( String param : params )
{
String[] p = param.split( ":" );
String key = p[ 0 ];
String value = p[ 1 ];
if( key.equals( "reqId" ) )
{
reqId = value;
}
}
}
json.object().key( "version" ).value( "0.6" );
if( reqId != null )
{
json.key( "reqId" ).value( reqId );
}
json.key( "status" ).value( "ok" );
json.key( "table" ).object();
List<Column> columnList = tableValue.cols().get();
json.key( "cols" ).array();
for( Column columnValue : columnList )
{
json.object().
key( "id" ).value( columnValue.id().get() ).
key( "label" ).value( columnValue.label().get() ).
key( "type" ).value( columnValue.columnType().get() ).
endObject();
}
json.endArray();
json.key( "rows" ).array();
for( Row rowValue : tableValue.rows().get() )
{
json.object();
json.key( "c" ).array();
int idx = 0;
for( Cell cellValue : rowValue.c().get() )
{
json.object();
Object value = cellValue.v().get();
if( columnList.get( idx )
.columnType()
.get()
.equals( Table.DATETIME ) && value != null )
{
value = Dates.toUtcString( (Date) value );
}
else if( columnList.get( idx )
.columnType()
.get()
.equals( Table.DATE ) && value != null )
{
value = new SimpleDateFormat( "yyyy-MM-dd" ).format( (Date) value );
}
else if( columnList.get( idx )
.columnType()
.get()
.equals( Table.TIME_OF_DAY ) && value != null )
{
value = new SimpleDateFormat( "HH:mm:ss" ).format( (Date) value );
}
if( value != null )
{
json.key( "v" ).value( value );
}
if( cellValue.f().get() != null )
{
json.key( "f" ).value( cellValue.f().get() );
}
json.endObject();
idx++;
}
json.endArray();
json.endObject();
}
json.endArray();
json.endObject();
json.endObject();
}
catch( JSONException e )
{
throw new IOException( e );
}
}
} );
return true;
}
else if( MediaType.TEXT_HTML.equals( type ) )
{
Representation rep = new WriterRepresentation( MediaType.TEXT_HTML )
{
@Override
public void write( Writer writer )
throws IOException
{
Map<String, Object> context = new HashMap<String, Object>();
context.put( "request", response.getRequest() );
context.put( "response", response );
context.put( "result", result );
try
{
cfg.getTemplate( "table.htm" ).process( context, writer );
}
catch( TemplateException e )
{
throw new IOException( e );
}
}
};
rep.setCharacterSet( CharacterSet.UTF_8 );
response.setEntity( rep );
return true;
}
}
return false;