Skip to content

Commit 25cbf40

Browse files
committed
more testing and multiple fixes to make queries on nested pojos work
1 parent 2400c44 commit 25cbf40

File tree

7 files changed

+168
-81
lines changed

7 files changed

+168
-81
lines changed

src/main/java/com/marklogic/client/impl/JerseyServices.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4233,6 +4233,7 @@ public JerseyResultIterator(RequestLogger reqlog,
42334233
super();
42344234
this.clazz = clazz;
42354235
if (partList != null && partList.size() > 0) {
4236+
this.size = partList.size();
42364237
this.reqlog = reqlog;
42374238
this.partQueue = new ConcurrentLinkedQueue<BodyPart>(
42384239
partList).iterator();

src/main/java/com/marklogic/client/impl/PojoPageImpl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class PojoPageImpl<T> extends BasicPage<T> implements PojoPage<T>, Iterat
1717
public PojoPageImpl(DocumentPage docPage, Class<T> entityClass) {
1818
super(entityClass);
1919
setStart( docPage.getStart() );
20+
setSize( docPage.size() );
2021
setPageSize( docPage.getPageSize() );
2122
setTotalSize( docPage.getTotalSize() );
2223

@@ -37,7 +38,8 @@ public boolean hasNext() {
3738
@Override
3839
public T next() {
3940
JacksonPojoHandle<T> handle = new JacksonPojoHandle<T>(entityClass);
40-
handle.getMapper().enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.WRAPPER_OBJECT);
41+
handle.getMapper().enableDefaultTyping(
42+
ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.WRAPPER_OBJECT);
4143
return docPage.nextContent(handle).get();
4244
}
4345

src/main/java/com/marklogic/client/impl/PojoQueryBuilderImpl.java

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class PojoQueryBuilderImpl<T> extends StructuredQueryBuilder implements P
3030
private HashMap<String, String> rangeIndextypes = new HashMap<String, String>();
3131
private Class<?> clazz;
3232
private String classWrapper;
33+
private boolean wrapQueries = false;
3334

3435
public PojoQueryBuilderImpl(Class<T> clazz) {
3536
super();
@@ -38,21 +39,30 @@ public PojoQueryBuilderImpl(Class<T> clazz) {
3839
this.classWrapper = clazz.getName();
3940
}
4041

42+
public PojoQueryBuilderImpl(Class<T> clazz, boolean wrapQueries) {
43+
this(clazz);
44+
this.wrapQueries = wrapQueries;
45+
}
46+
4147
private StructuredQueryBuilder.PathIndex pojoFieldPath(String pojoField) {
42-
//System.out.println("DEBUG: [PojoQueryBuilderImpl] =[" + "*[local-name()=\"" + classWrapper + "\"]/" + pojoField + "]");
4348
//return pathIndex("*[local-name()=\"" + classWrapper + "\"]/" + pojoField);
4449
return pathIndex(classWrapper + "/" + pojoField);
4550
}
4651

4752
public StructuredQueryDefinition containerQuery(String pojoField, StructuredQueryDefinition query) {
48-
return containerQuery(jsonProperty(pojoField), query);
53+
if ( wrapQueries ) {
54+
return super.containerQuery(jsonProperty(classWrapper),
55+
super.containerQuery(jsonProperty(pojoField), query));
56+
} else {
57+
return super.containerQuery(jsonProperty(pojoField), query);
58+
}
4959
}
5060
@Override
5161
public StructuredQueryDefinition containerQuery(StructuredQueryDefinition query) {
52-
return containerQuery(jsonProperty(classWrapper), query);
62+
return super.containerQuery(jsonProperty(classWrapper), query);
5363
}
5464
public PojoQueryBuilder containerQuery(String pojoField) {
55-
return new PojoQueryBuilderImpl(getType(pojoField));
65+
return new PojoQueryBuilderImpl(getType(pojoField), true);
5666
}
5767
@Override
5868
public StructuredQueryBuilder.GeospatialIndex
@@ -67,7 +77,8 @@ public StructuredQueryBuilder.GeospatialIndex geoPath(String pojoField) {
6777
return geoPath(pojoFieldPath(pojoField));
6878
}
6979
public StructuredQueryDefinition range(String pojoField,
70-
StructuredQueryBuilder.Operator operator, Object... values) {
80+
StructuredQueryBuilder.Operator operator, Object... values)
81+
{
7182
return range(pojoFieldPath(pojoField), getRangeIndexType(pojoField), operator, values);
7283
}
7384
public StructuredQueryDefinition range(String pojoField, String[] options,
@@ -77,20 +88,40 @@ public StructuredQueryDefinition range(String pojoField, String[] options,
7788
operator, values);
7889
}
7990
public StructuredQueryDefinition value(String pojoField, String... values) {
80-
return value(jsonProperty(pojoField), values);
91+
if ( wrapQueries ) {
92+
return super.containerQuery(jsonProperty(classWrapper),
93+
value(jsonProperty(pojoField), values));
94+
} else {
95+
return value(jsonProperty(pojoField), values);
96+
}
8197
}
8298
public StructuredQueryDefinition value(String pojoField, String[] options,
8399
double weight, String... values)
84100
{
85-
return value(jsonProperty(pojoField), null, options, weight, values);
101+
if ( wrapQueries ) {
102+
return super.containerQuery(jsonProperty(classWrapper),
103+
value(jsonProperty(pojoField), null, options, weight, values));
104+
} else {
105+
return value(jsonProperty(pojoField), null, options, weight, values);
106+
}
86107
}
87108
public StructuredQueryDefinition word(String pojoField, String[] words) {
88-
return super.word(jsonProperty(pojoField), words);
109+
if ( wrapQueries ) {
110+
return super.containerQuery(jsonProperty(classWrapper),
111+
super.word(jsonProperty(pojoField), words));
112+
} else {
113+
return super.word(jsonProperty(pojoField), words);
114+
}
89115
}
90116
public StructuredQueryDefinition word(String pojoField, String[] options,
91117
double weight, String... words)
92118
{
93-
return super.word(jsonProperty(pojoField), null, options, weight, words);
119+
if ( wrapQueries ) {
120+
return super.containerQuery(jsonProperty(classWrapper),
121+
super.word(jsonProperty(pojoField), null, options, weight, words));
122+
} else {
123+
return super.word(jsonProperty(pojoField), null, options, weight, words);
124+
}
94125
}
95126
public StructuredQueryDefinition word(String... words) {
96127
return super.word(jsonProperty(classWrapper), words);

src/main/java/com/marklogic/client/impl/PojoRepositoryImpl.java

Lines changed: 25 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,18 @@ public class PojoRepositoryImpl<T, ID extends Serializable>
5252
private Field idField;
5353
private String idFieldName;
5454

55-
public PojoRepositoryImpl(DatabaseClient client, Class<T> entityClass, Class<ID> idClass) {
55+
PojoRepositoryImpl(DatabaseClient client, Class<T> entityClass) {
5656
this.client = client;
5757
this.entityClass = entityClass;
58-
this.idClass = idClass;
58+
this.idClass = null;
5959
this.docMgr = client.newJSONDocumentManager();
60+
this.docMgr.setResponseFormat(Format.JSON);
6061
this.qb = new PojoQueryBuilderImpl<T>(entityClass);
62+
}
63+
64+
PojoRepositoryImpl(DatabaseClient client, Class<T> entityClass, Class<ID> idClass) {
65+
this(client, entityClass);
66+
this.idClass = idClass;
6167
findId();
6268
if ( idMethod == null && idField == null ) {
6369
throw new IllegalArgumentException("Your class " + entityClass.getName() +
@@ -77,7 +83,8 @@ public void write(T entity, Transaction transaction) {
7783
public void write(T entity, Transaction transaction, String... collections) {
7884
if ( entity == null ) return;
7985
JacksonPojoHandle contentHandle = new JacksonPojoHandle(entity);
80-
contentHandle.getMapper().enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.WRAPPER_OBJECT);
86+
contentHandle.getMapper().enableDefaultTyping(
87+
ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.WRAPPER_OBJECT);
8188
DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle();
8289
metadataHandle = metadataHandle.withCollections(entityClass.getName());
8390
if ( collections != null && collections.length > 0 ) {
@@ -125,56 +132,33 @@ public void delete(String... collections) {
125132
}
126133

127134
public T read(ID id) {
128-
return read(id, null, null);
129-
}
130-
public T read(ID id, String... collections) {
131-
return read(id, null, collections);
135+
return read(id, null);
132136
}
133137
public T read(ID id, Transaction transaction) {
134-
return read(id, transaction, null);
135-
}
136-
public T read(ID id, Transaction transaction, String... collections) {
137-
PojoPage<T> page = read(transaction, collections, id);
138+
ArrayList<ID> ids = new ArrayList<ID>();
139+
ids.add(id);
140+
PojoPage<T> page = read(ids.toArray((ID[])new Serializable[0]), transaction);
138141
if ( page == null ) return null;
139142
Iterator<T> iterator = page.iterator();
140143
if ( iterator.hasNext() ) return iterator.next();
141144
return null;
142145
}
143-
public PojoPage<T> read(ID... ids) {
144-
return read(null, null, ids);
146+
public PojoPage<T> read(ID[] ids) {
147+
return read(ids, null);
145148
}
146-
public PojoPage<T> read(Transaction transaction, ID... ids) {
147-
return read(transaction, null, ids);
148-
}
149-
public PojoPage<T> read(Transaction transaction, String[] collections, ID... ids) {
150-
long pageLength = getPageLength();
151-
QueryDefinition query = null;
152-
if ( ids != null ) {
153-
long tempPageLength = pageLength;
154-
tempPageLength = ids.length;
155-
setPageLength(tempPageLength);
156-
if ( ids.length == 1 ) {
157-
query = qb.value(idFieldName, String.valueOf(ids[0]));
158-
} else {
159-
ArrayList<StructuredQueryDefinition> idQueries =
160-
new ArrayList<StructuredQueryDefinition>(ids.length);
161-
for ( ID id : ids ) {
162-
idQueries.add( qb.value(idFieldName, String.valueOf(id)) );
163-
}
164-
query = qb.and( idQueries.toArray(new StructuredQueryDefinition[0]));
165-
}
166-
if ( collections != null ) query.setCollections(collections);
167-
} else {
168-
if ( collections != null ) query = qb.collection(collections);
149+
public PojoPage<T> read(ID[] ids, Transaction transaction) {
150+
ArrayList<String> uris = new ArrayList<String>();
151+
for ( ID id : ids ) {
152+
uris.add(createUri(id));
169153
}
170-
PojoPage page = search(wrapQuery(query), 1, null, transaction);
171-
setPageLength(pageLength);
172-
return page;
154+
DocumentPage docPage = (DocumentPage) docMgr.read(transaction, uris.toArray(new String[0]));
155+
PojoPage<T> pojoPage = new PojoPageImpl(docPage, entityClass);
156+
return pojoPage;
173157
}
174-
public PojoPage<T> read(long start) {
158+
public PojoPage<T> readAll(long start) {
175159
return search(null, start, null, null);
176160
}
177-
public PojoPage<T> read(long start, Transaction transaction) {
161+
public PojoPage<T> readAll(long start, Transaction transaction) {
178162
return search(null, start, null, transaction);
179163
}
180164

@@ -213,8 +197,6 @@ public PojoPage<T> search(QueryDefinition query, long start, SearchReadHandle se
213197
}
214198

215199
String tid = transaction == null ? null : transaction.getTransactionId();
216-
// we don't need any metadata
217-
Set metadata = null;
218200
DocumentPage docPage = docMgr.search(wrapQuery(query), start, searchHandle, transaction);
219201
docMgr.setResponseFormat(docMgrFormat);
220202
PojoPage<T> pojoPage = new PojoPageImpl(docPage, entityClass);

src/main/java/com/marklogic/client/pojo/PojoRepository.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,11 @@ public interface PojoRepository<T, ID extends Serializable> {
2323
public void delete(String... collections);
2424

2525
public T read(ID id);
26-
public T read(ID id, String... collections);
2726
public T read(ID id, Transaction transaction);
28-
public T read(ID id, Transaction transaction, String... collections);
29-
public PojoPage<T> read(ID... ids);
30-
public PojoPage<T> read(Transaction transaction, ID... ids);
31-
public PojoPage<T> read(long start);
32-
public PojoPage<T> read(long start, Transaction transaction);
27+
public PojoPage<T> read(ID[] ids);
28+
public PojoPage<T> read(ID[] ids, Transaction transaction);
29+
public PojoPage<T> readAll(long start);
30+
public PojoPage<T> readAll(long start, Transaction transaction);
3331

3432
public PojoPage<T> search(long start, String... collections);
3533
public PojoPage<T> search(long start, Transaction transaction, String... collections);

src/test/java/com/marklogic/client/test/City.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class City {
3535
private String currencyName;
3636
private long population;
3737
private int elevation;
38+
private Country country;
3839

3940
@Id
4041
public int getGeoNameId() {
@@ -162,4 +163,13 @@ public City setElevation(int elevation) {
162163
this.elevation = elevation;
163164
return this;
164165
}
166+
167+
public Country getCountry() {
168+
return country;
169+
}
170+
171+
public City setCountry(Country country) {
172+
this.country = country;
173+
return this;
174+
}
165175
}

0 commit comments

Comments
 (0)