String sqlSchema, String sqlTablePrefix, boolean sqlOverwrite, String configTitle,
String configKeyType, String projectionSRS, String[] nullValues, boolean importDBFData, boolean append)
throws RemoteException
{
ConnectionInfo connInfo = getConnectionInfo(configConnectionName, password);
DataConfig dataConfig = getDataConfig();
if (Strings.isEmpty(sqlSchema))
throw new RemoteException("SQL schema must be specified.");
if (Strings.isEmpty(sqlTablePrefix))
throw new RemoteException("SQL table prefix must be specified.");
// use lower case sql table names (fix for mysql linux problems)
sqlTablePrefix = sqlTablePrefix.toLowerCase();
if (sqlOverwrite && !connInfo.is_superuser)
throw new RemoteException(String.format(
"User \"%s\" does not have permission to overwrite SQL tables.", configConnectionName));
String dbfTableName = sqlTablePrefix + "_dbfdata";
Connection conn = null;
int tableId = -1;
try
{
conn = connInfo.getConnection();
// store dbf data to database
if (importDBFData)
{
importDBF(
configConnectionName, password, fileNameWithoutExtension, sqlSchema, dbfTableName,
sqlOverwrite, nullValues);
}
GeometryStreamConverter converter = new GeometryStreamConverter(new SQLGeometryStreamDestination(
conn, sqlSchema, sqlTablePrefix, sqlOverwrite));
for (String file : fileNameWithoutExtension)
{
// convert shape data to streaming sql format
String shpfile = getUploadPath() + file + ".shp";
SHPGeometryStreamUtils.convertShapefile(converter, shpfile, Arrays.asList(keyColumns));
}
converter.flushAndCommitAll();
}
catch (Exception e)
{
e.printStackTrace();
throw new RemoteException("Shapefile import failed", e);
}
finally
{
SQLUtils.cleanup(conn);
}
DataEntityMetadata tableInfo = new DataEntityMetadata();
tableInfo.setPrivateValues(
PrivateMetadata.IMPORTMETHOD, "importSHP",
PrivateMetadata.FILENAME, CSVParser.defaultParser.createCSVRow(fileNameWithoutExtension, true),
PrivateMetadata.KEYCOLUMN, CSVParser.defaultParser.createCSVRow(keyColumns, true),
PrivateMetadata.CONNECTION, configConnectionName,
PrivateMetadata.SQLSCHEMA, sqlSchema,
PrivateMetadata.SQLTABLEPREFIX, sqlTablePrefix
);
if (importDBFData)
{
// get key column SQL code
String keyColumnsString;
if (keyColumns.length == 1)
{
keyColumnsString = keyColumns[0];
}
else
{
keyColumnsString = "CONCAT(";
for (int i = 0; i < keyColumns.length; i++)
{
if (i > 0)
keyColumnsString += ",";
keyColumnsString += "CAST(" + keyColumns[i] + " AS CHAR)";
}
keyColumnsString += ")";
}
// add SQL statements to sqlconfig
String[] columnNames = getSQLColumnNames(configConnectionName, password, sqlSchema, dbfTableName);
tableId = addConfigDataTable(
configTitle, configConnectionName,
configKeyType, keyColumnsString, -1, columnNames, columnNames,
sqlSchema, dbfTableName, false, null, tableInfo, append);
}
else
{
tableInfo.setPublicValues(
PublicMetadata.ENTITYTYPE, EntityType.TABLE,
PublicMetadata.TITLE, configTitle
);
tableId = dataConfig.newEntity(tableInfo, DataConfig.NULL, DataConfig.NULL);
}
try
{
// prepare metadata for existing column check
DataEntityMetadata geomInfo = new DataEntityMetadata();
// we don't search public metadata because that would require two sql queries
geomInfo.setPrivateValues(
PrivateMetadata.CONNECTION, configConnectionName,
PrivateMetadata.SQLSCHEMA, sqlSchema,
PrivateMetadata.SQLTABLEPREFIX, sqlTablePrefix
);
// check for existing geometry column using private metadata only
int existingGeomId = ListUtils.getFirstSortedItem(
dataConfig.searchPrivateMetadata(geomInfo.privateMetadata, null),
DataConfig.NULL
);
if (existingGeomId != DataConfig.NULL)
{
// see if the existing geometry column has the same parent table
boolean foundSameTableId = false;
for (int parentId : dataConfig.getParentIds(existingGeomId))
if (parentId == tableId)
foundSameTableId = true;
// if it does not have the same parent table, clear the existingGeomId
// so a new column entity will be created under the new table
if (!foundSameTableId)
existingGeomId = DataConfig.NULL;
}
// set the new public metadata
geomInfo.setPublicValues(
PublicMetadata.ENTITYTYPE, EntityType.COLUMN,
PublicMetadata.DATATYPE, DataType.GEOMETRY,
PublicMetadata.KEYTYPE, configKeyType,
PublicMetadata.PROJECTION, projectionSRS
);
if (existingGeomId == DataConfig.NULL)
{
// we only update the title if the column doesn't already exist
geomInfo.setPublicValues(PublicMetadata.TITLE, configTitle);
// create new column
dataConfig.newEntity(geomInfo, tableId, 0);
}
else
{
// update existing column
dataConfig.updateEntity(existingGeomId, geomInfo);
}
}
catch (IOException e)
{
throw new RemoteException("Unexpected error",e);