<commit_before>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();
        }
    }
}<commit_msg>Return multi line string as GeoJSON to front end<commit_after>package fi.nls.oskari.control;

import fi.nls.oskari.annotation.OskariActionRoute;
import fi.nls.oskari.util.ResponseHelper;
import org.codehaus.jackson.map.ObjectMapper;
import org.postgis.LineString;
import org.postgis.MultiLineString;
import org.postgis.PGgeometry;
import org.postgis.Point;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@OskariActionRoute("CalculateRoute")
public class CalculateRouteHandler extends ActionHandler {
    private Map<String, Object> toGeoJSON(PGgeometry geometry) {
        Map<String, Object> ret = new HashMap<String, Object>();
        MultiLineString multiLineString = (MultiLineString)geometry.getGeometry();
        List<List<List<Double>>> coordinates = new ArrayList<List<List<Double>>>();
        for(int i = 0; i < multiLineString.getLines().length; ++i) {
            LineString lineString = multiLineString.getLine(i);
            List<List<Double>> lineStringCoordinates = new ArrayList<List<Double>>();
            for(int j = 0; j < lineString.numPoints(); ++j) {
                Point point = lineString.getPoint(j);
                List<Double> pointCoordinates = new ArrayList<Double>();
                pointCoordinates.add(point.x);
                pointCoordinates.add(point.y);
                lineStringCoordinates.add(pointCoordinates);
            }
            coordinates.add(lineStringCoordinates);
        }
        ret.put("type", "MultiLineString");
        ret.put("coordinates", coordinates);
        return ret;
    }

    @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");
            ObjectMapper mapper = new ObjectMapper();

    /*
    * 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");
            PGgeometry geometry = null;
            while (r.next()) {
      /*
      * Retrieve the geometry as an object then cast it to the geometry type.
      * Print things out.
      */
                geometry = (PGgeometry) r.getObject(1);
                System.out.println(geometry.toString());
            }
            s.close();
            conn.close();
            ResponseHelper.writeResponse(params, mapper.writeValueAsString(toGeoJSON(geometry)));
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
}
{'fraction_non_alphanumeric': 0.11167711598746081, 'fraction_numerical': 0.005094043887147335, 'mean_word_length': 2.7073347857661583, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1893653', 'n_tokens_mistral': 1437, 'n_tokens_neox': 1410, 'n_words': 319}