Skip to content

Commit

Permalink
WMATA admin UI demo
Browse files Browse the repository at this point in the history
  • Loading branch information
Jared Koester committed Feb 21, 2023
1 parent 3b33898 commit a1159a0
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,25 @@ public boolean saveParameters(Map<String, String> parameters) {

@Override
public Map<String, String> getParameters() {
Map<String, String> displayParameters = new HashMap<String, String>();
Map<String, String> displayParameters = new HashMap<String, String>();

//Get config values from TDM
Map<String, String> configParameters = configurationService.getConfiguration();
Map<String, String> configParameters = configurationService.getConfigFromLocalFile();

for(Map.Entry<String, String> entry : configParameters.entrySet()) {
String configKey = entry.getKey();
String configValue = entry.getValue();
//Translate config key to its UI counterpart.
//Translate config key to its UI counterpart.
String uiKey = keyTranslator.getUIKey(configKey);
if(StringUtils.isNotBlank(uiKey)) {
//uikey can be null if there is a mismatch between
//the keys on TDM and admin UI
displayParameters.put(uiKey, configValue);
}
}




return displayParameters;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
package org.onebusaway.util.impl.configuration;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -87,10 +89,32 @@ public URL buildUrl(String baseObject, String... params) throws Exception {
}

@Override
public void setConfigItem(String baseObject, String component, String key,
public void setConfigItem(String baseObject, String component, String configurationKey,
String value) throws Exception {
// TODO Auto-generated method stub
_log.info("empty setConfigItem");
String key = configurationKey.substring(configurationKey.indexOf(".")+1);

try {
ObjectMapper mapper = new ObjectMapper();
ConfigFileStructure cfs = mapper.readValue(new File(configFile), ConfigFileStructure.class);
boolean found = false;
for (ConfigItem item : cfs.config) {
if (item.component.equals(component) && item.key.equals(key)) {
item.value = value;
found = true;

}
}
if(!found){
_log.warn("Could not find an existing configuration item for " + component + ":" + key + " so one will be created");
cfs.config.add(new ConfigItem(component, key, value));
}
mapper.writerWithDefaultPrettyPrinter().writeValue(new File(configFile), cfs);
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(cfs));
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}

}

@Override
Expand Down Expand Up @@ -160,8 +184,46 @@ private HashMap<String, Object> getConfig() {

}

@Override
public HashMap<String, String> getConfigFromLocalFile() {
try {
ObjectMapper mapper = new ObjectMapper();
ConfigFileStructure cfs = mapper.readValue(new File(configFile), ConfigFileStructure.class);

HashMap<String, String> config = new HashMap<>();
for(ConfigItem item : cfs.config){
config.put(item.component + "." + item.key, item.value);
}
return config;

} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}

@Override
public boolean isLocal() {
return isLocal;
}


private static class ConfigItem{
public String component;
public String key;
public String value;

public ConfigItem(String c, String k, String v){
this.component = c;
this.key = k;
this.value = v;
}
public ConfigItem(){

}
}
private static class ConfigFileStructure{
public HashMap<String, String> oba;
public ArrayList<ConfigurationServiceClientFileImpl.ConfigItem> config;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,11 @@ public boolean getConfigurationFlagForAgency(String agencyId, String configurati
}
}

@Override
public Map<String, String> getConfigFromLocalFile() {
return _configurationServiceClient.getConfigFromLocalFile();
}

// Local Config Methods

private String getLocalConfigurationValue(String configurationItemKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,6 @@ public void setConfigurationValue(String component, String configurationItemKey,
* {"component": "agency_1", "key": "hideScheduleInfo", "value": "true"}
*/
public boolean getConfigurationFlagForAgency(String agencyId, String configurationItemKey);

Map<String, String> getConfigFromLocalFile();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.onebusaway.util.services.configuration;

import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand All @@ -39,7 +40,9 @@ void setConfigItem(String baseObject, String component, String key, String value
List<Map<String, String>> getItems(String baseObject, String... params) throws Exception;

String getItem(String baseObject, String key) throws Exception;


HashMap<String, String> getConfigFromLocalFile();

boolean isLocal();

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@

import static org.junit.Assert.*;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.onebusaway.container.refresh.RefreshService;
import org.onebusaway.util.rest.RestApiLibrary;
import org.onebusaway.util.services.configuration.ConfigurationServiceClient;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class ConfigurationServiceImplTest {

Expand Down Expand Up @@ -67,6 +73,67 @@ public void testGetConfigurationFlagForAgency() throws Exception {

hideScheduleInfo = service.getConfigurationFlagForAgency("1", "hideScheduleInfo");
assertEquals("expected true", true, hideScheduleInfo);

}

@Test
public void testMergeConfig(){
HashMap<String, Object> old = new HashMap<>();
ArrayList<HashMap> items = new ArrayList<>();
HashMap<String, String> item1 = new HashMap<>();
item1.put("component", "admin");
item1.put("key", "test");
item1.put("value","hello World");
items.add(item1);
old.put("config", items);

}

@Test
public void testWriteConfig(){

// ConfigFileStructure cfs = new ConfigFileStructure();
// cfs.oba = new HashMap<String, String>();
// cfs.oba.put("env", "jared");
// cfs.config = new ArrayList<ConfigItem>(Arrays.asList(new ConfigItem("testing","jared","hello world!")));


try {
ObjectMapper mapper = new ObjectMapper();
ConfigFileStructure cfs = mapper.readValue(new File("/opt/nyc/oba/config.json"), ConfigFileStructure.class);

This comment has been minimized.

Copy link
@aaronbrethorst

aaronbrethorst May 23, 2024

Member

this breaks running tests.

String output = mapper.writeValueAsString(cfs);
System.out.println(output);
String pretty = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(cfs);
System.out.println(pretty);
mapper.writerWithDefaultPrettyPrinter().writeValue(new File("/Users/jkoester/Desktop/configOutput.json"), cfs);

This comment has been minimized.

Copy link
@aaronbrethorst

aaronbrethorst May 23, 2024

Member

This literally will not work on anyone else's computer besides Jared's.

} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}

}

@Test
public void testReadConfig(){
ObjectMapper mapper = new ObjectMapper();
try {

String jsonString = "{\"oba\":{\"env\":\"jared\"},\"config\":[{\"component\":\"testing\",\"key\":\"jared\",\"value\":\"hello world!\"}]}";
ConfigFileStructure cfs = mapper.readValue(new File("/opt/nyc/oba/config.json"), ConfigFileStructure.class);


// compact print
System.out.println(cfs);

// pretty print
String prettyFile = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(cfs);

System.out.println(prettyFile);


} catch (IOException e) {
e.printStackTrace();
}
}

private void addToSettings(ArrayList settings, String component, String key, String value) {
Expand All @@ -78,4 +145,23 @@ private void addToSettings(ArrayList settings, String component, String key, Str
settings.add(kv1);
}

private static class ConfigItem{
public String component;
public String key;
public String value;

public ConfigItem(String c, String k, String v){
this.component = c;
this.key = k;
this.value = v;
}
public ConfigItem(){

}
}
private static class ConfigFileStructure{
public HashMap<String, String> oba;
public ArrayList<ConfigItem> config;
}

}

0 comments on commit a1159a0

Please sign in to comment.