Package org.geotools.data

Examples of org.geotools.data.VersioningFeatureStore


    @Override
    protected void setUp() throws Exception {
        super.setUp();
       
        // create a feature store
        VersioningFeatureStore mockStore = createNiceMock(VersioningFeatureStore.class);
        mockStore.removeFeatures(Filter.INCLUDE);
        expect(mockStore.getSchema()).andReturn(DataUtilities.createType(SCHEMA, "id:int"));
        replay(mockStore);

        // create a data store
        mock = createNiceMock(VersioningDataStore.class);
        expect(mock.getFeatureSource(SCHEMA)).andReturn(mockStore);
View Full Code Here


    public void execute(EObject element, TransactionType request, Map featureStores,
            TransactionResponseType response, TransactionListener listener)
            throws WFSTransactionException {
        RollbackType rollback = (RollbackType) element;
        final QName layerName = rollback.getTypeName();
        VersioningFeatureStore vstore = (VersioningFeatureStore) featureStores.get(layerName);
        if(vstore == null)
            throw new WFSTransactionException("Could not locate feature type " + layerName);
       
        long inserted = response.getTransactionSummary().getTotalInserted().longValue();
        long updated = response.getTransactionSummary().getTotalUpdated().longValue();
        long deleted = response.getTransactionSummary().getTotalDeleted().longValue();

        FeatureDiffReader reader = null;

        try {
            // we use the difference to compute the number of inserted,
            // updated and deleted features, but we can't use these to
            // actually perform the rollback, since we would be unable to
            // preserve the fids of the ones that were deleted and need to
            // be re-inserted
            Filter filter = (rollback.getFilter() != null) ? (Filter) rollback.getFilter()
                    : Filter.INCLUDE;
            String version = rollback.getToFeatureVersion();
            String user = rollback.getUser();
            String[] users = ((user != null) && !user.trim().equals("")) ? new String[] { user }
                    : null;
            reader = vstore.getDifferences("LAST", version, filter, users);

            Set insertedIds = new HashSet();
            Set updatedIds = new HashSet();
            Set deletedIds = new HashSet();
            while (reader.hasNext()) {
                FeatureDiff fd = reader.next();

                if (fd.getState() == FeatureDiff.INSERTED) {
                    inserted++;

                    InsertedFeatureType insertedFeature = WfsFactory.eINSTANCE
                            .createInsertedFeatureType();
                    insertedFeature.setHandle(rollback.getHandle());
                    insertedFeature.getFeatureId().add(filterFactory.featureId(fd.getID()));
                    response.getInsertResults().getFeature().add(insertedFeature);
                    // accumulate fids for transaction event handling
                    insertedIds.add(filterFactory.featureId(fd.getID()));
                } else if (fd.getState() == FeatureDiff.UPDATED) {
                    updated++;
                    // accumulate fids for transaction event handling
                    updatedIds.add(filterFactory.featureId(fd.getID()));
                } else if (fd.getState() == FeatureDiff.DELETED) {
                    deleted++;
                    // accumulate fids for transaction event handling
                    deletedIds.add(filterFactory.featureId(fd.getID()));
                }
            }

            // build filters
            Filter insertedFilter = filterFactory.id(insertedIds);
            Filter updatedFilter = filterFactory.id(updatedIds);
            Filter deletedFilter = filterFactory.id(deletedIds);

            // notify pre-update and pre-delete
           
            listener.dataStoreChange(new TransactionEvent(TransactionEventType.PRE_UPDATE, layerName,
                    vstore.getFeatures(updatedFilter), rollback));
            listener.dataStoreChange(new TransactionEvent(TransactionEventType.PRE_DELETE, layerName,
                    vstore.getFeatures(deletedFilter), rollback));

            // now do the actual rollback
            try {
                vstore.rollback(version, (Filter) rollback.getFilter(), users);
            } catch (Exception e) {
                throw new WFSTransactionException("Could not perform the rollback", e, rollback
                        .getHandle());
            }

            // notify post update and post insert
            listener.dataStoreChange(new TransactionEvent(TransactionEventType.POST_INSERT, layerName,
                    vstore.getFeatures(insertedFilter)));
            listener.dataStoreChange(new TransactionEvent(TransactionEventType.POST_UPDATE, layerName,
                    vstore.getFeatures(updatedFilter)));

            // update summary information
            response.getTransactionSummary().setTotalInserted(BigInteger.valueOf(inserted));
            response.getTransactionSummary().setTotalUpdated(BigInteger.valueOf(updated));
            response.getTransactionSummary().setTotalDeleted(BigInteger.valueOf(deleted));
View Full Code Here

     * Sheer luck, the local changes are on the same feature, and are the same changes Central is pushing onto us
     * @throws Exception
     */
    public void testCleanMerge() throws Exception {
        // grab the datastore
        VersioningFeatureStore restricted = (VersioningFeatureStore) synchStore.getFeatureSource("restricted");
        SimpleFeatureType schema = restricted.getSchema();
        // make the same changes as in the post diff
        Id updateFilter = ff.id(singleton(ff.featureId("restricted.be7cafea-d0b7-4257-9b9c-1ed3de3f7ef4")));
        restricted.modifyFeatures(schema.getDescriptor("cat"), -48, updateFilter);
        // remove the third feature
        Id removeFilter = ff.id(singleton(ff.featureId("restricted.d91fe390-bdc7-4b22-9316-2cd6c8737ef5")));
        restricted.removeFeatures(removeFilter);
        assertEquals(3, restricted.getCount(Query.ALL));
       

        // get the response and do the basic checks
        MockHttpServletResponse response = postAsServletResponse(root(true),
                loadTextResource("PostDiffInitial.xml"));
View Full Code Here

        assertEquals(0, gss.getActiveConflicts("restricted").size());
    }

    public void testDeleteConflict() throws Exception {
        // grab the datastore so that we can make some changes that will generate conflicts
        VersioningFeatureStore restricted = (VersioningFeatureStore) synchStore.getFeatureSource("restricted");
        restricted.removeFeatures(ff.id(singleton(ff.featureId("restricted.be7cafea-d0b7-4257-9b9c-1ed3de3f7ef4"))));
        assertEquals(3, restricted.getCount(Query.ALL));
    
        // get the response and do the basic checks
        MockHttpServletResponse response = postAsServletResponse(root(true),
                loadTextResource("PostDiffInitial.xml"));
        checkPostDiffSuccessResponse(response);
View Full Code Here

        assertNull(f.getAttribute("local_feature"));
    }
   
    public void testUpdateConflict() throws Exception {
        // grab the datastore so that we can make some changes that will generate conflicts
        VersioningFeatureStore restricted = (VersioningFeatureStore) synchStore.getFeatureSource("restricted");
        SimpleFeatureType schema = restricted.getSchema();
        Id fidFilter = ff.id(singleton(ff.featureId("restricted.be7cafea-d0b7-4257-9b9c-1ed3de3f7ef4")));
        restricted.modifyFeatures(schema.getDescriptor("cat"), 123456, fidFilter);
        assertEquals(4, restricted.getCount(Query.ALL));
    
        // get the response and do the basic checks
        MockHttpServletResponse response = postAsServletResponse(root(true),
                loadTextResource("PostDiffInitial.xml"));
        checkPostDiffSuccessResponse(response);
View Full Code Here

           
            // see if there is anything at all to do, if both sides have no changes there
            // is no point eating away a revision number (this avoid the local revision number to
            // skyrocket for nothing if there are frequent synchronisations)
            String tableName = request.getTypeName().getLocalPart();
            VersioningFeatureStore fs = (VersioningFeatureStore) ds.getFeatureSource(tableName);
            FeatureStore history = (FeatureStore) ds.getFeatureSource(SYNCH_HISTORY);
            PropertyIsEqualTo ftSyncRecord = ff.equals(ff.property("table_name"), ff.literal(tableName));
            TransactionType changes = request.getTransaction();
            int changesCount = core.countChanges(changes);
            // ... if we have no changes from remote
            if(changesCount == 0) {
                // ... and we have no changes locally
                String lastLocalRevisionId = lastLocalRevision != -1 ? String.valueOf(lastLocalRevision) : "FIRST";
                if(fs.getLog(lastLocalRevisionId, "LAST", null, null, 1).size() == 0) {
                    // add a new record without the need to grab a new local revision
                    // (if necessary, that is, if at least the Central revision changed or if
                    // we don't have a synch history at all)
                    long newCentralRevision = request.getToVersion();
                    if(lastCentralRevision != newCentralRevision || record == null) {
                        SimpleFeatureType hft = (SimpleFeatureType) history.getSchema();
                        SimpleFeature f = SimpleFeatureBuilder.build(hft, new Object[] { tableName,
                                lastLocalRevision, newCentralRevision }, null);
                        history.addFeatures(DataUtilities.collection(f));
                    }
                   
                    // ... let's just return directly, no need to store or do anything
                    return new PostDiffResponseType();
                }
            }
           
            // setup the commit message and author
            transaction.putProperty(VersioningDataStore.AUTHOR, "gss");
            transaction.putProperty(VersioningDataStore.MESSAGE, "Applying " + changesCount
                    + " changes coming from Central on layer '" + tableName + "'");

            // grab the feature stores and bind them all to the same transaction
            VersioningFeatureStore conflicts = (VersioningFeatureStore) ds
                    .getFeatureSource(SYNCH_CONFLICTS);
            conflicts.setTransaction(transaction);
            history.setTransaction(transaction);
            fs.setTransaction(transaction);

            // get a hold on a revision number early so that we don't get concurrent changes
            // from the user (the datastore will make it so that no new revision numbers will
            // be generated until we commit or rollback this transaction
            long newLocalRevision = Long.parseLong(conflicts.getVersion());

            // apply changes
            LOGGER.info("About to apply " + core.countChanges(changes)
                    + " changes coming from Central");
            if (core.countChanges(changes) > 0) {
View Full Code Here

public class MergingFeatureDiffReaderTest extends GSSTestSupport {

    public void testMergeSingle() throws Exception {
        // grab the datastore
        VersioningFeatureStore restricted = (VersioningFeatureStore) synchStore
                .getFeatureSource("restricted");
        SimpleFeatureType schema = restricted.getSchema();

        // an update
        Id updateFilter = ff.id(singleton(ff
                .featureId("restricted.be7cafea-d0b7-4257-9b9c-1ed3de3f7ef4")));
        restricted.modifyFeatures(schema.getDescriptor("cat"), -48, updateFilter);
        // a delete
        Id removeFilter = ff.id(singleton(ff
                .featureId("restricted.d91fe390-bdc7-4b22-9316-2cd6c8737ef5")));
        restricted.removeFeatures(removeFilter);
        // and an insert
        WKTReader wkt = new WKTReader();
        SimpleFeature f = SimpleFeatureBuilder.build(schema, new Object[] { 123,
                wkt.read("POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))") }, null);
        restricted.addFeatures(collection(f));

        // grab a diff reader
        FeatureDiffReader reader = restricted.getDifferences("FIRST", "LAST", null, null);
        FeatureDiffReader reader2 = restricted.getDifferences("FIRST", "LAST", null, null);

        // build a merging one, it should report the same resuls
        MergingFeatureDiffReader merge = new MergingFeatureDiffReader(reader2);

        int count = 0;
View Full Code Here

        assertFalse((Boolean) f.getAttribute("errors"));
    }
   
    public void testLocalChanges() throws Exception {
        // apply a local change on Central so that we'll get a non empty transaction sent to the client
        VersioningFeatureStore restricted = (VersioningFeatureStore) synchStore.getFeatureSource("restricted");
        SimpleFeatureType schema = restricted.getSchema();
        // remove the third feature
        Id removeFilter = ff.id(singleton(ff.featureId("restricted.c15e76ab-e44b-423e-8f85-f6d9927b878a")));
        restricted.removeFeatures(removeFilter);
        assertEquals(3, restricted.getCount(Query.ALL));
       
        // build the expected PostDiff request
        QName typeName = new QName("http://www.openplans.org/spearfish", "restricted");
        PostDiffType postDiff = new PostDiffType();
        postDiff.setFromVersion(-1);
View Full Code Here

    }
   
   
    public void testRemoteChanges() throws Exception {
        // make sure we start with 4 features
        VersioningFeatureStore restricted = (VersioningFeatureStore) synchStore.getFeatureSource("restricted");
        assertEquals(4, restricted.getCount(Query.ALL));
       
        // build a "no local changes" postdiff
        QName typeName = new QName("http://www.openplans.org/spearfish", "restricted");
        PostDiffType postDiff = new PostDiffType();
        postDiff.setFromVersion(-1);
        postDiff.setToVersion(-1);
        postDiff.setTypeName(typeName);
        postDiff.setTransaction(WfsFactory.eINSTANCE.createTransactionType());
       
        // build the expected GetDiff object
        GetDiffType getDiff = new GetDiffType();
        getDiff.setTypeName(typeName);
        getDiff.setFromVersion(-1);
       
        // build a GetDiffResponse that will trigger a deletion
        GetDiffResponseType gdr = new GetDiffResponseType();
        gdr.setFromVersion(-1);
        gdr.setFromVersion(6);
        gdr.setTypeName(typeName);
        TransactionType changes = WfsFactory.eINSTANCE.createTransactionType();
        DeleteElementType delete = WfsFactory.eINSTANCE.createDeleteElementType();
        delete.setTypeName(typeName);
        Id removeFilter = ff.id(singleton(ff.featureId("restricted.c15e76ab-e44b-423e-8f85-f6d9927b878a")));
        delete.setFilter(removeFilter);
        changes.getDelete().add(delete);
        gdr.setTransaction(changes);
       
        // create mock objects that will check the calls are flowing as expected
        GSSClient client = createMock(GSSClient.class);
        expect(client.getCentralRevision((QName) anyObject())).andReturn(new Long(-1));
        client.postDiff(postDiff);
        expect(client.getDiff((GetDiffType) anyObject())).andReturn(gdr);
        replay(client);
        GSSClientFactory factory = createMock(GSSClientFactory.class);
        expect(factory.createClient(new URL("http://localhost:8081/geoserver/ows"), null, null))
                .andReturn(client);
        replay(factory);
       
        synch.clientFactory = factory;
       
        // perform synch
        Date start = new Date();
        synch.synchronizeOustandlingLayers();
        Date end = new Date();

        // check we stored the last synch marker
        SimpleFeature f = getSingleFeature(fsUnitTables, ff.equal(ff.property("table_id"), ff.literal(1), false));
        Date lastSynch = (Date) f.getAttribute("last_synchronization");
        assertNotNull(lastSynch);
        assertTrue(lastSynch.compareTo(start) >= 0 && lastSynch.compareTo(end) <= 0);
        assertNull(f.getAttribute("last_failure"));
       
        // check we marked the unit as succeded
        f = getSingleFeature(fsUnits, ff.equal(ff.property("unit_name"), ff.literal("unit1"), false));
        assertFalse((Boolean) f.getAttribute("errors"));
       
        // check the deletion actually happened locally
        assertEquals(3, restricted.getCount(Query.ALL));
        assertEquals(0, restricted.getCount(new DefaultQuery("restricted", removeFilter)));
    }
View Full Code Here

     * Local but not conflicting changes
     * @throws Exception
     */
    public void testLocalChangesNoConflict() throws Exception {
        // grab the datastore so that we can make some changes that will not generate conflicts
        VersioningFeatureStore restricted = (VersioningFeatureStore) synchStore.getFeatureSource("restricted");
        SimpleFeatureType schema = restricted.getSchema();
        // modify the fourth feature, change its cat from 400 to 450
        Id updateFilter = ff.id(singleton(ff.featureId("restricted.1b99be2b-2480-4742-ad52-95c294efda3b")));
        restricted.modifyFeatures(schema.getDescriptor("cat"), 450, updateFilter);
        // remove the third feature
        Id removeFilter = ff.id(singleton(ff.featureId("restricted.c15e76ab-e44b-423e-8f85-f6d9927b878a")));
        restricted.removeFeatures(removeFilter);
        assertEquals(3, restricted.getCount(Query.ALL));
       

        // get the response and do the basic checks
        MockHttpServletResponse response = postAsServletResponse(root(true),
                loadTextResource("PostDiffInitial.xml"));
        checkPostDiffSuccessResponse(response);
        checkPostDiffInitialChanges(restricted);

        // check there are no conflicts
        assertEquals(0, gss.getActiveConflicts("restricted").size());
       
        // check the local changes are still there
        assertEquals(0, restricted.getCount(new DefaultQuery(null, removeFilter)));
        FeatureIterator<SimpleFeature> fi;
        fi = restricted.getFeatures(updateFilter).features();
        assertTrue(fi.hasNext());
        SimpleFeature f = fi.next();
        fi.close();
        assertEquals(450l, f.getAttribute("cat"));
    }
View Full Code Here

TOP

Related Classes of org.geotools.data.VersioningFeatureStore

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.