Skip to content

Commit 75cdc06

Browse files
committed
initial working implementation of the pojo facade
1 parent 97f8311 commit 75cdc06

File tree

6 files changed

+47
-84
lines changed

6 files changed

+47
-84
lines changed

src/main/java/com/marklogic/client/DatabaseClient.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.marklogic.client;
1717

1818
import java.io.OutputStream;
19+
import java.io.Serializable;
1920

2021
import com.marklogic.client.admin.ServerConfigurationManager;
2122
import com.marklogic.client.document.BinaryDocumentManager;
@@ -28,6 +29,7 @@
2829
import com.marklogic.client.query.QueryManager;
2930
import com.marklogic.client.alerting.RuleManager;
3031
import com.marklogic.client.util.RequestLogger;
32+
import com.marklogic.client.pojo.PojoRepository;
3133

3234
/**
3335
* A Database Client instantiates document and query managers and other objects
@@ -110,6 +112,14 @@ public interface DatabaseClient {
110112
*/
111113
public ServerConfigurationManager newServerConfigManager();
112114

115+
/**
116+
* Creates a PojoRepository specific to the specified class and id types.
117+
* The PojoRepository provides a facade for persisting, retrieving, and
118+
* querying data contained in Java objects. Annotations are required to
119+
* identify the id field and any fields for which you wish to create indexes.
120+
**/
121+
public <T, ID extends Serializable> PojoRepository<T, ID> newPojoRepository(Class<T> clazz, Class<ID> idClass);
122+
113123
/**
114124
* Initializes a manager for a extension resource.
115125
*

src/main/java/com/marklogic/client/document/DocumentManager.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.marklogic.client.io.marker.DocumentPatchHandle;
3131
import com.marklogic.client.io.marker.SearchReadHandle;
3232
import com.marklogic.client.query.QueryDefinition;
33+
import com.marklogic.client.query.QueryManager.QueryView;
3334

3435
/**
3536
* A Document Manager provides database operations on a document.
@@ -387,6 +388,14 @@ public <T extends R> T read(DocumentDescriptor desc, DocumentMetadataReadHandle
387388

388389
public void setPageLength(long length);
389390

391+
public Format getResponseFormat();
392+
393+
public void setResponseFormat(Format format);
394+
395+
public QueryView getSearchView();
396+
397+
public void setSearchView(QueryView view);
398+
390399
public DocumentWriteSet newWriteSet();
391400

392401
public void write(DocumentWriteSet writeSet);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public class BasicPage<T> implements Page<T> {
2525
private long pageSize;
2626
private long totalSize;
2727

28+
protected BasicPage(Class<T> type) {
29+
}
30+
2831
public BasicPage(Iterable<T> iterable, long start, long pageSize, long totalSize) {
2932
this.iterable = iterable;
3033
this.start = start;

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.marklogic.client.impl;
1717

1818
import java.io.OutputStream;
19+
import java.io.Serializable;
1920

2021
import org.slf4j.Logger;
2122
import org.slf4j.LoggerFactory;
@@ -36,6 +37,8 @@
3637
import com.marklogic.client.document.TextDocumentManager;
3738
import com.marklogic.client.Transaction;
3839
import com.marklogic.client.document.XMLDocumentManager;
40+
import com.marklogic.client.pojo.PojoRepository;
41+
import com.marklogic.client.impl.PojoRepositoryImpl;
3942

4043
public class DatabaseClientImpl implements DatabaseClient {
4144
static final private Logger logger = LoggerFactory.getLogger(DatabaseClientImpl.class);
@@ -120,6 +123,11 @@ public ServerConfigurationManager newServerConfigManager() {
120123
configMgr.setHandleRegistry(getHandleRegistry());
121124
return configMgr;
122125
}
126+
@Override
127+
public <T, ID extends Serializable> PojoRepository<T, ID> newPojoRepository(Class<T> clazz, Class<ID> idClass) {
128+
return new PojoRepositoryImpl<T, ID>(this, clazz, idClass);
129+
130+
}
123131

124132
@Override
125133
public RequestLogger newLogger(OutputStream out) {

src/main/java/com/marklogic/client/io/JacksonHandle.java

Lines changed: 10 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -16,43 +16,31 @@
1616
package com.marklogic.client.io;
1717

1818
import java.io.ByteArrayInputStream;
19-
import java.io.ByteArrayOutputStream;
2019
import java.io.IOException;
2120
import java.io.InputStream;
2221
import java.io.InputStreamReader;
2322
import java.io.OutputStream;
2423
import java.io.OutputStreamWriter;
25-
import java.io.UnsupportedEncodingException;
2624

2725
import com.fasterxml.jackson.core.JsonParseException;
2826
import com.fasterxml.jackson.databind.JsonMappingException;
2927
import com.fasterxml.jackson.databind.JsonNode;
30-
import com.fasterxml.jackson.databind.ObjectMapper;
28+
3129
import com.marklogic.client.MarkLogicIOException;
32-
import com.marklogic.client.io.BaseHandle;
33-
import com.marklogic.client.io.Format;
34-
import com.marklogic.client.io.OutputStreamSender;
35-
import com.marklogic.client.io.marker.BufferableHandle;
3630
import com.marklogic.client.io.marker.ContentHandle;
3731
import com.marklogic.client.io.marker.ContentHandleFactory;
38-
import com.marklogic.client.io.marker.JSONReadHandle;
39-
import com.marklogic.client.io.marker.JSONWriteHandle;
40-
import com.marklogic.client.io.marker.StructureReadHandle;
41-
import com.marklogic.client.io.marker.StructureWriteHandle;
32+
import com.marklogic.client.impl.JacksonBaseHandle;
4233

4334
/**
4435
* An adapter for using the Jackson Open Source library for JSON; represents
4536
* JSON content as a Jackson JsonNode for reading or writing. Enables reading and
4637
* writing JSON documents, JSON structured search, and other JSON input and output.
4738
*/
4839
public class JacksonHandle
49-
extends BaseHandle<InputStream, OutputStreamSender>
50-
implements OutputStreamSender, BufferableHandle, ContentHandle<JsonNode>,
51-
JSONReadHandle, JSONWriteHandle,
52-
StructureReadHandle, StructureWriteHandle
40+
extends JacksonBaseHandle
41+
implements ContentHandle<JsonNode>
5342
{
5443
private JsonNode content;
55-
private ObjectMapper mapper;
5644

5745
/**
5846
* Creates a factory to create a JacksonHandle instance for a JSON node.
@@ -83,7 +71,6 @@ public <C> ContentHandle<C> newHandle(Class<C> type) {
8371
*/
8472
public JacksonHandle() {
8573
super();
86-
super.setFormat(Format.JSON);
8774
setResendable(true);
8875
}
8976
/**
@@ -95,16 +82,6 @@ public JacksonHandle(JsonNode content) {
9582
set(content);
9683
}
9784

98-
/**
99-
* Returns the mapper used to construct node objects from JSON.
100-
* @return the JSON mapper.
101-
*/
102-
public ObjectMapper getMapper() {
103-
if (mapper == null)
104-
mapper = new ObjectMapper();
105-
return mapper;
106-
}
107-
10885
/**
10986
* Returns the root node of the JSON tree.
11087
* @return the JSON root node.
@@ -131,63 +108,22 @@ public JacksonHandle with(JsonNode content) {
131108
return this;
132109
}
133110

134-
/**
135-
* Restricts the format to JSON.
136-
*/
137-
@Override
138-
public void setFormat(Format format) {
139-
if (format != Format.JSON)
140-
throw new IllegalArgumentException(
141-
"JacksonHandle supports the JSON format only");
142-
}
143-
144111
@Override
145112
public void fromBuffer(byte[] buffer) {
146113
if (buffer == null || buffer.length == 0)
147-
content = null;
114+
set(null);
148115
else
149116
receiveContent(new ByteArrayInputStream(buffer));
150117
}
151118
@Override
152-
public byte[] toBuffer() {
153-
try {
154-
if (content == null)
155-
return null;
156-
157-
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
158-
write(buffer);
159-
160-
return buffer.toByteArray();
161-
} catch (IOException e) {
162-
throw new MarkLogicIOException(e);
163-
}
164-
}
165-
166-
/**
167-
* Returns the JSON tree as a string.
168-
*/
169-
@Override
170-
public String toString() {
171-
try {
172-
return new String(toBuffer(),"UTF-8");
173-
} catch (UnsupportedEncodingException e) {
174-
throw new MarkLogicIOException(e);
175-
}
176-
}
177-
178-
@Override
179-
protected Class<InputStream> receiveAs() {
180-
return InputStream.class;
181-
}
182-
@Override
183119
protected void receiveContent(InputStream content) {
184120
if (content == null)
185121
return;
186122

187123
try {
188-
this.content = getMapper().readValue(
124+
set( getMapper().readValue(
189125
new InputStreamReader(content, "UTF-8"), JsonNode.class
190-
);
126+
));
191127
} catch (JsonParseException e) {
192128
throw new MarkLogicIOException(e);
193129
} catch (JsonMappingException e) {
@@ -198,14 +134,11 @@ protected void receiveContent(InputStream content) {
198134

199135
}
200136
@Override
201-
protected OutputStreamSender sendContent() {
202-
if (content == null) {
203-
throw new IllegalStateException("No document to write");
204-
}
205-
return this;
137+
protected boolean hasContent() {
138+
return content != null;
206139
}
207140
@Override
208141
public void write(OutputStream out) throws IOException {
209-
getMapper().writeValue(new OutputStreamWriter(out, "UTF-8"), content);
142+
getMapper().writeValue(new OutputStreamWriter(out, "UTF-8"), get());
210143
}
211144
}

src/main/java/com/marklogic/client/query/StructuredQueryBuilder.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ public interface ElementAttribute extends RangeIndex, TextIndex {
146146
public interface Field extends RangeIndex, TextIndex {
147147
}
148148
/**
149-
* A JSONKey represents a key in JSON database documents.
149+
* A JSONProperty represents a key in JSON database documents.
150150
*/
151-
public interface JSONKey extends ContainerIndex, RangeIndex, TextIndex {
151+
public interface JSONProperty extends Element, ContainerIndex, RangeIndex, TextIndex {
152152
}
153153
/**
154154
* A PathIndex represents an index defined with an XPath
@@ -614,8 +614,8 @@ public Field field(String name) {
614614
* @param name the name of the JSON key
615615
* @return the identifier for the JSON key
616616
*/
617-
public JSONKey jsonKey(String name) {
618-
return new JSONKeyImpl(name);
617+
public JSONProperty jsonProperty(String name) {
618+
return new JSONPropertyImpl(name);
619619
}
620620
/**
621621
* Identifies a path index to match with a query.
@@ -1814,14 +1814,14 @@ void innerSerialize(XMLStreamWriter serializer) throws Exception {
18141814
serializer.writeEndElement();
18151815
}
18161816
}
1817-
class JSONKeyImpl extends IndexImpl implements JSONKey {
1817+
class JSONPropertyImpl extends IndexImpl implements JSONProperty {
18181818
String name;
1819-
JSONKeyImpl(String name) {
1819+
JSONPropertyImpl(String name) {
18201820
this.name = name;
18211821
}
18221822
@Override
18231823
void innerSerialize(XMLStreamWriter serializer) throws Exception {
1824-
writeText(serializer, "json-key", name);
1824+
writeText(serializer, "json-property", name);
18251825
}
18261826
}
18271827
class PathIndexImpl extends IndexImpl implements PathIndex {

0 commit comments

Comments
 (0)