use of org.locationtech.geogig.api.RevObject.TYPE in project GeoGig by boundlessgeo.
the class BlameOp method _call.
@Override
protected BlameReport _call() {
String fullPath = (commit != null ? commit.toString() : Ref.HEAD) + ":" + path;
Optional<ObjectId> id = command(RevParse.class).setRefSpec(fullPath).call();
if (!id.isPresent()) {
throw new BlameException(StatusCode.FEATURE_NOT_FOUND);
}
TYPE type = command(ResolveObjectType.class).setObjectId(id.get()).call();
if (!type.equals(TYPE.FEATURE)) {
throw new BlameException(StatusCode.PATH_NOT_FEATURE);
}
Optional<RevFeatureType> featureType = command(ResolveFeatureType.class).setRefSpec(path).call();
BlameReport report = new BlameReport(featureType.get());
Iterator<RevCommit> log = command(LogOp.class).addPath(path).setUntil(commit).call();
RevCommit commit = log.next();
RevObjectParse revObjectParse = command(RevObjectParse.class);
DiffOp diffOp = command(DiffOp.class);
DiffFeature diffFeature = command(DiffFeature.class);
while (!report.isComplete()) {
if (!log.hasNext()) {
String refSpec = commit.getId().toString() + ":" + path;
RevFeature feature = revObjectParse.setRefSpec(refSpec).call(RevFeature.class).get();
report.setFirstVersion(feature, commit);
break;
}
RevCommit commitB = log.next();
Iterator<DiffEntry> diffs = diffOp.setNewVersion(commit.getId()).setOldVersion(commitB.getId()).setReportTrees(false).call();
while (diffs.hasNext()) {
DiffEntry diff = diffs.next();
if (path.equals(diff.newPath())) {
if (diff.isAdd()) {
String refSpec = commit.getId().toString() + ":" + path;
RevFeature feature = revObjectParse.setRefSpec(refSpec).call(RevFeature.class).get();
report.setFirstVersion(feature, commit);
break;
}
FeatureDiff featureDiff = diffFeature.setNewVersion(Suppliers.ofInstance(diff.getNewObject())).setOldVersion(Suppliers.ofInstance(diff.getOldObject())).call();
Map<PropertyDescriptor, AttributeDiff> attribDiffs = featureDiff.getDiffs();
Iterator<PropertyDescriptor> iter = attribDiffs.keySet().iterator();
while (iter.hasNext()) {
PropertyDescriptor key = iter.next();
Optional<?> value = attribDiffs.get(key).getNewValue();
String attribute = key.getName().toString();
report.addDiff(attribute, value, commit);
}
}
}
commit = commitB;
}
return report;
}
use of org.locationtech.geogig.api.RevObject.TYPE in project GeoGig by boundlessgeo.
the class BranchCreateOp method resolveOriginCommitId.
private ObjectId resolveOriginCommitId(String branchOrigin) {
Optional<Ref> ref = command(RefParse.class).setName(branchOrigin).call();
if (ref.isPresent()) {
ObjectId commitId = ref.get().getObjectId();
checkArgument(!commitId.isNull(), branchOrigin + " has no commits yet, branch cannot be created.");
return commitId;
}
Optional<ObjectId> objectId = command(RevParse.class).setRefSpec(branchOrigin).call();
checkArgument(objectId.isPresent(), branchOrigin + " does not resolve to a repository object");
ObjectId commitId = objectId.get();
TYPE objectType = command(ResolveObjectType.class).setObjectId(commitId).call();
checkArgument(TYPE.COMMIT.equals(objectType), branchOrigin + " does not resolve to a commit: " + objectType);
return commitId;
}
use of org.locationtech.geogig.api.RevObject.TYPE in project GeoGig by boundlessgeo.
the class Commit method runInternal.
/**
* Executes the commit command using the provided options.
*
* @param cli
* @see org.locationtech.geogig.cli.AbstractCommand#runInternal(org.locationtech.geogig.cli.GeogigCLI)
*/
@Override
public void runInternal(GeogigCLI cli) throws IOException {
final GeoGIG geogig = cli.getGeogig();
if (message == null || Strings.isNullOrEmpty(message)) {
message = geogig.command(ReadMergeCommitMessageOp.class).call();
}
checkParameter(!Strings.isNullOrEmpty(message) || commitToReuse != null || amend, "No commit message provided");
ConsoleReader console = cli.getConsole();
Ansi ansi = newAnsi(console.getTerminal());
RevCommit commit;
try {
CommitOp commitOp = geogig.command(CommitOp.class).setMessage(message).setAmend(amend);
if (commitTimestamp != null && !Strings.isNullOrEmpty(commitTimestamp)) {
Long millis = geogig.command(ParseTimestamp.class).setString(commitTimestamp).call();
commitOp.setCommitterTimestamp(millis.longValue());
}
if (commitToReuse != null) {
Optional<ObjectId> commitId = geogig.command(RevParse.class).setRefSpec(commitToReuse).call();
checkParameter(commitId.isPresent(), "Provided reference does not exist");
TYPE type = geogig.command(ResolveObjectType.class).setObjectId(commitId.get()).call();
checkParameter(TYPE.COMMIT.equals(type), "Provided reference does not resolve to a commit");
commitOp.setCommit(geogig.getRepository().getCommit(commitId.get()));
}
commit = commitOp.setPathFilters(pathFilters).setProgressListener(cli.getProgressListener()).call();
} catch (NothingToCommitException noChanges) {
throw new CommandFailedException(noChanges.getMessage(), noChanges);
}
final ObjectId parentId = commit.parentN(0).or(ObjectId.NULL);
console.println("[" + commit.getId() + "] " + commit.getMessage());
console.print("Committed, counting objects...");
Iterator<DiffEntry> diff = geogig.command(DiffOp.class).setOldVersion(parentId).setNewVersion(commit.getId()).call();
int adds = 0, deletes = 0, changes = 0;
DiffEntry diffEntry;
while (diff.hasNext()) {
diffEntry = diff.next();
switch(diffEntry.changeType()) {
case ADDED:
++adds;
break;
case REMOVED:
++deletes;
break;
case MODIFIED:
++changes;
break;
}
}
ansi.fg(Color.GREEN).a(adds).reset().a(" features added, ").fg(Color.YELLOW).a(changes).reset().a(" changed, ").fg(Color.RED).a(deletes).reset().a(" deleted.").reset().newline();
console.print(ansi.toString());
}
use of org.locationtech.geogig.api.RevObject.TYPE in project GeoGig by boundlessgeo.
the class SQLServerExport method runInternal.
/**
* Executes the export command using the provided options.
*/
@Override
protected void runInternal(GeogigCLI cli) throws IOException {
if (args.isEmpty()) {
printUsage(cli);
throw new CommandFailedException();
}
String path = args.get(0);
String tableName = args.get(1);
checkParameter(tableName != null && !tableName.isEmpty(), "No table name specified");
DataStore dataStore = getDataStore();
ObjectId featureTypeId = null;
if (!Arrays.asList(dataStore.getTypeNames()).contains(tableName)) {
SimpleFeatureType outputFeatureType;
if (sFeatureTypeId != null) {
// Check the feature type id string is a correct id
Optional<ObjectId> id = cli.getGeogig().command(RevParse.class).setRefSpec(sFeatureTypeId).call();
checkParameter(id.isPresent(), "Invalid feature type reference", sFeatureTypeId);
TYPE type = cli.getGeogig().command(ResolveObjectType.class).setObjectId(id.get()).call();
checkParameter(type.equals(TYPE.FEATURETYPE), "Provided reference does not resolve to a feature type: ", sFeatureTypeId);
outputFeatureType = (SimpleFeatureType) cli.getGeogig().command(RevObjectParse.class).setObjectId(id.get()).call(RevFeatureType.class).get().type();
featureTypeId = id.get();
} else {
try {
SimpleFeatureType sft = getFeatureType(path, cli);
outputFeatureType = new SimpleFeatureTypeImpl(new NameImpl(tableName), sft.getAttributeDescriptors(), sft.getGeometryDescriptor(), sft.isAbstract(), sft.getRestrictions(), sft.getSuper(), sft.getDescription());
} catch (GeoToolsOpException e) {
throw new CommandFailedException("No features to export.", e);
}
}
try {
dataStore.createSchema(outputFeatureType);
} catch (IOException e) {
throw new CommandFailedException("Cannot create new table in database", e);
}
} else {
if (!overwrite) {
throw new CommandFailedException("The selected table already exists. Use -o to overwrite");
}
}
SimpleFeatureSource featureSource = dataStore.getFeatureSource(tableName);
if (!(featureSource instanceof SimpleFeatureStore)) {
throw new CommandFailedException("Can't write to the selected table");
}
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
if (overwrite) {
try {
featureStore.removeFeatures(Filter.INCLUDE);
} catch (IOException e) {
throw new CommandFailedException("Error accessing table: " + e.getMessage(), e);
}
}
ExportOp op = cli.getGeogig().command(ExportOp.class).setFeatureStore(featureStore).setPath(path).setFilterFeatureTypeId(featureTypeId).setAlter(alter);
if (defaultType) {
op.exportDefaultFeatureType();
}
try {
op.setProgressListener(cli.getProgressListener()).call();
} catch (IllegalArgumentException iae) {
throw new org.locationtech.geogig.cli.InvalidParameterException(iae.getMessage(), iae);
} catch (GeoToolsOpException e) {
switch(e.statusCode) {
case MIXED_FEATURE_TYPES:
throw new CommandFailedException("The selected tree contains mixed feature types. Use --defaulttype or --featuretype <feature_type_ref> to export.", e);
default:
throw new CommandFailedException("Could not export. Error:" + e.statusCode.name(), e);
}
}
cli.getConsole().println(path + " exported successfully to " + tableName);
}
use of org.locationtech.geogig.api.RevObject.TYPE in project GeoGig by boundlessgeo.
the class ShpExport method runInternal.
/**
* Executes the export command using the provided options.
*/
@Override
protected void runInternal(GeogigCLI cli) throws IOException {
if (args.isEmpty()) {
printUsage(cli);
throw new CommandFailedException();
}
String path = args.get(0);
String shapefile = args.get(1);
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
File targetShapefile = new File(shapefile);
if (!targetShapefile.isAbsolute()) {
File pwd = cli.getGeogig().getPlatform().pwd();
String relativePath = targetShapefile.getPath();
targetShapefile = new File(pwd, relativePath);
}
if (targetShapefile.exists() && !overwrite) {
throw new CommandFailedException("The selected shapefile already exists. Use -o to overwrite");
}
Map<String, Serializable> params = new HashMap<String, Serializable>();
URL targetShapefileAsUrl = targetShapefile.toURI().toURL();
params.put(ShapefileDataStoreFactory.URLP.key, targetShapefileAsUrl);
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;
ObjectId featureTypeId;
if (sFeatureTypeId != null) {
// Check the feature type id string is a correct id
Optional<ObjectId> id = cli.getGeogig().command(RevParse.class).setRefSpec(sFeatureTypeId).call();
checkParameter(id.isPresent(), "Invalid feature type reference", sFeatureTypeId);
TYPE type = cli.getGeogig().command(ResolveObjectType.class).setObjectId(id.get()).call();
checkParameter(type.equals(TYPE.FEATURETYPE), "Provided reference does not resolve to a feature type: ", sFeatureTypeId);
outputFeatureType = (SimpleFeatureType) cli.getGeogig().command(RevObjectParse.class).setObjectId(id.get()).call(RevFeatureType.class).get().type();
featureTypeId = id.get();
} else {
try {
outputFeatureType = getFeatureType(path, cli);
featureTypeId = null;
} catch (GeoToolsOpException e) {
cli.getConsole().println("No features to export.");
return;
}
}
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.");
}
Function<Feature, Optional<Feature>> function = getTransformingFunction(dataStore.getSchema());
final SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
ExportOp op = cli.getGeogig().command(ExportOp.class).setFeatureStore(featureStore).setPath(path).setFilterFeatureTypeId(featureTypeId).setAlter(alter).setFeatureTypeConversionFunction(function);
// shapefile transactions are memory bound, so avoid them
op.setTransactional(false);
if (defaultType) {
op.exportDefaultFeatureType();
}
try {
op.setProgressListener(cli.getProgressListener()).call();
} catch (IllegalArgumentException iae) {
throw new org.locationtech.geogig.cli.InvalidParameterException(iae.getMessage(), iae);
} catch (GeoToolsOpException e) {
targetShapefile.delete();
switch(e.statusCode) {
case MIXED_FEATURE_TYPES:
throw new CommandFailedException("Error: The selected tree contains mixed feature types. Use --defaulttype or --featuretype <feature_type_ref> to export.", e);
default:
throw new CommandFailedException("Could not export. Error:" + e.statusCode.name(), e);
}
}
cli.getConsole().println(path + " exported successfully to " + shapefile);
}
Aggregations