|
15 | 15 | */
|
16 | 16 | package com.marklogic.client.eval;
|
17 | 17 |
|
| 18 | +import com.marklogic.client.DatabaseClientFactory; |
18 | 19 | import com.marklogic.client.FailedRequestException;
|
19 | 20 | import com.marklogic.client.ForbiddenUserException;
|
20 | 21 | import com.marklogic.client.Transaction;
|
|
23 | 24 | import com.marklogic.client.io.marker.TextWriteHandle;
|
24 | 25 | import com.marklogic.client.util.EditableNamespaceContext;
|
25 | 26 |
|
| 27 | +/** |
| 28 | + * ServerEvaluationCall uses a fluent builder-style API to collect the parameters |
| 29 | + * for a server-side {@link #xquery xquery} or {@link #javascript javascript} eval or |
| 30 | + * invoke ({@link #modulePath modulePath}) call. ServerEvaluationCall also |
| 31 | + * conveniently has the eval* methods which execute those calls and return the |
| 32 | + * results. You must call one and only one of the following methods: xquery, |
| 33 | + * javascript, or modulePath. The xquery |
| 34 | + * and javascript methods initialize this call for server-side eval and accept |
| 35 | + * source code as a String or a TextWriteHandle (in case you are streaming the |
| 36 | + * source code from the file system, a URL, or other source that is most easily |
| 37 | + * accessed via io handles). The modulePath method initializes this call for server- |
| 38 | + * side invoke given the path to a module previously installed on the server. |
| 39 | + * |
| 40 | + * Here is a simple "hello world" junit example: <pre>{@code |
| 41 | + *String javascript = "'hello world'"; |
| 42 | + *String response = client.newServerEval() |
| 43 | + * .javascript(javascript) |
| 44 | + * .evalAs(String.class); |
| 45 | + *assertEquals("hello world", response); |
| 46 | + *}</pre> |
| 47 | + * or in xquery: <pre>{@code |
| 48 | + *String xquery = "'hello world'"; |
| 49 | + *String response = client.newServerEval() |
| 50 | + * .xquery(xquery) |
| 51 | + * .evalAs(String.class); |
| 52 | + *assertEquals("hello world", response); |
| 53 | + *}</pre> |
| 54 | + * |
| 55 | + * Variables can be added with the addVariable methods. |
| 56 | + * {@link #addVariable(String, AbstractWriteHandle) addVariable(String, AbstractWriteHandle)} |
| 57 | + * allows you to pass complex JSON or XML values directly from io handles. |
| 58 | + * {@link #addVariableAs(String, Object) addVariableAs(String, Object)} |
| 59 | + * follows the <a href="http://www.marklogic.com/blog/io-shortcut-marklogic-java-client-api/"> |
| 60 | + * shortcut pattern</a> which maps objects by type to the appropriate handle. |
| 61 | + * For simpler atomic values, convenience addVariable methods are provided for |
| 62 | + * String, Number, and Boolean types. |
| 63 | + * |
| 64 | + * Here is a simple "hello solar system" example with a variable: <pre>{@code |
| 65 | + *String javascript = "var planet;'hello solar system from ' + planet"; |
| 66 | + *String response = client.newServerEval() |
| 67 | + * .javascript(javascript) |
| 68 | + * .addVariable("planet", "Mars) |
| 69 | + * .evalAs(String.class); |
| 70 | + *assertEquals( "hello solar system from Mars", response); |
| 71 | + *}</pre> |
| 72 | + * or in xquery: <pre>{@code |
| 73 | + *String xquery = "declare variable $planet external;'hello solar system from ' || $planet"; |
| 74 | + *String response = client.newServerEval() |
| 75 | + * .xquery(xquery) |
| 76 | + * .addVariable("planet", "Mars) |
| 77 | + * .evalAs(String.class); |
| 78 | + *assertEquals( "hello solar system from Mars", response); |
| 79 | + * }</pre> |
| 80 | + * |
| 81 | + * Each call can be executed within a {@link #transaction transaction}, within a |
| 82 | + * {@link DatabaseClientFactory#newClient(String, int, String) particular database}, |
| 83 | + * and with particular {@link #namespaceContext namespaces} available for expansion |
| 84 | + * of prefixed variable names. |
| 85 | + * |
| 86 | + * Each call can be executed with only one expected response of a particular |
| 87 | + * {@link #evalAs type} or {@link #eval(AbstractReadHandle) handle type}. Or calls can be executed |
| 88 | + * with {@link #eval() multiple responses expected}. |
| 89 | + */ |
26 | 90 | public interface ServerEvaluationCall {
|
| 91 | + /** Initialize this server-side eval with xquery-syntax source code. |
| 92 | + * @param xquery the xquery-syntax source code to eval on the server |
| 93 | + * @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder |
| 94 | + */ |
27 | 95 | public ServerEvaluationCall xquery(String xquery);
|
| 96 | + |
| 97 | + /** Initialize this server-side eval with xquery-syntax source code. |
| 98 | + * @param xquery a handle containing the xquery-syntax source code to eval on the server |
| 99 | + * @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder |
| 100 | + */ |
28 | 101 | public ServerEvaluationCall xquery(TextWriteHandle xquery);
|
| 102 | + |
| 103 | + /** Initialize this server-side eval with javascript-syntax source code. |
| 104 | + * @param javascript the javascript-syntax source code to eval on the server |
| 105 | + * @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder |
| 106 | + */ |
29 | 107 | public ServerEvaluationCall javascript(String javascript);
|
30 |
| - public ServerEvaluationCall javascript(TextWriteHandle xquery); |
| 108 | + |
| 109 | + /** Initialize this server-side eval call with javascript-syntax source code. |
| 110 | + * @param javascript a handle containing the javascript-syntax source code to eval on the server |
| 111 | + * @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder |
| 112 | + */ |
| 113 | + public ServerEvaluationCall javascript(TextWriteHandle javascript); |
| 114 | + |
| 115 | + /** Initialize this server-side invoke call with a path to the module to invoke. |
| 116 | + * @param modulePath a path to a module previously installed in the server |
| 117 | + * @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder |
| 118 | + */ |
31 | 119 | public ServerEvaluationCall modulePath(String modulePath);
|
| 120 | + |
| 121 | + /** Set a variable name-value pair to pass to the code executing server-side. |
| 122 | + * @param name the variable name, including a namespace prefix if the prefix is |
| 123 | + * mapped to a uri in the {@link #namespaceContext namespace context} |
| 124 | + * @param value the atomic variable value |
| 125 | + * @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder |
| 126 | + */ |
32 | 127 | public ServerEvaluationCall addVariable(String name, String value);
|
| 128 | + |
| 129 | + /** Set a variable name-value pair to pass to the code executing server-side. |
| 130 | + * @param name the variable name, including a namespace prefix if the prefix is |
| 131 | + * mapped to a uri in the {@link #namespaceContext namespace context} |
| 132 | + * @param value the atomic variable value |
| 133 | + * @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder |
| 134 | + */ |
33 | 135 | public ServerEvaluationCall addVariable(String name, Number value);
|
| 136 | + |
| 137 | + /** Set a variable name-value pair to pass to the code executing server-side. |
| 138 | + * @param name the variable name, including a namespace prefix if the prefix is |
| 139 | + * mapped to a uri in the {@link #namespaceContext namespace context} |
| 140 | + * @param value the atomic variable value |
| 141 | + * @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder |
| 142 | + */ |
34 | 143 | public ServerEvaluationCall addVariable(String name, Boolean value);
|
| 144 | + |
| 145 | + /** Set a variable name-value pair to pass to the code executing server-side. |
| 146 | + * @param name the variable name, including a namespace prefix if the prefix is |
| 147 | + * mapped to a uri in the {@link #namespaceContext namespace context} |
| 148 | + * @param value the handle containing the variable value, most likely XML or JSON |
| 149 | + * @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder |
| 150 | + */ |
35 | 151 | public ServerEvaluationCall addVariable(String name, AbstractWriteHandle value);
|
36 |
| - /** Like other *As convenience methods throughout the API, the Object value |
37 |
| - * is managed by the Handle registered for that Class. */ |
| 152 | + |
| 153 | + /** Convenience method to set a variable of a type mapped to an io handle. Like other |
| 154 | + * <a href="http://www.marklogic.com/blog/io-shortcut-marklogic-java-client-api/"> |
| 155 | + * *As convenience methods</a> throughout the API, the Object value |
| 156 | + * is provided by the handle registered for that Class. |
| 157 | + * |
| 158 | + * @param name the variable name, including a namespace prefix if the prefix is |
| 159 | + * mapped to a uri in the {@link #namespaceContext namespace context} |
| 160 | + * @param value the handle containing the variable value |
| 161 | + * @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder |
| 162 | + */ |
38 | 163 | public ServerEvaluationCall addVariableAs(String name, Object value);
|
| 164 | + |
| 165 | + /** Initialize this call with a transaction under which server-side execution should occur. |
| 166 | + * @param transaction the open transaction under which to run this call in the server |
| 167 | + * @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder |
| 168 | + */ |
39 | 169 | public ServerEvaluationCall transaction(Transaction transaction);
|
| 170 | + |
| 171 | + /** Add a single namespace prefix-to-uri mapping to the namespace context. |
| 172 | + * @param prefix the prefix for this mapping |
| 173 | + * @param namespaceURI the uri for this mapping |
| 174 | + * @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder |
| 175 | + */ |
40 | 176 | public ServerEvaluationCall addNamespace(String prefix, String namespaceURI);
|
| 177 | + |
| 178 | + /** Initialize this call with namespaces so variables with prefixes can be sent with |
| 179 | + * their prefixes translated to uris that will match the uris in the code to be |
| 180 | + * executed on the server. |
| 181 | + * @param namespaces a namespace context specifying the mapping from prefixes to namespaces |
| 182 | + * @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder |
| 183 | + */ |
41 | 184 | public ServerEvaluationCall namespaceContext(EditableNamespaceContext namespaces);
|
| 185 | + |
| 186 | + /** Conveneince method to get the response serialized to a particular type by an io handle. |
| 187 | + * Like other <a href="http://www.marklogic.com/blog/io-shortcut-marklogic-java-client-api/"> |
| 188 | + * *As convenience methods</a> throughout the API, the return value |
| 189 | + * is provided by the handle registered for the provided responseType. |
| 190 | + * |
| 191 | + * @param responseType the type desired for the response. Must be a Class regiestered |
| 192 | + * to a handle. |
| 193 | + * @return the result deserialized by the implicit handle mapped to this responseType |
| 194 | + */ |
42 | 195 | public <T> T evalAs(Class<T> responseType)
|
43 | 196 | throws ForbiddenUserException, FailedRequestException;
|
| 197 | + |
| 198 | + /** Provides the single result of the server-side eval or invoke call, wrapped in an io |
| 199 | + * handle. |
| 200 | + * @param responseHandle the type of handle appropriate for the expected single result |
| 201 | + * @return the handle which wraps the response |
| 202 | + */ |
44 | 203 | public <H extends AbstractReadHandle> H eval(H responseHandle)
|
45 | 204 | throws ForbiddenUserException, FailedRequestException;
|
| 205 | + |
| 206 | + /** Provides all results returned by the server-side eval or invoke call. |
| 207 | + * @return an EvalResultIterator which provides access to all the results returned by |
| 208 | + * the server-side eval or invoke call. |
| 209 | + */ |
46 | 210 | public EvalResultIterator eval()
|
47 | 211 | throws ForbiddenUserException, FailedRequestException;
|
48 | 212 | }
|
0 commit comments