Skip to content

Commit

Permalink
Merge pull request #1006 from DenverCoder544/myplaces_use_database_di…
Browse files Browse the repository at this point in the history
…rectly

insert directly into database bypassing wfs-t in myplaces
  • Loading branch information
ZakarFin authored Oct 2, 2023
2 parents d3dcf37 + 13ffb25 commit eaaed62
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
package fi.nls.oskari.control.myplaces.handler;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

import fi.nls.oskari.domain.map.MyPlaceCategory;
import org.oskari.log.AuditLog;
import org.json.JSONException;
import org.json.JSONObject;

import fi.nls.oskari.annotation.OskariActionRoute;
import fi.nls.oskari.control.ActionDeniedException;
import fi.nls.oskari.control.ActionException;
Expand All @@ -18,17 +8,27 @@
import fi.nls.oskari.control.RestActionHandler;
import fi.nls.oskari.domain.User;
import fi.nls.oskari.domain.map.MyPlace;
import fi.nls.oskari.domain.map.MyPlaceCategory;
import fi.nls.oskari.log.LogFactory;
import fi.nls.oskari.log.Logger;
import fi.nls.oskari.myplaces.MyPlacesService;
import fi.nls.oskari.myplaces.service.MyPlacesFeaturesService;
import fi.nls.oskari.myplaces.service.MyPlacesFeaturesServiceMybatisImpl;
import fi.nls.oskari.myplaces.service.wfst.MyPlacesFeaturesServiceWFST;
import fi.nls.oskari.myplaces.service.wfst.MyPlacesFeaturesWFSTRequestBuilder;
import fi.nls.oskari.service.OskariComponentManager;
import fi.nls.oskari.service.ServiceException;
import fi.nls.oskari.util.IOHelper;
import fi.nls.oskari.util.JSONHelper;
import fi.nls.oskari.util.ResponseHelper;
import org.json.JSONException;
import org.json.JSONObject;
import org.oskari.log.AuditLog;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

@OskariActionRoute("MyPlacesFeatures")
public class MyPlacesFeaturesHandler extends RestActionHandler {
Expand All @@ -43,11 +43,13 @@ public class MyPlacesFeaturesHandler extends RestActionHandler {
private MyPlacesService service;
private MyPlacesFeaturesService featureService;

private MyPlacesFeaturesService mybatisFeatureService;
@Override
public void init() {
super.init();
service = OskariComponentManager.getComponentOfType(MyPlacesService.class);
featureService = new MyPlacesFeaturesServiceWFST();
mybatisFeatureService = new MyPlacesFeaturesServiceMybatisImpl();
}

@Override
Expand Down Expand Up @@ -102,7 +104,7 @@ public void handlePost(ActionParameters params) throws ActionException {

long[] ids;
try {
ids = featureService.insert(places);
ids = mybatisFeatureService.insert(places);
LOG.info("Inserted MyPlaces:", ids);
} catch (ServiceException e) {
LOG.warn(e);
Expand Down Expand Up @@ -200,6 +202,7 @@ private List<MyPlace> readMyPlaces(ActionParameters params, boolean checkId)
} catch (IOException e) {
throw new ActionException("IOException occured");
}
// TODO: move parseMyPlaces outside MyPlacesFeaturesWFSTRequestBuilder
return MyPlacesFeaturesWFSTRequestBuilder.parseMyPlaces(payload, checkId);
} catch (JSONException e) {
throw new ActionParamsException("Invalid input", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class MyPlace {
private String imageUrl;
private String attentionText;
private Geometry geometry;

public long getId() {
return id;
}
Expand Down Expand Up @@ -86,6 +85,14 @@ public void setGeometry(Geometry geometry) {
this.geometry = geometry;
}

public String getGeomAsText() {
return geometry.toText();
}

public int getSrid() {
return this.geometry.getSRID();
}

public boolean isOwnedBy(final String uuid) {
if(uuid == null || getUuid() == null) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import fi.nls.oskari.domain.map.MyPlace;
import fi.nls.oskari.domain.map.MyPlaceCategory;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

Expand All @@ -24,4 +26,30 @@ public interface MyPlaceMapper {
List<MyPlaceCategory> findAll();
@Delete("delete from categories where uuid = #{uid}")
void deleteByUid(String uid);

@Insert("INSERT INTO my_places (" +
" uuid, " +
" category_id, " +
" name, " +
" attention_text, " +
" updated, " +
" geometry, " +
" place_desc, " +
" link, " +
" image_url " +
") " +
" VALUES (" +
" #{uuid}, " +
" #{categoryId}, " +
" #{name}, " +
" #{attentionText}, " +
" now(), " +
" ST_SetSRID(ST_GeometryFromText(#{geomAsText}), #{srid}), " +
" #{desc}, " +
" #{link}, " +
" #{imageUrl} " +
")")
@Options(useGeneratedKeys=true, keyColumn="id", keyProperty="id")
Long addMyPlace(MyPlace myPlace);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package fi.nls.oskari.myplaces.service;

import fi.nls.oskari.control.ActionConstants;
import fi.nls.oskari.control.ActionParamsException;
import fi.nls.oskari.db.DatasourceHelper;
import fi.nls.oskari.domain.map.MyPlace;
import fi.nls.oskari.domain.map.MyPlaceCategory;
import fi.nls.oskari.log.LogFactory;
import fi.nls.oskari.log.Logger;
import fi.nls.oskari.mybatis.JSONObjectMybatisTypeHandler;
import fi.nls.oskari.myplaces.MyPlaceMapper;
import fi.nls.oskari.service.ServiceException;
import fi.nls.oskari.util.PropertyUtil;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.geotools.geometry.jts.JTS;
import org.geotools.referencing.CRS;
import org.json.JSONObject;
import org.locationtech.jts.geom.Geometry;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;

import javax.sql.DataSource;
import java.util.List;

public class MyPlacesFeaturesServiceMybatisImpl implements MyPlacesFeaturesService {
private static final Logger LOG = LogFactory.getLogger(
MyPlacesFeaturesServiceMybatisImpl.class);

private SqlSessionFactory factory = null;

public MyPlacesFeaturesServiceMybatisImpl() {
final DatasourceHelper helper = DatasourceHelper.getInstance();
final DataSource dataSource = helper.getDataSource(helper.getOskariDataSourceName("myplaces"));
if(dataSource != null) {
factory = initializeMyBatis(dataSource);
}
else {
LOG.error("Couldn't get datasource for myplaces");
}
}

private SqlSessionFactory initializeMyBatis(final DataSource dataSource) {
final TransactionFactory transactionFactory = new JdbcTransactionFactory();
final Environment environment = new Environment("development", transactionFactory, dataSource);

final Configuration configuration = new Configuration(environment);
configuration.getTypeAliasRegistry().registerAlias(MyPlaceCategory.class);
configuration.getTypeAliasRegistry().registerAlias(MyPlace.class);
configuration.getTypeHandlerRegistry().register(JSONObjectMybatisTypeHandler.class);
configuration.setLazyLoadingEnabled(true);
configuration.addMapper(MyPlaceMapper.class);

return new SqlSessionFactoryBuilder().build(configuration);
}

@Override
public JSONObject getFeaturesByCategoryId(long categoryId, String crs) throws ServiceException {
return null;
}

@Override
public JSONObject getFeaturesByUserId(String uuid, String crs) throws ServiceException {
return null;
}

@Override
public JSONObject getFeaturesByMyPlaceId(long[] ids, String crs) throws ServiceException {
return null;
}

@Override
public long[] insert(List<MyPlace> places) throws ServiceException {
try (SqlSession session = factory.openSession()) {
LOG.debug("Adding new places: ", places);
final MyPlaceMapper mapper = session.getMapper(MyPlaceMapper.class);
for (MyPlace place : places) {
Geometry transformed = this.doGeometryTransform(place.getGeometry());
place.setGeometry(transformed);

mapper.addMyPlace(place);
LOG.info("inserted myplace: ", place.getId());
}
session.commit();
} catch (Exception e) {
LOG.warn(e, "Exception when trying to add MyPlaces: ");
}

long[] ids = places.stream().mapToLong(place -> place.getId()).toArray();
return ids;
}

private Geometry doGeometryTransform(Geometry geometry) throws ServiceException {
try {
String targetSRSName = PropertyUtil.get("oskari.native.srs", "EPSG:3857");
String sourceSRSName = "EPSG:" + geometry.getSRID();
CoordinateReferenceSystem targetCRS, sourceCRS;
MathTransform transform;

try {
targetCRS = CRS.decode(targetSRSName);
sourceCRS = CRS.decode(sourceSRSName);
transform = CRS.findMathTransform(sourceCRS, targetCRS);
} catch (Exception e) {
throw new ActionParamsException("Invalid " + ActionConstants.PARAM_SRS);
}
Geometry transformed = JTS.transform(geometry, transform);
transformed.setSRID(getSRID(targetSRSName));
return transformed;

} catch(Exception e) {
LOG.warn(e, "Exception transforming geometry");
}
return null;
}

private int getSRID(String srsName) {
String srid = srsName.substring(srsName.indexOf(':') + 1);
return Integer.parseInt(srid);
}

@Override
public int update(List<MyPlace> places) throws ServiceException {
return 0;
}

@Override
public int delete(long[] ids) throws ServiceException {
return 0;
}
}

0 comments on commit eaaed62

Please sign in to comment.