Skip to content

Commit

Permalink
replace usernames with systemnames in web xml
Browse files Browse the repository at this point in the history
also add support for orientation attribute for text widgets
  • Loading branch information
mstevetodd committed Aug 26, 2019
1 parent 40529b7 commit bda9902
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 17 deletions.
103 changes: 90 additions & 13 deletions java/src/jmri/web/servlet/panel/LayoutPanelServlet.java
Expand Up @@ -25,6 +25,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import edu.umd.cs.findbugs.annotations.NonNull;

/**
* Return xml (for specified LayoutPanel) suitable for use by external clients
*
Expand Down Expand Up @@ -117,9 +119,11 @@ protected String getXmlPanel(String name) {
}
//if layoutblock has no occupancy sensor, use one from block, if it is populated
} else {
Sensor s = b.getBlock().getSensor();
if (s != null) {
elem.setAttribute("occupancysensor", s.getSystemName()); //send systemname
if (b.getBlock() != null) {
Sensor s = b.getBlock().getSensor();
if (s != null) {
elem.setAttribute("occupancysensor", s.getSystemName()); //send systemname
}
}
}

Expand All @@ -141,21 +145,12 @@ protected String getXmlPanel(String name) {
log.debug("Number of layoutblock elements: {}", num);

// include LayoutTracks
TurnoutManager tm = InstanceManager.turnoutManagerInstance();
List<LayoutTrack> layoutTracks = editor.getLayoutTracks();
for (Object sub : layoutTracks) {
try {
Element e = jmri.configurexml.ConfigXmlManager.elementFromObject(sub);
if (e != null) {
if (e.getName().equals("layoutturnout")) { // add turnout systemName for layoutturnout
Attribute tna = e.getAttribute("turnoutname");
if (tna != null) {
Turnout t = tm.getTurnout(tna.getValue());
if (t != null) {
e.setAttribute("systemName", t.getSystemName());
}
}
}
replaceUserNames(e);
panel.addContent(e);
}
} catch (Exception e) {
Expand All @@ -174,6 +169,88 @@ protected String getXmlPanel(String name) {
return fmt.outputString(doc);
}

/**
* replace userName value of attrName with systemName for type attrType
*
* @param e element to be updated
* @param beanType bean type to use for userName lookup
* @param attrName attribute name to replace
*
*/
private void replaceUserNameAttribute(@NonNull Element e, @NonNull String beanType, @NonNull String attrName) {

String sn = "";
Attribute a = e.getAttribute(attrName);
if (a == null) return;
String un = a.getValue();

switch(beanType) {
case "turnout" :
Turnout t = InstanceManager.getDefault(TurnoutManager.class).getTurnout(un);
if (t == null) return;
sn = t.getSystemName();
break;
case "layoutBlock" :
LayoutBlock lb = InstanceManager.getDefault(LayoutBlockManager.class).getLayoutBlock(un);
if (lb == null) return;
sn = lb.getSystemName();
break;
default:
return;
}
if (!un.equals(sn)) {
a.setValue(sn);
log.debug("systemName '{}' replaced userName '{}' for {}", sn, un, attrName);
}
}

/**
* replace child element value of attrName with systemName for type attrType
*
* @param e element to be updated
* @param beanType bean type to use for userName lookup
* @param childName child element name whose text will be replaced
*
*/
private void replaceUserNameChild(@NonNull Element e, @NonNull String beanType, @NonNull String childName) {

String sn = "";
Element c = e.getChild(childName);
if (c == null) return;
String un = c.getText();

switch(beanType) {
case "turnout" :
Turnout t = InstanceManager.getDefault(TurnoutManager.class).getTurnout(un);
if (t == null) return;
sn = t.getSystemName();
break;
default:
return;
}
if (!un.equals(sn)) {
c.setText(sn);
log.debug("systemName '{}' replaced userName '{}' for {}", sn, un, childName);
}
}


/**
* update the element replacing username with systemname for known attributes and children
*
* @param e element to be updated
*/
private void replaceUserNames(Element e) {
replaceUserNameAttribute(e, "turnout", "turnoutname");
replaceUserNameAttribute(e, "turnout", "secondturnoutname");
replaceUserNameAttribute(e, "layoutBlock", "blockname");
replaceUserNameAttribute(e, "layoutBlock", "blockbname");
replaceUserNameAttribute(e, "layoutBlock", "blockcname");
replaceUserNameAttribute(e, "layoutBlock", "blockdname");
replaceUserNameChild(e, "turnout", "turnout");
replaceUserNameChild(e, "turnout", "turnoutB");
}

@Override
protected String getJsonPanel(String name) {
// TODO Auto-generated method stub
Expand Down
10 changes: 6 additions & 4 deletions web/js/panel.js
Expand Up @@ -551,8 +551,12 @@ function processPanelXML($returnedData, $success, $xhr) {
}
break;
}
$widget['safeName'] = $safeName($widget.name);
$gWidgets[$widget.id] = $widget; //store widget in persistent array
$widget['safeName'] = $safeName($widget.name);
switch ($widget['orientation']) { //use orientation instead of degrees if populated
case "vertical_up" : $widget.degrees = 270;
case "vertical_down" : $widget.degrees = 90;
}
$gWidgets[$widget.id] = $widget; //store widget in persistent array
//put the text element on the page
$("#panel-area").append("<div id=" + $widget.id + " class='" + $widget.classes + "'>" + $widget.text + "</div>");
$("#panel-area>#" + $widget.id).css($widget.styles); //apply style array to widget
Expand All @@ -579,8 +583,6 @@ function processPanelXML($returnedData, $success, $xhr) {
$widget['state'] = UNKNOWN; //add a state member for this block
$widget["blockcolor"] = $widget.trackcolor; //init blockcolor to trackcolor
//store these blocks in a persistent var
//by both username and systemname since references may be by either
$gBlks[$widget.username] = $widget
$gBlks[$widget.systemName] = $widget;
jmri.getLayoutBlock($widget.systemName);
break;
Expand Down

0 comments on commit bda9902

Please sign in to comment.