Commit fa8c8644 authored by Matija Obreza's avatar Matija Obreza
Browse files

Save/Load CSV configuration

parent 30bdbbf3
......@@ -28,6 +28,7 @@ import org.genesys2.anno.model.Column;
import org.genesys2.anno.model.ColumnDataType;
import org.genesys2.anno.model.OAuthSettings;
import org.genesys2.anno.model.Settings;
import org.genesys2.anno.parser.CsvDataSourceSheet;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
......@@ -52,142 +53,175 @@ public class DwcaBuilder extends AbstractModelObject {
firePropertyChange("fileRoots", null, this);
}
public void saveWorkspace(String filePath,Settings sourceSettings) throws JSONException, IOException {
JSONObject main = new JSONObject();
JSONArray files = new JSONArray();
JSONObject oauthSettings = new JSONObject();
oauthSettings.put("accessToken", sourceSettings.getOauthSettings().getAccessToken());
oauthSettings.put("apiUrl", sourceSettings.getOauthSettings().getApiUrl());
oauthSettings.put("authorizationEndpoint", sourceSettings.getOauthSettings().getAuthorizationEndpoint());
oauthSettings.put("clientKey", sourceSettings.getOauthSettings().getClientKey());
oauthSettings.put("clientSecret", sourceSettings.getOauthSettings().getClientSecret());
oauthSettings.put("refreshToken", sourceSettings.getOauthSettings().getRefreshToken());
oauthSettings.put("serverUrl", sourceSettings.getOauthSettings().getServerUrl());
oauthSettings.put("tokenEndpoint", sourceSettings.getOauthSettings().getTokenEndpoint());
JSONObject settings = new JSONObject();
settings.put("oauthSettings",oauthSettings);
for (DataSourceFile sourceFile : getFileRoots()) {
String path = sourceFile.getFile().getPath();
JSONObject file = new JSONObject();
JSONArray sheets = new JSONArray();
file.put("path", path);
file.put("sheets", sheets);
files.put(file);
for (DataSourceSheet sourceSheet : sourceFile.getSheets()) {
JSONObject sheet = new JSONObject();
JSONArray columns = new JSONArray();
String name = sourceSheet.getSheetName();
sheet.put("name", name);
sheet.put("columns", columns);
sheets.put(sheet);
for (Column sourceColumn : sourceSheet.getColumns()) {
JSONObject column= new JSONObject();
sourceColumn.setDataType(ColumnDataType.TEXT);
column.put("name",sourceColumn.getPreferredName());
column.put("rdfTerm",sourceColumn.getRdfTerm());
column.put("description",sourceColumn.getDescription());
column.put("multiple", sourceColumn.isMultiple());
column.put("dataType",sourceColumn.getDataType());
column.put("pattern",sourceColumn.getPattern());
column.put("separator",sourceColumn.getSeparator());
column.put("groupPattern", sourceColumn.getGroupPattern());
columns.put(column);
}
}
}
main.put("settings",settings);
main.put("files", files);
main.put("dateCreated", System.currentTimeMillis());
main.put("version", 1);
FileWriter fileWriter = new FileWriter(filePath);
fileWriter.write(main.toString());
fileWriter.flush();
fileWriter.close();
System.out.println(main.toString());
}
public void loadWorkspace(String filePath,Settings settings) throws FileNotFoundException, JSONException {
fileRoots.clear();
FileReader reader = new FileReader(filePath);
JSONTokener tokener = new JSONTokener(reader);
JSONObject root = new JSONObject(tokener);
JSONArray files = root.getJSONArray("files");
for (int i = 0; i < files.length(); i++) {
String path = files.getJSONObject(i).getString("path");
File file = new File(path);
JSONArray sheets = files.getJSONObject(i).getJSONArray("sheets");
DataSourceFile sourceFile = new DataSourceFile();
List<DataSourceSheet> sourceSheetList = new ArrayList<DataSourceSheet>();
for (int j = 0; j < sheets.length(); j++) {
DataSourceSheet sourceSheet = new DataSourceSheet(file);
sourceSheet.setSheetName(sheets.getJSONObject(j).getString("name"));
JSONArray columns = sheets.getJSONObject(j).getJSONArray("columns");
List<Column> columnList= new ArrayList<Column>();
for (int x = 0; x < columns.length(); x++) {
JSONObject column = columns.getJSONObject(x);
Column sourceColumn = new Column();
if (column.has("name")) sourceColumn.setPreferredName(column.getString("name"));
if (column.has("rdfTerm")) sourceColumn.setRdfTerm(column.getString("rdfTerm"));
if (column.has("description")) sourceColumn.setDescription(column.getString("description"));
if (column.has("pattern")) sourceColumn.setPattern(column.getString("pattern"));
if (column.has("separator")) sourceColumn.setSeparator(column.getString("separator"));
if (column.has("groupPattern")) sourceColumn.setGroupPattern(column.getString("groupPattern"));
if (column.has("dataType")) sourceColumn.setDataType( ColumnDataType.valueOf(column.getString("dataType")));
if (column.has("multiple")) sourceColumn.setMultiple(column.getBoolean("multiple"));
columnList.add(sourceColumn);
}
sourceSheet.setColumns(columnList);
sourceSheetList.add(sourceSheet);
}
sourceFile.setFile(file);
sourceFile.setSheets(sourceSheetList);
addDataSource(sourceFile);
}
JSONObject jsonSettings = root.getJSONObject("settings").getJSONObject("oauthSettings");
OAuthSettings oAuthSettings= new OAuthSettings();
oAuthSettings.setAccessToken(jsonSettings.getString("accessToken"));
oAuthSettings.setApiUrl(jsonSettings.getString("apiUrl"));
oAuthSettings.setAuthorizationEndpoint(jsonSettings.getString("authorizationEndpoint"));
oAuthSettings.setClientKey(jsonSettings.getString("clientKey"));
oAuthSettings.setClientSecret(jsonSettings.getString("clientSecret"));
oAuthSettings.setRefreshToken(jsonSettings.getString("refreshToken"));
oAuthSettings.setServerUrl(jsonSettings.getString("serverUrl"));
oAuthSettings.setTokenEndpoint(jsonSettings.getString("tokenEndpoint"));
public void saveWorkspace(String filePath, Settings sourceSettings) throws JSONException, IOException {
JSONObject main = new JSONObject();
JSONArray files = new JSONArray();
JSONObject oauthSettings = new JSONObject();
oauthSettings.put("accessToken", sourceSettings.getOauthSettings().getAccessToken());
oauthSettings.put("apiUrl", sourceSettings.getOauthSettings().getApiUrl());
oauthSettings.put("authorizationEndpoint", sourceSettings.getOauthSettings().getAuthorizationEndpoint());
oauthSettings.put("clientKey", sourceSettings.getOauthSettings().getClientKey());
oauthSettings.put("clientSecret", sourceSettings.getOauthSettings().getClientSecret());
oauthSettings.put("refreshToken", sourceSettings.getOauthSettings().getRefreshToken());
oauthSettings.put("serverUrl", sourceSettings.getOauthSettings().getServerUrl());
oauthSettings.put("tokenEndpoint", sourceSettings.getOauthSettings().getTokenEndpoint());
JSONObject settings = new JSONObject();
settings.put("oauthSettings", oauthSettings);
for (DataSourceFile sourceFile : getFileRoots()) {
String path = sourceFile.getFile().getPath();
JSONObject file = new JSONObject();
JSONArray sheets = new JSONArray();
file.put("path", path);
file.put("sheets", sheets);
files.put(file);
for (DataSourceSheet sourceSheet : sourceFile.getSheets()) {
JSONObject sheet = new JSONObject();
JSONArray columns = new JSONArray();
String name = sourceSheet.getSheetName();
sheet.put("name", name);
sheet.put("columns", columns);
sheet.put("headers", sourceSheet.isHeadersIncluded());
sheet.put("headerRow", sourceSheet.getHeaderRowIndex());
if (sourceSheet instanceof CsvDataSourceSheet) {
CsvDataSourceSheet csvSheet = (CsvDataSourceSheet) sourceSheet;
JSONObject csv = new JSONObject();
csv.put("charset", csvSheet.getCharset());
csv.put("delimiterChar", "" + csvSheet.getDelimiterChar());
csv.put("quoteChar", "" + csvSheet.getQuoteChar());
csv.put("unixEol", csvSheet.isUnixEol());
sheet.put("csv", csv);
}
sheets.put(sheet);
for (Column sourceColumn : sourceSheet.getColumns()) {
JSONObject column = new JSONObject();
sourceColumn.setDataType(ColumnDataType.TEXT);
column.put("name", sourceColumn.getPreferredName());
column.put("rdfTerm", sourceColumn.getRdfTerm());
column.put("description", sourceColumn.getDescription());
column.put("multiple", sourceColumn.isMultiple());
column.put("dataType", sourceColumn.getDataType());
column.put("pattern", sourceColumn.getPattern());
column.put("separator", sourceColumn.getSeparator());
column.put("groupPattern", sourceColumn.getGroupPattern());
columns.put(column);
}
}
}
main.put("settings", settings);
main.put("files", files);
main.put("dateCreated", System.currentTimeMillis());
main.put("version", 1);
FileWriter fileWriter = new FileWriter(filePath);
fileWriter.write(main.toString());
fileWriter.flush();
fileWriter.close();
System.out.println(main.toString());
}
settings.setOauthSettings(oAuthSettings);
public void loadWorkspace(String filePath, Settings settings) throws FileNotFoundException, JSONException {
fileRoots.clear();
FileReader reader = new FileReader(filePath);
JSONTokener tokener = new JSONTokener(reader);
JSONObject root = new JSONObject(tokener);
JSONArray files = root.getJSONArray("files");
for (int i = 0; i < files.length(); i++) {
JSONObject sourceJ = files.getJSONObject(i);
String path = sourceJ.getString("path");
File file = new File(path);
JSONArray sheets = sourceJ.getJSONArray("sheets");
DataSourceFile sourceFile = new DataSourceFile();
List<DataSourceSheet> sourceSheetList = new ArrayList<DataSourceSheet>();
for (int j = 0; j < sheets.length(); j++) {
JSONObject sheetJ = sheets.getJSONObject(j);
DataSourceSheet sourceSheet = null;
if (sheetJ.has("csv")) {
CsvDataSourceSheet csvSourceSheet = new CsvDataSourceSheet(file);
JSONObject csvJ = sheetJ.getJSONObject("csv");
csvSourceSheet.setCharset(csvJ.getString("charset"));
csvSourceSheet.setDelimiterChar(csvJ.getString("delimiterChar").charAt(0));
csvSourceSheet.setQuoteChar(csvJ.getString("quoteChar").charAt(0));
csvSourceSheet.setUnixEol(csvJ.getBoolean("unixEol"));
sourceSheet = csvSourceSheet;
} else {
sourceSheet = new DataSourceSheet(file);
}
sourceSheet.setSheetName(sheetJ.getString("name"));
JSONArray columns = sheetJ.getJSONArray("columns");
List<Column> columnList = new ArrayList<Column>();
for (int x = 0; x < columns.length(); x++) {
JSONObject column = columns.getJSONObject(x);
Column sourceColumn = new Column();
if (column.has("name"))
sourceColumn.setPreferredName(column.getString("name"));
if (column.has("rdfTerm"))
sourceColumn.setRdfTerm(column.getString("rdfTerm"));
if (column.has("description"))
sourceColumn.setDescription(column.getString("description"));
if (column.has("pattern"))
sourceColumn.setPattern(column.getString("pattern"));
if (column.has("separator"))
sourceColumn.setSeparator(column.getString("separator"));
if (column.has("groupPattern"))
sourceColumn.setGroupPattern(column.getString("groupPattern"));
if (column.has("dataType"))
sourceColumn.setDataType(ColumnDataType.valueOf(column.getString("dataType")));
if (column.has("multiple"))
sourceColumn.setMultiple(column.getBoolean("multiple"));
columnList.add(sourceColumn);
}
sourceSheet.setColumns(columnList);
sourceSheetList.add(sourceSheet);
}
sourceFile.setFile(file);
sourceFile.setSheets(sourceSheetList);
addDataSource(sourceFile);
}
JSONObject jsonSettings = root.getJSONObject("settings").getJSONObject("oauthSettings");
OAuthSettings oAuthSettings = new OAuthSettings();
oAuthSettings.setAccessToken(jsonSettings.getString("accessToken"));
oAuthSettings.setApiUrl(jsonSettings.getString("apiUrl"));
oAuthSettings.setAuthorizationEndpoint(jsonSettings.getString("authorizationEndpoint"));
oAuthSettings.setClientKey(jsonSettings.getString("clientKey"));
oAuthSettings.setClientSecret(jsonSettings.getString("clientSecret"));
oAuthSettings.setRefreshToken(jsonSettings.getString("refreshToken"));
oAuthSettings.setServerUrl(jsonSettings.getString("serverUrl"));
oAuthSettings.setTokenEndpoint(jsonSettings.getString("tokenEndpoint"));
settings.setOauthSettings(oAuthSettings);
}
}
}
......@@ -390,7 +390,7 @@ public class SheetDisplay extends Composite {
lblCharacterSet.setText("Character set");
comboCharset = new Combo(compoCSV, SWT.NONE);
comboCharset.setItems(new String[] { "UTF-8", "US-ASCII", "ISO-8859-1", "UTF-16", "windows-1250" });
comboCharset.setItems(new String[] {"UTF-8", "US-ASCII", "Mac", "MacCentralEurope", "ISO-8859-1", "UTF-16", "windows-1250"});
comboCharset.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
comboCharset.select(0);
......
......@@ -63,7 +63,7 @@ public class CsvDataSourceSheet extends DataSourceSheet {
return unixEol ? "\r" : "\r\n";
}
public boolean getsUnixEol() {
public boolean isUnixEol() {
return unixEol;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment