All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.marklogic.client.document.DocumentWriteOperation Maven / Gradle / Ivy

The newest version!
/*
 * Copyright © 2024 MarkLogic Corporation. All Rights Reserved.
 */
package com.marklogic.client.document;

import java.util.UUID;
import java.util.function.Function;
import java.util.stream.Stream;

import com.marklogic.client.impl.DocumentWriteOperationImpl;
import com.marklogic.client.io.marker.AbstractWriteHandle;
import com.marklogic.client.io.marker.DocumentMetadataWriteHandle;

/** A reflection of the write operations queued by calls to add,
 * {@link DocumentWriteSet#add add}, {@link DocumentWriteSet#addDefault addDefault}, or
 * {@link DocumentWriteSet#disableDefault disableDefault}.
 */
public interface DocumentWriteOperation extends Comparable {
  enum OperationType {
    /** This write operation (REST API mime part) sets the defaults for the
     * rest of the request.
     * @see 
     *     REST API Guide -> Constructing a Metadata Part
     */
    METADATA_DEFAULT,
    /** This write operation (REST API mime part) clears the defaults for the
     * rest of the request.  While this removes defaults set previously on the
     * request, this does not completely restore server-side defaults.  For
     * more information see the
     * 
     * REST API Guide -> Example: Reverting to System Default Metadata
     */
    DISABLE_METADATA_DEFAULT,

    /** This write operation (REST API mime part) creates or overwrites
     * one document and/or document metadata.
     * @see 
     *     REST API Guide -> Constructing a Content Part
     * @see 
     *     REST API Guide -> Constructing a Metadata Part
     * @see 
     *     REST API Guide -> Understanding When Metadata is Preserved or Replaced
     */
    DOCUMENT_WRITE
  };

  /** Returns the {@link DocumentWriteOperation.OperationType} set implicitly by your call to
   * {@link DocumentWriteSet#add add}, {@link DocumentWriteSet#addDefault addDefault}, or
   * {@link DocumentWriteSet#disableDefault disableDefault}.
   * @return the operation type which was set implicitly
   */
  OperationType getOperationType();

  // The uri for this document, whether set explicitly or received from the
  // server after a write with a DocumentDescriptor.
  /** The uri for this document if set explicitly by your call to
   * {@link DocumentWriteSet#add(String, AbstractWriteHandle) add(String, ...)}
   * @return the uri
   */
  String getUri();

  /** The handle with the metadata as set by your call to
   * {@link DocumentWriteSet#add(String, DocumentMetadataWriteHandle, AbstractWriteHandle) add} or
   * {@link DocumentWriteSet#add(DocumentDescriptor, DocumentMetadataWriteHandle, AbstractWriteHandle) add}.
   * @return the handle with the metadata
   */
  DocumentMetadataWriteHandle getMetadata();

  /** The handle with the content as set by your call to
   * {@link DocumentWriteSet#add(String, AbstractWriteHandle) add} or
   * {@link DocumentWriteSet#add(DocumentDescriptor, AbstractWriteHandle) add}.
   * @return the handle with the content
   */
  AbstractWriteHandle getContent();

  /**
   * The logical temporal document URI of the document as set by your call to
   * one of the 'add' methods which adds a document to a {@link DocumentWriteSet}
   * @return the logical temporal document URI
   */
  String getTemporalDocumentURI();

  /**
   * The from method prepares each content object for writing as a document including generating a URI by inserting a UUID.
   * @param content a subclass of AbstractWriteHandle
   * @param uriMaker DocumentUriMaker which internally accepts an AbstractWriteHandle and returns a String
   * @return a stream of DocumentWriteOperation to be written in the database.
   */
    public static Stream from(Stream content,
            final DocumentUriMaker uriMaker) {
        if(content == null || uriMaker == null)
            throw new IllegalArgumentException("Content and/or Uri maker cannot be null");

        final class WrapperImpl {
            private DocumentUriMaker docUriMaker;
            WrapperImpl(DocumentUriMaker uriMaker){
                this.docUriMaker = uriMaker;
            }
            DocumentWriteOperation mapper(AbstractWriteHandle content) {
                String uri = docUriMaker.apply(content);
                return new DocumentWriteOperationImpl(uri, content);
            }

        }
        WrapperImpl wrapperImpl = new WrapperImpl(uriMaker);
        return content.map(wrapperImpl::mapper);

    }

    /**
     * The uriMaker method creates a uri for each document written in the database
     * @param format refers to the pattern passed.
     * @return DocumentUriMaker which contains the formatted uri for the new document.
     */
    public static DocumentUriMaker uriMaker(String format) throws IllegalArgumentException{

        if(format == null || format.length() == 0)
            throw new IllegalArgumentException("Format cannot be null or empty");

        final class FormatUriMaker {
            private String uriFormat;

            FormatUriMaker(String format) {
                this.uriFormat = format;
            }

            String makeUri(AbstractWriteHandle content) {
                return String.format(uriFormat, UUID.randomUUID());
            }
        }
        FormatUriMaker formatUriMaker = new FormatUriMaker(format);

        return formatUriMaker::makeUri;
    }

    @FunctionalInterface
    public interface DocumentUriMaker extends Function {
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy