Example #1
0
    def _publishExisting(self, layer, workspace, overwrite,
                         name, storename=None):
        uri = QgsDataSourceURI(layer.dataProvider().dataSourceUri())

        # check for table.name conflict in existing layer names where the
        # table.name is not the same as the user-chosen layer name,
        # i.e. unintended overwrite
        resource = self.catalog.get_resource(uri.table())
        if resource is not None and uri.table() != name:
            raise Exception("QGIS PostGIS layer has table name conflict with "
                            "existing GeoServer layer name: {0}\n"
                            "You may need to rename GeoServer layer name."
                            .format(uri.table()))

        conname = self.getConnectionNameFromLayer(layer)
        storename = xmlNameFixUp(storename or conname)

        if not xmlNameIsValid(storename):
            raise Exception("Database connection name is invalid XML and can "
                            "not be auto-fixed: {0} -> {1}"
                            .format(conname, storename))

        if not uri.username():
            raise Exception("GeoServer requires database connection's username "
                            "to be defined")

        store = createPGFeatureStore(self.catalog,
                                     storename,
                                     workspace = workspace,
                                     overwrite = overwrite,
                                     host = uri.host(),
                                     database = uri.database(),
                                     schema = uri.schema(),
                                     port = uri.port(),
                                     user = uri.username(),
                                     passwd = uri.password())
        if store is not None:
            rscname = name if uri.table() != name else uri.table()
            grpswlyr = []
            if overwrite:
                # TODO: How do we honor *unchecked* user setting of
                #   "Delete resource when deleting layer" here?
                #   Is it an issue, if overwrite is expected?

                # We will soon have two layers with slightly different names,
                # a temp based upon table.name, the other possibly existing
                # layer with the same custom name, which may belong to group(s).
                # If so, remove existing layer from any layer group, before
                # continuing on with layer delete and renaming of new feature
                # type layer to custom name, then add new resultant layer back
                # to any layer groups the existing layer belonged to. Phew!

                flyr = self.catalog.get_layer(rscname)
                if flyr is not None:
                    grpswlyr = groupsWithLayer(self.catalog, flyr)
                    if grpswlyr:
                        removeLayerFromGroups(self.catalog, flyr, grpswlyr)
                    self.catalog.delete(flyr)
                # TODO: What about when the layer name is the same, but the
                #   underlying db connection/store has changed? Not an issue?
                #   The layer is deleted, which is correct, but the original
                #   db store and feature type will not be changed. A conflict?
                frsc = store.get_resources(name=rscname)
                if frsc is not None:
                    self.catalog.delete(frsc)

            # for dbs the name has to be the table name, initially
            ftype = self.catalog.publish_featuretype(uri.table(), store,
                                                     layer.crs().authid())

            # once table-based feature type created, switch name to user-chosen
            if ftype.name != rscname:
                ftype.dirty["name"] = rscname
            self.catalog.save(ftype)

            # now re-add to any previously assigned-to layer groups
            if overwrite and grpswlyr:
                ftype = self.catalog.get_resource(rscname)
                if ftype:
                    addLayerToGroups(self.catalog, ftype, grpswlyr,
                                     workspace=workspace)
Example #2
0
def publishTable(table, catalog = None, workspace = None, overwrite=True,
                 name=None, storename=None):
    if catalog is None:
        pass

    lyrname = xmlNameFixUp(name)  # usually fixed up by now

    # check for table.name conflict in existing layer names where the table.name
    # is not the same as the user-chosen layer name, i.e. unintended overwrite
    resource = catalog.get_resource(table.name)
    if resource is not None and table.name != lyrname:
        raise Exception("PostGIS table name conflicts with "
                        "existing GeoServer layer name: {0}\n"
                        "You may need to rename GeoServer layer name."
                        .format(table.name))

    workspace = workspace if workspace is not None else catalog.get_default_workspace()
    connection = table.conn
    geodb = connection.geodb
    conname = "{0}_{1}".format(connection.name, table.schema)
    storename = xmlNameFixUp(storename or conname)

    if not xmlNameIsValid(storename):
        raise Exception("Database connection name is invalid XML and can "
                        "not be auto-fixed: {0} -> {1}"
                        .format(conname, storename))

    if not geodb.user:
        raise Exception("GeoServer requires database connection's username "
                        "to be defined")

    store = createPGFeatureStore(catalog,
                         storename,
                         workspace = workspace,
                         overwrite = True,
                         host = geodb.host,
                         database = geodb.dbname,
                         schema = table.schema,
                         port = geodb.port,
                         user = geodb.user,
                         passwd = geodb.passwd)
    if store is not None:
        rscname = name if table.name != name else table.name
        grpswlyr = []
        if overwrite:
            # See notes about possible issues in OGCatalog._publishExisting()
            flyr = catalog.get_layer(rscname)
            if flyr is not None:
                grpswlyr = groupsWithLayer(catalog, flyr)
                if grpswlyr:
                    removeLayerFromGroups(catalog, flyr, grpswlyr)
                catalog.delete(flyr)

            frsc = store.get_resources(name=rscname)
            if frsc is not None:
                catalog.delete(frsc)

        epsg = table.srid if int(table.srid) != 0 else 4326
        ftype = catalog.publish_featuretype(table.name, store,
                                            "EPSG:" + str(epsg))
        # once table-based layer created, switch name to user-chosen
        if table.name != rscname:
            ftype.dirty["name"] = rscname
            ftype.dirty["title"] = rscname
        catalog.save(ftype)

        if overwrite and grpswlyr:
            ftype = catalog.get_resource(rscname)
            if ftype:
                addLayerToGroups(catalog, ftype, grpswlyr, workspace=workspace)