routerPort: getRouterPortForConnection()

in gui/extension/src/MRSCommandHandler.ts [685:1003]


                                        routerPort: getRouterPortForConnection(connectionId),
                                        connectionId,
                                        connectionDetails: entry.connection.details,
                                        directory: value.fsPath,
                                    },
                                },
                            ], "newConnection");
                        }
                    } catch (e) {
                        // do nothing
                    }
                }
            }));

        context.subscriptions.push(commands.registerCommand("msg.mrs.rebuildMrsSdk",
            async (directory?: Uri) => {
                if (directory) {
                    const connection = await host.determineConnection(DBType.MySQL);
                    if (connection) {
                        const sqlEditor = new ShellInterfaceSqlEditor();
                        const statusbarItem = window.createStatusBarItem();
                        try {
                            statusbarItem.text = "$(loading~spin) Starting Database Session ...";
                            statusbarItem.show();

                            statusbarItem.text = "$(loading~spin) Starting Database Session ...";
                            await sqlEditor.startSession(String(connection.details.id) + "MrsSdkGeneration");

                            statusbarItem.text = "$(loading~spin) Opening Database Connection ...";
                            await openSqlEditorConnection(sqlEditor, connection.details.id, (message) => {
                                statusbarItem.text = "$(loading~spin) " + message;
                            });

                            await sqlEditor.mrs.dumpSdkServiceFiles(directory.fsPath);
                            void ui.showInformationMessage("MRS SDK Files exported successfully.", {});
                        } catch (reason) {
                            const message = convertErrorToString(reason);
                            void ui.showErrorMessage("A error occurred when trying to show the MRS Static " +
                                `Content Set Dialog. Error: ${message}`, {});
                        } finally {
                            statusbarItem.hide();
                            await sqlEditor.closeSession();
                        }
                    }
                }
            }));

        context.subscriptions.push(commands.registerCommand("msg.mrs.deleteSchema",
            async (entry?: ICdmRestSchemaEntry) => {
                if (entry) {
                    const connection = entry.connection;
                    const answer = await ui.showInformationMessage(`Are you sure the MRS schema ` +
                        `${entry.details.name} should be deleted?`, {}, "Yes", "No");

                    if (answer === "Yes") {
                        try {
                            await connection.backend.mrs.deleteSchema(entry.details.id, entry.details.serviceId);
                            await commands.executeCommand("msg.refreshConnections");
                            void ui.showInformationMessage("The MRS schema has been deleted successfully.", {});
                        } catch (reason) {
                            const message = convertErrorToString(reason);
                            void ui.showErrorMessage(`Error removing an MRS schema: ${message}`, {});
                        }
                    }
                }
            }));

        context.subscriptions.push(commands.registerCommand("msg.mrs.editSchema",
            (entry?: ICdmRestSchemaEntry) => {
                if (entry) {
                    const connectionId = entry.connection.details.id;
                    const provider = this.#host.currentProvider;
                    if (provider) {
                        void provider.runCommand("job", [
                            { requestType: "showPage", parameter: { connectionId } },
                            {
                                requestType: "showMrsSchemaDialog",
                                parameter: { schemaName: entry.details.name, schema: entry.details },
                            },
                        ], "newConnection");
                    }
                }
            }));

        context.subscriptions.push(commands.registerCommand("msg.mrs.addSchema",
            async (entry?: ICdmSchemaEntry) => {
                if (entry) {
                    const connection = entry.parent;
                    const connectionId = connection.details.id;
                    const provider = this.#host.currentProvider;
                    if (provider) {
                        // Check if there is at least one MRS Service
                        const services = await connection.backend.mrs.listServices();

                        if (services.length > 0) {
                            void provider.runCommand("job", [
                                { requestType: "showPage", parameter: { connectionId } },
                                { requestType: "showMrsSchemaDialog", parameter: { schemaName: entry.caption } },
                            ], "newConnection");
                        } else {
                            void ui.showErrorMessage(`Please create a REST Service before adding a DB Schema.`, {});
                        }
                    }
                }
            }));

        context.subscriptions.push(commands.registerCommand("msg.mrs.copyDbObjectRequestPath",
            async (entry?: ICdmRestDbObjectEntry) => {
                if (entry) {
                    const dbObjectPath = this.buildDbObjectRequestPath(entry);
                    if (dbObjectPath) {
                        await env.clipboard.writeText(String(dbObjectPath));
                        void ui.showInformationMessage("The DB Object Path was copied to the system clipboard", {});
                    }
                }
            }));

        context.subscriptions.push(commands.registerCommand("msg.mrs.openDbObjectRequestPath",
            async (entry?: ICdmRestDbObjectEntry) => {
                if (entry) {
                    const dbObjectPath = this.buildDbObjectRequestPath(entry);
                    if (dbObjectPath) {
                        await env.openExternal(dbObjectPath);
                    }
                }
            }));

        context.subscriptions.push(commands.registerCommand("msg.mrs.openContentFileRequestPath",
            async (entry?: ICdmRestContentFileEntry) => {
                if (entry) {
                    if (entry.details) {
                        const connection = entry.connection;
                        const o = entry.details;
                        const port = getRouterPortForConnection(connection.details.id);
                        let url = (o.hostCtx ?? "") + (o.contentSetRequestPath ?? "") + o.requestPath;

                        if (url.startsWith("/")) {
                            url = `https://localhost:${port}${url}`;
                        } else {
                            url = `https://${url}`;
                        }

                        await env.openExternal(Uri.parse(url));
                    }
                }
            }));

        context.subscriptions.push(commands.registerCommand("msg.mrs.deleteDbObject",
            async (entry?: ICdmRestDbObjectEntry) => {
                if (entry) {
                    const connection = entry.connection;
                    if (entry.details) {
                        const answer = await ui.showInformationMessage(`Are you sure you want to delete the REST ` +
                            `DB Object ${entry.details.name}?`,
                            { modal: true, detail: "This operation cannot be reverted!" }, "Delete DB Object",
                            "Cancel");

                        if (answer === "Delete DB Object") {
                            try {
                                await connection.backend.mrs.deleteDbObject(entry.details.id);

                                // TODO: refresh only the affected connection.
                                void commands.executeCommand("msg.refreshConnections");
                                void ui.showInformationMessage(`The REST DB Object ${entry.details.name} has ` +
                                    `been deleted.`, {});
                            } catch (reason) {
                                const message = convertErrorToString(reason);
                                void ui.showErrorMessage(`Error deleting the REST DB Object: ${message}`, {});
                            }
                        }
                    }
                }
            }));

        context.subscriptions.push(commands.registerCommand("msg.mrs.editAuthApp",
            (entry?: ICdmRestAuthAppEntry) => {
                if (entry) {
                    const connection = entry.connection;
                    const connectionId = connection.details.id;
                    const provider = this.#host.currentProvider;
                    if (provider) {
                        void provider.runCommand("job", [
                            { requestType: "showPage", parameter: { connectionId } },
                            { requestType: "showMrsAuthAppDialog", parameter: { authApp: entry.details } },
                        ], "newConnection");
                    }
                }
            }));

        context.subscriptions.push(commands.registerCommand("msg.mrs.addAndLinkAuthApp",
            (entry?: ICdmRestServiceEntry) => {
                try {
                    if (entry) {
                        const connection = entry.connection;
                        if (entry.details) {
                            const connectionId = connection.details.id;
                            const provider = this.#host.currentProvider;
                            if (provider) {
                                void provider.runCommand("job", [
                                    { requestType: "showPage", parameter: { connectionId } },
                                    { requestType: "showMrsAuthAppDialog", parameter: { service: entry.details } },
                                ], "newConnection");
                            }
                        }
                    }
                } catch (reason) {
                    const message = convertErrorToString(reason);
                    void ui.showErrorMessage(`Error while adding a new MRS Authentication App: ${message}`, {});
                }
            }));

        context.subscriptions.push(commands.registerCommand("msg.mrs.addAuthApp",
            (entry?: ICdmRestAuthAppGroupEntry) => {
                try {
                    if (entry) {
                        const connection = entry.connection;
                        const connectionId = connection.details.id;
                        const provider = this.#host.currentProvider;
                        if (provider) {
                            void provider.runCommand("job", [
                                { requestType: "showPage", parameter: { connectionId } },
                                { requestType: "showMrsAuthAppDialog", parameter: {} },
                            ], "newConnection");
                        }
                    }
                } catch (reason) {
                    const message = convertErrorToString(reason);
                    void ui.showErrorMessage(`Error while adding a new MRS Authentication App: ${message}`, {});
                }
            }));

        context.subscriptions.push(commands.registerCommand("msg.mrs.linkAuthApp",
            async (entry?: ICdmRestServiceEntry) => {
                try {
                    if (entry) {
                        const connection = entry.connection;
                        const apps = await connection.backend.mrs.listAuthApps();

                        const items = apps.map((app) => {
                            return app.name!;
                        });

                        const selection = await window.showQuickPick(items, {
                            title: "Select a REST Authentication app to link it to this service.",
                            matchOnDescription: true,
                            placeHolder: "Type the name of an existing MRS Service",
                        });

                        const app = apps.find((candidate) => {
                            return candidate.name === selection;
                        });

                        if (app !== undefined) {
                            try {
                                await connection.backend.mrs.linkAuthAppToService(app.id!, entry.id);
                                this.connectionsProvider.refresh(entry);
                                void ui.showInformationMessage("The MRS Authentication App has " +
                                    `been linked to service ${entry.details.name}`, {});
                            } catch (reason) {
                                const message = convertErrorToString(reason);
                                void ui.showErrorMessage("Error linking an MRS Authentication App: " +
                                    `${message}`, {});
                            }
                        }
                    }
                } catch (reason) {
                    const message = convertErrorToString(reason);
                    void ui.showErrorMessage(`Error while adding a new MRS Authentication App: ${message}`, {});
                }
            }));

        context.subscriptions.push(commands.registerCommand("msg.mrs.unlinkAuthApp",
            async (entry?: ICdmRestServiceAuthAppEntry) => {
                if (entry) {
                    const prompt = `Are you sure the MRS authentication app "${entry.details.name}" should ` +
                        `be unlinked from the service "${entry.parent.caption}"?`;
                    const answer = await ui.showInformationMessage(prompt, {}, "Yes", "No");

                    if (answer === "Yes") {
                        await entry.connection.backend.mrs.unlinkAuthAppFromService(entry.details.id!,
                            entry.parent.details.id);

                        this.connectionsProvider.refresh(entry.parent);
                        void ui.showInformationMessage(`The MRS Authentication App ` +
                            `"${entry.details.name}" has been unlinked.`, {});
                    }
                }
            }));

        context.subscriptions.push(commands.registerCommand("msg.mrs.addContentSet",
            (entry?: ICdmRestContentSetEntry) => {
                try {
                    if (entry) {
                        const connectionId = entry.connection.details.id;
                        const provider = this.#host.currentProvider;
                        if (provider) {
                            void provider.runCommand("job", [
                                { requestType: "showPage", parameter: { connectionId } },
                                { requestType: "showMrsContentSetDialog", parameter: {} },
                            ], "newConnection");
                        }
                    }
                } catch (reason) {
                    const message = convertErrorToString(reason);
                    void ui.showErrorMessage(`Error while adding a new MRS Content Set: ${message}`, {});
                }
            }));

        context.subscriptions.push(commands.registerCommand("msg.mrs.addOpenApiUiContentSet",
            (entry?: ICdmRestServiceEntry) => {
                try {
                    if (entry) {
                        const connectionId = entry.connection.details.id;
                        const provider = this.#host.currentProvider;
                        if (provider) {
                            void provider.runCommand("job", [
                                { requestType: "showPage", parameter: { connectionId } },
                                {
                                    requestType: "showMrsContentSetDialog", parameter: {