Package org.geotools.data

Examples of org.geotools.data.Transaction


      return ff;
    }
   
    public static void doDelete(DataStore ds,SimpleFeatureType ft, Id ff) throws NoSuchElementException, IllegalAttributeException, IOException{
      assertNotNull("doInsertFailed?",ff);
      Transaction t = new DefaultTransaction();
      SimpleFeatureStore fs = (SimpleFeatureStore)ds.getFeatureSource(ft.getTypeName());
      fs.setTransaction(t);
     
      System.out.println("Delete Read 1");
      SimpleFeatureIterator fr = fs.getFeatures().features();
      int count1 = 0;
      while(fr.hasNext()){
        count1 ++; fr.next();
      }
        fr.close();

      System.out.println("Delete Remove "+ff);
      fs.removeFeatures(ff);

      System.out.println("Delete Read 2");
      fr = fs.getFeatures().features();
      int count2 = 0;
      while(fr.hasNext()){
        count2 ++;
        if(count2<5)
          System.out.println("# == "+count2+" "+fr.next().getID());
        else
          fr.next();
      }
        fr.close();
      assertTrue("Read 1 == "+count1+" Read 2 == "+count2,count2<count1);

      System.out.println("Delete Commit");
      t.commit();

      System.out.println("Delete Read 3");
      fr = fs.getFeatures().features();
      int count3 = 0;
      while(fr.hasNext()){
View Full Code Here


        fr.close();
      assertTrue(count2==count3);
    }
   
    public static void doUpdate(DataStore ds,SimpleFeatureType ft, String attributeToChange, Object newValue ) throws IllegalFilterException, FactoryRegistryException, NoSuchElementException, IOException, IllegalAttributeException{
      Transaction t = new DefaultTransaction();
      SimpleFeatureStore fs = (SimpleFeatureStore)ds.getFeatureSource(ft.getTypeName());
      fs.setTransaction(t);
     
      AttributeDescriptor at = ft.getDescriptor(attributeToChange);
      assertNotNull("Attribute "+attributeToChange+" does not exist",at);
     
      FilterFactory filterFactory = CommonFactoryFinder.getFilterFactory(null);
        Filter f = filterFactory.equals(filterFactory.property(at.getLocalName()), filterFactory
                .literal(newValue));

      System.out.println("Update Read 1");
      SimpleFeatureIterator fr = fs.getFeatures(f).features();
     
      int count1 = 0;
      Object oldValue=null;
        if(fr!=null)
      while(fr.hasNext()){
        count1 ++; oldValue=fr.next().getAttribute(attributeToChange);
      }

        fr.close();
      System.out.println("Update Modify");
      fs.modifyFeatures(at,newValue,Filter.INCLUDE);

      System.out.println("Update Read 2");
      fr = fs.getFeatures(f).features();
      int count2 = 0;
      while(fr.hasNext()){
        count2 ++;
//        System.out.println(fr.next());
        fr.next();
      }
        fr.close();
//System.out.println("Read 1 == "+count1+" Read 2 == "+count2);
      assertTrue("Read 1 == "+count1+" Read 2 == "+count2,count2>count1);

      System.out.println("Update Commit");
        try {
            t.commit();

            assertTrue(((WFSTransactionState) t.getState(ds)).getFids(ft.getTypeName()) != null);

            System.out.println("Update Read 3");
            fr = fs.getFeatures(f).features();
            int count3 = 0;
            while( fr.hasNext() ) {
                count3++;
                fr.next();
            }
            fr.close();
            assertEquals(count2, count3);
        } finally {
            // cleanup
            fs.modifyFeatures(at, oldValue, Filter.INCLUDE);
            t.commit();
        }
    }
View Full Code Here

        if (transaction != null && state.containsKey(transaction)) {
            return state.get(transaction);
        } else {
            ContentState auto = state.get(Transaction.AUTO_COMMIT);
            ContentState copy = (ContentState) auto.copy();
            Transaction t = (transaction != null ? transaction : Transaction.AUTO_COMMIT);
            copy.setTransaction(t);
            state.put(t, copy);

            return copy;
        }
View Full Code Here

       
        SimpleFeature feat = new SimpleFeatureImpl(Arrays.asList(new Object[]{myPoint, "mypoint""pics/x.jpg", "pics/y.jpg"}), featureType1, new FeatureIdImpl("myid") );
       
        collection.add(feat);
       
        Transaction transaction = new DefaultTransaction();
        store.setTransaction(transaction);
       
        List<FeatureId> fids = store.addFeatures((SimpleFeatureCollection) collection);       
        assertEquals(1, fids.size());
       
        Filter filterRemove = filterfac.id(filterfac.featureId("poi.2"));       
        store.removeFeatures(filterRemove);
       
        Filter filterUpdate = filterfac.id(filterfac.featureId("poi.3"));       
        store.modifyFeatures("NAME", "blah", filterUpdate);
               
        transaction.commit();
       
        ContentFeatureCollection coll = store.getFeatures();
        FeatureIterator it= coll.features();
        while (it.hasNext()) {
            System.err.println(it.next());
View Full Code Here

        assertEquals(1, count(reader));
    }

    @Test
    public void testGetFeatureReaderFilterTransaction() throws Exception {
        Transaction t = new DefaultTransaction();
        SimpleFeatureType type = data.getSchema(getRoadTypeName());
        FeatureReader<SimpleFeatureType, SimpleFeature> reader;

        reader = data.getFeatureReader(new Query(getRoadTypeName(), Filter.EXCLUDE), t);

        // TODO: remove this silly check!
        // assertTrue(reader instanceof EmptyFeatureReader);
        assertEquals(type, reader.getFeatureType());
        assertEquals(0, count(reader));

        reader = data.getFeatureReader(new Query(getRoadTypeName()), t);
        assertTrue(reader instanceof DiffFeatureReader);
        assertEquals(type, reader.getFeatureType());
        assertEquals(roadFeatures.length, count(reader));

        reader = data.getFeatureReader(new Query(getRoadTypeName(), rd1Filter), t);

        // assertTrue(reader instanceof DiffFeatureReader);//Currently wrapped by a filtering
        // feature reader
        assertEquals(type, reader.getFeatureType());
        assertEquals(1, count(reader));

        SimpleFeatureStore store = (SimpleFeatureStore) data.getFeatureSource(getRoadTypeName());
        store.setTransaction(t);
        store.removeFeatures(rd1Filter);

        reader = data.getFeatureReader(new Query(getRoadTypeName(), Filter.EXCLUDE), t);
        assertEquals(0, count(reader));

        reader = data.getFeatureReader(new Query(getRoadTypeName()), t);
        assertEquals(roadFeatures.length - 1, count(reader));

        reader = data.getFeatureReader(new Query(getRoadTypeName(), rd1Filter), t);
        assertEquals(0, count(reader));

        t.rollback();
        reader = data.getFeatureReader(new Query(getRoadTypeName(), Filter.EXCLUDE), t);
        assertEquals(0, count(reader));

        reader = data.getFeatureReader(new Query(getRoadTypeName()), t);
        assertEquals(roadFeatures.length, count(reader));
View Full Code Here

    /**
     * Test two transactions one removing feature, and one adding a feature.
     */
    @Test
    public void testGetFeatureWriterTransaction() throws Exception {
        Transaction t1 = new DefaultTransaction();
        Transaction t2 = new DefaultTransaction();
        FeatureWriter<SimpleFeatureType, SimpleFeature> writer1 = data.getFeatureWriter(
                getRoadTypeName(), rd1Filter, t1);
        FeatureWriter<SimpleFeatureType, SimpleFeature> writer2 = data.getFeatureWriterAppend(
                getRoadTypeName(), t2);

        SimpleFeatureType road = data.getSchema(getRoadTypeName());
        FeatureReader<SimpleFeatureType, SimpleFeature> reader;
        SimpleFeature feature;
        SimpleFeature[] ORIGIONAL = roadFeatures;
        SimpleFeature[] REMOVE = new SimpleFeature[ORIGIONAL.length - 1];
        SimpleFeature[] ADD = new SimpleFeature[ORIGIONAL.length + 1];
        SimpleFeature[] FINAL = new SimpleFeature[ORIGIONAL.length];
        int i;
        int index;
        index = 0;

        for (i = 0; i < ORIGIONAL.length; i++) {
            feature = ORIGIONAL[i];

            if (!feature.getID().equals(roadFeatures[0].getID())) {
                REMOVE[index++] = feature;
            }
        }

        for (i = 0; i < ORIGIONAL.length; i++) {
            ADD[i] = ORIGIONAL[i];
        }

        ADD[i] = newRoad;

        for (i = 0; i < REMOVE.length; i++) {
            FINAL[i] = REMOVE[i];
        }

        FINAL[i] = newRoad;

        // start of with ORIGINAL
        final Query allRoadsQuery = new Query(getRoadTypeName());
        reader = data.getFeatureReader(allRoadsQuery, Transaction.AUTO_COMMIT);
        assertTrue(covers(reader, ORIGIONAL));

        // writer 1 removes road.rd1 on t1
        // -------------------------------
        // - tests transaction independence from DataStore
        while (writer1.hasNext()) {
            feature = (SimpleFeature) writer1.next();
            assertEquals(roadFeatures[0].getID(), feature.getID());
            writer1.remove();
        }

        // still have ORIGIONAL and t1 has REMOVE
        reader = data.getFeatureReader(allRoadsQuery, Transaction.AUTO_COMMIT);

        assertTrue(covers(reader, ORIGIONAL));

        reader = data.getFeatureReader(allRoadsQuery, t1);
        assertTrue(covers(reader, REMOVE));

        // close writer1
        // --------------
        // ensure that modification is left up to transaction commmit
        writer1.close();

        // We still have ORIGIONAL and t1 has REMOVE
        reader = data.getFeatureReader(allRoadsQuery, Transaction.AUTO_COMMIT);
        assertTrue(covers(reader, ORIGIONAL));
        reader = data.getFeatureReader(allRoadsQuery, t1);
        assertTrue(covers(reader, REMOVE));

        // writer 2 adds road.rd4 on t2
        // ----------------------------
        // - tests transaction independence from each other
        feature = (SimpleFeature) writer2.next();
        feature.setAttributes(newRoad.getAttributes());
        writer2.write();

        // We still have ORIGIONAL and t2 has ADD
        reader = data.getFeatureReader(allRoadsQuery, Transaction.AUTO_COMMIT);
        assertTrue(covers(reader, ORIGIONAL));
        reader = data.getFeatureReader(allRoadsQuery, t2);
        assertTrue(coversLax(reader, ADD));

        // close writer2
        // -------------
        // ensure that modification is left up to transaction commmit
        writer2.close();

        // Still have ORIGIONAL and t2 has ADD
        reader = data.getFeatureReader(allRoadsQuery, Transaction.AUTO_COMMIT);
        assertTrue(covers(reader, ORIGIONAL));
        reader = data.getFeatureReader(allRoadsQuery, t2);
        assertTrue(coversLax(reader, ADD));

        // commit t1
        // ---------
        // -ensure that delayed writing of transactions takes place
        //
        t1.commit();

        // We now have REMOVE, as does t1 (which has not additional diffs)
        // t2 will have FINAL
        reader = data.getFeatureReader(allRoadsQuery, Transaction.AUTO_COMMIT);
        assertTrue(covers(reader, REMOVE));
        reader = data.getFeatureReader(allRoadsQuery, t1);
        assertTrue(covers(reader, REMOVE));
        reader = data.getFeatureReader(allRoadsQuery, t2);
        assertTrue(coversLax(reader, FINAL));

        // commit t2
        // ---------
        // -ensure that everyone is FINAL at the end of the day
        t2.commit();

        // We now have Number( remove one and add one)
        reader = data.getFeatureReader(allRoadsQuery, Transaction.AUTO_COMMIT);
        reader = data.getFeatureReader(allRoadsQuery, Transaction.AUTO_COMMIT);
        assertTrue(coversLax(reader, FINAL));
View Full Code Here

        assertEquals(1, road.getFeatures().size());
    }

    @Test
    public void testGetFeatureStoreTransactionSupport() throws Exception {
        Transaction t1 = new DefaultTransaction();
        Transaction t2 = new DefaultTransaction();

        SimpleFeatureStore road = (SimpleFeatureStore) data.getFeatureSource(getRoadTypeName());
        SimpleFeatureStore road1 = (SimpleFeatureStore) data.getFeatureSource(getRoadTypeName());
        SimpleFeatureStore road2 = (SimpleFeatureStore) data.getFeatureSource(getRoadTypeName());

        road1.setTransaction(t1);
        road2.setTransaction(t2);

        SimpleFeature feature;
        SimpleFeature[] ORIGINAL = roadFeatures;
        SimpleFeature[] REMOVE = new SimpleFeature[ORIGINAL.length - 1];
        SimpleFeature[] ADD = new SimpleFeature[ORIGINAL.length + 1];
        SimpleFeature[] FINAL = new SimpleFeature[ORIGINAL.length];
        int i;
        int index;
        index = 0;

        for (i = 0; i < ORIGINAL.length; i++) {
            feature = ORIGINAL[i];

            if (!feature.getID().equals(roadFeatures[0].getID())) {
                REMOVE[index++] = feature;
            }
        }

        for (i = 0; i < ORIGINAL.length; i++) {
            ADD[i] = ORIGINAL[i];
        }

        ADD[i] = newRoad;

        for (i = 0; i < REMOVE.length; i++) {
            FINAL[i] = REMOVE[i];
        }

        FINAL[i] = newRoad;

        // start of with ORIGINAL
        assertTrue(covers(road.getFeatures().features(), ORIGINAL));

        // road1 removes road.rd1 on t1
        // -------------------------------
        // - tests transaction independence from DataStore
        road1.removeFeatures(rd1Filter);

        // still have ORIGIONAL and t1 has REMOVE
        assertTrue(covers(road.getFeatures().features(), ORIGINAL));
        assertTrue(covers(road1.getFeatures().features(), REMOVE));

        // road2 adds road.rd4 on t2
        // ----------------------------
        // - tests transaction independence from each other
        SimpleFeatureCollection collection = DataUtilities
                .collection(new SimpleFeature[] { newRoad, });
        road2.addFeatures(collection);

        // We still have ORIGIONAL, t1 has REMOVE, and t2 has ADD
        assertTrue(covers(road.getFeatures().features(), ORIGINAL));
        assertTrue(covers(road1.getFeatures().features(), REMOVE));
        assertTrue(coversLax(road2.getFeatures().features(), ADD));

        // commit t1
        // ---------
        // -ensure that delayed writing of transactions takes place
        //
        t1.commit();

        // We now have REMOVE, as does t1 (which has not additional diffs)
        // t2 will have FINAL
        assertTrue(covers(road.getFeatures().features(), REMOVE));
        assertTrue(covers(road1.getFeatures().features(), REMOVE));
        assertTrue(coversLax(road2.getFeatures().features(), FINAL));

        // commit t2
        // ---------
        // -ensure that everyone is FINAL at the end of the day
        t2.commit();

        // We now have Number( remove one and add one)
        assertTrue(coversLax(road.getFeatures().features(), FINAL));
        assertTrue(coversLax(road1.getFeatures().features(), FINAL));
        assertTrue(coversLax(road2.getFeatures().features(), FINAL));
View Full Code Here

            road.unLockFeatures();
            fail("unlock should fail due on AUTO_COMMIT");
        } catch (IOException expected) {
        }

        Transaction t = new DefaultTransaction();
        road.setTransaction(t);

        try {
            road.unLockFeatures();
            fail("unlock should fail due lack of authorization");
        } catch (IOException expected) {
        }

        t.addAuthorization(lock.getAuthorization());
        road.unLockFeatures();
    }
View Full Code Here

    @SuppressWarnings("unchecked")
    @Test
    public void testLockFeatureInteraction() throws IOException {
        FeatureLock lockA = new FeatureLock("LockA", 3600);
        FeatureLock lockB = new FeatureLock("LockB", 3600);
        Transaction t1 = new DefaultTransaction();
        Transaction t2 = new DefaultTransaction();
        FeatureLocking<SimpleFeatureType, SimpleFeature> road1;
        FeatureLocking<SimpleFeatureType, SimpleFeature> road2;

        {
            SimpleFeatureSource source = data.getFeatureSource(getRoadTypeName());
            if (!(source instanceof FeatureLocking)) {
                LOGGER.info("testLockFeatureInteraction ignored, store does not support locking");
                return;
            }

            road1 = (FeatureLocking<SimpleFeatureType, SimpleFeature>) source;

            source = data.getFeatureSource(getRoadTypeName());
            if (!(source instanceof FeatureLocking)) {
                LOGGER.info("testLockFeatureInteraction ignored, store does not support locking");
                return;
            }
            road2 = (FeatureLocking<SimpleFeatureType, SimpleFeature>) source;
        }
        road1.setTransaction(t1);
        road2.setTransaction(t2);
        road1.setFeatureLock(lockA);
        road2.setFeatureLock(lockB);

        assertFalse(isLocked(getRoadTypeName(), roadFeatures[0].getID()));
        assertFalse(isLocked(getRoadTypeName(), roadFeatures[1].getID()));
        assertFalse(isLocked(getRoadTypeName(), roadFeatures[2].getID()));

        road1.lockFeatures(rd1Filter);
        assertTrue(isLocked(getRoadTypeName(), roadFeatures[0].getID()));
        assertFalse(isLocked(getRoadTypeName(), roadFeatures[1].getID()));
        assertFalse(isLocked(getRoadTypeName(), roadFeatures[2].getID()));

        road2.lockFeatures(rd2Filter);
        assertTrue(isLocked(getRoadTypeName(), roadFeatures[0].getID()));
        assertTrue(isLocked(getRoadTypeName(), roadFeatures[1].getID()));
        assertFalse(isLocked(getRoadTypeName(), roadFeatures[2].getID()));

        try {
            road1.unLockFeatures(rd1Filter);
            fail("need authorization");
        } catch (IOException expected) {
        }

        t1.addAuthorization(lockA.getAuthorization());

        try {
            road1.unLockFeatures(rd2Filter);
            fail("need correct authorization");
        } catch (IOException expected) {
        }

        road1.unLockFeatures(rd1Filter);
        assertFalse(isLocked(getRoadTypeName(), roadFeatures[0].getID()));
        assertTrue(isLocked(getRoadTypeName(), roadFeatures[1].getID()));
        assertFalse(isLocked(getRoadTypeName(), roadFeatures[2].getID()));

        t2.addAuthorization(lockB.getAuthorization());
        road2.unLockFeatures(rd2Filter);
        assertFalse(isLocked(getRoadTypeName(), roadFeatures[0].getID()));
        assertFalse(isLocked(getRoadTypeName(), roadFeatures[1].getID()));
        assertFalse(isLocked(getRoadTypeName(), roadFeatures[2].getID()));
    }
View Full Code Here

            e.setBounds(collection.getBounds());
        }

        create(e, collection.getSchema());

        Transaction tx = new DefaultTransaction();
        SimpleFeatureWriter w = writer(e, true, null, tx);
        SimpleFeatureIterator it = collection.features();
        try {
            while(it.hasNext()) {
                SimpleFeature f = it.next();
                SimpleFeature g = w.next();
                for (PropertyDescriptor pd : collection.getSchema().getDescriptors()) {
                    String name = pd.getName().getLocalPart();
                    g.setAttribute(name, f.getAttribute(name));
                }
                                            
                w.write();
            }
            tx.commit();
        }
        catch(Exception ex) {
            tx.rollback();
            throw new IOException(ex);
        }
        finally {
            w.close();
            it.close();
            tx.close();
        }

        entry.init(e);
    }
View Full Code Here

TOP

Related Classes of org.geotools.data.Transaction

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.