package fi.nls.oskari.control;

import fi.nls.oskari.annotation.OskariActionRoute;
import fi.nls.oskari.util.ResponseHelper;
import org.postgis.PGgeometry;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

@OskariActionRoute("CalculateRoute")
public class CalculateRouteHandler extends ActionHandler {
    @Override
    public void handleAction(ActionParameters params) throws ActionException {
        try {
            Class.forName("org.postgresql.Driver");
            String url = "jdbc:postgresql://localhost:5432/oskaridb";
            java.sql.Connection conn = DriverManager.getConnection(url, "oskari", "W3jept2MqqZRX4J");

    /*
    * Add the geometry types to the connection. Note that you
    * must cast the connection to the pgsql-specific connection
    * implementation before calling the addDataType() method.
    */
            ((org.postgresql.PGConnection) conn).addDataType("geometry", Class.forName("org.postgis.PGgeometry"));
            ((org.postgresql.PGConnection) conn).addDataType("box3d", Class.forName("org.postgis.PGbox3d"));
    /*
    * Create a statement and execute a select query.
    */
            Statement s = conn.createStatement();
            ResultSet r = s.executeQuery("select geom from tieviiva where gid = 10");
            while (r.next()) {
      /*
      * Retrieve the geometry as an object then cast it to the geometry type.
      * Print things out.
      */
                PGgeometry geom = (PGgeometry) r.getObject(1);
                System.out.println(geom.toString());
            }
            s.close();
            conn.close();
            ResponseHelper.writeResponse(params, "Hello " + params.getUser().getFirstname());
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
}

@@ -133,210 +133,1500 @@
 org.
-postgis.PGgeometry;%0A%0Aimport java.sql.DriverManager;%0Aimport java.sql.ResultSet;%0Aimport java.sql.Statement;%0A%0A@OskariActionRoute(%22CalculateRoute%22)%0Apublic class CalculateRouteHandler extends ActionHandler %7B
+codehaus.jackson.map.ObjectMapper;%0Aimport org.postgis.LineString;%0Aimport org.postgis.MultiLineString;%0Aimport org.postgis.PGgeometry;%0Aimport org.postgis.Point;%0A%0Aimport java.sql.DriverManager;%0Aimport java.sql.ResultSet;%0Aimport java.sql.Statement;%0Aimport java.util.ArrayList;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;%0A%0A@OskariActionRoute(%22CalculateRoute%22)%0Apublic class CalculateRouteHandler extends ActionHandler %7B%0A    private Map%3CString, Object%3E toGeoJSON(PGgeometry geometry) %7B%0A        Map%3CString, Object%3E ret = new HashMap%3CString, Object%3E();%0A        MultiLineString multiLineString = (MultiLineString)geometry.getGeometry();%0A        List%3CList%3CList%3CDouble%3E%3E%3E coordinates = new ArrayList%3CList%3CList%3CDouble%3E%3E%3E();%0A        for(int i = 0; i %3C multiLineString.getLines().length; ++i) %7B%0A            LineString lineString = multiLineString.getLine(i);%0A            List%3CList%3CDouble%3E%3E lineStringCoordinates = new ArrayList%3CList%3CDouble%3E%3E();%0A            for(int j = 0; j %3C lineString.numPoints(); ++j) %7B%0A                Point point = lineString.getPoint(j);%0A                List%3CDouble%3E pointCoordinates = new ArrayList%3CDouble%3E();%0A                pointCoordinates.add(point.x);%0A                pointCoordinates.add(point.y);%0A                lineStringCoordinates.add(pointCoordinates);%0A            %7D%0A            coordinates.add(lineStringCoordinates);%0A        %7D%0A        ret.put(%22type%22, %22MultiLineString%22);%0A        ret.put(%22coordinates%22, coordinates);%0A        return ret;%0A    %7D%0A
 %0A   
@@ -1952,16 +1952,70 @@
 ZRX4J%22);
+%0A            ObjectMapper mapper = new ObjectMapper();
 %0A%0A    /*
@@ -2633,24 +2633,64 @@
 gid = 10%22);%0A
+            PGgeometry geometry = null;%0A
             
@@ -2838,34 +2838,32 @@
                 
-PG
 geometry geom = 
@@ -2855,21 +2855,16 @@
 eometry 
-geom 
 = (PGgeo
@@ -2925,16 +2925,20 @@
 tln(geom
+etry
 .toStrin
@@ -3059,49 +3059,53 @@
 ms, 
-%22Hello %22 + params.getUser().getFirstname(
+mapper.writeValueAsString(toGeoJSON(geometry)
 ));%0A

Return multi line string as GeoJSON to front end