package com.rthoni.intellij.codefromds.dbo.options; import com.intellij.database.psi.DbDataSource; import com.intellij.database.util.DasUtil; import com.rthoni.intellij.codefromds.business.Helper; import org.json.JSONArray; import org.json.JSONObject; import java.util.HashMap; import java.util.List; import java.util.Vector; import java.util.stream.Collectors; /** * Created by robin on 11/15/16. */ public class DataSourceSelection { private DbDataSource _source; private List _tables; private List _storedProcedures; public DataSourceSelection(DbDataSource source) { _source = source; _tables = DasUtil.getTables(source).toList().stream().map(TableSelection::new).collect(Collectors.toList()); _storedProcedures = Helper.getRoutines(source).stream().map(StoredProcedureSelection::new).collect(Collectors.toList()); } public HashMap toMap() { HashMap map = new HashMap<>(); map.put("source", _source == null ? null : _source.getName()); List tables = new Vector<>(); for (TableSelection table : _tables) { tables.add(table.toMap()); } map.put("tables", tables); List sps = new Vector<>(); for (StoredProcedureSelection sp : _storedProcedures) { sps.add(sp.toMap()); } map.put("storedProcedures", sps); return map; } public void fromJson(JSONObject json) { JSONArray array = json.getJSONArray("tables"); for (TableSelection table : _tables) { JSONObject obj = Helper.findInJson(array, "table", table.getTable().getName()); if (obj != null) { table.fromJson(obj); } else { for (ColumnSelection column : table.getColumns()) { column.setSelected(false); } } } array = json.getJSONArray("storedProcedures"); for (StoredProcedureSelection sp : _storedProcedures) { JSONObject obj = Helper.findInJson(array, "name", sp.getStoredProcedure().getText()); if (obj != null) { sp.fromJson(obj); } } } public DbDataSource getSource() { return _source; } public void setSource(DbDataSource source) { _source = source; } public List getTables() { return _tables; } public void setTables(List tables) { _tables = tables; } public List getStoredProcedures() { return _storedProcedures; } public void setStoredProcedures(List storedProcedures) { _storedProcedures = storedProcedures; } }