@@ -0,0 +1,3286 @@
+package flyway.userlayer;%0A%0Aimport fi.nls.oskari.log.LogFactory;%0Aimport fi.nls.oskari.log.Logger;%0Aimport fi.nls.oskari.util.JSONHelper;%0Aimport org.flywaydb.core.api.migration.jdbc.JdbcMigration;%0Aimport org.json.JSONArray;%0Aimport org.json.JSONException;%0Aimport org.json.JSONObject;%0A%0Aimport java.sql.Connection;%0Aimport java.sql.PreparedStatement;%0Aimport java.sql.ResultSet;%0Aimport java.sql.SQLException;%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0A%0A/**%0A * Add name and type to fields%0A * %5B%7B%22the_geom%22,%22MultiPolygon%22%7D,..%5D -%3E %5B%7B%22name%22: %22the_geom%22, %22type%22:%22MultiPolygon%22%7D,..%5D%0A * %0A * Split java class name to simpleName format %0A * java.lang.Long -%3E Long%0A */%0Apublic class __add_name_and_type_to_fields implements JdbcMigration %7B%0A    private static final Logger LOG = LogFactory.getLogger(__add_name_and_type_to_fields.class);%0A%0A    public void migrate(Connection connection) throws Exception %7B%0A        Map%3CLong, String%3E resultMap = getFields (connection);%0A        for (Map.Entry%3CLong, String%3E entry: resultMap.entrySet())%7B%0A            Long id = entry.getKey();%0A            JSONArray json = JSONHelper.createJSONArray(entry.getValue()); %0A            JSONArray updatedJson = addNameAndType(json);%0A            if (updatedJson != null)%7B%0A                updateFields (connection, id, updatedJson);%0A                LOG.debug(%22Userlayer id:%22,id,%22fields:%22,json.toString(),%22migrated to:%22, updatedJson.toString());%0A            %7Delse%7B%0A                LOG.error(%22Error on updating userlayer fields. Skipping id:%22, id);%0A            %7D           %0A        %7D%0A    %7D%0A%0A    private Map%3CLong, String%3E getFields (Connection conn) throws SQLException %7B%0A        final String sql = %22SELECT id, fields::text FROM user_layer%22;%0A        Map%3CLong, String%3E result = new  HashMap%3CLong, String%3E();%0A        try (final PreparedStatement statement = conn.prepareStatement(sql)) %7B%0A            try (ResultSet rs = statement.executeQuery()) %7B%0A                while(rs.next()) %7B%0A                    result.put(rs.getLong(%22id%22), rs.getString(%22fields%22));%0A                %7D%0A            %7D%0A        %7D%0A        return result;%0A    %7D%0A%0A    private void updateFields (Connection conn, Long id, JSONArray json) throws SQLException %7B%0A        final String sql = %22UPDATE user_layer SET %22 +%0A                %22fields=?::json %22 +%0A                %22WHERE id=?%22;%0A%0A        try (final PreparedStatement statement = conn.prepareStatement(sql)) %7B%0A            statement.setString(1, json.toString());%0A            statement.setLong(2, id);%0A            statement.execute();%0A        %7D%0A%0A    %7D%0A    private JSONArray addNameAndType (JSONArray json)%7B%0A        JSONArray jsonArr = new JSONArray();        %0A        try%7B%0A            for (int i=0; i%3Cjson.length(); i++)%7B%0A                JSONObject obj = new JSONObject();%0A                String key = jsonArr.getJSONObject(i).keys().next().toString();%0A                String type =jsonArr.getJSONObject(i).get(key).toString();%0A                // simpleName%0A                String simpleName = type.substring(type.lastIndexOf('.')+1);%0A                obj.put(%22name%22, key);%0A                obj.put(%22type%22, simpleName);%0A                jsonArr.put(obj);%0A            %7D%0A        %7Dcatch (JSONException e)%7B%0A            //LOG.error(%22Error on converting json%22, e);%0A            return null;%0A        %7D%0A        return jsonArr;%0A    %7D%0A%0A%7D%0A

add name and type to user_layer fields