1515 * specific language governing permissions and limitations
1616 * under the License.
1717 */
18- import { Range , DSSResource , DSSOperation } from "@wso2/mi-syntax-tree/lib/src" ;
18+ import { Range , DSSResource , DSSOperation , DSSQuery } from "@wso2/mi-syntax-tree/lib/src" ;
1919import { RpcClient } from "@wso2/mi-rpc-client" ;
2020import { DSS_TEMPLATES } from "../constants" ;
2121import { getXML } from "./template-engine/mustache-templates/templateUtils" ;
2222import { ResourceFormData , ResourceType } from "../views/Forms/DataServiceForm/SidePanelForms/ResourceForm" ;
2323import { OperationFormData , OperationType } from "../views/Forms/DataServiceForm/SidePanelForms/OperationForm" ;
24+ import { QueryFormData , QueryType } from "../views/Forms/DataServiceForm/SidePanelForms/QueryForm" ;
2425
2526export const generateResourceData = ( model : DSSResource ) : ResourceType => {
2627
@@ -29,7 +30,8 @@ export const generateResourceData = (model: DSSResource): ResourceType => {
2930 resourceMethod : model . method ,
3031 description : model . description ,
3132 enableStreaming : model . enableStreaming ,
32- returnRequestStatus : model . returnRequestStatus
33+ returnRequestStatus : model . returnRequestStatus ,
34+ queryId : model . queryId
3335 } ;
3436 return resourceData ;
3537} ;
@@ -39,14 +41,26 @@ export const generateOperationData = (model: DSSOperation): OperationType => {
3941 const operationData : OperationType = {
4042 operationName : model . name ,
4143 description : model . description ,
42- enableStreaming : model . enableStreaming
44+ enableStreaming : model . enableStreaming ,
45+ queryId : model . queryId
4346 } ;
4447 return operationData ;
4548} ;
4649
50+ export const generateQueryData = ( model : DSSQuery ) : QueryType => {
51+
52+ const queryData : QueryType = {
53+ name : model . name ,
54+ datasource : model . datasource ,
55+ query : model . query ,
56+ } ;
57+ return queryData ;
58+ } ;
59+
4760export const onResourceCreate = ( data : ResourceFormData , range : Range , documentUri : string , rpcClient : RpcClient , dbName : string ) => {
4861
49- const queryName = data . resourceMethod . toLowerCase ( ) + "_" + data . resourcePath . replace ( / [ ^ a - z A - Z ] / g, '' ) . toLowerCase ( ) + "_query" ;
62+ const queryName = data . queryId !== "" ? data . queryId :data . resourceMethod . toLowerCase ( ) + "_" + data . resourcePath . replace ( / [ ^ a - z A - Z ] / g, '' ) . toLowerCase ( ) + "_query" ;
63+ const shouldCreateQuery = data . queryId === "" ;
5064
5165 const formValues = {
5266 path : data . resourcePath ,
@@ -57,7 +71,7 @@ export const onResourceCreate = (data: ResourceFormData, range: Range, documentU
5771 query : queryName
5872 } ;
5973
60- const queryContent = getXML ( DSS_TEMPLATES . ADD_QUERY , { name : queryName , dbName : dbName } ) ;
74+ const queryContent = shouldCreateQuery ? getXML ( DSS_TEMPLATES . ADD_QUERY , { name : queryName , dbName : dbName } ) : "" ;
6175 const resourceContent = getXML ( DSS_TEMPLATES . ADD_RESOURCE , formValues ) ;
6276 rpcClient . getMiDiagramRpcClient ( ) . applyEdit ( {
6377 text : queryContent + resourceContent ,
@@ -77,7 +91,8 @@ export const onResourceCreate = (data: ResourceFormData, range: Range, documentU
7791
7892export const onOperationCreate = ( data : OperationFormData , range : Range , documentUri : string , rpcClient : RpcClient , dbName : string ) => {
7993
80- const queryName = data . operationName . replace ( / [ ^ a - z A - Z ] / g, '' ) . toLowerCase ( ) + "_query" ;
94+ const queryName = data . queryId !== "" ? data . queryId : data . operationName . replace ( / [ ^ a - z A - Z ] / g, '' ) . toLowerCase ( ) + "_query" ;
95+ const shouldCreateQuery = data . queryId === "" ;
8196
8297 const formValues = {
8398 name : data . operationName ,
@@ -86,7 +101,7 @@ export const onOperationCreate = (data: OperationFormData, range: Range, documen
86101 query : queryName
87102 } ;
88103
89- const queryContent = getXML ( DSS_TEMPLATES . ADD_QUERY , { name : queryName , dbName : dbName } ) ;
104+ const queryContent = shouldCreateQuery ? getXML ( DSS_TEMPLATES . ADD_QUERY , { name : queryName , dbName : dbName } ) : "" ;
90105 const operationContent = getXML ( DSS_TEMPLATES . ADD_OPERATION , formValues ) ;
91106 rpcClient . getMiDiagramRpcClient ( ) . applyEdit ( {
92107 text : queryContent + operationContent ,
@@ -104,10 +119,45 @@ export const onOperationCreate = (data: OperationFormData, range: Range, documen
104119 } ) ;
105120} ;
106121
122+ export const onQueryCreate = ( data : QueryFormData , range : Range , documentUri : string , rpcClient : RpcClient , dbName : string ) => {
123+
124+ const formValues = {
125+ name : data . name ,
126+ datasource : data . datasource ,
127+ query : data . query
128+ } ;
129+
130+ getQueryType ( rpcClient , documentUri , data . datasource )
131+ . then ( ( queryType ) => {
132+ const queryContent = getXML ( DSS_TEMPLATES . ADD_FULL_QUERY , {
133+ ...formValues ,
134+ isExpression : queryType === "expression"
135+ } ) ;
136+
137+ return rpcClient . getMiDiagramRpcClient ( ) . applyEdit ( {
138+ text : queryContent ,
139+ documentUri,
140+ range : {
141+ start : {
142+ line : range . end . line ,
143+ character : range . end . character ,
144+ } ,
145+ end : {
146+ line : range . end . line ,
147+ character : range . end . character ,
148+ }
149+ }
150+ } ) ;
151+ } ) ;
152+
153+ } ;
154+
107155export const onResourceEdit = ( data : ResourceFormData , selectedResource : any , documentUri : string , rpcClient : RpcClient ) => {
108156 const resourceStartTagRange = selectedResource . range . startTagRange ;
109157 const descriptionStartTagRange = selectedResource . description ? selectedResource . description . range . startTagRange : undefined ;
110158 const descriptionEndTagRange = selectedResource . description ? selectedResource . description . range . endTagRange : undefined ;
159+ const referenceQueryTagRange = selectedResource . callQuery ? selectedResource . callQuery . range . startTagRange : undefined ;
160+ const isReferenceSelfClosed = selectedResource . callQuery . selfClosed ?? true ;
111161 const formValues = {
112162 path : data . resourcePath ,
113163 method : data . resourceMethod ,
@@ -118,6 +168,7 @@ export const onResourceEdit = (data: ResourceFormData, selectedResource: any, do
118168 const resourceXML = getXML ( DSS_TEMPLATES . EDIT_RESOURCE , formValues ) ;
119169 const descriptionXML = data . description === "" ? "" :
120170 getXML ( DSS_TEMPLATES . EDIT_DESCRIPTION , { description : data . description } ) ;
171+ const queryReferenceXML = getXML ( DSS_TEMPLATES . EDIT_QUERY_REFERENCE , { queryId : data . queryId , isSelfClosed : isReferenceSelfClosed } ) ;
121172
122173 rpcClient . getMiDiagramRpcClient ( ) . applyEdit ( {
123174 text : resourceXML ,
@@ -132,13 +183,21 @@ export const onResourceEdit = (data: ResourceFormData, selectedResource: any, do
132183 end : descriptionEndTagRange ? descriptionEndTagRange . end : resourceStartTagRange . end
133184 }
134185 } )
135- } )
186+ } ) . then ( async ( ) => {
187+ await rpcClient . getMiDiagramRpcClient ( ) . applyEdit ( {
188+ text : queryReferenceXML ,
189+ documentUri : documentUri ,
190+ range : referenceQueryTagRange
191+ } )
192+ } ) ;
136193} ;
137194
138195export const onOperationEdit = ( data : OperationFormData , selectedOperation : any , documentUri : string , rpcClient : RpcClient ) => {
139196 const operationStartTagRange = selectedOperation . range . startTagRange ;
140197 const descriptionStartTagRange = selectedOperation . description ? selectedOperation . description . range . startTagRange : undefined ;
141198 const descriptionEndTagRange = selectedOperation . description ? selectedOperation . description . range . endTagRange : undefined ;
199+ const referenceQueryTagRange = selectedOperation . callQuery ? selectedOperation . callQuery . range . startTagRange : undefined ;
200+ const isReferenceSelfClosed = selectedOperation . callQuery . selfClosed ?? true ;
142201 const formValues = {
143202 name : data . operationName ,
144203 enableStreaming : data . enableStreaming ? undefined : ! data . enableStreaming ,
@@ -147,6 +206,7 @@ export const onOperationEdit = (data: OperationFormData, selectedOperation: any,
147206 const operationXML = getXML ( DSS_TEMPLATES . EDIT_OPERATION , formValues ) ;
148207 const descriptionXML = data . description === "" ? "" :
149208 getXML ( DSS_TEMPLATES . EDIT_DESCRIPTION , { description : data . description } ) ;
209+ const queryReferenceXML = getXML ( DSS_TEMPLATES . EDIT_QUERY_REFERENCE , { queryId : data . queryId , isSelfClosed : isReferenceSelfClosed } ) ;
150210
151211 rpcClient . getMiDiagramRpcClient ( ) . applyEdit ( {
152212 text : operationXML ,
@@ -160,6 +220,62 @@ export const onOperationEdit = (data: OperationFormData, selectedOperation: any,
160220 start : descriptionStartTagRange ? descriptionStartTagRange . start : operationStartTagRange . end ,
161221 end : descriptionEndTagRange ? descriptionEndTagRange . end : operationStartTagRange . end
162222 }
163- } )
223+ } ) . then ( async ( ) => {
224+ await rpcClient . getMiDiagramRpcClient ( ) . applyEdit ( {
225+ text : queryReferenceXML ,
226+ documentUri : documentUri ,
227+ range : referenceQueryTagRange
228+ } )
229+ } ) ;
164230 } )
165231} ;
232+
233+ export const onQueryEdit = ( data : QueryFormData , selectedQuery : any , documentUri : string , rpcClient : RpcClient ) => {
234+ const sqlStartTagRange = selectedQuery . sql ? selectedQuery . sql . range . startTagRange : undefined ;
235+ const sqlEndTagRange = selectedQuery . sql ? selectedQuery . sql . range . endTagRange : undefined ;
236+ const formValues = {
237+ name : data . name ,
238+ datasource : data . datasource ,
239+ query : data . query
240+ } ;
241+
242+ getQueryType ( rpcClient , documentUri , data . datasource )
243+ . then ( ( queryType ) => {
244+ return rpcClient . getMiDiagramRpcClient ( ) . applyEdit ( {
245+ text : getXML ( DSS_TEMPLATES . UPDATE_QUERY_CONFIG , formValues ) ,
246+ documentUri,
247+ range : selectedQuery . range . startTagRange ,
248+ } ) . then ( ( ) => ( { queryType } ) ) ;
249+ } )
250+ . then ( ( { queryType } ) => {
251+ return rpcClient . getMiDiagramRpcClient ( ) . applyEdit ( {
252+ text : getXML ( DSS_TEMPLATES . UPDATE_QUERY , {
253+ query : data . query ,
254+ isExpression : queryType === "expression"
255+ } ) ,
256+ documentUri,
257+ range : {
258+ start : sqlStartTagRange ? sqlStartTagRange . start : selectedQuery . range . startTagRange . end ,
259+ end : sqlEndTagRange ? sqlEndTagRange . end : selectedQuery . range . startTagRange . end
260+ }
261+ } ) ;
262+ } ) ;
263+
264+ } ;
265+
266+ async function getQueryType ( rpcClient : RpcClient , documentUri : string , datasource : string ) : Promise < string > {
267+ let queryType = "sql" ;
268+ const existingDataService = await rpcClient . getMiDiagramRpcClient ( ) . getDataService ( { path : documentUri } ) ;
269+ existingDataService . datasources . forEach ( ( ds ) => {
270+ if ( ds . dataSourceName === datasource ) {
271+ const propertyKeys : string [ ] = [ ] ;
272+ ds . datasourceProperties . forEach ( ( attr : any ) => {
273+ propertyKeys . push ( attr . key ) ;
274+ } ) ;
275+ if ( propertyKeys . includes ( "mongoDB_servers" ) ) {
276+ queryType = "expression" ;
277+ }
278+ }
279+ } ) ;
280+ return queryType ;
281+ }
0 commit comments