generated from hms-networks/sc-java-maven-starter-project
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
72 additions
and
0 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
src/main/java/com/hms_networks/americas/sc/extensions/config/ConfigUpdater.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |