-
Notifications
You must be signed in to change notification settings - Fork 3
TASK-1816943: Added support for startingFields for WKWebView based en… #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
ec7a2da
8b64205
9c74514
e32a89b
634eaa9
30b9313
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| package com.pega.constellation.sdk.kmp.core | ||
|
|
||
| import kotlinx.serialization.KSerializer | ||
| import kotlinx.serialization.Serializable | ||
| import kotlinx.serialization.SerializationException | ||
| import kotlinx.serialization.descriptors.SerialDescriptor | ||
| import kotlinx.serialization.descriptors.buildClassSerialDescriptor | ||
| import kotlinx.serialization.encoding.Decoder | ||
| import kotlinx.serialization.encoding.Encoder | ||
| import kotlinx.serialization.json.JsonDecoder | ||
| import kotlinx.serialization.json.JsonEncoder | ||
| import kotlinx.serialization.json.JsonObject | ||
| import kotlinx.serialization.json.JsonPrimitive | ||
| import kotlinx.serialization.json.buildJsonObject | ||
| import kotlinx.serialization.json.jsonObject | ||
| import kotlinx.serialization.json.jsonPrimitive | ||
|
|
||
| /** | ||
| * Represents various actions that can be performed using the Constellation SDK. | ||
| */ | ||
| @Serializable(with = ConstellationSdkKActionSerializer::class) | ||
| sealed class ConstellationSdkAction { | ||
|
|
||
| data class CreateCase( | ||
| val caseClassName: String, | ||
| val startingFields: JsonObject? = null | ||
| ) : ConstellationSdkAction() | ||
|
|
||
| data class OpenAssignment( | ||
| val assignmentId: String | ||
| ) : ConstellationSdkAction() | ||
| } | ||
|
|
||
|
|
||
| object ConstellationSdkKActionSerializer : KSerializer<ConstellationSdkAction> { | ||
| override val descriptor: SerialDescriptor = buildClassSerialDescriptor("ConstellationSdkAction") | ||
|
|
||
| override fun serialize(encoder: Encoder, value: ConstellationSdkAction) { | ||
| val jsonEncoder = encoder as? JsonEncoder | ||
| ?: throw SerializationException("This class can be serialized only by JSON") | ||
|
|
||
| val jsonObject = when (value) { | ||
| is ConstellationSdkAction.CreateCase -> { | ||
| buildJsonObject { | ||
| put("actionType", JsonPrimitive("CreateCase")) | ||
| put("caseClassName", JsonPrimitive(value.caseClassName)) | ||
| value.startingFields?.let { put("startingFields", it) } | ||
| } | ||
| } | ||
| is ConstellationSdkAction.OpenAssignment -> { | ||
| buildJsonObject { | ||
| put("actionType", JsonPrimitive("OpenAssignment")) | ||
| put("assignmentId", JsonPrimitive(value.assignmentId)) | ||
| } | ||
| } | ||
| } | ||
| jsonEncoder.encodeJsonElement(jsonObject) | ||
| } | ||
|
|
||
| override fun deserialize(decoder: Decoder): ConstellationSdkAction { | ||
| val jsonDecoder = decoder as? JsonDecoder | ||
| ?: throw SerializationException("This class can be deserialized only by JSON") | ||
|
|
||
| val json = jsonDecoder.decodeJsonElement().jsonObject | ||
| val actionType = json["actionType"]?.jsonPrimitive?.content | ||
| ?: throw SerializationException("Missing 'actionType' field") | ||
|
|
||
| return when (actionType) { | ||
| "CreateCase" -> { | ||
| val caseClassName = json["caseClassName"]?.jsonPrimitive?.content | ||
| ?: throw SerializationException("Missing 'caseClassName'") | ||
| val startingFields = json["startingFields"]?.jsonObject | ||
| ConstellationSdkAction.CreateCase(caseClassName, startingFields) | ||
| } | ||
| "OpenAssignment" -> { | ||
| val assignmentId = json["assignmentId"]?.jsonPrimitive?.content | ||
| ?: throw SerializationException("Missing 'assignmentId'") | ||
| ConstellationSdkAction.OpenAssignment(assignmentId) | ||
| } | ||
| else -> throw SerializationException("Unknown actionType: $actionType") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,17 @@ | ||
| package com.pega.constellation.sdk.kmp.core | ||
|
|
||
| import kotlinx.serialization.Serializable | ||
| import kotlinx.serialization.json.Json | ||
|
|
||
| @Serializable | ||
| data class EngineConfiguration( | ||
|
||
| val url: String, | ||
| val version: String, | ||
| val action: ConstellationSdkAction, | ||
| val debuggable: Boolean | ||
| ) { | ||
| fun toJsonString(): String = Json.encodeToString(this) | ||
| } | ||
|
|
||
| /** | ||
| * Constellation SDK Engine that orchestrates Pega application logic. | ||
|
|
@@ -12,9 +24,9 @@ interface ConstellationSdkEngine { | |
| fun configure(config: ConstellationSdkConfig, handler: EngineEventHandler) | ||
|
|
||
| /** | ||
| * Creates a case of the specified class name with the provided starting fields. | ||
| * Performs the specified action using the Constellation SDK. | ||
| */ | ||
| fun createCase(caseClassName: String, startingFields: Map<String, Any>) | ||
| fun performAction(action: ConstellationSdkAction) | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package com.pega.constellation.sdk.kmp.core.internal | ||
|
|
||
| import com.pega.constellation.sdk.kmp.core.Log | ||
| import kotlinx.serialization.json.JsonArray | ||
| import kotlinx.serialization.json.JsonElement | ||
| import kotlinx.serialization.json.JsonObject | ||
| import kotlinx.serialization.json.JsonPrimitive | ||
|
|
||
| private const val TAG = "JsonHelper" | ||
|
|
||
| fun Any.toJsonElement(): JsonElement? = when (this) { | ||
|
||
| is String -> JsonPrimitive(this) | ||
| is Int -> JsonPrimitive(this) | ||
| is Double -> JsonPrimitive(this) | ||
| is Float -> JsonPrimitive(this) | ||
| is Boolean -> JsonPrimitive(this) | ||
| is List<*> -> { | ||
| val jsonElements = this.mapNotNull { | ||
| it?.toJsonElement() ?: run { | ||
| val type = it?.let { it::class } ?: "null" | ||
| Log.w(TAG, "Unsupported type $type, cannot convert to JsonElement.") | ||
|
||
| null | ||
| } | ||
| } | ||
| JsonArray(jsonElements) | ||
| } | ||
| is Map<*, *> -> { | ||
| val map = this.entries.mapNotNull { (key, value) -> | ||
| (key as? String)?.let { k -> | ||
| value?.toJsonElement()?.let { v -> k to v } | ||
| } ?: run { | ||
| val keyType = key?.let { it::class } ?: "null" | ||
| val valueType = value?.let { it::class } ?: "null" | ||
| Log.w(TAG, "Unsupported key type <$keyType> or value type <$valueType>, cannot convert to JsonElement.") | ||
| null | ||
| } | ||
| }.toMap() | ||
| JsonObject(map) | ||
| } | ||
| else -> { | ||
| Log.w(TAG, "Unsupported type ${this::class}, cannot convert to JsonElement.") | ||
| null | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ import { initPlatforms } from './init-platforms.js'; | |
| import { initialRender } from './initial-render.js'; | ||
| import { bootstrap } from './bootstrap.js' | ||
| import { createCase } from './create-case.js'; | ||
| import { openAssignment } from './open-assignment.js'; | ||
| import { getSdkComponentMap } from '../dxcomponents/mappings/sdk-component-map.js'; | ||
| import { bridge } from '../bridge/native-bridge.js'; | ||
| import { subscribeForEvents } from './init-events.js'; | ||
|
|
@@ -17,11 +18,18 @@ async function init(sdkConfig, componentsOverridesStr) { | |
| initPlatforms(componentsOverridesStr); | ||
| const config = JSON.parse(sdkConfig); | ||
| await bootstrap(config.url, config.version, onPCoreReady); | ||
| await createCase(config.caseClassName, config.startingFields); | ||
| if (config.action.actionType === "CreateCase") { | ||
| await createCase(config.action.caseClassName, config.action.startingFields); | ||
| } else if (config.action.actionType === "OpenAssignment") { | ||
| await openAssignment(config.action.assignmentId); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that it is okay for now, but in the future we should consider making it possible to "interact" with the application/engine instead of just reinitializing everything each time we want to open or create something. |
||
| } else { | ||
| const errorMessage = "Unknown action type: " + config.action.actionType; | ||
| throw new Error(errorMessage); | ||
| } | ||
| console.log(TAG, "Constellation SDK initialization completed"); | ||
| bridge.onReady(); | ||
| } catch (error) { | ||
| const errorMessage = "Constellation SDK initialization failed! " + (error?.message ?? "") | ||
| const errorMessage = "Constellation SDK initialization failed! " + (error?.message ?? ""); | ||
| console.error(errorMessage); | ||
| bridge.onError("InitError", errorMessage); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| export async function openAssignment(assignmentId) { | ||
|
|
||
| const options = { | ||
| pageName: 'pyEmbedAssignment' | ||
| }; | ||
|
|
||
| console.log("[OpenAssignment]", "Opening assignment: " + assignmentId); | ||
| await PCore.getMashupApi().openAssignment(assignmentId, PCore.getConstants().APP.APP, options); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wow, that's so simple 😎 |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most probably this custom serializer is not needed as kotlin should handle serialization of sealed classes out of the box. You just need to add
@Serialiableto sealed class and subclasses and usetypeas a class discriminator instead ofactionType.