|
| 1 | +package org.geoserver.shell; |
| 2 | + |
| 3 | +import org.springframework.beans.factory.annotation.Autowired; |
| 4 | +import org.springframework.shell.core.CommandMarker; |
| 5 | +import org.springframework.shell.core.annotation.CliAvailabilityIndicator; |
| 6 | +import org.springframework.shell.core.annotation.CliCommand; |
| 7 | +import org.springframework.shell.core.annotation.CliOption; |
| 8 | +import org.springframework.stereotype.Component; |
| 9 | + |
| 10 | +@Component |
| 11 | +public class PostGISCommands implements CommandMarker { |
| 12 | + |
| 13 | + @Autowired |
| 14 | + private Geoserver geoserver; |
| 15 | + |
| 16 | + public void setGeoserver(Geoserver gs) { |
| 17 | + this.geoserver = gs; |
| 18 | + } |
| 19 | + |
| 20 | + @CliAvailabilityIndicator({"postgis datastore create", "postgis featuretype publish"}) |
| 21 | + public boolean isCommandAvailable() { |
| 22 | + return geoserver.isSet(); |
| 23 | + } |
| 24 | + |
| 25 | + @CliCommand(value = "postgis datastore create", help = "Create a PostGIS DataStore.") |
| 26 | + public boolean createDataStore( |
| 27 | + @CliOption(key = "workspace", mandatory = true, help = "The workspace") String workspace, |
| 28 | + @CliOption(key = "datastore", mandatory = true, help = "The datastore") String datastore, |
| 29 | + @CliOption(key = "host", mandatory = true, help = "The host") String host, |
| 30 | + @CliOption(key = "port", mandatory = false, unspecifiedDefaultValue = "5432", help = "The port") String port, |
| 31 | + @CliOption(key = "database", mandatory = true, help = "The database name") String database, |
| 32 | + @CliOption(key = "schema", mandatory = false, unspecifiedDefaultValue = "public", help = "The schema") String schema, |
| 33 | + @CliOption(key = "user", mandatory = true, help = "The user name") String user, |
| 34 | + @CliOption(key = "password", mandatory = true, help = "The password") String password |
| 35 | + ) throws Exception { |
| 36 | + StringBuilder connectionStringBuilder = new StringBuilder(); |
| 37 | + connectionStringBuilder.append("dbtype=postgis").append(" "); |
| 38 | + connectionStringBuilder.append("host=").append(host).append(" "); |
| 39 | + connectionStringBuilder.append("port=").append(port).append(" "); |
| 40 | + connectionStringBuilder.append("database='").append(database).append("' "); |
| 41 | + connectionStringBuilder.append("schema='").append(schema).append("' "); |
| 42 | + connectionStringBuilder.append("user='").append(user).append("' "); |
| 43 | + connectionStringBuilder.append("password='").append(password).append("'"); |
| 44 | + DataStoreCommands dataStoreCommands = new DataStoreCommands(); |
| 45 | + dataStoreCommands.setGeoserver(geoserver); |
| 46 | + return dataStoreCommands.create(workspace, datastore, connectionStringBuilder.toString(), null, true); |
| 47 | + } |
| 48 | + |
| 49 | + @CliCommand(value = "postgis featuretype publish", help = "Publish a PostGIS Table.") |
| 50 | + public boolean publishLayer( |
| 51 | + @CliOption(key = "workspace", mandatory = true, help = "The workspace") String workspace, |
| 52 | + @CliOption(key = "datastore", mandatory = true, help = "The datastore") String datastore, |
| 53 | + @CliOption(key = "table", mandatory = true, help = "The table") String table |
| 54 | + ) throws Exception { |
| 55 | + FeatureTypeCommands featureTypeCommands = new FeatureTypeCommands(); |
| 56 | + featureTypeCommands.setGeoserver(geoserver); |
| 57 | + return featureTypeCommands.publish(workspace, datastore, table); |
| 58 | + } |
| 59 | +} |
0 commit comments