//create a builder to help build catalog objects
CatalogBuilder builder = new CatalogBuilder(catalog);
builder.setWorkspace( catalog.getWorkspaceByName( workspace ) );
//check if the datastore already exists, if not auto configure one
DataStoreInfo info = catalog.getDataStoreByName( datastore );
boolean add = false;
if ( info == null ) {
LOGGER.info("Auto-configuring datastore: " + datastore);
info = builder.buildDataStore( datastore );
add = true;
}
else {
LOGGER.info("Using existing datastore: " + datastore);
}
builder.setStore(info);
//update the connection parameters to point to the new file
Map connectionParameters = info.getConnectionParameters();
for ( Param p : factory.getParametersInfo() ) {
//the nasty url / file hack
if ( File.class == p.type || URL.class == p.type ) {
File f = uploadedFile;
if ( "directory".equals( p.key ) ) {
//set the value to be the directory
f = directory;
}
//convert to the required type
//TODO: use geotools converter
Object converted = null;
if ( URI.class.equals( p.type ) ) {
converted = f.toURI();
}
else if ( URL.class.equals( p.type ) ) {
try {
converted = f.toURL();
}
catch (MalformedURLException e) {
}
}
//Converters.convert( f.getAbsolutePath(), p.type );
if ( converted != null ) {
connectionParameters.put( p.key, converted );
}
else {
connectionParameters.put( p.key, f );
}
continue;
}
if ( p.required ) {
try {
p.lookUp( connectionParameters );
}
catch( Exception e ) {
//set the sample value
connectionParameters.put( p.key, p.sample );
}
}
}
// set the namespace uri
NamespaceInfo namespace = catalog.getNamespaceByPrefix( info.getWorkspace().getName() );
connectionParameters.put( "namespace", namespace.getURI() );
// ensure the parameters are valid
if ( !factory.canProcess( connectionParameters ) ) {
//TODO: log the parameters at the debug level
throw new RestletException( "Unable to configure datastore, bad parameters.", Status.SERVER_ERROR_INTERNAL );
}
//add or update the datastore info
if ( add ) {
catalog.add( info );
}
else {
catalog.save( info );
}
//check configure parameter, if set to none to not try to configure
// data feature types
String configure = form.getFirstValue( "configure" );
if ( "none".equalsIgnoreCase( configure ) ) {
getResponse().setStatus( Status.SUCCESS_CREATED );
return;
}
//load the datastore
try {
DataStore ds = (DataStore) info.getDataStore(null);
String[] featureTypeNames = ds.getTypeNames();
for ( int i = 0; i < featureTypeNames.length; i++ ) {
//unless configure specified "all", only configure the first feature type