Search in sources :

Example 1 with PersonInfo

use of common.advanced.PersonInfo in project tesb-rt-se by Talend.

the class RESTClient method useSearchService.

/**
 * SearchService is a service which shares the information about Persons with the PersonService.
 * It lets users search for individual people using simple or complex search expressions.
 * The interaction with this service also verifies that the JAX-RS server is capable of supporting multiple
 * root resource classes
 */
private void useSearchService() throws Exception {
    System.out.println("Searching...");
    WebClient wc = WebClient.create("http://localhost:" + port + "/services/personservice/search");
    WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(10000000L);
    wc.accept(MediaType.APPLICATION_XML);
    // Moves to "/services/personservice/search"
    wc.path("person");
    SearchConditionBuilder builder = SearchConditionBuilder.instance();
    System.out.println("Find people with the name Fred or Lorraine:");
    String query = builder.is("name").equalTo("Fred").or().is("name").equalTo("Lorraine").query();
    findPersons(wc, query);
    System.out.println("Find all people who are no more than 30 years old");
    query = builder.is("age").lessOrEqualTo(30).query();
    findPersons(wc, query);
    System.out.println("Find all people who are older than 28 and whose father name is John");
    query = builder.is("age").greaterThan(28).and("fatherName").equalTo("John").query();
    findPersons(wc, query);
    System.out.println("Find all people who have children with name Fred");
    query = builder.is("childName").equalTo("Fred").query();
    findPersons(wc, query);
    // Moves to "/services/personservice/personinfo"
    wc.reset().accept(MediaType.APPLICATION_XML);
    wc.path("personinfo");
    System.out.println("Find all people younger than 40 using JPA2 Tuples");
    query = builder.is("age").lessThan(40).query();
    // Use URI path component to capture the query expression
    wc.path(query);
    Collection<? extends PersonInfo> personInfos = wc.getCollection(PersonInfo.class);
    for (PersonInfo pi : personInfos) {
        System.out.println("ID : " + pi.getId());
    }
    wc.close();
}
Also used : PersonInfo(common.advanced.PersonInfo) WebClient(org.apache.cxf.jaxrs.client.WebClient) SearchConditionBuilder(org.apache.cxf.jaxrs.ext.search.client.SearchConditionBuilder)

Example 2 with PersonInfo

use of common.advanced.PersonInfo in project tesb-rt-se by Talend.

the class PersonInfoStorage method getTypedQueryTuple.

public List<PersonInfo> getTypedQueryTuple(SearchContext context, String expression) {
    // Get search condition encapsulating the query expression
    SearchCondition<Person> filter = getSearchCondition(context, expression);
    // Initialise JPA2 visitor which can convert the captured search expression
    // into JPA2 TypedQuery
    JPACriteriaQueryVisitor<Person, Tuple> jpa = new JPACriteriaQueryVisitor<Person, Tuple>(em, Person.class, Tuple.class);
    // Convert
    filter.accept(jpa);
    // Shape the response data with selections and Tuple
    List<SingularAttribute<Person, ?>> selections = new ArrayList<SingularAttribute<Person, ?>>();
    selections.add(Person_.id);
    jpa.selectTuple(selections);
    // Get CriteriaQuery and create TypedQuery
    CriteriaQuery<Tuple> cquery = jpa.getQuery();
    TypedQuery<Tuple> typedQuery = em.createQuery(cquery);
    // Run the query
    List<Tuple> tuples = typedQuery.getResultList();
    // Return the results
    List<PersonInfo> infos = new ArrayList<PersonInfo>(tuples.size());
    for (Tuple tuple : tuples) {
        infos.add(new PersonInfo(tuple.get(Person_.id.getName(), Long.class)));
    }
    return infos;
}
Also used : PersonInfo(common.advanced.PersonInfo) JPACriteriaQueryVisitor(org.apache.cxf.jaxrs.ext.search.jpa.JPACriteriaQueryVisitor) ArrayList(java.util.ArrayList) SingularAttribute(javax.persistence.metamodel.SingularAttribute) Person(common.advanced.Person) Tuple(javax.persistence.Tuple)

Aggregations

PersonInfo (common.advanced.PersonInfo)2 Person (common.advanced.Person)1 ArrayList (java.util.ArrayList)1 Tuple (javax.persistence.Tuple)1 SingularAttribute (javax.persistence.metamodel.SingularAttribute)1 WebClient (org.apache.cxf.jaxrs.client.WebClient)1 SearchConditionBuilder (org.apache.cxf.jaxrs.ext.search.client.SearchConditionBuilder)1 JPACriteriaQueryVisitor (org.apache.cxf.jaxrs.ext.search.jpa.JPACriteriaQueryVisitor)1