use of net.geoprism.registry.io.ParentMatchStrategy in project geoprism-registry by terraframe.
the class BusinessObjectImporter method getGeoObject.
/**
* Returns the entity as defined by the 'parent' and 'parentType' attributes
* of the given feature. If an entity is not found then Earth is returned by
* default. The 'parent' value of the feature must define an entity name or a
* geo oid. The 'parentType' value of the feature must define the localized
* display label of the universal.
*
* This algorithm resolves parent contexts starting at the top of the
* hierarchy and traversing downward, resolving hierarchy inheritance as
* needed. If at any point a location cannot be found, a SmartException will
* be thrown which varies depending on the ParentMatchStrategy.
*
* @param feature
* Shapefile feature used to determine the parent
* @return Parent entity
*/
private ServerGeoObjectIF getGeoObject(FeatureRow feature) {
List<Location> locations = this.configuration.getLocations();
ServerGeoObjectIF parent = null;
JSONArray context = new JSONArray();
ArrayList<String> parentKeyBuilder = new ArrayList<String>();
for (Location location : locations) {
Object label = getParentCode(feature, location);
if (label != null) {
String key = parent != null ? parent.getCode() + "-" + label : label.toString();
parentKeyBuilder.add(label.toString());
if (this.configuration.isExclusion(GeoObjectImportConfiguration.PARENT_EXCLUSION, key)) {
throw new IgnoreRowException();
}
// Check the parent cache
String parentChainKey = StringUtils.join(parentKeyBuilder, parentConcatToken);
if (this.cache.containsKey(parentChainKey)) {
parent = this.cache.get(parentChainKey);
JSONObject element = new JSONObject();
element.put("label", label.toString());
element.put("type", location.getType().getLabel().getValue());
context.put(element);
continue;
}
final ParentMatchStrategy ms = location.getMatchStrategy();
// Search
ServerGeoObjectQuery query = new ServerGeoObjectService().createQuery(location.getType(), this.configuration.getDate());
query.setLimit(20);
if (ms.equals(ParentMatchStrategy.CODE)) {
query.setRestriction(new ServerCodeRestriction(label.toString()));
} else if (ms.equals(ParentMatchStrategy.EXTERNAL)) {
query.setRestriction(new ServerExternalIdRestriction(this.getConfiguration().getExternalSystem(), label.toString()));
} else if (ms.equals(ParentMatchStrategy.DHIS2_PATH)) {
String path = label.toString();
String dhis2Parent;
try {
if (path.startsWith("/")) {
path = path.substring(1);
}
String[] pathArr = path.split("/");
dhis2Parent = pathArr[pathArr.length - 2];
} catch (Throwable t) {
InvalidDhis2PathException ex = new InvalidDhis2PathException(t);
ex.setDhis2Path(path);
throw ex;
}
query.setRestriction(new ServerExternalIdRestriction(this.getConfiguration().getExternalSystem(), dhis2Parent));
} else {
query.setRestriction(new ServerSynonymRestriction(label.toString(), this.configuration.getDate(), parent, location.getHierarchy()));
}
List<ServerGeoObjectIF> results = query.getResults();
if (results != null && results.size() > 0) {
ServerGeoObjectIF result = null;
// into the query SQL.
for (ServerGeoObjectIF loop : results) {
if (result != null && !result.getCode().equals(loop.getCode())) {
AmbiguousParentException ex = new AmbiguousParentException();
ex.setParentLabel(label.toString());
ex.setContext(context.toString());
throw ex;
}
result = loop;
}
parent = result;
JSONObject element = new JSONObject();
element.put("label", label.toString());
element.put("type", location.getType().getLabel().getValue());
context.put(element);
this.cache.put(parentChainKey, parent);
} else {
if (ms.equals(ParentMatchStrategy.CODE)) {
final ParentCodeException ex = new ParentCodeException();
ex.setParentCode(label.toString());
ex.setParentType(location.getType().getLabel().getValue());
ex.setContext(context.toString());
throw ex;
} else if (ms.equals(ParentMatchStrategy.EXTERNAL)) {
final ExternalParentReferenceException ex = new ExternalParentReferenceException();
ex.setExternalId(label.toString());
ex.setParentType(location.getType().getLabel().getValue());
ex.setContext(context.toString());
throw ex;
} else {
String parentCode = (parent == null) ? null : parent.getCode();
ParentReferenceProblem prp = new ParentReferenceProblem(location.getType().getCode(), label.toString(), parentCode, context.toString());
prp.addAffectedRowNumber(this.progressListener.getWorkProgress() + 1);
prp.setHistoryId(this.configuration.historyId);
this.progressListener.addReferenceProblem(prp);
}
return null;
}
}
}
return parent;
}
use of net.geoprism.registry.io.ParentMatchStrategy in project geoprism-registry by terraframe.
the class GeoObjectImporter method getParent.
/**
* Returns the entity as defined by the 'parent' and 'parentType' attributes
* of the given feature. If an entity is not found then Earth is returned by
* default. The 'parent' value of the feature must define an entity name or a
* geo oid. The 'parentType' value of the feature must define the localized
* display label of the universal.
*
* This algorithm resolves parent contexts starting at the top of the
* hierarchy and traversing downward, resolving hierarchy inheritance as
* needed. If at any point a location cannot be found, a SmartException will
* be thrown which varies depending on the ParentMatchStrategy.
*
* @param feature
* Shapefile feature used to determine the parent
* @return Parent entity
*/
private ServerGeoObjectIF getParent(FeatureRow feature) {
List<Location> locations = this.configuration.getLocations();
ServerGeoObjectIF parent = null;
JSONArray context = new JSONArray();
ArrayList<String> parentKeyBuilder = new ArrayList<String>();
for (Location location : locations) {
Object label = getParentCode(feature, location);
if (label != null) {
String key = parent != null ? parent.getCode() + "-" + label : label.toString();
parentKeyBuilder.add(label.toString());
if (this.configuration.isExclusion(GeoObjectImportConfiguration.PARENT_EXCLUSION, key)) {
throw new IgnoreRowException();
}
// Check the parent cache
String parentChainKey = StringUtils.join(parentKeyBuilder, parentConcatToken);
if (this.parentCache.containsKey(parentChainKey)) {
parent = this.parentCache.get(parentChainKey);
JSONObject element = new JSONObject();
element.put("label", label.toString());
element.put("type", location.getType().getLabel().getValue());
context.put(element);
continue;
}
final ParentMatchStrategy ms = location.getMatchStrategy();
// Search
ServerGeoObjectQuery query = this.service.createQuery(location.getType(), this.configuration.getStartDate());
query.setLimit(20);
if (ms.equals(ParentMatchStrategy.CODE)) {
query.setRestriction(new ServerCodeRestriction(label.toString()));
} else if (ms.equals(ParentMatchStrategy.EXTERNAL)) {
query.setRestriction(new ServerExternalIdRestriction(this.getConfiguration().getExternalSystem(), label.toString()));
} else if (ms.equals(ParentMatchStrategy.DHIS2_PATH)) {
String path = label.toString();
String dhis2Parent;
try {
if (path.startsWith("/")) {
path = path.substring(1);
}
String[] pathArr = path.split("/");
dhis2Parent = pathArr[pathArr.length - 2];
} catch (Throwable t) {
InvalidDhis2PathException ex = new InvalidDhis2PathException(t);
ex.setDhis2Path(path);
throw ex;
}
query.setRestriction(new ServerExternalIdRestriction(this.getConfiguration().getExternalSystem(), dhis2Parent));
} else {
query.setRestriction(new ServerSynonymRestriction(label.toString(), this.configuration.getStartDate(), parent, location.getHierarchy()));
}
List<ServerGeoObjectIF> results = query.getResults();
if (results != null && results.size() > 0) {
ServerGeoObjectIF result = null;
// into the query SQL.
for (ServerGeoObjectIF loop : results) {
if (result != null && !result.getCode().equals(loop.getCode())) {
AmbiguousParentException ex = new AmbiguousParentException();
ex.setParentLabel(label.toString());
ex.setContext(context.toString());
throw ex;
}
result = loop;
}
parent = result;
JSONObject element = new JSONObject();
element.put("label", label.toString());
element.put("type", location.getType().getLabel().getValue());
context.put(element);
this.parentCache.put(parentChainKey, parent);
} else {
if (context.length() == 0) {
GeoObject root = this.configuration.getRoot();
if (root != null) {
JSONObject element = new JSONObject();
element.put("label", root.getLocalizedDisplayLabel());
element.put("type", root.getType().getLabel().getValue());
context.put(element);
}
}
if (ms.equals(ParentMatchStrategy.CODE)) {
final ParentCodeException ex = new ParentCodeException();
ex.setParentCode(label.toString());
ex.setParentType(location.getType().getLabel().getValue());
ex.setContext(context.toString());
throw ex;
} else if (ms.equals(ParentMatchStrategy.EXTERNAL)) {
final ExternalParentReferenceException ex = new ExternalParentReferenceException();
ex.setExternalId(label.toString());
ex.setParentType(location.getType().getLabel().getValue());
ex.setContext(context.toString());
throw ex;
} else {
String parentCode = (parent == null) ? null : parent.getCode();
ParentReferenceProblem prp = new ParentReferenceProblem(location.getType().getCode(), label.toString(), parentCode, context.toString());
prp.addAffectedRowNumber(this.progressListener.getWorkProgress() + 1);
prp.setHistoryId(this.configuration.historyId);
this.progressListener.addReferenceProblem(prp);
}
return null;
}
}
}
return parent;
}
use of net.geoprism.registry.io.ParentMatchStrategy in project geoprism-registry by terraframe.
the class BusinessObjectImportConfiguration method fromJSON.
@Request
public BusinessObjectImportConfiguration fromJSON(String json, boolean includeCoordinates) {
super.fromJSON(json);
SimpleDateFormat format = new SimpleDateFormat(BusinessObjectImportConfiguration.DATE_FORMAT);
format.setTimeZone(GeoRegistryUtil.SYSTEM_TIMEZONE);
JSONObject config = new JSONObject(json);
JSONObject type = config.getJSONObject(TYPE);
JSONArray locations = config.has(LOCATIONS) ? config.getJSONArray(LOCATIONS) : new JSONArray();
JSONArray attributes = type.getJSONArray(GeoObjectType.JSON_ATTRIBUTES);
String code = type.getString(GeoObjectType.JSON_CODE);
BusinessType businessType = BusinessType.getByCode(code);
this.setType(businessType);
try {
if (config.has(BusinessObjectImportConfiguration.DATE)) {
this.setDate(format.parse(config.getString(BusinessObjectImportConfiguration.DATE)));
}
} catch (ParseException e) {
throw new ProgrammingErrorException(e);
}
if (config.has(HIERARCHY)) {
String hCode = config.getString(HIERARCHY);
if (hCode.length() > 0) {
ServerHierarchyType hierarchyType = ServerHierarchyType.get(hCode);
this.setHierarchy(hierarchyType);
}
}
if (config.has(EXCLUSIONS)) {
JSONArray exclusions = config.getJSONArray(EXCLUSIONS);
for (int i = 0; i < exclusions.length(); i++) {
JSONObject exclusion = exclusions.getJSONObject(i);
String attributeName = exclusion.getString(AttributeType.JSON_CODE);
String value = exclusion.getString(VALUE);
this.addExclusion(attributeName, value);
}
}
for (int i = 0; i < attributes.length(); i++) {
JSONObject attribute = attributes.getJSONObject(i);
if (attribute.has(TARGET)) {
String attributeName = attribute.getString(AttributeType.JSON_CODE);
// In the case of a spreadsheet, this ends up being the column header
String target = attribute.getString(TARGET);
if (attribute.has("locale")) {
String locale = attribute.getString("locale");
if (this.getFunction(attributeName) == null) {
this.setFunction(attributeName, new LocalizedValueFunction());
}
LocalizedValueFunction function = (LocalizedValueFunction) this.getFunction(attributeName);
function.add(locale, new BasicColumnFunction(target));
} else {
this.setFunction(attributeName, new BasicColumnFunction(target));
}
}
}
for (int i = 0; i < locations.length(); i++) {
JSONObject location = locations.getJSONObject(i);
if (location.has(TARGET) && location.getString(TARGET).length() > 0 && location.has(MATCH_STRATEGY) && location.getString(MATCH_STRATEGY).length() > 0) {
String pCode = location.getString(AttributeType.JSON_CODE);
ServerGeoObjectType pType = ServerGeoObjectType.get(pCode);
String target = location.getString(TARGET);
ParentMatchStrategy matchStrategy = ParentMatchStrategy.valueOf(location.getString(MATCH_STRATEGY));
// coming in with use BasicColumnFunctions
if (location.has("type") && location.getString("type").equals(ConstantShapefileFunction.class.getName())) {
this.addLocation(new Location(pType, this.hierarchy, new ConstantShapefileFunction(target), matchStrategy));
} else {
this.addLocation(new Location(pType, this.hierarchy, new BasicColumnFunction(target), matchStrategy));
}
}
}
return this;
}
Aggregations