private List<DbOperation> createOperations( List<Interpretation> oldModels, List<Interpretation> newModels) { List<DbOperation> ops = new ArrayList<>(); Map<String, Interpretation> newModelsMap = modelUtils.toMap(newModels); Map<String, Interpretation> oldModelsMap = modelUtils.toMap(oldModels); for (String oldModelKey : oldModelsMap.keySet()) { Interpretation newModel = newModelsMap.get(oldModelKey); Interpretation oldModel = oldModelsMap.get(oldModelKey); if (newModel == null) { ops.add(DbOperation.with(mInterpretationStore).delete(oldModel)); continue; } if (newModel.getLastUpdated().isAfter(oldModel.getLastUpdated())) { newModel.setId(oldModel.getId()); ops.add(DbOperation.with(mInterpretationStore).update(newModel)); } newModelsMap.remove(oldModelKey); } for (String newModelKey : newModelsMap.keySet()) { Interpretation item = newModelsMap.get(newModelKey); // we also have to insert interpretation elements here ops.add(DbOperation.with(mInterpretationStore).insert(item)); List<InterpretationElement> elements = new ArrayList<>(); // = mInterpretationService.getInterpretationElements(item); for (InterpretationElement element : elements) { ops.add(DbOperation.with(mInterpretationElementStore).insert(element)); } } return ops; }