use of org.locationtech.geogig.web.api.CommandResponse in project GeoGig by boundlessgeo.
the class LsTree method run.
/**
* Runs the command and builds the appropriate response
*
* @param context - the context to use for this command
*/
@Override
public void run(CommandContext context) {
String ref = null;
if (refList != null && !refList.isEmpty()) {
ref = refList.get(0);
}
LsTreeOp.Strategy lsStrategy = LsTreeOp.Strategy.CHILDREN;
if (recursive) {
if (includeTrees) {
lsStrategy = LsTreeOp.Strategy.DEPTHFIRST;
} else if (onlyTrees) {
lsStrategy = LsTreeOp.Strategy.DEPTHFIRST_ONLY_TREES;
} else {
lsStrategy = LsTreeOp.Strategy.DEPTHFIRST_ONLY_FEATURES;
}
} else {
if (onlyTrees) {
lsStrategy = LsTreeOp.Strategy.TREES_ONLY;
}
}
final Context geogig = this.getCommandLocator(context);
final Iterator<NodeRef> iter = geogig.command(LsTreeOp.class).setReference(ref).setStrategy(lsStrategy).call();
context.setResponseContent(new CommandResponse() {
@Override
public void write(ResponseWriter out) throws Exception {
out.start(true);
out.writeLsTreeResponse(iter, verbose);
out.finish();
}
});
}
use of org.locationtech.geogig.web.api.CommandResponse in project GeoGig by boundlessgeo.
the class MergeWebOp method run.
/**
* Runs the command and builds the appropriate response.
*
* @param context - the context to use for this command
*
* @throws CommandSpecException
*/
@Override
public void run(CommandContext context) {
if (this.getTransactionId() == null) {
throw new CommandSpecException("No transaction was specified, merge requires a transaction to preserve the stability of the repository.");
} else if (this.commit == null) {
throw new CommandSpecException("No commits were specified for merging.");
}
final GeogigTransaction transaction = (GeogigTransaction) this.getCommandLocator(context);
final Optional<Ref> currHead = transaction.command(RefParse.class).setName(Ref.HEAD).call();
if (!currHead.isPresent()) {
throw new CommandSpecException("Repository has no HEAD, can't merge.");
}
MergeOp merge = transaction.command(MergeOp.class);
merge.setAuthor(authorName.orNull(), authorEmail.orNull());
final Optional<ObjectId> oid = transaction.command(RevParse.class).setRefSpec(commit).call();
if (oid.isPresent()) {
merge.addCommit(Suppliers.ofInstance(oid.get()));
} else {
throw new CommandSpecException("Couldn't resolve '" + commit + "' to a commit.");
}
try {
final MergeReport report = merge.setNoCommit(noCommit).call();
context.setResponseContent(new CommandResponse() {
@Override
public void write(ResponseWriter out) throws Exception {
out.start();
out.writeMergeResponse(Optional.fromNullable(report.getMergeCommit()), report.getReport().get(), transaction, report.getOurs(), report.getPairs().get(0).getTheirs(), report.getPairs().get(0).getAncestor());
out.finish();
}
});
} catch (Exception e) {
final RevCommit ours = context.getGeoGIG().getRepository().getCommit(currHead.get().getObjectId());
final RevCommit theirs = context.getGeoGIG().getRepository().getCommit(oid.get());
final Optional<ObjectId> ancestor = transaction.command(FindCommonAncestor.class).setLeft(ours).setRight(theirs).call();
context.setResponseContent(new CommandResponse() {
final MergeScenarioReport report = transaction.command(ReportMergeScenarioOp.class).setMergeIntoCommit(ours).setToMergeCommit(theirs).call();
@Override
public void write(ResponseWriter out) throws Exception {
out.start();
Optional<RevCommit> mergeCommit = Optional.absent();
out.writeMergeResponse(mergeCommit, report, transaction, ours.getId(), theirs.getId(), ancestor.get());
out.finish();
}
});
}
}
use of org.locationtech.geogig.web.api.CommandResponse in project GeoGig by boundlessgeo.
the class PullWebOp method run.
/**
* Runs the command and builds the appropriate response.
*
* @param context - the context to use for this command
*/
@Override
public void run(CommandContext context) {
final Context geogig = this.getCommandLocator(context);
PullOp command = geogig.command(PullOp.class).setAuthor(authorName.orNull(), authorEmail.orNull()).setRemote(remoteName).setAll(fetchAll).addRefSpec(refSpec);
try {
final PullResult result = command.call();
final Iterator<DiffEntry> iter;
if (result.getOldRef() != null && result.getNewRef() != null && result.getOldRef().equals(result.getNewRef())) {
iter = null;
} else {
if (result.getOldRef() == null) {
iter = geogig.command(DiffOp.class).setNewVersion(result.getNewRef().getObjectId()).setOldVersion(ObjectId.NULL).call();
} else {
iter = geogig.command(DiffOp.class).setNewVersion(result.getNewRef().getObjectId()).setOldVersion(result.getOldRef().getObjectId()).call();
}
}
context.setResponseContent(new CommandResponse() {
@Override
public void write(ResponseWriter out) throws Exception {
out.start();
out.writePullResponse(result, iter, geogig);
out.finish();
}
});
} catch (SynchronizationException e) {
switch(e.statusCode) {
case HISTORY_TOO_SHALLOW:
default:
context.setResponseContent(CommandResponse.error("Unable to pull, the remote history is shallow."));
}
} catch (MergeConflictsException e) {
String[] refs = refSpec.split(":");
String remoteRef = Ref.REMOTES_PREFIX + remoteName + "/" + refs[0];
Optional<Ref> sourceRef = geogig.command(RefParse.class).setName(remoteRef).call();
String destinationref = "";
if (refs.length == 2) {
destinationref = refs[1];
} else {
final Optional<Ref> currHead = geogig.command(RefParse.class).setName(Ref.HEAD).call();
if (!currHead.isPresent()) {
context.setResponseContent(CommandResponse.error("Repository has no HEAD, can't pull."));
} else if (!(currHead.get() instanceof SymRef)) {
context.setResponseContent(CommandResponse.error("Can't pull from detached HEAD"));
}
final SymRef headRef = (SymRef) currHead.get();
destinationref = headRef.getTarget();
}
Optional<Ref> destRef = geogig.command(RefParse.class).setName(destinationref).call();
final RevCommit theirs = context.getGeoGIG().getRepository().getCommit(sourceRef.get().getObjectId());
final RevCommit ours = context.getGeoGIG().getRepository().getCommit(destRef.get().getObjectId());
final Optional<ObjectId> ancestor = geogig.command(FindCommonAncestor.class).setLeft(ours).setRight(theirs).call();
context.setResponseContent(new CommandResponse() {
final MergeScenarioReport report = geogig.command(ReportMergeScenarioOp.class).setMergeIntoCommit(ours).setToMergeCommit(theirs).call();
@Override
public void write(ResponseWriter out) throws Exception {
out.start();
Optional<RevCommit> mergeCommit = Optional.absent();
out.writeMergeResponse(mergeCommit, report, geogig, ours.getId(), theirs.getId(), ancestor.get());
out.finish();
}
});
}
}
use of org.locationtech.geogig.web.api.CommandResponse in project GeoGig by boundlessgeo.
the class PushWebOp method run.
/**
* Runs the command and builds the appropriate response.
*
* @param context - the context to use for this command
*/
@Override
public void run(CommandContext context) {
final Context geogig = this.getCommandLocator(context);
PushOp command = geogig.command(PushOp.class);
if (refSpec != null) {
command.addRefSpec(refSpec);
}
try {
final TransferSummary dataPushed = command.setAll(pushAll).setRemote(remoteName).call();
context.setResponseContent(new CommandResponse() {
@Override
public void write(ResponseWriter out) throws Exception {
out.start();
out.writeElement("Push", "Success");
out.writeElement("dataPushed", String.valueOf(!dataPushed.isEmpty()));
out.finish();
}
});
} catch (SynchronizationException e) {
switch(e.statusCode) {
case REMOTE_HAS_CHANGES:
context.setResponseContent(CommandResponse.error("Push failed: The remote repository has changes that would be lost in the event of a push."));
break;
case HISTORY_TOO_SHALLOW:
context.setResponseContent(CommandResponse.error("Push failed: There is not enough local history to complete the push."));
default:
break;
}
}
}
use of org.locationtech.geogig.web.api.CommandResponse in project GeoGig by boundlessgeo.
the class FeatureDiffWeb method run.
/**
* Runs the command and builds the appropriate response
*
* @param context - the context to use for this command
*
* @throws CommandSpecException
*/
@Override
public void run(CommandContext context) {
if (path == null || path.trim().isEmpty()) {
throw new CommandSpecException("No path for feature name specifed");
}
final Context geogig = this.getCommandLocator(context);
ObjectId newId = geogig.command(ResolveTreeish.class).setTreeish(newTreeish).call().get();
ObjectId oldId = geogig.command(ResolveTreeish.class).setTreeish(oldTreeish).call().get();
RevFeature newFeature = null;
RevFeatureType newFeatureType = null;
RevFeature oldFeature = null;
RevFeatureType oldFeatureType = null;
final Map<PropertyDescriptor, AttributeDiff> diffs;
Optional<NodeRef> ref = parseID(newId, geogig);
Optional<RevObject> object;
// need these to determine if the feature was added or removed so I can build the diffs
// myself until the FeatureDiff supports null values
boolean removed = false;
boolean added = false;
if (ref.isPresent()) {
object = geogig.command(RevObjectParse.class).setObjectId(ref.get().getMetadataId()).call();
if (object.isPresent() && object.get() instanceof RevFeatureType) {
newFeatureType = (RevFeatureType) object.get();
} else {
throw new CommandSpecException("Couldn't resolve newCommit's featureType");
}
object = geogig.command(RevObjectParse.class).setObjectId(ref.get().objectId()).call();
if (object.isPresent() && object.get() instanceof RevFeature) {
newFeature = (RevFeature) object.get();
} else {
throw new CommandSpecException("Couldn't resolve newCommit's feature");
}
} else {
removed = true;
}
if (!oldId.equals(ObjectId.NULL)) {
ref = parseID(oldId, geogig);
if (ref.isPresent()) {
object = geogig.command(RevObjectParse.class).setObjectId(ref.get().getMetadataId()).call();
if (object.isPresent() && object.get() instanceof RevFeatureType) {
oldFeatureType = (RevFeatureType) object.get();
} else {
throw new CommandSpecException("Couldn't resolve oldCommit's featureType");
}
object = geogig.command(RevObjectParse.class).setObjectId(ref.get().objectId()).call();
if (object.isPresent() && object.get() instanceof RevFeature) {
oldFeature = (RevFeature) object.get();
} else {
throw new CommandSpecException("Couldn't resolve oldCommit's feature");
}
} else {
added = true;
}
} else {
added = true;
}
if (removed) {
Map<PropertyDescriptor, AttributeDiff> tempDiffs = new HashMap<PropertyDescriptor, AttributeDiff>();
ImmutableList<PropertyDescriptor> attributes = oldFeatureType.sortedDescriptors();
ImmutableList<Optional<Object>> values = oldFeature.getValues();
for (int index = 0; index < attributes.size(); index++) {
Optional<Object> value = values.get(index);
if (Geometry.class.isAssignableFrom(attributes.get(index).getType().getBinding())) {
Optional<Geometry> temp = Optional.absent();
if (value.isPresent() || all) {
tempDiffs.put(attributes.get(index), new GeometryAttributeDiff(Optional.fromNullable((Geometry) value.orNull()), temp));
}
} else {
if (value.isPresent() || all) {
tempDiffs.put(attributes.get(index), new GenericAttributeDiffImpl(value, Optional.absent()));
}
}
}
diffs = tempDiffs;
} else if (added) {
Map<PropertyDescriptor, AttributeDiff> tempDiffs = new HashMap<PropertyDescriptor, AttributeDiff>();
ImmutableList<PropertyDescriptor> attributes = newFeatureType.sortedDescriptors();
ImmutableList<Optional<Object>> values = newFeature.getValues();
for (int index = 0; index < attributes.size(); index++) {
Optional<Object> value = values.get(index);
if (Geometry.class.isAssignableFrom(attributes.get(index).getType().getBinding())) {
Optional<Geometry> temp = Optional.absent();
if (value.isPresent() || all) {
tempDiffs.put(attributes.get(index), new GeometryAttributeDiff(temp, Optional.fromNullable((Geometry) value.orNull())));
}
} else {
if (value.isPresent() || all) {
tempDiffs.put(attributes.get(index), new GenericAttributeDiffImpl(Optional.absent(), value));
}
}
}
diffs = tempDiffs;
} else {
FeatureDiff diff = new FeatureDiff(path, newFeature, oldFeature, newFeatureType, oldFeatureType, all);
diffs = diff.getDiffs();
}
context.setResponseContent(new CommandResponse() {
@Override
public void write(ResponseWriter out) throws Exception {
out.start();
out.writeFeatureDiffResponse(diffs);
out.finish();
}
});
}
Aggregations