Skip to content

Commit

Permalink
Webclient: fix Topic class (#76).
Browse files Browse the repository at this point in the history
... make its get() and find_child_topic() methods fit for multiple values.

Furthermore in Webclient: fix the form processor's safety net.

Furthermore in Core: Change DeepaMehtaObject interface:
- rename updateCompositeValue() to updateChildTopic()
- add updateChildTopics()

See ticket 76.
  • Loading branch information
jri committed May 8, 2012
1 parent 7005913 commit e4919b5
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 162 deletions.
Expand Up @@ -53,9 +53,10 @@ public interface DeepaMehtaObject extends Identifiable, JSONEnabled {

// ---

// ### FIXME: remove from interface. This is a low-level method.
void updateCompositeValue(AssociationDefinition assocDef, TopicModel valueTopic, ClientState clientState,
Directives directives);
void updateChildTopic(AssociationDefinition assocDef, TopicModel newChildTopic, ClientState clientState,
Directives directives);
void updateChildTopics(AssociationDefinition assocDef, List<TopicModel> newChildTopics, ClientState clientState,
Directives directives);



Expand Down
Expand Up @@ -183,6 +183,20 @@ public void setCompositeValue(CompositeValue comp, ClientState clientState, Dire
refreshLabel();
}

// ---

@Override
public void updateChildTopic(AssociationDefinition assocDef, TopicModel newChildTopic, ClientState clientState,
Directives directives) {
updateChildTopics(assocDef, true, newChildTopic, null, clientState, directives); // one=true
}

@Override
public void updateChildTopics(AssociationDefinition assocDef, List<TopicModel> newChildTopics,
ClientState clientState, Directives directives) {
updateChildTopics(assocDef, false, null, newChildTopics, clientState, directives); // one=false
}



// === Traversal ===
Expand Down Expand Up @@ -455,15 +469,15 @@ private void updateCompositeValue(CompositeValue newComp, ClientState clientStat
List<TopicModel> newChildTopics = null; // only used for "many"
boolean one = false;
if (cardinalityUri.equals("dm4.core.one")) {
newChildTopic = newComp.getTopic(assocDefUri, null); // defaultValue=null
newChildTopic = newComp.getTopic(assocDefUri, null); // defaultValue=null
// skip if not contained in update request
if (newChildTopic == null) {
continue;
}
//
one = true;
} else if (cardinalityUri.equals("dm4.core.many")) {
newChildTopics = newComp.getTopics(assocDefUri, null); // defaultValue=null
newChildTopics = newComp.getTopics(assocDefUri, null); // defaultValue=null
// skip if not contained in update request
if (newChildTopics == null) {
continue;
Expand All @@ -472,22 +486,16 @@ private void updateCompositeValue(CompositeValue newComp, ClientState clientStat
throw new RuntimeException("\"" + cardinalityUri + "\" is an unexpected cardinality URI");
}
//
updateCompositeValue(assocDef, one, newChildTopic, newChildTopics, clientState, directives);
updateChildTopics(assocDef, one, newChildTopic, newChildTopics, clientState, directives);
}
} catch (Exception e) {
throw new RuntimeException("Updating the composite value of " + className() + " " + getId() +
" failed (newComp=" + newComp + ")", e);
}
}

// ### FIXME: Remove from interface. Make it private.
@Override
public void updateCompositeValue(AssociationDefinition assocDef, TopicModel newChildTopic, ClientState clientState,
Directives directives) {
}

private void updateCompositeValue(AssociationDefinition assocDef, boolean one, TopicModel newChildTopic,
List<TopicModel> newChildTopics, ClientState clientState, Directives directives) {
private void updateChildTopics(AssociationDefinition assocDef, boolean one, TopicModel newChildTopic,
List<TopicModel> newChildTopics, ClientState clientState, Directives directives) {
String assocTypeUri = assocDef.getTypeUri();
if (assocTypeUri.equals("dm4.core.composition_def")) {
if (one) {
Expand Down
Expand Up @@ -14,6 +14,8 @@ function PagePanel() {
var page_state = PageState.NONE // Tracks page state. Used to fire the "post_destroy_form" event in case.
var from_processing_func // The form processing function: called to "submit" the form
// (only consulted if a form is displayed).
var from_processing_called // Tracks the form processing function call (boolean).
// Used to ensure the form processing function is called only once.

// View
var dom = $("<div>").attr("id", "page-panel")
Expand Down Expand Up @@ -78,14 +80,16 @@ function PagePanel() {
}

this.save = function() {
if (page_state == PageState.FORM) {
if (!from_processing_func) {
throw "PagePanelError: there is no form processing function"
}
//
from_processing_func()
from_processing_func = null // force a 2nd (unwanted) save call to fail
if (page_state != PageState.FORM) {
return
}
//
if (from_processing_called) {
throw "PagePanelError: the form processing function has already been called"
}
//
from_processing_called = true
from_processing_func()
}

// ----------------------------------------------------------------------------------------------- Private Functions
Expand Down Expand Up @@ -128,6 +132,7 @@ function PagePanel() {
function render_form() {
prepare_page()
from_processing_func = page_renderer.render_form(displayed_object)
from_processing_called = false
}

function render_buttons(context) {
Expand Down
Expand Up @@ -5,35 +5,32 @@ function Association(assoc) {
this.role_2 = assoc.role_2
}

Association.prototype = {
// === "Page Displayable" implementation ===

// === "Page Displayable" implementation ===

get_type: function() {
return dm4c.get_association_type(this.type_uri)
},
Association.prototype.get_type = function() {
return dm4c.get_association_type(this.type_uri)
}

get_commands: function(context) {
return dm4c.get_association_commands(this, context)
},
Association.prototype.get_commands = function(context) {
return dm4c.get_association_commands(this, context)
}

// === Public API ===
// === Public API ===

get_topic_1: function() {
return dm4c.fetch_topic(this.role_1.topic_id, false) // fetch_composite=false
},
Association.prototype.get_topic_1 = function() {
return dm4c.fetch_topic(this.role_1.topic_id, false) // fetch_composite=false
}

get_topic_2: function() {
return dm4c.fetch_topic(this.role_2.topic_id, false) // fetch_composite=false
},
Association.prototype.get_topic_2 = function() {
return dm4c.fetch_topic(this.role_2.topic_id, false) // fetch_composite=false
}

// ---
// ---

get_role_type_1: function() {
return dm4c.restc.get_topic_by_value("uri", this.role_1.role_type_uri)
},
Association.prototype.get_role_type_1 = function() {
return dm4c.restc.get_topic_by_value("uri", this.role_1.role_type_uri)
}

get_role_type_2: function() {
return dm4c.restc.get_topic_by_value("uri", this.role_2.role_type_uri)
}
Association.prototype.get_role_type_2 = function() {
return dm4c.restc.get_topic_by_value("uri", this.role_2.role_type_uri)
}
Expand Up @@ -7,24 +7,24 @@ function AssociationType(assoc_type) {
this.composite = assoc_type.composite
//
this.view_config_topics = dm4c.hash_by_type(dm4c.build_topics(assoc_type.view_config_topics))
}

// === "Page Displayable" implementation ===
// === "Page Displayable" implementation ===

this.get_type = function() {
return dm4c.get_topic_type(this.type_uri)
}
AssociationType.prototype.get_type = function() {
return dm4c.get_topic_type(this.type_uri)
}

this.get_commands = function(context) {
return dm4c.get_topic_commands(this, context)
}
AssociationType.prototype.get_commands = function(context) {
return dm4c.get_topic_commands(this, context)
}

this.get_page_renderer_class = function() {
return dm4c.get_view_config(this, "js_page_renderer_class") || "AssociationRenderer"
}
AssociationType.prototype.get_page_renderer_class = function() {
return dm4c.get_view_config(this, "js_page_renderer_class") || "AssociationRenderer"
}

// === Public API ===
// === Public API ===

this.get_color = function() {
return dm4c.get_view_config(this, "color") || dm4c.canvas.DEFAULT_ASSOC_COLOR
}
AssociationType.prototype.get_color = function() {
return dm4c.get_view_config(this, "color") || dm4c.canvas.DEFAULT_ASSOC_COLOR
}
96 changes: 48 additions & 48 deletions modules/dm4-webclient/src/main/resources/web/script/model/topic.js
Expand Up @@ -14,62 +14,62 @@ function Topic(topic) {
this.composite = topic.composite
}

Topic.prototype = {
// === "Page Displayable" implementation ===

// === "Page Displayable" implementation ===

get_type: function() {
return dm4c.get_topic_type(this.type_uri)
},
Topic.prototype.get_type = function() {
return dm4c.get_topic_type(this.type_uri)
}

get_commands: function(context) {
return dm4c.get_topic_commands(this, context)
},
Topic.prototype.get_commands = function(context) {
return dm4c.get_topic_commands(this, context)
}

// === Public API ===
// === Public API ===

/**
* Convenience method to lookup a value from this topic's direct composite.
* The looked up value may be a simple value or a composite value.
*
* @return A simple value (string, number, boolean) or a composite value (a Topic object).
* If no such value exists in this topic's direct composite or if this topic
* itself is a simple one undefined is returned.
*/
get: function(assoc_def_uri) {
var comp = this.composite[assoc_def_uri]
if (comp) {
var topic_type = dm4c.get_topic_type(assoc_def_uri)
// ### FIXME: should be if (topic.get_type().is_simple())
// ### but type_uri is not initialized in compact composite format
if (topic_type.is_simple()) {
return comp.value
} else {
return new Topic(comp)
}
/**
* Returns the value of a direct child topic, specified by type URI. The value may be simple or composite.
* You can lookup nested values by chaining the get() calls.
*
* If no such child topic exists undefined is returned.
*
* @param child_type_uri The URI of a direct child type.
*
* @return A simple value (string, number, boolean) or a composite value (a Topic object) or an array
* (in case of multiple values).
*/
Topic.prototype.get = function(child_type_uri) {
var child_topic = this.composite[child_type_uri]
if (child_topic) {
if (js.is_array(child_topic)) {
return child_topic
}
},
var topic_type = dm4c.get_topic_type(child_type_uri)
if (topic_type.is_simple()) {
return child_topic.value
} else {
return new Topic(child_topic)
}
}
}

find_child_topic: function(type_uri) {
Topic.prototype.find_child_topic = function(type_uri) {
var child_topic = find_child_topic(this)
if (child_topic) {
return new Topic(child_topic)
}

if (this.type_uri == type_uri) {
return this
function find_child_topic(topic) {
if (topic.type_uri == type_uri) {
return topic
}
return find_child_topic(this.composite)

function find_child_topic(composite) {
// alert("find_child_topic(): composite=" + JSON.stringify(composite))
for (var assoc_def_uri in composite) {
var child_topic = composite[assoc_def_uri]
// ### FIXME: should be if (child_topic.type_uri == type_uri)
// ### but type_uri is not initialized in compact composite format
if (assoc_def_uri == type_uri) {
return new Topic(child_topic)
}
child_topic = find_child_topic(child_topic.composite)
if (child_topic) {
return new Topic(child_topic)
}
for (var child_type_uri in topic.composite) {
var child_topic = topic.composite[child_type_uri]
if (js.is_array(child_topic)) {
child_topic = child_topic[0]
}
child_topic = find_child_topic(child_topic)
if (child_topic) {
return child_topic
}
}
}
Expand Down

0 comments on commit e4919b5

Please sign in to comment.