use of net.geoprism.registry.io.InvalidGeometryException in project geoprism-registry by terraframe.
the class GeoObjectImporter method validateRow.
@Transaction
public void validateRow(FeatureRow row) throws InterruptedException {
try {
// Refresh the session because it might expire on long imports
final long curWorkProgress = this.progressListener.getWorkProgress();
if ((this.lastValidateSessionRefresh + GeoObjectImporter.refreshSessionRecordCount) < curWorkProgress) {
SessionFacade.renewSession(Session.getCurrentSession().getOid());
this.lastValidateSessionRefresh = curWorkProgress;
}
try {
/*
* 1. Check for location problems
*/
if (this.configuration.isPostalCode() && PostalCodeFactory.isAvailable(this.configuration.getType())) {
// Skip location synonym check
} else if (this.configuration.getHierarchy() != null && this.configuration.getLocations().size() > 0) {
this.getParent(row);
}
/*
* 2. Check for serialization and term problems
*/
String code = this.getCode(row);
ServerGeoObjectIF entity;
if (code == null || code.length() <= 0) {
RequiredMappingException ex = new RequiredMappingException();
ex.setAttributeLabel(GeoObjectTypeMetadata.getAttributeDisplayLabel(DefaultAttribute.CODE.getName()));
throw ex;
}
entity = service.newInstance(this.configuration.getType());
entity.setCode(code);
entity.setInvalid(false);
try {
LocalizedValue entityName = this.getName(row);
if (entityName != null && this.hasValue(entityName)) {
entity.setDisplayLabel(entityName, this.configuration.getStartDate(), this.configuration.getEndDate());
}
Geometry geometry = (Geometry) this.getFormatSpecificImporter().getGeometry(row);
if (geometry != null) {
// geometry.getSRID().
if (geometry.isValid()) {
entity.setGeometry(geometry, this.configuration.getStartDate(), this.configuration.getEndDate());
} else {
InvalidGeometryException geomEx = new InvalidGeometryException();
throw geomEx;
}
}
Map<String, AttributeType> attributes = this.configuration.getType().getAttributeMap();
Set<Entry<String, AttributeType>> entries = attributes.entrySet();
for (Entry<String, AttributeType> entry : entries) {
String attributeName = entry.getKey();
if (!attributeName.equals(GeoObject.CODE)) {
ShapefileFunction function = this.configuration.getFunction(attributeName);
if (function != null) {
Object value = function.getValue(row);
if (value != null && !this.isEmptyString(value)) {
AttributeType attributeType = entry.getValue();
this.setValue(entity, attributeType, attributeName, value);
}
}
}
}
GeoObjectOverTime go = entity.toGeoObjectOverTime(false);
go.toJSON().toString();
if (this.configuration.isExternalImport()) {
ShapefileFunction function = this.configuration.getExternalIdFunction();
Object value = function.getValue(row);
if (value == null || !(value instanceof String || value instanceof Integer || value instanceof Long) || (value instanceof String && ((String) value).length() == 0)) {
throw new InvalidExternalIdException();
}
}
} finally {
entity.unlock();
}
} catch (IgnoreRowException e) {
// Do nothing
} catch (Throwable t) {
RowValidationProblem problem = new RowValidationProblem(t);
problem.addAffectedRowNumber(curWorkProgress + 1);
problem.setHistoryId(this.configuration.historyId);
this.progressListener.addRowValidationProblem(problem);
}
this.progressListener.setWorkProgress(curWorkProgress + 1);
if (Thread.interrupted()) {
throw new InterruptedException();
}
Thread.yield();
} catch (Throwable e) {
e.printStackTrace();
}
}
use of net.geoprism.registry.io.InvalidGeometryException in project geoprism-registry by terraframe.
the class RevealExcelContentHandler method getGeometry.
public Geometry getGeometry() {
if (!this.hasGeometry) {
return null;
}
try {
String sCoordinates = this.geometry.toString();
if (sCoordinates.endsWith(",")) {
sCoordinates = sCoordinates.substring(0, sCoordinates.length() - 1);
}
// remove newlines and spaces
sCoordinates = sCoordinates.replace("\n", "").replace("\r", "").replaceAll("\\s+", "");
JsonArray joCoordinates = JsonParser.parseString(sCoordinates).getAsJsonArray();
// TODO : Not sure if we want to keep this polygon -> multipolygon conversion code
if (this.geometryType.toUpperCase().equals("POLYGON")) {
this.geometryType = "MultiPolygon";
JsonArray joCoordinates2 = new JsonArray();
joCoordinates2.add(joCoordinates);
joCoordinates = joCoordinates2;
}
JsonObject joGeometry = new JsonObject();
{
joGeometry.add("coordinates", joCoordinates);
joGeometry.addProperty("type", this.geometryType);
}
GeoJSONReader reader = new GeoJSONReader();
Geometry jtsGeom = reader.read(joGeometry.toString());
return jtsGeom;
} catch (Throwable t) {
InvalidGeometryException geomEx = new InvalidGeometryException(t);
geomEx.setReason(RunwayException.localizeThrowable(t, Session.getCurrentLocale()));
throw geomEx;
}
}
use of net.geoprism.registry.io.InvalidGeometryException in project geoprism-registry by terraframe.
the class GeometryTester method getGeometry.
@Request
public static Geometry getGeometry(String input, String geometryType) {
try {
String sCoordinates = input;
if (sCoordinates.endsWith(",")) {
sCoordinates = sCoordinates.substring(0, sCoordinates.length() - 1);
}
// remove newlines and spaces
sCoordinates = sCoordinates.replace("\n", "").replace("\r", "").replaceAll("\\s+", "");
JsonArray joCoordinates = JsonParser.parseString(sCoordinates).getAsJsonArray();
// TODO : Not sure if we want to keep this polygon -> multipolygon conversion code
if (geometryType.toUpperCase().equals("POLYGON")) {
geometryType = "MultiPolygon";
JsonArray joCoordinates2 = new JsonArray();
joCoordinates2.add(joCoordinates);
joCoordinates = joCoordinates2;
}
JsonObject joGeometry = new JsonObject();
{
joGeometry.add("coordinates", joCoordinates);
joGeometry.addProperty("type", geometryType);
}
GeoJSONReader reader = new GeoJSONReader();
Geometry jtsGeom = reader.read(joGeometry.toString());
return jtsGeom;
} catch (Throwable t) {
InvalidGeometryException geomEx = new InvalidGeometryException(t);
geomEx.setReason(RunwayException.localizeThrowable(t, Session.getCurrentLocale()));
throw geomEx;
}
}
use of net.geoprism.registry.io.InvalidGeometryException in project geoprism-registry by terraframe.
the class GeoObjectImporter method importRowInTrans.
@Transaction
public void importRowInTrans(FeatureRow row, RowData data) {
// Refresh the session because it might expire on long imports
final long curWorkProgress = this.progressListener.getWorkProgress();
if ((this.lastImportSessionRefresh + GeoObjectImporter.refreshSessionRecordCount) < curWorkProgress) {
SessionFacade.renewSession(Session.getCurrentSession().getOid());
this.lastImportSessionRefresh = curWorkProgress;
}
GeoObjectOverTime go = null;
String goJson = null;
ServerGeoObjectIF serverGo = null;
ServerGeoObjectIF parent = null;
boolean isNew = false;
GeoObjectParentErrorBuilder parentBuilder = new GeoObjectParentErrorBuilder();
try {
String code = this.getCode(row);
if (code == null || code.length() <= 0) {
RequiredMappingException ex = new RequiredMappingException();
ex.setAttributeLabel(GeoObjectTypeMetadata.getAttributeDisplayLabel(DefaultAttribute.CODE.getName()));
throw ex;
}
if (this.configuration.getImportStrategy().equals(ImportStrategy.UPDATE_ONLY) || this.configuration.getImportStrategy().equals(ImportStrategy.NEW_AND_UPDATE)) {
serverGo = service.getGeoObjectByCode(code, this.configuration.getType(), false);
}
if (serverGo == null) {
if (this.configuration.getImportStrategy().equals(ImportStrategy.UPDATE_ONLY)) {
net.geoprism.registry.DataNotFoundException ex = new net.geoprism.registry.DataNotFoundException();
ex.setTypeLabel(GeoObjectMetadata.get().getClassDisplayLabel());
ex.setDataIdentifier(code);
ex.setAttributeLabel(GeoObjectMetadata.get().getAttributeDisplayLabel(DefaultAttribute.CODE.getName()));
throw ex;
}
isNew = true;
serverGo = service.newInstance(this.configuration.getType());
serverGo.setCode(code);
serverGo.setInvalid(false);
} else {
serverGo.lock();
}
try {
LocalizedValue entityName = this.getName(row);
if (entityName != null && this.hasValue(entityName)) {
serverGo.setDisplayLabel(entityName, this.configuration.getStartDate(), this.configuration.getEndDate());
}
Geometry geometry = (Geometry) this.getFormatSpecificImporter().getGeometry(row);
if (geometry != null) {
// geometry.getSRID().
if (geometry.isValid()) {
serverGo.setGeometry(geometry, this.configuration.getStartDate(), this.configuration.getEndDate());
} else {
// throw new SridException();
throw new InvalidGeometryException();
}
}
if (isNew) {
serverGo.setUid(ServiceFactory.getIdService().getUids(1)[0]);
}
// Set exists first so we can validate attributes on it
// ShapefileFunction existsFunction = this.configuration.getFunction(DefaultAttribute.EXISTS.getName());
//
// if (existsFunction != null)
// {
// Object value = existsFunction.getValue(row);
//
// if (value != null && !this.isEmptyString(value))
// {
// this.setValue(serverGo, this.configuration.getType().getAttribute(DefaultAttribute.EXISTS.getName()).get(), DefaultAttribute.EXISTS.getName(), value);
// }
// }
// else if (isNew)
// {
// ValueOverTime defaultExists = ((VertexServerGeoObject) serverGo).buildDefaultExists();
// if (defaultExists != null)
// {
// serverGo.setValue(DefaultAttribute.EXISTS.getName(), Boolean.TRUE, defaultExists.getStartDate(), defaultExists.getEndDate());
// }
// }
this.setValue(serverGo, this.configuration.getType().getAttribute(DefaultAttribute.EXISTS.getName()).get(), DefaultAttribute.EXISTS.getName(), true);
Map<String, AttributeType> attributes = this.configuration.getType().getAttributeMap();
Set<Entry<String, AttributeType>> entries = attributes.entrySet();
for (Entry<String, AttributeType> entry : entries) {
String attributeName = entry.getKey();
if (!attributeName.equals(GeoObject.CODE) && !attributeName.equals(DefaultAttribute.EXISTS.getName())) {
ShapefileFunction function = this.configuration.getFunction(attributeName);
if (function != null) {
Object value = function.getValue(row);
AttributeType attributeType = entry.getValue();
if (value != null && !this.isEmptyString(value)) {
// if (!(existsFunction == null && isNew))
// {
// try
// {
// ((VertexServerGeoObject) serverGo).enforceAttributeSetWithinRange(serverGo.getDisplayLabel().getValue(), attributeName, this.configuration.getStartDate(), this.configuration.getEndDate());
// }
// catch (ValueOutOfRangeException e)
// {
// final SimpleDateFormat format = ValueOverTimeDTO.getTimeFormatter();
//
// ImportOutOfRangeException ex = new ImportOutOfRangeException();
// ex.setStartDate(format.format(this.configuration.getStartDate()));
//
// if (ValueOverTime.INFINITY_END_DATE.equals(this.configuration.getEndDate()))
// {
// ex.setEndDate(LocalizationFacade.localize("changeovertime.present"));
// }
// else
// {
// ex.setEndDate(format.format(this.configuration.getEndDate()));
// }
//
// throw ex;
// }
// }
this.setValue(serverGo, attributeType, attributeName, value);
} else if (this.configuration.getCopyBlank()) {
this.setValue(serverGo, attributeType, attributeName, null);
}
}
}
}
go = serverGo.toGeoObjectOverTime(false);
goJson = go.toJSON().toString();
/*
* Try to get the parent and ensure that this row is not ignored. The
* getParent method will throw a IgnoreRowException if the parent is
* configured to be ignored.
*/
if (this.configuration.isPostalCode() && PostalCodeFactory.isAvailable(this.configuration.getType())) {
parent = this.parsePostalCode(row);
} else if (this.configuration.getHierarchy() != null && this.configuration.getLocations().size() > 0) {
parent = this.getParent(row);
}
parentBuilder.setParent(parent);
if (this.progressListener.hasValidationProblems()) {
throw new RuntimeException("Did not expect to encounter validation problems during import.");
}
data.setGoJson(goJson);
data.setNew(isNew);
data.setParentBuilder(parentBuilder);
serverGo.apply(true);
} finally {
if (serverGo != null) {
serverGo.unlock();
}
}
if (this.configuration.isExternalImport()) {
ShapefileFunction function = this.configuration.getExternalIdFunction();
Object value = function.getValue(row);
serverGo.createExternalId(this.configuration.getExternalSystem(), String.valueOf(value), this.configuration.getImportStrategy());
}
if (parent != null) {
parent.addChild(serverGo, this.configuration.getHierarchy(), this.configuration.getStartDate(), this.configuration.getEndDate());
} else if (isNew) {
// GeoEntity child = GeoEntity.getByKey(serverGo.getCode());
// GeoEntity root = GeoEntity.getByKey(GeoEntity.ROOT);
//
// child.addLink(root,
// this.configuration.getHierarchy().getEntityType());
}
// We must ensure that any problems created during the transaction are
// logged now instead of when the request returns. As such, if any
// problems exist immediately throw a ProblemException so that normal
// exception handling can occur.
List<ProblemIF> problems = RequestState.getProblemsInCurrentRequest();
List<ProblemIF> problems2 = new LinkedList<ProblemIF>();
for (ProblemIF problem : problems) {
problems2.add(problem);
}
if (problems.size() != 0) {
throw new ProblemException(null, problems2);
}
this.progressListener.setImportedRecords(this.progressListener.getImportedRecords() + 1);
} catch (IgnoreRowException e) {
// Do nothing
} catch (Throwable t) {
buildRecordException(goJson, isNew, parentBuilder, t);
}
this.progressListener.setWorkProgress(curWorkProgress + 1);
}
Aggregations