<s>[INST] ### No1 PostgreSQL extension

The GeoServer community is migrating fromsql functions to java functions. One of the main reasons to do this is because Java functions are more flexible and can be used to call external libraries like GDAL, JAI or other third-party libraries. However, this approach requires more Java coding skills.

The following example implements the Spatial Reference System Identifier (SRID) of the layers in the table, which is not a part of the layer’s geometry type. It will be set up manually to the srid value in the properties file. Therefore, you can see how to define the SRID using Java functions by implementing the following code:
```
CREATE OR REPLACE FUNCTION public.get_srid INT)
RETURNS INT AS $$
DECLARE 
    srid INT;
BEGIN

    SELECT srid
    INTO srid
    FROM T cadastr.areas_br WHERE 
    id= $1
    LIMIT 1;

    RETURN srid;   
END;
$$ LANGUAGE plpgsql;
```
It replaced by the following Java function implementation:
```
package org.geoserver.config;

import org.geoserver.catalog.Catalog;
import org.geoserver.catalog.LayerInfo;
import org.geoserver.catalog.ResourceInfo;
import org.geoserver.catalog.impl.LayerBuilder;
import org.geoserver.config.util.XStreamPersister;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlRootElement;
import org.apache.commons.codec.binary.StringUtils;

public class Layer pstLayer extends Resource implements Serializable {
    private static final long serialVersionUID = 1 L;

    private String srid;

    public Layer(String name, String workspace, String store) {
        super(name, workspace, store);
    }

    public void setSrid(String srid) {
        this.srid = srid;
    }

    public String getSrid() {
        return this.srid;
    }
}

public class Layer pstLayer extends PSTLayer {
    public Layer pstLayer(String name, String workspace, String store) {
        super(name, workspace, store);    
    }   

    public Integer getSrid(ResourceInfo layerInfo) {
        // the code to get srid using java 
    }   
}
```
In the above code the value of srid is taken from a properties file. This means that it is already available as a string constant somewhere in the GeoServer config file and used in the Layer implementation. Therefore, you can retrieve the srid from it.

However, you have two other methods to get the SRID using the following two functions:

1.  Using the PostgreSQL database directly to get the SRID.

2.  From the layer info.
```
public Integer getSrid(ResourceInfo layerInfo) {
    // method 1 getting the SRID directly from the PostgreSQL Database
    try (Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/geoserver", "geoserver", "geoserver");
         Statement statement = conn.createStatement()) {
        String query = "select srid from T cadastr.areas_br where id=" + layerInfo.getId();
        ResultSet resultSet = statement.executeQuery(query);
        if (resultSet.next()) {
            return resultSet.getInt("srid");
        }
    } catch (SQLException ex) {
        System.out.println("Error:" + ex.getMessage());
    }

    //method 2 get SRID from layer info
    LayerInfo layer = catalog.getLayer(layerInfo.getId());
    AttributeTypeInfo attributeType = layer.getAttributeTypes()[0];
    return attributeType.getBinding().getCoordinateReferenceSystem().getTypeName().hashCode();
}
```
What is wrong with Method 1 in the code?[/INST]