Skip to content

Commit

Permalink
Update linked data when Condition name is changed
Browse files Browse the repository at this point in the history
Fix crash when renaming Condition
  • Loading branch information
renyuneyun committed May 20, 2018
1 parent f305f55 commit 59338a3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@

import java.io.IOException;

import ryey.easer.commons.plugindef.eventplugin.EventData;
import ryey.easer.core.data.ConditionStructure;
import ryey.easer.core.data.ScenarioStructure;
import ryey.easer.core.data.ScriptStructure;
import ryey.easer.core.data.storage.backend.ConditionDataStorageBackendInterface;
import ryey.easer.core.data.storage.backend.json.condition.JsonConditionDataStorageBackend;
import ryey.easer.plugins.event.condition_event.ConditionEventEventData;

public class ConditionDataStorage extends AbstractDataStorage<ConditionStructure, ConditionDataStorageBackendInterface> {

Expand Down Expand Up @@ -56,15 +60,40 @@ boolean isSafeToDelete(String name) {
public boolean edit(String oldName, ConditionStructure condition) throws IOException {
boolean success = super.edit(oldName, condition);
if (success) {
ScriptDataStorage scriptDataStorage = ScriptDataStorage.getInstance(context);
if (!oldName.equals(condition.getName())) {
updateScriptsForNewName(scriptDataStorage, oldName, condition.getName());
ScriptDataStorage scriptDataStorage = ScriptDataStorage.getInstance(context);
updateScriptsForNewName(scriptDataStorage, oldName, condition);
ScenarioDataStorage scenarioDataStorage = ScenarioDataStorage.getInstance(context);
updateConditionEventForNewName(scenarioDataStorage, oldName, condition.getName());
}
}
return success;
}

private static void updateScriptsForNewName(ScriptDataStorage scriptDataStorage, String oldName, String newName) {
//TODO
private static void updateScriptsForNewName(ScriptDataStorage scriptDataStorage, String oldName, ConditionStructure condition) throws IOException {
for (String name : scriptDataStorage.list()) {
ScriptStructure script = scriptDataStorage.get(name);
if (script.isCondition()) {
if (script.getCondition().getName().equals(oldName)) {
script.setCondition(condition);
scriptDataStorage.update(script);
}
}
}
}

private static void updateConditionEventForNewName(ScenarioDataStorage scenarioDataStorage, String oldName, String newName) throws IOException {
for (String name : scenarioDataStorage.list()) {
ScenarioStructure scenario = scenarioDataStorage.get(name);
EventData eventData = scenario.getEventData();
if (eventData instanceof ConditionEventEventData) {
if (oldName.equals(((ConditionEventEventData) eventData).conditionName)) {
ConditionEventEventData newEventData =
new ConditionEventEventData((ConditionEventEventData) eventData, newName);
scenario.setEventData(newEventData);
scenarioDataStorage.update(scenario);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ enum ConditionEvent {
private static final String K_CONDITION_NAME = "condition_name";
private static final String K_CONDITION_EVENT = "condition_event";

final String conditionName;
public final String conditionName;
final ConditionEvent conditionEvent;

ConditionEventEventData(@NonNull String data, @NonNull C.Format format, int version) throws IllegalStorageDataException {
Expand All @@ -60,6 +60,11 @@ enum ConditionEvent {
this.conditionEvent = conditionEvent;
}

public ConditionEventEventData(ConditionEventEventData ref, String newConditionName) {
conditionName = newConditionName;
conditionEvent = ref.conditionEvent;
}

@NonNull
@Override
public String serialize(@NonNull C.Format format) {
Expand Down

0 comments on commit 59338a3

Please sign in to comment.