Package#send throws ZoomException YAZJ-18
[yaz4j-moved-to-github.git] / src / main / java / org / yaz4j / Package.java
index 590b172..1af9c01 100644 (file)
@@ -1,43 +1,77 @@
 package org.yaz4j;
 
+import org.yaz4j.exception.ZoomException;
 import org.yaz4j.jni.SWIGTYPE_p_ZOOM_package_p;
 import org.yaz4j.jni.yaz4jlib;
 
-public class Package
-{
-       private SWIGTYPE_p_ZOOM_package_p pack = null ;
-       private ConnectionExtended connection = null ;  
-       private String type;
-
-       Package(SWIGTYPE_p_ZOOM_package_p pack, ConnectionExtended connection, String type)
-       {
-               this.type = type;
-               this.connection = connection;
-               this.pack = pack;
-       }
-       
-       public void finalize()
-       {
-               Dispose();
-       }
-       
-       public PackageOptionsCollection getPackageOptions()
-       {
-          return new PackageOptionsCollection(pack);
-       }
-       
-       public void Send()
-       { 
-               yaz4jlib.ZOOM_package_send( pack, type );
-       }
-       
-       public void Dispose()
-       {
-               if ( pack != null )
-               {
-                       yaz4jlib.ZOOM_package_destroy( pack );
-                       connection = null;
-                       pack = null ;
-               }
-       }
+/**
+ * Allows to perform an extended-service operation.
+ * 
+ * Once created, a package is configured by means of options, then the package 
+ * is send and the result is inspected (again, by means of options).
+ * 
+ * @see org.yaz4j.ConnectionExtended#getPackage(java.lang.String)
+ * 
+ * @author jakub
+ */
+public class Package {
+  //for GC ref count
+  private ConnectionExtended conn;
+  private SWIGTYPE_p_ZOOM_package_p pack;
+  private final String type;
+
+  Package(SWIGTYPE_p_ZOOM_package_p pack, ConnectionExtended conn, String type) {
+    if (type == null)
+      throw new NullPointerException("type cannot be null");
+    this.type = type;
+    this.pack = pack;
+    this.conn = conn;
+  }
+
+  public void finalize() {
+    _dispose();
+  }
+
+  /**
+   * Write option for a specified key
+   * @param key option name
+   * @param value option value
+   * @return package (self) for chainability
+   */
+  public Package option(String key, String value) {
+    if (key == null)
+      throw new NullPointerException("option name cannot be null");
+    yaz4jlib.ZOOM_package_option_set(pack, key, value);
+    return this;
+  }
+
+  /**
+   * Read option for a specified key.
+   * @param key option name
+   * @return option value
+   */
+  public String option(String key) {
+    if (key == null)
+      throw new NullPointerException("option name cannot be null");
+    return yaz4jlib.ZOOM_package_option_get(pack, key);
+  }
+
+  /**
+   * Send the package.
+   */
+  public void send() throws ZoomException {
+    yaz4jlib.ZOOM_package_send(pack, type);
+    ZoomException e = conn.getZoomException();
+    if (e != null) {
+      throw e;
+    }
+  }
+
+  void _dispose() {
+    if (pack != null) {
+      yaz4jlib.ZOOM_package_destroy(pack);
+      pack = null;
+      conn = null;
+    }
+  }
 }