Skip to content

Commit b737877

Browse files
committed
merge latest from pojoFacade branch
2 parents 569c1c0 + 25cbf40 commit b737877

File tree

10 files changed

+506
-285
lines changed

10 files changed

+506
-285
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
@@ -4237,6 +4237,7 @@ public JerseyResultIterator(RequestLogger reqlog,
42374237
super();
42384238
this.clazz = clazz;
42394239
if (partList != null && partList.size() > 0) {
4240+
this.size = partList.size();
42404241
this.reqlog = reqlog;
42414242
this.partQueue = new ConcurrentLinkedQueue<BodyPart>(
42424243
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
JacksonDatabindHandle<T> handle = new JacksonDatabindHandle<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: 69 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2012-2014 MarkLogic Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package com.marklogic.client.impl;
217

318
import com.marklogic.client.pojo.PojoQueryBuilder;
@@ -15,6 +30,7 @@ public class PojoQueryBuilderImpl<T> extends StructuredQueryBuilder implements P
1530
private HashMap<String, String> rangeIndextypes = new HashMap<String, String>();
1631
private Class<?> clazz;
1732
private String classWrapper;
33+
private boolean wrapQueries = false;
1834

1935
public PojoQueryBuilderImpl(Class<T> clazz) {
2036
super();
@@ -23,19 +39,30 @@ public PojoQueryBuilderImpl(Class<T> clazz) {
2339
this.classWrapper = clazz.getName();
2440
}
2541

42+
public PojoQueryBuilderImpl(Class<T> clazz, boolean wrapQueries) {
43+
this(clazz);
44+
this.wrapQueries = wrapQueries;
45+
}
46+
2647
private StructuredQueryBuilder.PathIndex pojoFieldPath(String pojoField) {
48+
//return pathIndex("*[local-name()=\"" + classWrapper + "\"]/" + pojoField);
2749
return pathIndex(classWrapper + "/" + pojoField);
2850
}
2951

3052
public StructuredQueryDefinition containerQuery(String pojoField, StructuredQueryDefinition query) {
31-
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+
}
3259
}
3360
@Override
3461
public StructuredQueryDefinition containerQuery(StructuredQueryDefinition query) {
35-
return containerQuery(jsonProperty(classWrapper), query);
62+
return super.containerQuery(jsonProperty(classWrapper), query);
3663
}
3764
public PojoQueryBuilder containerQuery(String pojoField) {
38-
return new PojoQueryBuilderImpl(getType(pojoField));
65+
return new PojoQueryBuilderImpl(getType(pojoField), true);
3966
}
4067
@Override
4168
public StructuredQueryBuilder.GeospatialIndex
@@ -50,7 +77,8 @@ public StructuredQueryBuilder.GeospatialIndex geoPath(String pojoField) {
5077
return geoPath(pojoFieldPath(pojoField));
5178
}
5279
public StructuredQueryDefinition range(String pojoField,
53-
StructuredQueryBuilder.Operator operator, Object... values) {
80+
StructuredQueryBuilder.Operator operator, Object... values)
81+
{
5482
return range(pojoFieldPath(pojoField), getRangeIndexType(pojoField), operator, values);
5583
}
5684
public StructuredQueryDefinition range(String pojoField, String[] options,
@@ -60,20 +88,40 @@ public StructuredQueryDefinition range(String pojoField, String[] options,
6088
operator, values);
6189
}
6290
public StructuredQueryDefinition value(String pojoField, String... values) {
63-
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+
}
6497
}
6598
public StructuredQueryDefinition value(String pojoField, String[] options,
6699
double weight, String... values)
67100
{
68-
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+
}
69107
}
70108
public StructuredQueryDefinition word(String pojoField, String[] words) {
71-
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+
}
72115
}
73116
public StructuredQueryDefinition word(String pojoField, String[] options,
74117
double weight, String... words)
75118
{
76-
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+
}
77125
}
78126
public StructuredQueryDefinition word(String... words) {
79127
return super.word(jsonProperty(classWrapper), words);
@@ -88,19 +136,19 @@ public String getRangeIndexType(String fieldName) {
88136
if ( type == null ) {
89137
Class fieldClass = getType(fieldName);
90138
if ( String.class.isAssignableFrom(fieldClass) ) {
91-
type = "string";
92-
} else if ( Integer.class.isAssignableFrom(fieldClass) ) {
93-
type = "int";
94-
} else if ( Long.class.isAssignableFrom(fieldClass) ) {
95-
type = "long";
96-
} else if ( Float.class.isAssignableFrom(fieldClass) ) {
97-
type = "float";
98-
} else if ( Double.class.isAssignableFrom(fieldClass) ) {
99-
type = "double";
139+
type = "xs:string";
140+
} else if ( Integer.TYPE.equals(fieldClass) ) {
141+
type = "xs:int";
142+
} else if ( Long.TYPE.equals(fieldClass) ) {
143+
type = "xs:long";
144+
} else if ( Float.TYPE.equals(fieldClass) ) {
145+
type = "xs:float";
146+
} else if ( Double.TYPE.equals(fieldClass) ) {
147+
type = "xs:double";
100148
} else if ( Number.class.isAssignableFrom(fieldClass) ) {
101-
type = "decimal";
149+
type = "xs:decimal";
102150
} else if ( Date.class.isAssignableFrom(fieldClass) ) {
103-
type = "dateTime";
151+
type = "xs:dateTime";
104152
}
105153
if ( type == null ) {
106154
throw new IllegalArgumentException("Field " + fieldName + " is not a native Java type");
@@ -114,8 +162,8 @@ public Class getType(String fieldName) {
114162
Class fieldClass = types.get(fieldName);
115163
if ( fieldClass == null ) {
116164
// figure out the type of the java field
117-
String initCapPojoField = fieldName.substring(1,2).toUpperCase() +
118-
fieldName.substring(2);
165+
String initCapPojoField = fieldName.substring(0,1).toUpperCase() +
166+
fieldName.substring(1);
119167
try {
120168
fieldClass = clazz.getField(fieldName).getType();
121169
} catch(NoSuchFieldException e) {
@@ -162,5 +210,3 @@ public Class getType(String fieldName) {
162210
return fieldClass;
163211
}
164212
}
165-
166-

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

Lines changed: 32 additions & 46 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
JacksonDatabindHandle contentHandle = new JacksonDatabindHandle(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 ) {
@@ -93,12 +100,16 @@ public boolean exists(ID id) {
93100
}
94101

95102
public long count() {
96-
return count((String) null);
103+
return count((QueryDefinition) null);
97104
}
98105

99106
public long count(String... collections) {
100-
if ( collections == null ) return 0l;
101-
return count(wrapQuery(qb.collection(collections)));
107+
if ( collections != null && collections.length > 0 ) {
108+
if ( collections.length > 1 || collections[0] != null ) {
109+
return count(qb.collection(collections));
110+
}
111+
}
112+
return count((QueryDefinition) null);
102113
}
103114
public long count(QueryDefinition query) {
104115
long pageLength = getPageLength();
@@ -121,56 +132,33 @@ public void delete(String... collections) {
121132
}
122133

123134
public T read(ID id) {
124-
return read(id, null, null);
125-
}
126-
public T read(ID id, String... collections) {
127-
return read(id, null, collections);
135+
return read(id, null);
128136
}
129137
public T read(ID id, Transaction transaction) {
130-
return read(id, transaction, null);
131-
}
132-
public T read(ID id, Transaction transaction, String... collections) {
133-
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);
134141
if ( page == null ) return null;
135142
Iterator<T> iterator = page.iterator();
136143
if ( iterator.hasNext() ) return iterator.next();
137144
return null;
138145
}
139-
public PojoPage<T> read(ID... ids) {
140-
return read(null, null, ids);
146+
public PojoPage<T> read(ID[] ids) {
147+
return read(ids, null);
141148
}
142-
public PojoPage<T> read(Transaction transaction, ID... ids) {
143-
return read(transaction, null, ids);
144-
}
145-
public PojoPage<T> read(Transaction transaction, String[] collections, ID... ids) {
146-
long pageLength = getPageLength();
147-
QueryDefinition query = null;
148-
if ( ids != null ) {
149-
long tempPageLength = pageLength;
150-
tempPageLength = ids.length;
151-
setPageLength(tempPageLength);
152-
if ( ids.length == 1 ) {
153-
query = qb.value(idFieldName, String.valueOf(ids[0]));
154-
} else {
155-
ArrayList<StructuredQueryDefinition> idQueries =
156-
new ArrayList<StructuredQueryDefinition>(ids.length);
157-
for ( ID id : ids ) {
158-
idQueries.add( qb.value(idFieldName, String.valueOf(id)) );
159-
}
160-
query = qb.and( idQueries.toArray(new StructuredQueryDefinition[0]));
161-
}
162-
if ( collections != null ) query.setCollections(collections);
163-
} else {
164-
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));
165153
}
166-
PojoPage page = search(wrapQuery(query), 1, null, transaction);
167-
setPageLength(pageLength);
168-
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;
169157
}
170-
public PojoPage<T> read(long start) {
158+
public PojoPage<T> readAll(long start) {
171159
return search(null, start, null, null);
172160
}
173-
public PojoPage<T> read(long start, Transaction transaction) {
161+
public PojoPage<T> readAll(long start, Transaction transaction) {
174162
return search(null, start, null, transaction);
175163
}
176164

@@ -209,8 +197,6 @@ public PojoPage<T> search(QueryDefinition query, long start, SearchReadHandle se
209197
}
210198

211199
String tid = transaction == null ? null : transaction.getTransactionId();
212-
// we don't need any metadata
213-
Set metadata = null;
214200
DocumentPage docPage = docMgr.search(wrapQuery(query), start, searchHandle, transaction);
215201
docMgr.setResponseFormat(docMgrFormat);
216202
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/main/java/com/marklogic/client/query/StructuredQueryBuilder.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,12 +1847,20 @@ class GeoElementImpl extends IndexImpl implements GeospatialIndex {
18471847
}
18481848
@Override
18491849
void innerSerialize(XMLStreamWriter serializer) throws Exception {
1850-
if (parent != null) {
1850+
if (parent != null && parent instanceof ElementImpl) {
18511851
ElementImpl parentImpl = (ElementImpl) parent;
18521852
serializeNamedIndex(serializer, "parent", parentImpl.qname, parentImpl.name);
1853+
} else if (parent != null && parent instanceof IndexImpl) {
1854+
IndexImpl parentImpl = (IndexImpl) parent;
1855+
parentImpl.innerSerialize(serializer);
1856+
}
1857+
if ( element instanceof ElementImpl ) {
1858+
ElementImpl elementImpl = (ElementImpl) element;
1859+
serializeNamedIndex(serializer, "element", elementImpl.qname, elementImpl.name);
1860+
} else if ( element instanceof IndexImpl ) {
1861+
IndexImpl indexImpl = (IndexImpl) element;
1862+
indexImpl.innerSerialize(serializer);
18531863
}
1854-
ElementImpl elementImpl = (ElementImpl) element;
1855-
serializeNamedIndex(serializer, "element", elementImpl.qname, elementImpl.name);
18561864
}
18571865
}
18581866
class GeoElementPairImpl extends IndexImpl implements GeospatialIndex {

0 commit comments

Comments
 (0)