Multi-threaded DLL (do not use libcmt.lib)
[yaz4j-moved-to-github.git] / src / main / java / org / yaz4j / Connection.java
index 49d5aea..6a173f9 100644 (file)
@@ -1,5 +1,6 @@
 package org.yaz4j;
 
+import java.io.Closeable;
 import org.yaz4j.exception.ZoomException;
 import org.yaz4j.jni.SWIGTYPE_p_ZOOM_connection_p;
 import org.yaz4j.jni.SWIGTYPE_p_ZOOM_query_p;
@@ -32,12 +33,12 @@ import org.yaz4j.jni.yaz4jlib;
  * }
  *
  * </pre></blockquote>
+ * @see <a href="http://www.indexdata.com/yaz/doc/zoom.html#zoom-connections">YAZ ZOOM Connection</a>
  * @author jakub
  */
-public class Connection {
-
-  private String host;
-  private int port;
+public class Connection implements Closeable {
+  private final String host;
+  private final int port;
   protected SWIGTYPE_p_ZOOM_connection_p zoomConnection;
   //connection is initially closed
   protected boolean closed = true;
@@ -62,6 +63,8 @@ public class Connection {
    * @param port port of the server
    */
   public Connection(String host, int port) {
+    if (host == null)
+      throw new NullPointerException("host cannot be null");
     this.host = host;
     this.port = port;
     zoomConnection = yaz4jlib.ZOOM_connection_create(null);
@@ -75,16 +78,22 @@ public class Connection {
    * Performs a search operation (submits the query to the server, waits for
    * response and creates a new result set that allows to retrieve particular
    * results)
+   * @deprecated Does not allow specifying sort criteria prior to search
+   * use {@link #search(org.yaz4j.Query) search(Query)} instead.
    * @param query search query
    * @param queryType type of the query (e.g RPN. CQL)
    * @return result set containing records (hits)
    * @throws ZoomException protocol or network-level error
    */
+  @Deprecated
   public ResultSet search(String query, QueryType queryType) throws
     ZoomException {
-    if (closed) {
+    if (query == null)
+      throw new NullPointerException("query cannot be null");
+    if (queryType == null)
+      throw new NullPointerException("queryType cannot be null");
+    if (closed)
       throw new IllegalStateException("Connection is closed.");
-    }
     SWIGTYPE_p_ZOOM_query_p yazQuery = null;
     if (queryType == QueryType.CQLQuery) {
       yazQuery = yaz4jlib.ZOOM_query_create();
@@ -95,11 +104,36 @@ public class Connection {
     }
     SWIGTYPE_p_ZOOM_resultset_p yazResultSet = yaz4jlib.ZOOM_connection_search(
       zoomConnection, yazQuery);
+    yaz4jlib.ZOOM_query_destroy(yazQuery);
+    ZoomException err = ExceptionUtil.getError(zoomConnection, host,
+      port);
+    if (err != null) {
+      yaz4jlib.ZOOM_resultset_destroy(yazResultSet);
+      throw err;
+    }
+    return new ResultSet(yazResultSet, this);
+  }
+  
+    /**
+   * Performs a search operation (submits the query to the server, waits for
+   * response and creates a new result set that allows to retrieve particular
+   * results). Sort criteria may be specified prior to the search, directly
+   * on the query object.
+   * @param query search query of any type supported by YAZ.
+   * @return result set containing records (hits)
+   * @throws ZoomException protocol or network-level error
+   */
+  public ResultSet search(Query query) throws ZoomException {
+    if (query == null)
+      throw new NullPointerException("query cannot be null");
+    if (closed)
+      throw new IllegalStateException("Connection is closed.");
+    SWIGTYPE_p_ZOOM_resultset_p yazResultSet = yaz4jlib.ZOOM_connection_search(
+      zoomConnection, query.query);
     ZoomException err = ExceptionUtil.getError(zoomConnection, host,
       port);
     if (err != null) {
       yaz4jlib.ZOOM_resultset_destroy(yazResultSet);
-      yaz4jlib.ZOOM_query_destroy(yazQuery);
       throw err;
     }
     return new ResultSet(yazResultSet, this);
@@ -107,16 +141,18 @@ public class Connection {
 
   /**
    * Performs a scan operation (obtains a list of candidate search terms against
-   * a particular access point)
-   * @see <a href="http://zoom.z3950.org/api/zoom-1.4.html#3.2.7">ZOOM-API Scan</a>
+   * a particular access point). 
+   * @deprecated Only allows PQF scan queries, use {@link #scan(org.yaz4j.Query) scan(Query)} instead
    * @param query query for scanning
    * @return a scan set with the terms
    * @throws ZoomException a protocol or network-level error
    */
+  @Deprecated
   public ScanSet scan(String query) throws ZoomException {
-    if (closed) {
+    if (query == null)
+      throw new NullPointerException("query cannot be null");
+    if (closed)
       throw new IllegalStateException("Connection is closed.");
-    }
     SWIGTYPE_p_ZOOM_scanset_p yazScanSet = yaz4jlib.ZOOM_connection_scan(
       zoomConnection, query);
     ZoomException err = ExceptionUtil.getError(zoomConnection, host, port);
@@ -127,6 +163,30 @@ public class Connection {
     ScanSet scanSet = new ScanSet(yazScanSet, this);
     return scanSet;
   }
+  
+    /**
+   * Performs a scan operation (obtains a list of candidate search terms against
+   * a particular access point). Allows to use both CQL and PQF for Scan.
+   * @see <a href="http://www.indexdata.com/yaz/doc/zoom.scan.html">ZOOM-API Scan</a>
+   * @param query scan query of type supported by YAZ
+   * @return a scan set with the terms
+   * @throws ZoomException a protocol or network-level error
+   */
+  public ScanSet scan(Query query) throws ZoomException {
+    if (query == null)
+      throw new NullPointerException("query cannot be null");
+    if (closed)
+      throw new IllegalStateException("Connection is closed.");
+    SWIGTYPE_p_ZOOM_scanset_p yazScanSet = yaz4jlib.ZOOM_connection_scan1(
+      zoomConnection, query.query);
+    ZoomException err = ExceptionUtil.getError(zoomConnection, host, port);
+    if (err != null) {
+      yaz4jlib.ZOOM_scanset_destroy(yazScanSet);
+      throw err;
+    }
+    ScanSet scanSet = new ScanSet(yazScanSet, this);
+    return scanSet;
+  }
 
   /**
    * Establishes a connection to the remote server.
@@ -144,10 +204,21 @@ public class Connection {
   /**
    * Closes the connection.
    */
+  @Override
   public void close() {
     yaz4jlib.ZOOM_connection_close(zoomConnection);
     closed = true;
   }
+  
+  /**
+   * Return exception type from current connection
+   *
+   * @return null if no error
+   */
+  ZoomException getZoomException() {
+    ZoomException err = ExceptionUtil.getError(zoomConnection, host, port);
+    return err;
+  }
 
   /**
    * Write option with a given name.
@@ -156,6 +227,8 @@ public class Connection {
    * @return connection (self) for chainability
    */
   public Connection option(String name, String value) {
+    if (name == null)
+      throw new NullPointerException("option name cannot be null");
     yaz4jlib.ZOOM_connection_option_set(zoomConnection, name, value);
     return this;
   }
@@ -166,6 +239,8 @@ public class Connection {
    * @return option value
    */
   public String option(String name) {
+    if (name == null)
+      throw new NullPointerException("option name cannot be null");
     return yaz4jlib.ZOOM_connection_option_get(zoomConnection, name);
   }