--- /dev/null
+/*
+ * Copyright (c) 1995-2013, Index Datassss
+ * All rights reserved.
+ * See the file LICENSE for details.
+ */
+package org.yaz4j;
+
+import org.yaz4j.jni.yaz4jlib;
+
+/**
+ *
+ * @author jakub
+ */
+public class CQLQuery extends Query {
+
+ public CQLQuery(String cqlQuery) {
+ super();
+ yaz4jlib.ZOOM_query_cql(query, cqlQuery);
+ }
+
+}
* }
*
* </pre></blockquote>
+ * @see <a href="http://www.indexdata.com/yaz/doc/zoom.html#zoom-connections">YAZ ZOOM Connection</a>
* @author jakub
*/
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) {
}
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 (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);
+ throw err;
+ }
+ return new ResultSet(yazResultSet, this);
+ }
/**
* 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) {
throw new IllegalStateException("Connection is closed.");
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 (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.
* 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)
+ * @see org.yaz4j.ConnectionExtended#getPackage(java.lang.String)
*
* @author jakub
*/
--- /dev/null
+/*
+ * Copyright (c) 1995-2013, Index Datassss
+ * All rights reserved.
+ * See the file LICENSE for details.
+ */
+package org.yaz4j;
+
+import org.yaz4j.jni.yaz4jlib;
+
+/**
+ *
+ * @author jakub
+ */
+public class PrefixQuery extends Query {
+
+ public PrefixQuery(String prefixQuery) {
+ super();
+ yaz4jlib.ZOOM_query_prefix(query, prefixQuery);
+ }
+
+}
--- /dev/null
+/*
+ * Copyright (c) 1995-2013, Index Datassss
+ * All rights reserved.
+ * See the file LICENSE for details.
+ */
+package org.yaz4j;
+
+import org.yaz4j.exception.ZoomException;
+import org.yaz4j.jni.SWIGTYPE_p_ZOOM_query_p;
+import org.yaz4j.jni.yaz4jlib;
+
+/**
+ * @see <a href="http://www.indexdata.com/yaz/doc/zoom.query.html">YAZ ZOOM Query</a>
+ * @author jakub
+ */
+public abstract class Query {
+
+ SWIGTYPE_p_ZOOM_query_p query;
+ private boolean disposed = false;
+
+ protected Query() {
+ query = yaz4jlib.ZOOM_query_create();
+ }
+
+ public void sortBy(String strategy, String criteria) throws ZoomException {
+ int ret = yaz4jlib.ZOOM_query_sortby2(query, strategy, criteria);
+ if (ret != 0) {
+ throw new ZoomException("query sortBy failed");
+ }
+ }
+
+ protected void finalize() {
+ _dispose();
+ }
+
+ void _dispose() {
+ if (!disposed) {
+ yaz4jlib.ZOOM_query_destroy(query);
+ disposed = true;
+ }
+ }
+
+}
};
}
+ /**
+ *
+ * @param type
+ * @param spec
+ * @return
+ * @throws ZoomException
+ */
public ResultSet sort(String type, String spec) throws ZoomException {
int ret = yaz4jlib.ZOOM_resultset_sort1(resultSet, type, spec);
if (ret != 0) throw new ZoomException("Sorting resultset failed");
--- /dev/null
+/*
+ * Copyright (c) 1995-2013, Index Data
+ * All rights reserved.
+ * See the file LICENSE for details.
+ */
+/**
+ * Classes within this package expose the Z39.50 Object Orientation Model (ZOOM) API
+ * and additional extensions implemented by YAZ.
+ *
+ * @see <a href="http://zoom.z3950.org/api/zoom-1.4.html">ZOOM-API</a>
+ * @see <a href="http://www.indexdata.com/yaz/doc/zoom.html">YAZ ZOOM-API</a>
+ */
+package org.yaz4j;
import java.util.List;
public class ConnectionTest {
-
+
@Test
public void testConnection() {
Connection con = new Connection("z3950.indexdata.dk:210/gils", 0);
con.setSyntax("sutrs");
System.out.println("Open connection to z3950.indexdata.dk:210/gils...");
con.connect();
+ ResultSet s = con.search(new PrefixQuery("@attr 1=4 utah"));
+ System.out.println("Search for 'utah'...");
+ assertNotNull(s);
+ assertEquals(s.getHitCount(), 9);
+ Record rec = s.getRecord(0);
+ assertNotNull(rec);
+ byte[] content = rec.getContent();
+ // first SUTRS record
+ assertEquals(content.length, 1940);
+ assertEquals(content[0], 103);
+ assertEquals(rec.getSyntax(), "SUTRS");
+ assertEquals(rec.getDatabase(), "gils");
+ System.out.println("Read all records..");
+ // read all records
+ int i = 0;
+ for (Record r : s) {
+ assertNotNull(r);
+ System.out.println("Got "+i+" record of type "+r.getSyntax());
+ i++;
+ }
+ System.out.println("Try sorting them...");
+ s.sort("yaz", "1=4 >i 1=21 >s");
+ System.out.println("Try fetching them all at once...");
+ i = 0;
+ List<Record> all = s.getRecords(0, (int) s.getHitCount());
+ for (Record r : all) {
+ System.out.println("getRecords, rec '"+i+"'"+r.getSyntax());
+ System.out.println(r.render());
+ i++;
+ }
+ } catch (ZoomException ze) {
+ fail(ze.getMessage());
+ } finally {
+ con.close();
+ }
+ }
+
+ @Test
+ public void testConnectionDeprecated() {
+ Connection con = new Connection("z3950.indexdata.dk:210/gils", 0);
+ assertNotNull(con);
+ try {
+ con.setSyntax("sutrs");
+ System.out.println("Open connection to z3950.indexdata.dk:210/gils...");
+ con.connect();
ResultSet s = con.search("@attr 1=4 utah",
Connection.QueryType.PrefixQuery);
System.out.println("Search for 'utah'...");