|
22 | 22 | import com.marklogic.client.query.StructuredQueryDefinition; |
23 | 23 | import com.marklogic.client.util.IterableNamespaceContext; |
24 | 24 |
|
| 25 | +/** Extends StructuredQueryBuilder with convenience methods specific to working with pojos. |
| 26 | + * The goal of {@link com.marklogic.client.pojo the pojo facade} is to simplify working with |
| 27 | + * custom pojos. PojoQueryBuilder keeps all the powerful queries available via |
| 28 | + * StructuredQueryBuilder while enabling queries across objects persisted using |
| 29 | + * {@link PojoRepository}. |
| 30 | + * |
| 31 | + * <p>For methods which accept a "pojoField" argument and for {@link #geoField geoField}, we are refering to |
| 32 | + * fields (or properties) appropriate for |
| 33 | + * <a href="http://docs.oracle.com/javase/tutorial/javabeans/">JavaBeans</a>, |
| 34 | + * including fields accessible via public getters and setters, or public fields.</p> |
| 35 | + * |
| 36 | + * |
| 37 | + * <p>Where StructuredQueryBuilder accepts StructuredQueryBuilder.TextIndex as a first argument |
| 38 | + * to |
| 39 | + * {@link #value(StructuredQueryBuilder.TextIndex, String...) value(TextIndex, String...)} |
| 40 | + * and |
| 41 | + * {@link #word(StructuredQueryBuilder.TextIndex, String...) word(TextIndex, String...)} |
| 42 | + * methods, |
| 43 | + * PojoQueryBuilder adds shortcut methods which accept as the first argument a String name of the |
| 44 | + * pojoField. Similarly, PojoQueryBuilder accepts String pojoField arguments wherever |
| 45 | + * StructuredQueryBuilder accepts StructuredQueryBuilder.Element, |
| 46 | + * StructuredQueryBuilder.Attribute, and StructuredQueryBuilder.PathIndex |
| 47 | + * as arguments to |
| 48 | + * {@link #geoAttributePair(StructuredQueryBuilder.Element, StructuredQueryBuilder.Attribute, |
| 49 | + * StructuredQueryBuilder.Attribute) |
| 50 | + * geoAttributePair(Element, Attribute, Attribute)}, |
| 51 | + * {@link #geoElement(StructuredQueryBuilder.Element) |
| 52 | + * geoElement(Element)}, |
| 53 | + * {@link #geoElement(StructuredQueryBuilder.Element, StructuredQueryBuilder.Element) |
| 54 | + * geoElement(Element, Element)}, |
| 55 | + * {@link #geoElementPair(StructuredQueryBuilder.Element, StructuredQueryBuilder.Element, |
| 56 | + * StructuredQueryBuilder.Element) |
| 57 | + * geoElementPair(Element, Element, Element)}, |
| 58 | + * {@link #geoPath(StructuredQueryBuilder.PathIndex) |
| 59 | + * geoPath(PathIndex)} |
| 60 | + * </p> |
| 61 | + * |
| 62 | + * <p>Here are a couple examples. Without the pojo facade you might persist your products using |
| 63 | + * {@link com.marklogic.client.io.JacksonDatabindHandle JacksonDatabindHandle} and query the |
| 64 | + * json property thusly:</p> |
| 65 | + * <pre>{@code StructuredQueryBuilder sqb = new StructuredQueryBuilder(); |
| 66 | + * QueryDefinition query = sqb.value(sqb.jsonProperty("productId"), 12345);}</pre> |
| 67 | + * |
| 68 | + * <p>If you use {@link PojoRepository} to persist your products, you can query more simply:</p> |
| 69 | + * <pre>{@code PojoQueryBuilder pqb = pojoRepository.getQueryBuilder(); |
| 70 | + * QueryDefinition query = pqb.value("productId", 12345);}</pre> |
| 71 | + * |
| 72 | + * <p>Similarly, without the pojo facade you might persist your pojos using |
| 73 | + * {@link com.marklogic.client.io.JAXBHandle JAXBHandle} and if they |
| 74 | + * have a geoPosition field which is an object with latitude and longitude pojoFields |
| 75 | + * (which persist as elements) you might query them thusly:</p> |
| 76 | + * <pre>{@code StructuredQueryBuilder sqb = new StructuredQueryBuilder(); |
| 77 | + * StructuredQueryBuilder.GeospatialIndex geoIdx = sqb.geoElementPair( |
| 78 | + * sqb.element("geoPosition"), sqb.element("latitude"), sqb.element("longitude"));}</pre> |
| 79 | + * |
| 80 | + * <p>But if you use {@link PojoRepository} to persist your pojos with a latitude and longitude |
| 81 | + * pojoFields, you can query them more simply:</p> |
| 82 | + * <pre>{@code PojoQueryBuilder pqb = pojoRepository.getQueryBuilder(); |
| 83 | + * StructuredQueryBuilder.GeospatialIndex geoIdx = |
| 84 | + * pqb.geoPair("latitude", "longitude");}</pre> |
| 85 | + * |
| 86 | + * <p>As custom pojos may have nested pojos, PojoQueryBuilder also makes it easy to query |
| 87 | + * those nested pojos. For example, if you had the following classes:</p> |
| 88 | + * <pre> class City { |
| 89 | + * {@literal @}Id int id; |
| 90 | + * Country country; |
| 91 | + * int getId(); |
| 92 | + * void setId(int id); |
| 93 | + * Country getCountry(); |
| 94 | + * void setCountry(Country country); |
| 95 | + * } |
| 96 | + * class Country { |
| 97 | + * String continent; |
| 98 | + * String getContinent(); |
| 99 | + * void setContinent(); |
| 100 | + * }</pre> |
| 101 | + * |
| 102 | + * <p>That is, you have a pojo class City with a field "country" of type |
| 103 | + * Country, you could query fields on the nested country thusly:</p> |
| 104 | + * <pre>{@code PojoRepository<City, Integer> cities = |
| 105 | + * databaseClient.newPojoRepository(City.class, Integer.class); |
| 106 | + * PojoQueryBuilder citiesQb = cities.getQueryBuilder(); |
| 107 | + * PojoQueryBuilder countriesQb = citiesQb.containerQuery("country"); |
| 108 | + * QueryDefinition query = countriesQb.value("continent", "EU"); }</pre> |
| 109 | + */ |
25 | 110 | public interface PojoQueryBuilder<T> { |
26 | 111 | public StructuredQueryDefinition containerQuery(String pojoField, |
27 | 112 | StructuredQueryDefinition query); |
28 | 113 | public StructuredQueryDefinition containerQuery(StructuredQueryDefinition query); |
29 | 114 | public PojoQueryBuilder<T> containerQuery(String pojoField); |
30 | 115 | public StructuredQueryBuilder.GeospatialIndex |
31 | 116 | geoPair(String latitudeFieldName, String longitudeFieldName); |
| 117 | + /** |
| 118 | + * NOTE: Since the pojo facade abstracts away the persistence details, "field" here refers |
| 119 | + * to a pojoField like all other convenience methods in PojoQueryBuilder,not |
| 120 | + * a MarkLogic field specified on the server. |
| 121 | + * @param pojoField the name of a field (or getter or setter) on class T |
| 122 | + */ |
32 | 123 | public StructuredQueryBuilder.GeospatialIndex |
33 | 124 | geoField(String pojoField); |
34 | 125 | public StructuredQueryBuilder.GeospatialIndex |
|
0 commit comments