Content query with Java code

Enonic version: 6.8
OS: Linux

Hi,¨
For some reason I need to use java code to query content, how can I f.ex translate this JS code into Java?
var result = contentLib.query({
start: 0,
count: 10000,
sort: “pickupOrDeliveryTime ASC”,
query: “data.orderStatus = '” + orderStatus + “’”,
branch: “master”,
contentTypes: [
app.name + “:order”
]
});

Thanks for any help.

Hi.

Either create query expression the hard way:

    final CompareExpr statusExpr =
        CompareExpr.eq( FieldExpr.from( "data.orderStatus" ), ValueExpr.string( "fisk" ) );

   final FieldOrderExpr orderByPickupTime = 
        FieldOrderExpr.create( IndexPath.from( "pickupOrDeliveryTime" ), OrderExpr.Direction.ASC );

  final ContentQuery query = ContentQuery.create().
            queryExpr( QueryExpr.from( statusExpr, orderByPickupTime ) ).
            size( 10000 ).
            from( 0 ).
            build();

Or by using a query-parser:

 final String orderStatus = "fisk";
 final QueryExpr queryExpr = QueryParser.parse( "data.orderStatus = '" + orderStatus + "' order by pickupOrDeliveryTime ASC" );
 final ContentQuery query = ContentQuery.create().
            queryExpr( queryExpr ).
            size( 10000 ).
            from( 0 ).
            build();

Then, create a context with the user / repo / branch you should query, and run the query:

final FindContentIdsByQueryResult result = ContextBuilder.from( ContextAccessor.current() ).
            branch( "master" ).
            build().
            callWith( () -> contentService.find( query ) );

There exists a task for making the java query expression api more fluent, but we havent come around to that yet, but it will be improved pretty soon I hope.

3 Likes