use of org.locationtech.geogig.cli.CommandFailedException in project GeoGig by boundlessgeo.
the class ShpExportDiff method runInternal.
/**
* Executes the export command using the provided options.
*/
@Override
protected void runInternal(GeogigCLI cli) throws IOException {
if (args.size() != 4) {
printUsage(cli);
throw new CommandFailedException();
}
String commitOld = args.get(0);
String commitNew = args.get(1);
String path = args.get(2);
String shapefile = args.get(3);
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
File file = new File(shapefile);
if (file.exists() && !overwrite) {
throw new CommandFailedException("The selected shapefile already exists. Use -o to overwrite");
}
Map<String, Serializable> params = new HashMap<String, Serializable>();
params.put(ShapefileDataStoreFactory.URLP.key, file.toURI().toURL());
params.put(ShapefileDataStoreFactory.CREATE_SPATIAL_INDEX.key, Boolean.FALSE);
params.put(ShapefileDataStoreFactory.ENABLE_SPATIAL_INDEX.key, Boolean.FALSE);
ShapefileDataStore dataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
SimpleFeatureType outputFeatureType;
try {
outputFeatureType = getFeatureType(path, cli);
} catch (GeoToolsOpException e) {
cli.getConsole().println("No features to export.");
return;
}
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.add("geogig_fid", String.class);
for (AttributeDescriptor descriptor : outputFeatureType.getAttributeDescriptors()) {
builder.add(descriptor);
}
builder.setName(outputFeatureType.getName());
builder.setCRS(outputFeatureType.getCoordinateReferenceSystem());
outputFeatureType = builder.buildFeatureType();
dataStore.createSchema(outputFeatureType);
final String typeName = dataStore.getTypeNames()[0];
final SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
if (!(featureSource instanceof SimpleFeatureStore)) {
throw new CommandFailedException("Could not create feature store.");
}
final SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
Function<Feature, Optional<Feature>> function = getTransformingFunction(dataStore.getSchema());
ExportDiffOp op = cli.getGeogig().command(ExportDiffOp.class).setFeatureStore(featureStore).setPath(path).setOldRef(commitOld).setNewRef(commitNew).setUseOld(old).setTransactional(false).setFeatureTypeConversionFunction(function);
try {
op.setProgressListener(cli.getProgressListener()).call();
} catch (IllegalArgumentException iae) {
throw new org.locationtech.geogig.cli.InvalidParameterException(iae.getMessage(), iae);
} catch (GeoToolsOpException e) {
file.delete();
switch(e.statusCode) {
case MIXED_FEATURE_TYPES:
throw new CommandFailedException("Error: The selected tree contains mixed feature types.", e);
default:
throw new CommandFailedException("Could not export. Error:" + e.statusCode.name(), e);
}
}
cli.getConsole().println(path + " exported successfully to " + shapefile);
}
use of org.locationtech.geogig.cli.CommandFailedException in project GeoGig by boundlessgeo.
the class ShpImport method runInternal.
/**
* Executes the import command using the provided options.
*/
@Override
protected void runInternal(GeogigCLI cli) throws IOException {
checkParameter(shapeFile != null && !shapeFile.isEmpty(), "No shapefile specified");
for (String shp : shapeFile) {
DataStore dataStore = null;
try {
dataStore = getDataStore(shp);
} catch (InvalidParameterException e) {
cli.getConsole().println("The shapefile '" + shp + "' could not be found, skipping...");
continue;
}
if (fidAttribute != null) {
AttributeDescriptor attrib = dataStore.getSchema(dataStore.getNames().get(0)).getDescriptor(fidAttribute);
if (attrib == null) {
throw new InvalidParameterException("The specified attribute does not exist in the selected shapefile");
}
}
try {
cli.getConsole().println("Importing from shapefile " + shp);
ProgressListener progressListener = cli.getProgressListener();
ImportOp command = cli.getGeogig().command(ImportOp.class).setAll(true).setTable(null).setAlter(alter).setOverwrite(!add).setDestinationPath(destTable).setDataStore(dataStore).setFidAttribute(fidAttribute).setAdaptToDefaultFeatureType(!forceFeatureType);
// force the import not to use paging due to a bug in the shapefile datastore
command.setUsePaging(false);
command.setProgressListener(progressListener).call();
cli.getConsole().println(shp + " imported successfully.");
} catch (GeoToolsOpException e) {
switch(e.statusCode) {
case NO_FEATURES_FOUND:
throw new CommandFailedException("No features were found in the shapefile.", e);
case UNABLE_TO_GET_NAMES:
throw new CommandFailedException("Unable to get feature types from the shapefile.", e);
case UNABLE_TO_GET_FEATURES:
throw new CommandFailedException("Unable to get features from the shapefile.", e);
case UNABLE_TO_INSERT:
throw new CommandFailedException("Unable to insert features into the working tree.", e);
case INCOMPATIBLE_FEATURE_TYPE:
throw new CommandFailedException("The feature type of the data to import does not match the feature type of the destination tree and cannot be imported\n" + "USe the --force-featuretype switch to import using the original featuretype and crete a mixed type tree", e);
default:
throw new CommandFailedException("Import failed with exception: " + e.statusCode.name(), e);
}
} finally {
dataStore.dispose();
cli.getConsole().flush();
}
}
}
use of org.locationtech.geogig.cli.CommandFailedException in project GeoGig by boundlessgeo.
the class Apply method runInternal.
@Override
public void runInternal(GeogigCLI cli) throws IOException {
checkParameter(patchFiles.size() < 2, "Only one single patch file accepted");
checkParameter(!patchFiles.isEmpty(), "No patch file specified");
ConsoleReader console = cli.getConsole();
GeoGIG geogig = cli.getGeogig();
File patchFile = new File(patchFiles.get(0));
checkParameter(patchFile.exists(), "Patch file cannot be found");
FileInputStream stream;
try {
stream = new FileInputStream(patchFile);
} catch (FileNotFoundException e1) {
throw new CommandFailedException("Can't open patch file " + patchFile, e1);
}
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
} catch (UnsupportedEncodingException e) {
Closeables.closeQuietly(reader);
Closeables.closeQuietly(stream);
throw new CommandFailedException("Error reading patch file " + patchFile, e);
}
Patch patch = PatchSerializer.read(reader);
Closeables.closeQuietly(reader);
Closeables.closeQuietly(stream);
if (reverse) {
patch = patch.reversed();
}
if (summary) {
console.println(patch.toString());
} else if (check) {
VerifyPatchResults verify = cli.getGeogig().command(VerifyPatchOp.class).setPatch(patch).call();
Patch toReject = verify.getToReject();
Patch toApply = verify.getToApply();
if (toReject.isEmpty()) {
console.println("Patch can be applied.");
} else {
console.println("Error: Patch cannot be applied\n");
console.println("Applicable entries:\n");
console.println(toApply.toString());
console.println("\nConflicting entries:\n");
console.println(toReject.toString());
}
} else {
try {
Patch rejected = geogig.command(ApplyPatchOp.class).setPatch(patch).setApplyPartial(reject).call();
if (reject) {
if (rejected.isEmpty()) {
console.println("Patch applied succesfully");
} else {
int accepted = patch.count() - rejected.count();
StringBuilder sb = new StringBuilder();
File file = new File(patchFile.getAbsolutePath() + ".rej");
sb.append("Patch applied only partially.\n");
sb.append(Integer.toString(accepted) + " changes were applied.\n");
sb.append(Integer.toString(rejected.count()) + " changes were rejected.\n");
BufferedWriter writer = Files.newWriter(file, Charsets.UTF_8);
PatchSerializer.write(writer, patch);
writer.flush();
writer.close();
sb.append("Patch file with rejected changes created at " + file.getAbsolutePath() + "\n");
throw new CommandFailedException(sb.toString());
}
} else {
console.println("Patch applied succesfully");
}
} catch (CannotApplyPatchException e) {
throw new CommandFailedException(e);
}
}
}
use of org.locationtech.geogig.cli.CommandFailedException in project GeoGig by boundlessgeo.
the class Checkout method runInternal.
@Override
public void runInternal(GeogigCLI cli) throws IOException {
final GeoGIG geogig = cli.getGeogig();
checkParameter(branchOrStartPoint.size() != 0 || !paths.isEmpty(), "no branch or paths specified");
checkParameter(branchOrStartPoint.size() < 2, "too many arguments");
try {
final ConsoleReader console = cli.getConsole();
String branchOrCommit = (branchOrStartPoint.size() > 0 ? branchOrStartPoint.get(0) : null);
CheckoutResult result = geogig.command(CheckoutOp.class).setForce(force).setSource(branchOrCommit).addPaths(paths).setOurs(ours).setTheirs(theirs).call();
switch(result.getResult()) {
case CHECKOUT_LOCAL_BRANCH:
console.println("Switched to branch '" + result.getNewRef().localName() + "'");
break;
case CHECKOUT_REMOTE_BRANCH:
console.println("Branch '" + result.getNewRef().localName() + "' was set up to track remote branch '" + result.getNewRef().localName() + "' from " + result.getRemoteName() + ".");
console.println("Switched to a new branch '" + result.getNewRef().localName() + "'");
break;
case UPDATE_OBJECTS:
console.println("Objects in the working tree were updated to the specifed version.");
break;
case DETACHED_HEAD:
console.println("You are in 'detached HEAD' state. HEAD is now at " + result.getOid().toString().substring(0, 7) + "...");
break;
default:
break;
}
} catch (CheckoutException e) {
switch(e.statusCode) {
case LOCAL_CHANGES_NOT_COMMITTED:
throw new CommandFailedException("Working tree and index are not clean. To overwrite local changes, use the --force option", e);
case UNMERGED_PATHS:
throw new CommandFailedException(e.getMessage(), e);
}
}
}
use of org.locationtech.geogig.cli.CommandFailedException in project GeoGig by boundlessgeo.
the class Config method runInternal.
/**
* Executes the config command using the provided options.
*/
@Override
public void runInternal(GeogigCLI cli) throws IOException {
GeoGIG geogig = cli.getGeogig();
boolean closeIt = geogig == null;
if (closeIt) {
// we're not in a repository, need a geogig anyways to run the global commands
geogig = cli.newGeoGIG(Hints.readOnly());
}
try {
String name = null;
String value = null;
if (nameValuePair != null && !nameValuePair.isEmpty()) {
name = nameValuePair.get(0);
value = buildValueString();
}
ConfigAction action = resolveConfigAction();
if (action == ConfigAction.CONFIG_NO_ACTION) {
printUsage(cli);
throw new CommandFailedException();
}
if (global && local) {
printUsage(cli);
throw new CommandFailedException();
}
ConfigScope scope = ConfigScope.DEFAULT;
if (global) {
scope = ConfigScope.GLOBAL;
} else if (local) {
scope = ConfigScope.LOCAL;
}
final Optional<Map<String, String>> commandResult = geogig.command(ConfigOp.class).setScope(scope).setAction(action).setName(name).setValue(value).call();
if (commandResult.isPresent()) {
switch(action) {
case CONFIG_GET:
{
cli.getConsole().println(commandResult.get().get(name));
break;
}
case CONFIG_LIST:
{
Iterator<Map.Entry<String, String>> it = commandResult.get().entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> pairs = (Map.Entry<String, String>) it.next();
cli.getConsole().println(pairs.getKey() + "=" + pairs.getValue());
}
break;
}
default:
break;
}
}
} catch (ConfigException e) {
// since we don't have regex support yet.
switch(e.statusCode) {
case INVALID_LOCATION:
// TODO: This could probably be more descriptive.
throw new CommandFailedException("The config location is invalid", e);
case CANNOT_WRITE:
throw new CommandFailedException("Cannot write to the config", e);
case SECTION_OR_NAME_NOT_PROVIDED:
throw new InvalidParameterException("No section or name was provided", e);
case SECTION_OR_KEY_INVALID:
throw new InvalidParameterException("The section or key is invalid", e);
case OPTION_DOES_NOT_EXIST:
throw new InvalidParameterException("Tried to unset an option that does not exist", e);
case MULTIPLE_OPTIONS_MATCH:
throw new InvalidParameterException("Tried to unset/set an option for which multiple lines match", e);
case INVALID_REGEXP:
throw new InvalidParameterException("Tried to use an invalid regexp", e);
case USERHOME_NOT_SET:
throw new InvalidParameterException("Used --global option without $HOME being properly set", e);
case TOO_MANY_ACTIONS:
throw new InvalidParameterException("Tried to use more than one action at a time", e);
case MISSING_SECTION:
throw new InvalidParameterException("Could not find a section with the name provided", e);
case TOO_MANY_ARGS:
throw new InvalidParameterException("Too many arguments provided.", e);
}
} finally {
if (closeIt) {
geogig.close();
}
}
}
Aggregations