task_24859 #13

Merged
francesco.mangiacrapa merged 14 commits from task_24859 into master 2023-03-30 11:00:17 +02:00
2 changed files with 259 additions and 259 deletions
Showing only changes of commit c23245151f - Show all commits

View File

@ -31,315 +31,314 @@ import lombok.extern.slf4j.Slf4j;
@ToString @ToString
public class PostgisTable { public class PostgisTable {
@Getter
@AllArgsConstructor
public static enum GeometryType {
MULTIPOINT("4326", "geometry (MULTIPOINT,4326)", "", ""), POINT("4326", "geometry (POINT,4326)", "", ""),
LINE("4326", "geometry (MULTILINESTRING,4326)", "", ""),
POLYGON("4326", "geometry (MULTIPOLYGON,4326)", "", "");
@Getter private final String SRID;
@AllArgsConstructor private final String definition;
public static enum GeometryType{ private final String InsertWKT;
MULTIPOINT("4326","geometry (MULTIPOINT,4326)","",""), private final String insertWKB;
POINT("4326","geometry (POINT,4326)","",""),
LINE("4326","geometry (MULTILINESTRING,4326)","",""),
POLYGON("4326","geometry (MULTIPOLYGON,4326)","","");
private final String SRID;
private final String definition;
private final String InsertWKT;
private final String insertWKB;
}
} @RequiredArgsConstructor
@Getter
@Setter
@ToString
public static class Field {
@NonNull
private String name;
@NonNull
private FieldType type;
private Boolean isIndexed;
private Object constantValue;
@RequiredArgsConstructor public static final Field fromMapping(MappingObject m) {
@Getter return new PostgisTable.Field(m.getName(), PostgisTable.FieldType.valueOf(m.getType()));
@Setter }
@ToString
public static class Field{
@NonNull
private String name;
@NonNull
private FieldType type;
private Boolean isIndexed;
private Object constantValue;
public static final Field fromMapping(MappingObject m){ public static final List<Field> fromMappings(Collection<MappingObject> coll) {
return new PostgisTable.Field(m.getName(), PostgisTable.FieldType.valueOf(m.getType())); ArrayList<Field> toReturn = new ArrayList<>();
} if (coll != null)
coll.forEach(m -> toReturn.add(fromMapping(m)));
return toReturn;
}
}
public static final List<Field> fromMappings (Collection<MappingObject> coll){ @Getter
ArrayList<Field> toReturn = new ArrayList<>(); @AllArgsConstructor
if(coll!=null) public enum FieldType {
coll.forEach(m -> toReturn.add(fromMapping(m))); INT("int", java.sql.Types.INTEGER), BOOLEAN("boolean", java.sql.Types.BOOLEAN),
return toReturn; TEXT("text", java.sql.Types.LONGVARCHAR), FLOAT("float", java.sql.Types.FLOAT), GEOMETRY("", 0),
} AUTOINCREMENT("BIGSERIAL PRIMARY KEY", java.sql.Types.BIGINT);
}
@Getter private String definition;
@AllArgsConstructor private int sqlType;
public enum FieldType{ }
INT("int",java.sql.Types.INTEGER),
BOOLEAN("boolean", java.sql.Types.BOOLEAN),
TEXT("text",java.sql.Types.LONGVARCHAR),
FLOAT("float",java.sql.Types.FLOAT),
GEOMETRY("",0),
AUTOINCREMENT("BIGSERIAL PRIMARY KEY",java.sql.Types.BIGINT);
private String definition; @RequiredArgsConstructor
private int sqlType; @Getter
} @ToString
public static class POINT {
private static Pattern pattern = Pattern.compile("(?!=\\d\\.\\d\\.)([\\d.]+)");
@RequiredArgsConstructor public static POINT parsePOINT(String point) throws DataParsingException {
@Getter // POINT(8.30230113965909 44.8011688237011)
@ToString // x,y
public static class POINT{ try {
log.debug("Parsing POINT " + point);
Matcher m = pattern.matcher(point);
private static Pattern pattern = Pattern.compile("(?!=\\d\\.\\d\\.)([\\d.]+)"); if (!m.find())
throw new DataParsingException("Unable to get x ");
Double x = Double.parseDouble(m.group(1));
public static POINT parsePOINT(String point) throws DataParsingException { if (!m.find())
//POINT(8.30230113965909 44.8011688237011) throw new DataParsingException("Unable to get y ");
// x,y Double y = Double.parseDouble(m.group(1));
try {
log.debug("Parsing POINT "+point);
Matcher m=pattern.matcher(point);
if(!m.find()) throw new DataParsingException("Unable to get x "); return new POINT(x, y);
Double x=Double.parseDouble(m.group(1)); } catch (Throwable t) {
throw new DataParsingException("Invalid POINT " + point, t);
}
}
if(!m.find()) throw new DataParsingException("Unable to get y "); @NonNull
Double y=Double.parseDouble(m.group(1)); private Double x;
@NonNull
private Double y;
return new POINT(x,y); }
}catch(Throwable t) {
throw new DataParsingException("Invalid POINT "+point,t);
}
}
@NonNull private static final NumberFormat DECIMAL_FORMAT = NumberFormat.getInstance(Locale.US);
private Double x;
@NonNull
private Double y;
} static {
((DecimalFormat) DECIMAL_FORMAT).setGroupingUsed(false);
}
public String getGeometryColumn() {
for (Field f : fields)
if (f.getType().equals(FieldType.GEOMETRY))
return f.getName();
return null;
}
private static final NumberFormat DECIMAL_FORMAT=NumberFormat.getInstance(Locale.US); @NonNull
private String tablename;
static { @NonNull
((DecimalFormat) DECIMAL_FORMAT).setGroupingUsed(false); private List<Field> fields;
}
@NonNull
private GeometryType geometryColumnType;
public String getGeometryColumn() { @Setter
for(Field f:fields) private BBOX boundingBox = null;
if(f.getType().equals(FieldType.GEOMETRY)) return f.getName();
return null;
}
@NonNull @Setter
private String tablename; private POINT centroid = null;
@NonNull public void setTablename(String tablename) {
private List<Field> fields; this.tablename = sanitizeFieldName(tablename);
}
public String getCreateStatement() {
StringBuilder stmt = new StringBuilder();
stmt.append("CREATE TABLE IF NOT EXISTS " + tablename + "( ");
for (Field field : fields) {
@NonNull String fieldDefinition = field.getType().getDefinition();
private GeometryType geometryColumnType; if (field.getType().equals(FieldType.GEOMETRY))
fieldDefinition = this.getGeometryColumnType().definition;
@Setter stmt.append(field.getName() + " " + fieldDefinition + ",");
private BBOX boundingBox=null; }
stmt.deleteCharAt(stmt.lastIndexOf(","));
stmt.append(")");
return stmt.toString();
}
@Setter public String getDeleteByFieldStatement(Field field) {
private POINT centroid=null; return "DELETE FROM " + tablename + " WHERE " + field.getName() + " = ? ";
}
public void setTablename(String tablename) { public String getInsertionStatement(boolean geometryText) {
this.tablename = sanitizeFieldName(tablename); StringBuilder fieldList = new StringBuilder();
} StringBuilder fieldInsertion = new StringBuilder();
public String getCreateStatement() { for (Field field : fields) {
StringBuilder stmt=new StringBuilder(); switch (field.getType()) {
stmt.append("CREATE TABLE IF NOT EXISTS "+tablename+"( "); case AUTOINCREMENT:
for(Field field:fields){ break;
case GEOMETRY: {
fieldList.append(field.getName() + ",");
if (geometryText)
fieldInsertion.append("ST_GeomFromText(?, 4326),");
else
fieldInsertion.append("ST_GeomFromWKB(?, 4326),");
break;
}
default: {
fieldList.append(field.getName() + ",");
fieldInsertion.append("?,");
}
}
}
String fieldDefinition=field.getType().getDefinition(); fieldList.deleteCharAt(fieldList.lastIndexOf(","));
if(field.getType().equals(FieldType.GEOMETRY)) fieldInsertion.deleteCharAt(fieldInsertion.lastIndexOf(","));
fieldDefinition=this.getGeometryColumnType().definition;
stmt.append(field.getName()+" "+fieldDefinition+","); return "Insert into " + tablename + " (" + fieldList + ") VALUES (" + fieldInsertion + ")";
} }
stmt.deleteCharAt(stmt.lastIndexOf(","));
stmt.append(")");
return stmt.toString();
}
public String getDeleteByFieldStatement(Field field) { public void fillObjectsPreparedStatement(Map<String, Object> row, PreparedStatement toFill) throws SQLException {
return "DELETE FROM "+tablename+" WHERE "+field.getName()+" = ? "; int psFieldIndex = 0;
} HashMap<String, Object> rowValues = new HashMap<String, Object>();
for (Map.Entry<String, Object> entry : row.entrySet())
rowValues.put(sanitizeFieldName(entry.getKey()), entry.getValue());
for (Field field : fields) {
if (!field.getType().equals(FieldType.AUTOINCREMENT)) {
psFieldIndex++;
public String getInsertionStatement(boolean geometryText) { Object value = rowValues.get(field.getName());
StringBuilder fieldList=new StringBuilder(); setObjectInPreparedStatement(field, value, toFill, psFieldIndex);
StringBuilder fieldInsertion=new StringBuilder();
for(Field field:fields) { }
switch(field.getType()) {
case AUTOINCREMENT : break;
case GEOMETRY : {
fieldList.append(field.getName()+",");
if(geometryText)
fieldInsertion.append("ST_GeomFromText(?, 4326),");
else
fieldInsertion.append("ST_GeomFromWKB(?, 4326),");
break;
}
default : {
fieldList.append(field.getName()+",");
fieldInsertion.append("?,");
}
}
}
fieldList.deleteCharAt(fieldList.lastIndexOf(",")); }
fieldInsertion.deleteCharAt(fieldInsertion.lastIndexOf(",")); }
public void setObjectInPreparedStatement(Field field, Object value, PreparedStatement toFill, int psFieldIndex)
throws SQLException {
if (value == null) {
try {
toFill.setNull(psFieldIndex, field.getType().sqlType);
} catch (SQLException e) {
log.error("Unable to set null for field " + field);
throw e;
}
} else {
switch (field.getType()) {
case FLOAT: {
toFill.setFloat(psFieldIndex, (Float) value);
break;
}
case INT: {
toFill.setInt(psFieldIndex, (Integer) value);
break;
}
case TEXT: {
toFill.setString(psFieldIndex, value.toString());
break;
}
case GEOMETRY: {
if (value instanceof String)
toFill.setString(psFieldIndex, ((String) value));
else
toFill.setBytes(psFieldIndex, (byte[]) value);
break;
}
case BOOLEAN: {
if (value instanceof String)
toFill.setBoolean(psFieldIndex, Boolean.parseBoolean(value.toString()));
if (value instanceof Boolean)
toFill.setBoolean(psFieldIndex, (Boolean) value);
break;
}
// Added by Francesco
default: {
if (value instanceof String) {
toFill.setString(psFieldIndex, ((String) value));
} else {
try {
String toStringValue = value.toString();
toFill.setString(psFieldIndex, toStringValue);
} catch (Exception e) {
// silence
}
}
}
}
}
}
return "Insert into "+tablename+" ("+fieldList+") VALUES ("+fieldInsertion+")"; public void fillCSVPreparedStatament(Map<String, String> row, PreparedStatement toFill, boolean explicitGeometry)
} throws SQLException {
int psFieldIndex = 0;
HashMap<String, String> rowValues = new HashMap<String, String>();
for (Map.Entry<String, String> entry : row.entrySet())
rowValues.put(sanitizeFieldName(entry.getKey()), entry.getValue());
public void fillObjectsPreparedStatement(Map<String,Object> row, PreparedStatement toFill) throws SQLException { for (Field field : fields) {
int psFieldIndex=0;
HashMap<String,Object> rowValues=new HashMap<String,Object>();
for(Map.Entry<String,Object> entry:row.entrySet())
rowValues.put(sanitizeFieldName(entry.getKey()), entry.getValue());
if (!field.getType().equals(FieldType.AUTOINCREMENT)) {
psFieldIndex++;
for(Field field:fields) { String value = rowValues.get(field.getName());
if(!field.getType().equals(FieldType.AUTOINCREMENT)) {
psFieldIndex++;
Object value=rowValues.get(field.getName());
setObjectInPreparedStatement(field,value,toFill,psFieldIndex);
}
}
}
public void setObjectInPreparedStatement(Field field,Object value, PreparedStatement toFill, int psFieldIndex) throws SQLException {
if(value==null) {
try{
toFill.setNull(psFieldIndex, field.getType().sqlType);
}catch(SQLException e) {
log.error("Unable to set null for field "+field);
throw e;
}
}
else
switch(field.getType()) {
case FLOAT :{
toFill.setFloat(psFieldIndex, (Float)value);
break;
}
case INT : {
toFill.setInt(psFieldIndex, (Integer)value);
break;
}
case TEXT : {
toFill.setString(psFieldIndex, value.toString());
break;
}
case GEOMETRY : {
if(value instanceof String)
toFill.setString(psFieldIndex, ((String) value));
else toFill.setBytes(psFieldIndex, (byte[])value);
break;
}
case BOOLEAN: {
if(value instanceof String)
toFill.setBoolean(psFieldIndex,Boolean.parseBoolean(value.toString()));
if(value instanceof Boolean)
toFill.setBoolean(psFieldIndex,(Boolean) value);
break;
}
}
}
public void fillCSVPreparedStatament(Map<String,String> row, PreparedStatement toFill,boolean explicitGeometry) throws SQLException {
int psFieldIndex=0;
HashMap<String,String> rowValues=new HashMap<String,String>();
for(Map.Entry<String,String> entry:row.entrySet())
rowValues.put(sanitizeFieldName(entry.getKey()), entry.getValue());
for(Field field:fields) {
if(!field.getType().equals(FieldType.AUTOINCREMENT)) {
psFieldIndex++;
String value=rowValues.get(field.getName());
// if(value==null||value.equalsIgnoreCase("null")) toFill.setNull(psFieldIndex, field.getType().sqlType); // if(value==null||value.equalsIgnoreCase("null")) toFill.setNull(psFieldIndex, field.getType().sqlType);
// else // else
switch(field.getType()) { switch (field.getType()) {
case FLOAT :{ case FLOAT: {
try{ try {
toFill.setFloat(psFieldIndex, Float.parseFloat(value)); toFill.setFloat(psFieldIndex, Float.parseFloat(value));
}catch(NumberFormatException e) { } catch (NumberFormatException e) {
throw new SQLException(field+" cannot be null. CSV Row is "+rowValues,e); throw new SQLException(field + " cannot be null. CSV Row is " + rowValues, e);
} }
break; break;
} }
case INT : { case INT: {
try{ try {
toFill.setInt(psFieldIndex, Integer.parseInt(value)); toFill.setInt(psFieldIndex, Integer.parseInt(value));
}catch(NumberFormatException e) { } catch (NumberFormatException e) {
log.warn("Skipping value for "+field+" row was "+rowValues,e); log.warn("Skipping value for " + field + " row was " + rowValues, e);
toFill.setNull(psFieldIndex, java.sql.Types.INTEGER); toFill.setNull(psFieldIndex, java.sql.Types.INTEGER);
} }
break; break;
} }
case TEXT : { case TEXT: {
toFill.setString(psFieldIndex, value.toString()); toFill.setString(psFieldIndex, value.toString());
break; break;
} }
case GEOMETRY : { case GEOMETRY: {
if(explicitGeometry) { if (explicitGeometry) {
toFill.setString(psFieldIndex,value); toFill.setString(psFieldIndex, value);
}else { } else {
switch(geometryColumnType){ switch (geometryColumnType) {
case POINT: { case POINT: {
String xRepresentation=DECIMAL_FORMAT.format(Double.parseDouble(rowValues.get(DBConstants.Defaults.XCOORD_FIELD))); String xRepresentation = DECIMAL_FORMAT
String yRepresentation=DECIMAL_FORMAT.format(Double.parseDouble(rowValues.get(DBConstants.Defaults.YCOORD_FIELD))); .format(Double.parseDouble(rowValues.get(DBConstants.Defaults.XCOORD_FIELD)));
String yRepresentation = DECIMAL_FORMAT
.format(Double.parseDouble(rowValues.get(DBConstants.Defaults.YCOORD_FIELD)));
toFill.setString(psFieldIndex, "POINT("+xRepresentation+" "+ toFill.setString(psFieldIndex, "POINT(" + xRepresentation + " " + yRepresentation + ")");
yRepresentation+")"); break;
break; }
} default: {
default :{ toFill.setString(psFieldIndex, rowValues.get("wkt"));
toFill.setString(psFieldIndex,rowValues.get("wkt")); break;
break; }
} }
} }
} break;
break; }
} }
} }
}
}
}
public static String sanitizeFieldName(String fieldName) {
}
}
public static String sanitizeFieldName(String fieldName) {
// return fieldName.toLowerCase().replaceAll(" ", "_").replaceAll("\\.", "").replaceAll("-", "_").replaceAll("////","_"); // return fieldName.toLowerCase().replaceAll(" ", "_").replaceAll("\\.", "").replaceAll("-", "_").replaceAll("////","_");
return fieldName.toLowerCase().replaceAll("[^a-z0-9_\\\\]", "_"); return fieldName.toLowerCase().replaceAll("[^a-z0-9_\\\\]", "_");
} }
} }

View File

@ -118,6 +118,7 @@ public class SDIIndexerPlugin extends SDIAbstractPlugin implements IndexerPlugin
centroidDoc.put(DBConstants.Defaults.PROJECT_ID, project.getId()); centroidDoc.put(DBConstants.Defaults.PROJECT_ID, project.getId());
centroidDoc.put(DBConstants.Defaults.DISPLAYED, true); centroidDoc.put(DBConstants.Defaults.DISPLAYED, true);
//Added by Francesco. Creating Gis Viewer Link as public or private
Boolean isInternalIndex = null; Boolean isInternalIndex = null;
try { try {
isInternalIndex = requestArguments.getBoolean("flagInternalIndex"); isInternalIndex = requestArguments.getBoolean("flagInternalIndex");