long clientCentralRevision = client.getCentralRevision(layerName);
// compute the diff that we have to send the client. Notice that we have
// to skip over the local change occurred when we last performed a GetDiff
// against the client
VersioningFeatureStore fs = (VersioningFeatureStore) ds
.getFeatureSource(tableName);
fs.setTransaction(transaction);
String fromRevision = clientCentralRevision == -1 ? "FIRST" : String
.valueOf(clientCentralRevision);
TransactionType centralChanges;
LOGGER.log(Level.INFO, "About to compute PostDiff changes. Last central revision known to client " + clientCentralRevision + ", last GetDiff central revision " + getDiffCentralRevision);
if (getDiffCentralRevision == null || clientCentralRevision >= getDiffCentralRevision) {
// either first time or we don't need to make jumps
LOGGER.log(Level.INFO, "First PostDiff or clientRevion same as the last central one, computing diff from " + fromRevision + " to LAST");
FeatureDiffReader fdr = fs.getDifferences(fromRevision, "LAST", null, null);
centralChanges = new VersioningTransactionConverter().convert(fdr,
TransactionType.class);
} else {
// we need to jump over the last local changes
String before = String.valueOf(getDiffCentralRevision - 1);
String after = String.valueOf(getDiffCentralRevision);
LOGGER.log(Level.INFO, "Client revision lower than the server one, computing diff from " + fromRevision + " to " + before + " and merging with diffs from " + after + " to LAST");
FeatureDiffReader fdr1 = fs.getDifferences(fromRevision, before, null, null);
FeatureDiffReader fdr2 = fs.getDifferences(after, "LAST", null, null);
FeatureDiffReader[] fdr = new FeatureDiffReader[] { fdr1, fdr2 };
centralChanges = new VersioningTransactionConverter().convert(fdr,
TransactionType.class);
}
// what is the latest change on this layer? (worst case it's the last GetDiff
// from this Unit)
long lastCentralRevision = clientCentralRevision;
li = fs.getLog("LAST", fromRevision, null, null, 1).features();
if (li.hasNext()) {
lastCentralRevision = (Long) li.next().getAttribute("revision");
}
li.close();
// finally run the PostDiff
PostDiffType postDiff = new PostDiffType();
postDiff.setTypeName(layerName);
postDiff.setFromVersion(clientCentralRevision);
postDiff.setToVersion(lastCentralRevision);
postDiff.setTransaction(centralChanges);
client.postDiff(postDiff);
// grab the changes from the client and apply them locally
GetDiffType getDiff = new GetDiffType();
getDiff.setFromVersion(lastUnitRevision == null ? -1 : lastUnitRevision);
getDiff.setTypeName(layerName);
GetDiffResponseType gdr = client.getDiff(getDiff);
TransactionType unitChanges = gdr.getTransaction();
core.applyChanges(unitChanges, fs);
// mark down this layer as succesfully synchronised
FeatureStore<SimpleFeatureType, SimpleFeature> tuMetadata = (FeatureStore<SimpleFeatureType, SimpleFeature>) ds
.getFeatureSource(SYNCH_UNIT_TABLES);
tuMetadata.setTransaction(transaction);
SimpleFeatureType tuSchema = tuMetadata.getSchema();
int unitChangeCount = core.countChanges(unitChanges);
int centralChangeCount = core.countChanges(centralChanges);
if (unitChangeCount == 0 && centralChangeCount == 0) {
// just update the last_synch marker, as nothing else happened and
// this way we can avoid eating away central revision number (which
// might go up very rapidly otherwise)
AttributeDescriptor[] atts = new AttributeDescriptor[] { tuSchema
.getDescriptor("last_synchronization") };
Object[] values = new Object[] { new Date() };
Filter filter = ff.and(ff.equals(ff.property("table_id"), ff
.literal(tableId)), ff.equals(ff.property("unit_id"), ff
.literal(unitId)));
tuMetadata.modifyFeatures(atts, values, filter);
} else {
AttributeDescriptor[] atts = new AttributeDescriptor[] {
tuSchema.getDescriptor("last_synchronization"),
tuSchema.getDescriptor("getdiff_central_revision"),
tuSchema.getDescriptor("last_unit_revision") };
Object[] values = new Object[] { new Date(),
Long.parseLong(fs.getVersion()), gdr.getToVersion() };
Filter filter = ff.and(ff.equals(ff.property("table_id"), ff
.literal(tableId)), ff.equals(ff.property("unit_id"), ff
.literal(unitId)));
tuMetadata.modifyFeatures(atts, values, filter);
}