Select to view content in your preferred language

SOI: Querying features in the current branch during a post

985
2
10-20-2022 08:14 AM
Labels (1)
Frédéric_ALBERT
New Contributor II

Hello,

My use case is to prevent users to post edits when a query condition is met in the edited data.
Dataset is branch versioned.
A SOI is used for that purpose, because I want the check to be true for any editing client.
The SOI is intercepting post and reconcile with post from the version management REST endpoint.
I can easily query the versioned feature class using the name of the version (eg. user.version).
Unfortunately, the request interceptor only provides the guid of the version (from the REST resource name).
I cannot find a clean way to infer the version name from the guid. I can only query the branch table:

select * from sde.sde_branches where branch_guid='{" + guid + "}'"

This implementation is fragile,as I really need something robust across databases, schema naming and access rights.
Is there a proper way to do this in an interceptor in the Java Enterprise SDK ?

 

0 Kudos
2 Replies
omar-marji
New Contributor III

Not sure if you managed to resolve this without querying the table.

I'd be interested in the solution if you have found one, as I cannot seem to find any reference to APIs within the SDK to get the branch version GUID .

0 Kudos
Frédéric_ALBERT
New Contributor II

No answers so far.
I believe there is no proper "sdk way" to do this currently.
I do wonder if there is another way to prevent posting a version on some condition.

Anyhow, here is a snippet of how I did implement it, if someone is interested:

@Override
public byte[] handleRESTRequest(String capabilities, String resourceName, String operationName,
String operationInput, String outputFormat, String requestProperties, String[] responseProperties)
throws IOException, AutomationException {

MapServer mapserver = (MapServer) this.serverObject;

// Operation filter
// .... "post".equalsIgnoreCase(operationName) ....
// Operation filter

IRESTRequestHandler restRequestHandler = this.soiHelper.findRestRequestHandlerDelegate(this.serverObject);

// Logic to fetch layer id
int layerId = 0;
// ... "post".equalsIgnoreCase(operationName) ...
// Logic to fetch layer id

// Extract GUID from REST resource
String guid = resourceName.split("/")[1];

// Get layer feature class as well as its branch versioned workspace
IMapServerDataAccess mapServerDataAccess = (IMapServerDataAccess) mapserver;
FeatureClass featureClass = new FeatureClass(
mapServerDataAccess.getDataSource(mapserver.getDefaultMapName(), layerId));
IDataset dataset = new IDatasetProxy(featureClass);
IVersionedWorkspace versionWorkspace = new IVersionedWorkspaceProxy(dataset.getWorkspace());
ISqlWorkspace sqlWorkspace = new ISqlWorkspaceProxy(dataset.getWorkspace());

// Infer version name from version guid
ICursor cursor = sqlWorkspace
.openQueryCursor("select * from sde.sde_branches where branch_guid='{" + guid + "}'");
IRow row;
String versionName = null;
if ((row = cursor.nextRow()) != null) {
versionName = (String) row.getValue(cursor.findField("owner")) + "."
+ (String) row.getValue(cursor.findField("name"));
}
if (versionName == null) {
throw new IOException("Cannot find version {" + guid + "}");
}
IVersion version = versionWorkspace.findVersion(versionName);

// Switch feature class branch
IFeatureWorkspace versionedWorkspace = new IFeatureWorkspaceProxy(version);
IFeatureClass versionedFeatureClass = versionedWorkspace.openFeatureClass(dataset.getName());

// Query feature class with condition
// Error request if any row meet the condition
QueryFilter queryFilter = new QueryFilter();
queryFilter.setWhereClause("1=0");
queryFilter.setSubFields("*");

long count = versionedFeatureClass.featureCount(queryFilter);

if (count > 0) {
return new JSONObject().put("error", new JSONObject().put("code", 400).put("message", "Invalid condition"))
.toString().getBytes();
}

// Go on with request handling
//
return restRequestHandler.handleRESTRequest(capabilities, resourceName, operationName, operationInput,
outputFormat, requestProperties, responseProperties);
}