Skip to content

Commit

Permalink
#42 Combine XML (Grid) and JSON based Layout in DisplayList/Object 2/n
Browse files Browse the repository at this point in the history
  • Loading branch information
joerg-rade committed Mar 12, 2020
1 parent fec2981 commit 0f759b4
Show file tree
Hide file tree
Showing 32 changed files with 478 additions and 168 deletions.
5 changes: 2 additions & 3 deletions src/main/kotlin/org/ro/core/aggregator/ActionDispatcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import org.ro.to.Method
import org.ro.ui.Point
import org.ro.ui.kv.ActionPrompt
import org.ro.utils.Utils
import org.w3c.dom.MimeType

class ActionDispatcher(private val at: Point = Point(100, 100)) : BaseAggregator() {

override fun update(logEntry: LogEntry, mimeType: String) {
override fun update(logEntry: LogEntry, subType: String) {
val action = logEntry.getTransferObject() as Action
action.links.forEach { link ->
// it.rel should be neither: (self | up | describedBy )
Expand All @@ -36,7 +35,7 @@ class ActionDispatcher(private val at: Point = Point(100, 100)) : BaseAggregator
if (link.hasArguments()) {
ActionPrompt(action).open(at)
} else {
invoke(link)
link.invokeWith(this)
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/org/ro/core/aggregator/BaseAggregator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ abstract class BaseAggregator {

open lateinit var dsp: BaseDisplayable

open fun update(logEntry: LogEntry, mimeType: String) {}
open fun update(logEntry: LogEntry, subType: String) {}

open fun reset(): BaseAggregator {
/* do nothing and */ return this
Expand Down Expand Up @@ -60,8 +60,8 @@ abstract class BaseAggregator {
// save a line break in when formatting
}

fun Link.invokeWith(aggregator: BaseAggregator, mimeType: String = "json") {
RoXmlHttpRequest().invoke(this, aggregator, mimeType)
fun Link.invokeWith(aggregator: BaseAggregator, subType: String = "json") {
RoXmlHttpRequest().invoke(this, aggregator, subType)
}

}
3 changes: 1 addition & 2 deletions src/main/kotlin/org/ro/core/aggregator/DiagramDispatcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package org.ro.core.aggregator

import org.ro.core.event.LogEntry
import org.ro.utils.DomHelper
import org.w3c.dom.MimeType

class DiagramDispatcher(private val uuid: String) : BaseAggregator() {

override fun update(logEntry: LogEntry, mimeType: String) {
override fun update(logEntry: LogEntry, subType: String) {
val response = logEntry.response
DomHelper.appendTo(response, uuid)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class DomainTypesAggregator(val url: String) : BaseAggregator() {
dsp = DiagramDisplay(url)
}

override fun update(logEntry: LogEntry, mimeType: String) {
override fun update(logEntry: LogEntry, subType: String) {
when (val obj = logEntry.getTransferObject()) {
is DomainTypes -> handleDomainTypes(obj)
is DomainType -> handleDomainType(obj)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.ro.ui.FileAlert

class DownloadDispatcher(val actionTitle: String) : BaseAggregator() {

override fun update(logEntry: LogEntry, mimeType: String) {
override fun update(logEntry: LogEntry, subType: String) {
FileAlert(logEntry).open()
}

Expand Down
3 changes: 1 addition & 2 deletions src/main/kotlin/org/ro/core/aggregator/ErrorDispatcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import org.ro.core.event.LogEntry
import org.ro.core.event.ResourceSpecification
import org.ro.to.HttpError
import org.ro.ui.ErrorAlert
import org.w3c.dom.MimeType

class ErrorDispatcher : BaseAggregator() {

override fun update(logEntry: LogEntry, mimeType: String) {
override fun update(logEntry: LogEntry, subType: String) {
val error = logEntry.getTransferObject() as HttpError
val url = logEntry.url
val message = error.message
Expand Down
14 changes: 7 additions & 7 deletions src/main/kotlin/org/ro/core/aggregator/ListAggregator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.ro.core.aggregator
import org.ro.core.event.LogEntry
import org.ro.core.model.DisplayList
import org.ro.layout.Layout
import org.ro.layout.PropertyLt
import org.ro.to.Property
import org.ro.to.ResultList
import org.ro.to.TObject
Expand All @@ -22,7 +23,7 @@ class ListAggregator(actionTitle: String) : BaseAggregator() {
dsp = DisplayList(actionTitle)
}

override fun update(logEntry: LogEntry, mimeType: String) {
override fun update(logEntry: LogEntry, subType: String) {

when (val obj = logEntry.getTransferObject()) {
null -> log(logEntry)
Expand Down Expand Up @@ -59,19 +60,18 @@ class ListAggregator(actionTitle: String) : BaseAggregator() {
}

private fun handleLayout(layout: Layout) {
dsp.layout = layout
layout.propertyList.forEach { p ->
val l = p.link!!
val dspl = dsp as DisplayList
dspl.addLayout(layout)
dspl.propertyList.forEach { p ->
val l = p.links.firstOrNull{it.rel == "describedby"}!!
if (!l.href.contains("datanucleus")) {
l.invokeWith(this)
}
}
}

private fun handleGrid(grid: Grid) {
// console.log("[LA.handleGrid]")
// console.log(grid)
// dsp.layout!!.initGrid(grid)
(dsp as DisplayList).grid = grid
}

private fun handleProperty(p: Property) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.ro.ui.kv.UiManager

class NavigationDispatcher() : BaseAggregator() {

override fun update(logEntry: LogEntry, mimeType: String) {
override fun update(logEntry: LogEntry, subType: String) {
val obj = logEntry.getTransferObject()
val result = obj as Menubars
UiManager.amendMenu(result)
Expand Down
17 changes: 13 additions & 4 deletions src/main/kotlin/org/ro/core/aggregator/ObjectAggregator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.ro.core.model.DisplayObject
import org.ro.layout.Layout
import org.ro.to.HttpError
import org.ro.to.Property
import org.ro.to.ResultObject
import org.ro.to.TObject
import org.ro.ui.ErrorAlert
import org.ro.ui.kv.UiManager
Expand All @@ -15,10 +16,11 @@ class ObjectAggregator(val actionTitle: String) : BaseAggregator() {
dsp = DisplayObject(actionTitle)
}

override fun update(logEntry: LogEntry, mimeType: String) {
override fun update(logEntry: LogEntry, subType: String) {

when (val obj = logEntry.getTransferObject()) {
is TObject -> handleObject(obj)
is ResultObject -> handleResultObject(obj)
is Property -> handleProperty(obj)
is Layout -> handleLayout(obj)
is HttpError -> ErrorAlert(logEntry).open()
Expand All @@ -35,6 +37,12 @@ class ObjectAggregator(val actionTitle: String) : BaseAggregator() {
obj.getLayoutLink()?.invokeWith(this)
}

fun handleResultObject(obj: ResultObject) {
console.log("[OA.handleResultObject]")
console.log(obj)
// dsp.addData(obj)
}

override fun getObject(): TObject? {
return dsp.getObject()
}
Expand All @@ -45,13 +53,14 @@ class ObjectAggregator(val actionTitle: String) : BaseAggregator() {
}

private fun handleLayout(layout: Layout) {
dsp.layout = layout
layout.propertyDescriptionList.forEach {
(dsp as DisplayObject).addLayout(layout)
//FIXME do analogous to DisplayList
/* layout.propertyDescriptionList.forEach {
it.links.forEach { l ->
//TODO correct link?
l.invokeWith(this)
}
}
} */
}

override fun reset(): ObjectAggregator {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.ro.to.Restful

class RestfulDispatcher() : BaseAggregator() {

override fun update(logEntry: LogEntry, mimeType: String) {
override fun update(logEntry: LogEntry, subType: String) {
val restful = logEntry.getTransferObject() as Restful
restful.links.forEach {
when {
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/org/ro/core/aggregator/SystemAggregator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class SystemAggregator() : BaseAggregator() {
dsp = DisplaySystem("not filled (yet)")
}

override fun update(logEntry: LogEntry, mimeType: String) {
override fun update(logEntry: LogEntry, subType: String) {

when (val obj = logEntry.getTransferObject()) {
is User -> dsp.addData(obj)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.ro.view.UndefinedAlert

class UndefinedDispatcher : BaseAggregator() {

override fun update(logEntry: LogEntry, mimeType: String) {
override fun update(logEntry: LogEntry, subType: String) {
UndefinedAlert(logEntry).open()
}

Expand Down
14 changes: 7 additions & 7 deletions src/main/kotlin/org/ro/core/event/EventStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org.ro.core.event

import org.ro.utils.Utils
import org.ro.core.aggregator.BaseAggregator
import org.ro.core.event.ResourceSpecification
import org.ro.to.TObject
import org.ro.ui.kv.UiManager
import pl.treksoft.kvision.panel.SimplePanel
Expand All @@ -29,9 +28,9 @@ object EventStore {

fun start(reSpec: ResourceSpecification,
method: String,
body: String = "",
body:String = "",
aggregator: BaseAggregator? = null): LogEntry {
val entry = LogEntry(reSpec.url, method, body, reSpec.mimeType)
val entry = LogEntry(reSpec.url, method, request = body, subType= reSpec.subType)
if (aggregator != null) {
entry.addAggregator(aggregator)
}
Expand Down Expand Up @@ -114,16 +113,17 @@ object EventStore {
}

internal fun findExact(reSpec: ResourceSpecification): LogEntry? {
return log.firstOrNull { it.url == reSpec.url && it.mimeType == reSpec.mimeType}
return log.firstOrNull { it.url == reSpec.url && it.subType == reSpec.subType}
}

internal fun findView(title: String): LogEntry? {
return log.firstOrNull { it.title == title && it.isView() }
}

internal fun findEquivalent(reSpec: ResourceSpecification): LogEntry? {
val url = reSpec.url
return log.firstOrNull { areEquivalent(it.url, url) }
return log.firstOrNull {
it.subType == reSpec.subType
&& areEquivalent(it.url, reSpec.url) }
}

private fun areEquivalent(searchUrl: String, compareUrl: String, allowedDiff: Int = 1): Boolean {
Expand Down Expand Up @@ -155,7 +155,7 @@ object EventStore {
val le = this.find(reSpec)
if (le != null) {
when {
le.hasResponse() && le.method == method && le.mimeType == reSpec.mimeType -> answer = true
le.hasResponse() && le.method == method && le.subType == reSpec.subType -> answer = true
le.isView() -> answer = true
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/org/ro/core/event/LogEntry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ data class LogEntry(
val url: String,
val method: String? = "",
val request: String = "",
val mimeType: String = "json",
val subType: String = "json",
@ContextualSerialization val createdAt: Date = Date()) {
var state = EventState.INITIAL
var title: String = ""
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/org/ro/core/event/ResourceSpecification.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package org.ro.core.event

class ResourceSpecification(
val url: String,
val mimeType: String = "Json")
val subType: String = "json")
12 changes: 6 additions & 6 deletions src/main/kotlin/org/ro/core/event/RoXmlHttpRequest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import org.w3c.xhr.XMLHttpRequest
*/
class RoXmlHttpRequest {

fun invoke(link: Link, aggregator: BaseAggregator?, mimeType: String = "json") {
fun invoke(link: Link, aggregator: BaseAggregator?, subType: String = "json") {
val reSpec = ResourceSpecification(link.href)
if (EventStore.isCached(reSpec, link.method)) {
processCached(reSpec)
} else {
process(link, aggregator, mimeType)
process(link, aggregator, subType)
}
}

Expand All @@ -30,7 +30,7 @@ class RoXmlHttpRequest {
EventStore.cached(reSpec)
}

private fun process(link: Link, aggregator: BaseAggregator?, mimeType: String) {
private fun process(link: Link, aggregator: BaseAggregator?, subType: String) {
val method = link.method
var url = link.href
if (method != Method.POST.operation) {
Expand All @@ -41,10 +41,10 @@ class RoXmlHttpRequest {
val xhr = XMLHttpRequest()
xhr.open(method, url, true)
xhr.setRequestHeader("Authorization", "Basic $credentials")
xhr.setRequestHeader("Content-Type", "application/$mimeType;charset=UTF-8")
xhr.setRequestHeader("Accept", "application/$mimeType")
xhr.setRequestHeader("Content-Type", "application/$subType;charset=UTF-8")
xhr.setRequestHeader("Accept", "application/$subType")

val reSpec = ResourceSpecification(url, mimeType)
val reSpec = ResourceSpecification(url, subType)
xhr.onload = { _ -> resultHandler(reSpec, xhr.responseText) }
xhr.onerror = { _ -> errorHandler(reSpec, xhr.responseText) }
xhr.ontimeout = { _ -> errorHandler(reSpec, xhr.responseText) }
Expand Down
38 changes: 33 additions & 5 deletions src/main/kotlin/org/ro/core/model/DisplayList.kt
Original file line number Diff line number Diff line change
@@ -1,36 +1,65 @@
package org.ro.core.model

import org.ro.layout.Layout
import org.ro.layout.PropertyLt
import org.ro.to.Extensions
import org.ro.to.Property
import org.ro.to.TObject
import org.ro.to.TransferObject
import org.ro.to.bs3.Grid
import pl.treksoft.kvision.state.observableListOf

class DisplayList(override val title: String) : BaseDisplayable() {
var data = observableListOf<Exposer>()
override var layout: Layout? = null
var grid: Grid? = null
var propertyDescriptionList = mutableMapOf<String, String>()
var propertyList = mutableListOf<Property>()
var propertyLayoutList = mutableListOf<PropertyLt>()

override fun canBeDisplayed(): Boolean {
when {
isRendered -> return false
layout == null -> return false
grid == null -> return false
else -> {
val lps = layout!!.propertyList.size
val lps = propertyList.size
val pds = propertyDescriptionList.size
// val ps = propertyList.size
// val ps = propertyList.size
val descriptionsComplete = lps <= pds
// val propertiesComplete = lps <= ps
// val propertiesComplete = lps <= ps
console.log("[DL.canBeDisplayed] layout.properties: $lps")
console.log("[DL.canBeDisplayed] propertyDescriptions: $pds")
// console.log("[DL.canBeDisplayed] properties: $ps")
// console.log("[DL.canBeDisplayed] properties: $ps")
return descriptionsComplete //&& propertiesComplete
}
}
}

fun addLayout(layout: Layout) {
this.layout = layout
layout.row.forEach { r ->
r.cols.forEach { cs ->
cs.col.fieldSet.forEach { fs ->
fs.property.forEach { p ->
propertyLayoutList.add(p)
}
}
cs.col.row.forEach { r ->
r.cols.forEach { cs ->
cs.col.fieldSet.forEach { fs ->
fs.property.forEach { p ->
propertyLayoutList.add(p)
}
}
}
}
}
}
console.log("[DL.addLayout]")
console.log(propertyLayoutList)
}

override fun addData(obj: TransferObject) {
val exo = Exposer(obj as TObject)
data.add(exo.dynamise()) //if exposer is not dynamised, data access in tables won't work
Expand All @@ -41,7 +70,6 @@ class DisplayList(override val title: String) : BaseDisplayable() {
val e: Extensions = p.extensions!!
val friendlyName = e.friendlyName
propertyDescriptionList.put(id, friendlyName)
layout?.addPropertyDescription(p)
}

fun addProperty(property: Property) {
Expand Down
Loading

0 comments on commit 0f759b4

Please sign in to comment.