11
11
import java .util .concurrent .CompletableFuture ;
12
12
import java .util .concurrent .CompletionStage ;
13
13
14
+ import jakarta .enterprise .inject .Instance ;
14
15
import jakarta .inject .Inject ;
15
16
16
17
import org .eclipse .microprofile .config .ConfigProvider ;
28
29
import io .agroal .api .AgroalDataSource ;
29
30
import io .agroal .api .configuration .AgroalDataSourceConfiguration ;
30
31
import io .quarkus .assistant .runtime .dev .Assistant ;
31
- import io .quarkus .devui .runtime .comms .JsonRpcMessage ;
32
- import io .quarkus .devui .runtime .comms .JsonRpcRouter ;
33
- import io .quarkus .devui .runtime .comms .MessageType ;
34
32
import io .quarkus .hibernate .orm .dev .HibernateOrmDevController ;
35
33
import io .quarkus .hibernate .orm .dev .HibernateOrmDevInfo ;
36
34
import io .quarkus .hibernate .orm .runtime .customized .QuarkusConnectionProvider ;
@@ -44,7 +42,7 @@ public class HibernateOrmDevJsonRpcService {
44
42
private final String allowedHost ;
45
43
46
44
@ Inject
47
- Optional <Assistant > assistant ;
45
+ Instance < Optional <Assistant > > assistant ;
48
46
49
47
public HibernateOrmDevJsonRpcService () {
50
48
this .isDev = LaunchMode .current ().isDev () && !LaunchMode .current ().isRemoteDev ();
@@ -104,18 +102,18 @@ You are an expert in writing Hibernate Query Language (HQL) queries.
104
102
* based on pageNumber and pageSize are returned. For mutation statements, a custom message including the number of affected
105
103
* records is returned.
106
104
* <p>
107
- * This method handles result serialization (to JSON) internally, and returns a {@link JsonRpcMessage<String>} to avoid
108
- * further processing by the {@link JsonRpcRouter} .
105
+ * This method handles result serialization (to JSON) internally, and returns a JsonRpc Message in Map format to avoid
106
+ * further processing by the Dev UI JsonRpcRouter.
109
107
*
110
108
* @param persistenceUnit The name of the persistence unit within which the query will be executed
111
109
* @param query The user query (be it an HQL query or a plain-text statement when using assistant)
112
110
* @param pageNumber The page number, used for selection query results pagination
113
111
* @param pageSize The page size, used for selection query results pagination
114
112
* @param assistant Whether to use the assistant to generate the HQL query based on the user input
115
113
* @param interactive Enable assistant's interactive mode, answering the original user request in natural language
116
- * @return a {@link JsonRpcMessage<String>} containing the resulting {@link DataSet} serialized to JSON.
114
+ * @return a JsonRpcMessage containing the resulting {@link DataSet} serialized to JSON.
117
115
*/
118
- public CompletionStage <JsonRpcMessage < String >> executeHQL (
116
+ public CompletionStage <Map < String , String >> executeHQL (
119
117
String persistenceUnit ,
120
118
String query ,
121
119
Integer pageNumber ,
@@ -150,7 +148,11 @@ public CompletionStage<JsonRpcMessage<String>> executeHQL(
150
148
}
151
149
152
150
if (Boolean .TRUE .equals (assistant )) {
153
- Assistant a = this .assistant .orElse (null );
151
+ if (!this .assistant .isResolvable ()) {
152
+ return errorDataSet (
153
+ "The assistant is not available, please install the Chappie extension." );
154
+ }
155
+ Assistant a = this .assistant .get ().orElse (null );
154
156
if (a == null || !a .isAvailable ()) {
155
157
return errorDataSet (
156
158
"The assistant is not available, please check the Quarkus assistant extension is correctly configured." );
@@ -176,7 +178,7 @@ public CompletionStage<JsonRpcMessage<String>> executeHQL(
176
178
return dataSetCompletionStage .thenCompose (dataSet -> {
177
179
if (dataSet .error () != null ) {
178
180
// If there was an error executing the query, return it directly
179
- return CompletableFuture .completedStage (toJson (dataSet ));
181
+ return CompletableFuture .completedStage (toMap (dataSet ));
180
182
}
181
183
CompletionStage <Map <String , String >> interactiveCompletionStage = a .assistBuilder ()
182
184
.systemMessage (SYSTEM_MESSAGE )
@@ -192,11 +194,11 @@ public CompletionStage<JsonRpcMessage<String>> executeHQL(
192
194
});
193
195
});
194
196
} else {
195
- return dataSetCompletionStage .thenApply (HibernateOrmDevJsonRpcService ::toJson );
197
+ return dataSetCompletionStage .thenApply (HibernateOrmDevJsonRpcService ::toMap );
196
198
}
197
199
} else {
198
200
DataSet result = executeHqlQuery (query , sf , pageNumber , pageSize );
199
- return CompletableFuture .completedStage (toJson (result ));
201
+ return CompletableFuture .completedStage (toMap (result ));
200
202
}
201
203
}
202
204
@@ -259,15 +261,15 @@ private static DataSet executeHqlQuery(String hql, SessionFactoryImplementor sf,
259
261
});
260
262
}
261
263
262
- private static CompletionStage <JsonRpcMessage < String >> errorDataSet (String errorMessage ) {
263
- return CompletableFuture .completedStage (toJson (new DataSet (null , null , -1 , null , errorMessage )));
264
+ private static CompletionStage <Map < String , String >> errorDataSet (String errorMessage ) {
265
+ return CompletableFuture .completedStage (toMap (new DataSet (null , null , -1 , null , errorMessage )));
264
266
}
265
267
266
- private static JsonRpcMessage < String > messageDataset (String query , String message , long resultCount ) {
267
- return toJson (new DataSet (null , query , resultCount , message , null ));
268
+ private static Map < String , String > messageDataset (String query , String message , long resultCount ) {
269
+ return toMap (new DataSet (null , query , resultCount , message , null ));
268
270
}
269
271
270
- private static JsonRpcMessage <String > toJson (DataSet dataSet ) {
272
+ private static Map <String , String > toMap (DataSet dataSet ) {
271
273
StringBuilder jsonBuilder = new StringBuilder ("{" );
272
274
jsonBuilder .append ("\" resultCount\" :" ).append (dataSet .resultCount ());
273
275
if (dataSet .data () != null ) {
@@ -277,9 +279,13 @@ private static JsonRpcMessage<String> toJson(DataSet dataSet) {
277
279
appendIfNonNull (jsonBuilder , "message" , dataSet .message ());
278
280
appendIfNonNull (jsonBuilder , "error" , dataSet .error ());
279
281
jsonBuilder .append ("}" );
280
- JsonRpcMessage <String > message = new JsonRpcMessage <>(jsonBuilder .toString (), MessageType .Response );
281
- message .setAlreadySerialized (true );
282
- return message ;
282
+
283
+ Map <String , String > map = Map .of (
284
+ "response" , jsonBuilder .toString (),
285
+ "messageType" , "Response" ,
286
+ "alreadySerialized" , "true" );
287
+
288
+ return map ;
283
289
}
284
290
285
291
private static void appendIfNonNull (StringBuilder sb , String fieldName , String value ) {
0 commit comments