1515 */
1616package com .marklogic .client .impl ;
1717
18+ import java .util .ArrayList ;
1819import java .util .HashSet ;
1920import java .util .Set ;
2021
2122import org .slf4j .Logger ;
2223import org .slf4j .LoggerFactory ;
2324
25+ import com .fasterxml .jackson .databind .JsonNode ;
26+
2427import com .marklogic .client .DatabaseClientFactory .HandleFactoryRegistry ;
2528import com .marklogic .client .FailedRequestException ;
2629import com .marklogic .client .ForbiddenUserException ;
3033import com .marklogic .client .document .DocumentManager ;
3134import com .marklogic .client .document .DocumentMetadataPatchBuilder ;
3235import com .marklogic .client .document .DocumentUriTemplate ;
36+ import com .marklogic .client .document .DocumentPage ;
37+ import com .marklogic .client .document .DocumentRecord ;
38+ import com .marklogic .client .document .DocumentWriteOperation ;
39+ import com .marklogic .client .document .DocumentWriteSet ;
3340import com .marklogic .client .document .ServerTransform ;
3441import com .marklogic .client .impl .DocumentMetadataPatchBuilderImpl .DocumentPatchHandleImpl ;
3542import com .marklogic .client .io .Format ;
3946import com .marklogic .client .io .marker .DocumentMetadataReadHandle ;
4047import com .marklogic .client .io .marker .DocumentMetadataWriteHandle ;
4148import com .marklogic .client .io .marker .DocumentPatchHandle ;
49+ import com .marklogic .client .io .marker .SearchReadHandle ;
50+ import com .marklogic .client .io .JacksonHandle ;
51+ import com .marklogic .client .io .SearchHandle ;
52+ import com .marklogic .client .query .QueryDefinition ;
53+ import com .marklogic .client .query .QueryManager .QueryView ;
4254import com .marklogic .client .util .RequestParameters ;
4355
4456abstract class DocumentManagerImpl <R extends AbstractReadHandle , W extends AbstractWriteHandle >
4557 extends AbstractLoggingManager
4658 implements DocumentManager <R , W >
4759{
60+ static final private long DEFAULT_PAGE_LENGTH = 50 ;
61+
4862 static final private Logger logger = LoggerFactory .getLogger (DocumentManagerImpl .class );
4963
50- final private Set <Metadata > processedMetadata ;
64+ private boolean isProcessedMetadataModified = false ;
65+ final private Set <Metadata > processedMetadata = new HashSet <Metadata >() {
66+ public boolean add (Metadata e ) {
67+ isProcessedMetadataModified = true ;
68+ return super .add (e );
69+ }
70+ };
71+ {
72+ processedMetadata .add (Metadata .ALL );
73+ // we need to know if the user modifies after us
74+ isProcessedMetadataModified = false ;
75+ }
76+
5177
5278 private RESTServices services ;
5379 private Format contentFormat ;
5480 private HandleFactoryRegistry handleRegistry ;
5581 private ServerTransform readTransform ;
5682 private ServerTransform writeTransform ;
5783 private String forestName ;
84+ private long pageLength = DEFAULT_PAGE_LENGTH ;
85+ private QueryView searchView = QueryView .RESULTS ;
86+ private Format responseFormat = Format .XML ;
5887
5988 DocumentManagerImpl (RESTServices services , Format contentFormat ) {
6089 super ();
@@ -82,15 +111,6 @@ public Format getContentFormat() {
82111 }
83112
84113 // select categories of metadata to read, write, or reset
85- {
86- HashSet <Metadata > metadata = new HashSet <Metadata >();
87- metadata .add (Metadata .ALL );
88- processedMetadata = metadata ;
89- }
90- @ Override
91- public Set <Metadata > getMetadataCategories () {
92- return processedMetadata ;
93- }
94114 @ Override
95115 public void setMetadataCategories (Set <Metadata > categories ) {
96116 clearMetadataCategories ();
@@ -103,6 +123,10 @@ public void setMetadataCategories(Metadata... categories) {
103123 processedMetadata .add (category );
104124 }
105125 @ Override
126+ public Set <Metadata > getMetadataCategories () {
127+ return processedMetadata ;
128+ }
129+ @ Override
106130 public void clearMetadataCategories () {
107131 processedMetadata .clear ();
108132 }
@@ -250,7 +274,7 @@ public <T extends R> T read(DocumentDescriptor desc, DocumentMetadataReadHandle
250274 public <T extends R > T read (DocumentDescriptor desc , DocumentMetadataReadHandle metadataHandle , T contentHandle , ServerTransform transform , Transaction transaction , RequestParameters extraParams )
251275 throws ResourceNotFoundException , ForbiddenUserException , FailedRequestException {
252276 if (desc == null )
253- throw new IllegalArgumentException ("Reading document with null identifier " );
277+ throw new IllegalArgumentException ("Attempt to call read with null DocumentDescriptor " );
254278
255279 if (logger .isInfoEnabled ())
256280 logger .info ("Reading metadata and content for {}" , desc .getUri ());
@@ -285,6 +309,130 @@ public <T extends R> T read(DocumentDescriptor desc, DocumentMetadataReadHandle
285309 return wasModified ? contentHandle : null ;
286310 }
287311
312+ public DocumentPage read (String ... uris ) {
313+ return read (null , null , uris );
314+ }
315+
316+ public DocumentPage read (Transaction transaction , String ... uris ) {
317+ return read (null , transaction , uris );
318+ }
319+
320+ public DocumentPage read (ServerTransform transform , String ... uris ) {
321+ return read (transform , null , uris );
322+ }
323+
324+ public DocumentPage read (ServerTransform transform , Transaction transaction , String ... uris ) {
325+ if (uris == null || uris .length == 0 )
326+ throw new IllegalArgumentException ("Attempt to call read with no uris" );
327+
328+ if (logger .isInfoEnabled ())
329+ logger .info ("Reading metadata and content for multiple uris beginning with {}" , uris [0 ]);
330+
331+ return services .getBulkDocuments (
332+ requestLogger ,
333+ (transaction == null ) ? null : transaction .getTransactionId (),
334+ // the default for bulk is no metadata, which differs from the normal default of ALL
335+ isProcessedMetadataModified ? processedMetadata : null ,
336+ responseFormat ,
337+ null ,
338+ uris );
339+ }
340+
341+ public DocumentPage search (QueryDefinition querydef , long start ) {
342+ return search (querydef , start , null , null );
343+ }
344+
345+ public DocumentPage search (QueryDefinition querydef , long start , SearchReadHandle searchHandle ) {
346+ return search (querydef , start , searchHandle , null );
347+ }
348+
349+ public DocumentPage search (QueryDefinition querydef , long start , Transaction transaction ) {
350+ return search (querydef , start , null , transaction );
351+ }
352+
353+ public DocumentPage search (QueryDefinition querydef , long start , SearchReadHandle searchHandle , Transaction transaction ) {
354+
355+ if ( searchHandle != null ) {
356+ HandleImplementation searchBase = HandleAccessor .checkHandle (searchHandle , "search" );
357+ if (searchHandle instanceof SearchHandle ) {
358+ SearchHandle responseHandle = (SearchHandle ) searchHandle ;
359+ responseHandle .setHandleRegistry (getHandleRegistry ());
360+ responseHandle .setQueryCriteria (querydef );
361+ }
362+ if ( responseFormat != searchBase .getFormat () ) {
363+ throw new UnsupportedOperationException ("The format supported by your handle:[" +
364+ searchBase .getFormat () + "] does not match your setResponseFormat:[" + responseFormat + "]" );
365+ }
366+ }
367+
368+ String tid = transaction == null ? null : transaction .getTransactionId ();
369+ // the default for bulk is no metadata, which differs from the normal default of ALL
370+ Set <Metadata > metadata = isProcessedMetadataModified ? processedMetadata : null ;
371+ return services .getBulkDocuments ( requestLogger , querydef , start , getPageLength (),
372+ tid , searchHandle , searchView , metadata , responseFormat , null );
373+ }
374+
375+ public long getPageLength () {
376+ return pageLength ;
377+ }
378+
379+ public void setPageLength (long length ) {
380+ this .pageLength = length ;
381+ }
382+
383+ public QueryView getSearchView () {
384+ return searchView ;
385+ }
386+
387+ public void setSearchView (QueryView view ) {
388+ this .searchView = view ;
389+ }
390+
391+ public Format getResponseFormat () {
392+ return responseFormat ;
393+ }
394+
395+ public void setResponseFormat (Format responseFormat ) {
396+ if ( responseFormat != Format .XML && responseFormat != Format .JSON ) {
397+ throw new UnsupportedOperationException ("Only XML and JSON are valid response formats. You specified:[" +
398+ responseFormat + "]" );
399+ }
400+ this .responseFormat = responseFormat ;
401+ }
402+
403+ public DocumentWriteSet newWriteSet () {
404+ return new DocumentWriteSetImpl ();
405+ }
406+
407+ public void write (DocumentWriteSet writeSet ) {
408+ write (writeSet , null , null );
409+ }
410+
411+ public void write (DocumentWriteSet writeSet , ServerTransform transform ) {
412+ write (writeSet , transform , null );
413+ }
414+
415+ public void write (DocumentWriteSet writeSet , Transaction transaction ) {
416+ write (writeSet , null , transaction );
417+ }
418+
419+ public void write (DocumentWriteSet writeSet , ServerTransform transform , Transaction transaction ) {
420+ JacksonHandle jacksonHandle = new JacksonHandle ();
421+ jacksonHandle = services .postBulkDocuments (
422+ requestLogger ,
423+ writeSet ,
424+ (transform != null ) ? transform : getWriteTransform (),
425+ (transaction == null ) ? null : transaction .getTransactionId (),
426+ jacksonHandle );
427+ JsonNode root = jacksonHandle .get ();
428+ for (JsonNode item : root .get ("documents" )) {
429+ String uri = item .get ("uri" ).asText ();
430+ String mimetype = item .get ("mime-type" ).asText ();
431+ String category = item .get ("category" ).get (0 ).asText ();
432+ }
433+
434+ }
435+
288436 // shortcut writers
289437 @ Override
290438 public void writeAs (String uri , Object content )
0 commit comments