Package org.geoserver.wfs.request

Examples of org.geoserver.wfs.request.Insert


        TransactionRequest t = TransactionRequest.adapt(
            OwsUtils.parameter(operation.getParameters(), EObject.class));
        if (t != null) {
            for (TransactionElement el : t.getElements()) {
                if (el instanceof Insert) {
                    Insert in = (Insert) el;
                    //in the insert case the objects are gt feature types which are not mutable
                    // so we just check them and throw an exception if a name does not match
                    for (Iterator j = in.getFeatures().iterator(); j.hasNext(); ) {
                        Feature f = (Feature) j.next();
                        Name n = f.getType().getName();
                        if (n.getNamespaceURI() != null && !ns.getURI().equals(n.getNamespaceURI())) {
                            throw new WFSException(t, "No such feature type " + n);
                        }
View Full Code Here


    @SuppressWarnings("unchecked")
    public void execute(TransactionElement element, TransactionRequest request, Map featureStores,
        TransactionResponse response, TransactionListener listener) throws WFSTransactionException {
       
        Insert insert = (Insert) element;
        LOGGER.finer("Transasction Insert:" + insert);

        long inserted = response.getTotalInserted().longValue();

        try {
            // group features by their schema
            HashMap /* <SimpleFeatureType,FeatureCollection> */ schema2features = new HashMap();

           
            List featureList = insert.getFeatures();
            for (Iterator f = featureList.iterator(); f.hasNext();) {
                SimpleFeature feature = (SimpleFeature) f.next();
                SimpleFeatureType schema = feature.getFeatureType();
                DefaultFeatureCollection collection =
                    (DefaultFeatureCollection) schema2features.get(schema);

                if (collection == null) {
                    collection = new DefaultFeatureCollection(null, schema);
                    schema2features.put(schema, collection);
                }

                // do a check for idegen = useExisting, if set try to tell the datastore to use
                // the privided fid
                if (insert.isIdGenUseExisting()) {
                    feature.getUserData().put(Hints.USE_PROVIDED_FID, true);
                }

                collection.add(feature);
            }

            // JD: change from set fo list because if inserting
            // features into different feature stores, they could very well
            // get given the same id
            // JD: change from list to map so that the map can later be
            // processed and we can report the fids back in the same order
            // as they were supplied
            Map<String, List<FeatureId>> schema2fids = new HashMap<String, List<FeatureId>>();

            for (Iterator c = schema2features.values().iterator(); c.hasNext();) {
                SimpleFeatureCollection collection = (SimpleFeatureCollection) c.next();
                SimpleFeatureType schema = collection.getSchema();

                final QName elementName = new QName(schema.getName().getNamespaceURI(), schema.getTypeName());
                SimpleFeatureStore store;
                store = DataUtilities.simple((FeatureStore) featureStores.get(elementName));

                if (store == null) {
                    throw new WFSException(request, "Could not locate FeatureStore for '" + elementName
                        + "'");
                }

                if (collection != null) {
                    // if we really need to, make sure we are inserting coordinates that do
                    // match the CRS area of validity
                    if(getInfo().isCiteCompliant()) {
                        checkFeatureCoordinatesRange(collection);
                    }
                   
                    // reprojection
                    final GeometryDescriptor defaultGeometry = store.getSchema().getGeometryDescriptor();
                    if(defaultGeometry != null) {
                        CoordinateReferenceSystem target = defaultGeometry.getCoordinateReferenceSystem();
                        if (target != null) {
                            collection = new ReprojectingFeatureCollection(collection, target);
                        }
                    }
                   
                    // Need to use the namespace here for the
                    // lookup, due to our weird
                    // prefixed internal typenames. see
                    // http://jira.codehaus.org/secure/ViewIssue.jspa?key=GEOS-143

                    // Once we get our datastores making features
                    // with the correct namespaces
                    // we can do something like this:
                    // FeatureTypeInfo typeInfo =
                    // catalog.getFeatureTypeInfo(schema.getTypeName(),
                    // schema.getNamespace());
                    // until then (when geos-144 is resolved) we're
                    // stuck with:
                    // QName qName = (QName) typeNames.get( i );
                    // FeatureTypeInfo typeInfo =
                    // catalog.featureType( qName.getPrefix(),
                    // qName.getLocalPart() );

                    // this is possible with the insert hack above.
                    LOGGER.finer("Use featureValidation to check contents of insert");

                    // featureValidation(
                    // typeInfo.getDataStore().getId(), schema,
                    // collection );
                    List<FeatureId> fids = schema2fids.get(schema.getTypeName());

                    if (fids == null) {
                        fids = new LinkedList<FeatureId>();
                        schema2fids.put(schema.getTypeName(), fids);
                    }

                    //fire pre insert event
                    TransactionEvent event = new TransactionEvent(TransactionEventType.PRE_INSERT,
                            request, elementName, collection);
                    event.setSource(Insert.WFS11.unadapt(insert));
                   
                    listener.dataStoreChange( event );
                    fids.addAll(store.addFeatures(collection));
                   
                    //fire post insert event
                    SimpleFeatureCollection features = store.getFeatures(filterFactory.id(new HashSet<FeatureId>(fids)));
                    event = new TransactionEvent(TransactionEventType.POST_INSERT, request,
                        elementName, features, Insert.WFS11.unadapt(insert));
                    listener.dataStoreChange( event );
                }
            }

            // report back fids, we need to keep the same order the
            // fids were reported in the original feature collection
            for (Iterator f = featureList.iterator(); f.hasNext();) {
                SimpleFeature feature = (SimpleFeature) f.next();
                SimpleFeatureType schema = feature.getFeatureType();

                // get the next fid
                LinkedList<FeatureId> fids = (LinkedList<FeatureId>) schema2fids.get(schema.getTypeName());
                FeatureId fid = fids.removeFirst();

                response.addInsertedFeature(insert.getHandle(), fid);
            }

            // update the insert counter
            inserted += featureList.size();
        } catch (Exception e) {
            String msg = "Error performing insert: " + e.getMessage();
            throw new WFSTransactionException(msg, e, insert.getHandle());
        }

        // update transaction summary
        response.setTotalInserted(BigInteger.valueOf(inserted));
    }
View Full Code Here

    public Class getElementClass() {
        return Insert.class;
    }

    public QName[] getTypeNames(TransactionElement element) throws WFSTransactionException {
        Insert insert = (Insert) element;
       
        List typeNames = new ArrayList();

        List features = insert.getFeatures();
        if (!features.isEmpty()) {
            for (Iterator f = features.iterator(); f.hasNext();) {
                SimpleFeature feature = (SimpleFeature) f.next();

                String name = feature.getFeatureType().getTypeName();
View Full Code Here

TOP

Related Classes of org.geoserver.wfs.request.Insert

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.