Skip to content

Commit

Permalink
Added ConfigUpdater.java
Browse files Browse the repository at this point in the history
  • Loading branch information
TomKimsey committed Oct 27, 2023
1 parent d3f1572 commit 8dad14f
Showing 1 changed file with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.hms_networks.americas.sc.extensions.config;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import com.hms_networks.americas.sc.extensions.json.JSONException;
import com.hms_networks.americas.sc.extensions.json.JSONObject;

public class ConfigUpdater {

private JSONObject config;
private ArrayList steps;
private String applicationJsonKey;
private String configVersionJsonKey;

public ConfigUpdater(JSONObject config, String applicationJsonKey, String configVersionJsonKey)
{
this.config = config;
steps = new ArrayList();
this.applicationJsonKey = applicationJsonKey;
this.configVersionJsonKey = configVersionJsonKey;
}

public JSONObject getConfig(){
return config;
}

private ConfigFileVersion getConfigFileVersion(){
String versionString;
try {
versionString = config.getJSONObject(applicationJsonKey).getString(configVersionJsonKey);
} catch (JSONException e) {
versionString = "0.0.0";
}
return new ConfigFileVersion(versionString);
}

private void setConfigFileVersion(ConfigFileVersion version){
try {
config.getJSONObject(applicationJsonKey).put(configVersionJsonKey, version.getVersionString());
} catch (JSONException e) {
e.printStackTrace();
}
}

public void addStep(ConfigFileUpdateStep step){
steps.add(step);
}

public boolean updateConfig(){
boolean success = true;
Collections.sort(steps);
for (int i = 0; i < steps.size(); i++){
ConfigFileUpdateStep step = (ConfigFileUpdateStep) steps.get(i);
if (step.isCompatibleWithVersion(getConfigFileVersion())){
success = success && step.updateConfig(config);
if (success){
setConfigFileVersion(step.getStepVersion());
}
else {
break;
}
}
else {
success = false;
break;
}
}
return success;
}
}

0 comments on commit 8dad14f

Please sign in to comment.