3fa0c81d36cd31d5bc13cd0cc8833b47db155832
[yaz4j-moved-to-github.git] / src / main / java / org / yaz4j / async / AsyncConnection.java
1 /*
2  * Copyright (c) 1995-2015, Index Data
3  * All rights reserved.
4  * See the file LICENSE for details.
5  */
6 package org.yaz4j.async;
7
8 import org.yaz4j.Connection;
9 import org.yaz4j.Query;
10 import org.yaz4j.Record;
11 import org.yaz4j.ResultSet;
12 import org.yaz4j.exception.ZoomException;
13 import org.yaz4j.jni.SWIGTYPE_p_ZOOM_connection_p;
14 import static org.yaz4j.jni.yaz4jlib.*;
15 import org.yaz4j.util.Unstable;
16
17 /**
18  *
19  * @author jakub
20  */
21 @Unstable
22 public class AsyncConnection extends Connection {
23   private ResultSet lastResultSet;
24   ErrorHandler eh;
25   //make sure error is only handled once
26   boolean errorHandled = false;
27   int handledRecordOffset = 0;
28   ErrorHandler reh;
29   SearchHandler sh;
30   RecordHandler rh;
31   
32   public interface SearchHandler {
33     public void handle(ResultSet rs);
34   }
35   
36   public interface RecordHandler {
37     public void handle(Record r);
38   }
39   
40   public interface ErrorHandler {
41     public void handle(ZoomException e);
42   }
43
44   public AsyncConnection(String host, int port) {
45     super(host, port);
46     ZOOM_connection_option_set(zoomConnection, "async", "1");
47     closed = false;
48   }
49
50   @Override
51   public ResultSet search(Query query) throws ZoomException {
52     errorHandled = false;
53     lastResultSet = super.search(query);
54     return null;
55   }
56   
57   public AsyncConnection onSearch(SearchHandler sh) {
58     this.sh = sh;
59     return this;
60   }
61   
62   public AsyncConnection onRecord(RecordHandler rh) {
63     this.rh = rh;
64     return this;
65   }
66   
67   public AsyncConnection onError(ErrorHandler eh) {
68     this.eh = eh;
69     return this;
70   }
71   
72   public AsyncConnection onRecordError(ErrorHandler reh) {
73     this.reh = reh;
74     return this;
75   }
76   
77   //actuall handler, pkg-private
78   
79   void handleSearch() {
80     handleError();
81     //handle search
82     if (sh != null) sh.handle(lastResultSet);
83   }
84   
85   void handleRecord() {
86     //TODO clone the record to detach it from the result set
87     try {
88       if (rh != null) rh.handle(lastResultSet.getRecord(handledRecordOffset));
89     } catch (ZoomException ex) {
90       if (reh != null) reh.handle(ex);
91     } finally {
92       handledRecordOffset++;
93     }
94   }
95   
96   void handleError() {
97     //handle error
98     if (!errorHandled) {
99       ZoomException err = getZoomException();
100       if (err != null) {
101         if (eh != null) {
102           eh.handle(err);
103           errorHandled = true;
104         }
105       }
106     }
107   }
108   
109   /**
110    * Expose native connection to the async package, keep it package private.
111    */
112   SWIGTYPE_p_ZOOM_connection_p getNativeConnection() {
113     return zoomConnection;
114   }
115   
116 }