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,23 @@ export const onResourceEdit = (data: ResourceFormData, selectedResource: any, do
132183 end : descriptionEndTagRange ? descriptionEndTagRange . end : resourceStartTagRange . end
133184 }
134185 } )
135- } )
186+ } ) . then ( async ( ) => {
187+ if ( referenceQueryTagRange ) {
188+ await rpcClient . getMiDiagramRpcClient ( ) . applyEdit ( {
189+ text : queryReferenceXML ,
190+ documentUri : documentUri ,
191+ range : referenceQueryTagRange
192+ } )
193+ }
194+ } ) ;
136195} ;
137196
138197export const onOperationEdit = ( data : OperationFormData , selectedOperation : any , documentUri : string , rpcClient : RpcClient ) => {
139198 const operationStartTagRange = selectedOperation . range . startTagRange ;
140199 const descriptionStartTagRange = selectedOperation . description ? selectedOperation . description . range . startTagRange : undefined ;
141200 const descriptionEndTagRange = selectedOperation . description ? selectedOperation . description . range . endTagRange : undefined ;
201+ const referenceQueryTagRange = selectedOperation . callQuery ? selectedOperation . callQuery . range . startTagRange : undefined ;
202+ const isReferenceSelfClosed = selectedOperation . callQuery ?. selfClosed ?? true ;
142203 const formValues = {
143204 name : data . operationName ,
144205 enableStreaming : data . enableStreaming ? undefined : ! data . enableStreaming ,
@@ -147,6 +208,7 @@ export const onOperationEdit = (data: OperationFormData, selectedOperation: any,
147208 const operationXML = getXML ( DSS_TEMPLATES . EDIT_OPERATION , formValues ) ;
148209 const descriptionXML = data . description === "" ? "" :
149210 getXML ( DSS_TEMPLATES . EDIT_DESCRIPTION , { description : data . description } ) ;
211+ const queryReferenceXML = getXML ( DSS_TEMPLATES . EDIT_QUERY_REFERENCE , { queryId : data . queryId , isSelfClosed : isReferenceSelfClosed } ) ;
150212
151213 rpcClient . getMiDiagramRpcClient ( ) . applyEdit ( {
152214 text : operationXML ,
@@ -160,6 +222,64 @@ export const onOperationEdit = (data: OperationFormData, selectedOperation: any,
160222 start : descriptionStartTagRange ? descriptionStartTagRange . start : operationStartTagRange . end ,
161223 end : descriptionEndTagRange ? descriptionEndTagRange . end : operationStartTagRange . end
162224 }
163- } )
225+ } ) . then ( async ( ) => {
226+ if ( referenceQueryTagRange ) {
227+ await rpcClient . getMiDiagramRpcClient ( ) . applyEdit ( {
228+ text : queryReferenceXML ,
229+ documentUri : documentUri ,
230+ range : referenceQueryTagRange
231+ } )
232+ }
233+ } ) ;
164234 } )
165235} ;
236+
237+ export const onQueryEdit = ( data : QueryFormData , selectedQuery : any , documentUri : string , rpcClient : RpcClient ) => {
238+ const sqlStartTagRange = selectedQuery . sql ? selectedQuery . sql . range . startTagRange : undefined ;
239+ const sqlEndTagRange = selectedQuery . sql ? selectedQuery . sql . range . endTagRange : undefined ;
240+ const formValues = {
241+ name : data . name ,
242+ datasource : data . datasource ,
243+ query : data . query
244+ } ;
245+
246+ getQueryType ( rpcClient , documentUri , data . datasource )
247+ . then ( ( queryType ) => {
248+ return rpcClient . getMiDiagramRpcClient ( ) . applyEdit ( {
249+ text : getXML ( DSS_TEMPLATES . UPDATE_QUERY_CONFIG , formValues ) ,
250+ documentUri,
251+ range : selectedQuery . range . startTagRange ,
252+ } ) . then ( ( ) => ( { queryType } ) ) ;
253+ } )
254+ . then ( ( { queryType } ) => {
255+ return rpcClient . getMiDiagramRpcClient ( ) . applyEdit ( {
256+ text : getXML ( DSS_TEMPLATES . UPDATE_QUERY , {
257+ query : data . query ,
258+ isExpression : queryType === "expression"
259+ } ) ,
260+ documentUri,
261+ range : {
262+ start : sqlStartTagRange ? sqlStartTagRange . start : selectedQuery . range . startTagRange . end ,
263+ end : sqlEndTagRange ? sqlEndTagRange . end : selectedQuery . range . startTagRange . end
264+ }
265+ } ) ;
266+ } ) ;
267+
268+ } ;
269+
270+ async function getQueryType ( rpcClient : RpcClient , documentUri : string , datasource : string ) : Promise < string > {
271+ let queryType = "sql" ;
272+ const existingDataService = await rpcClient . getMiDiagramRpcClient ( ) . getDataService ( { path : documentUri } ) ;
273+ existingDataService . datasources . forEach ( ( ds ) => {
274+ if ( ds . dataSourceName === datasource ) {
275+ const propertyKeys : string [ ] = [ ] ;
276+ ds . datasourceProperties . forEach ( ( attr : any ) => {
277+ propertyKeys . push ( attr . key ) ;
278+ } ) ;
279+ if ( propertyKeys . includes ( "mongoDB_servers" ) ) {
280+ queryType = "expression" ;
281+ }
282+ }
283+ } ) ;
284+ return queryType ;
285+ }
0 commit comments