Search in sources :

Example 1 with Location

use of net.geoprism.registry.io.Location 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;
}
Also used : ServerGeoObjectService(net.geoprism.registry.geoobject.ServerGeoObjectService) ServerGeoObjectIF(net.geoprism.registry.model.ServerGeoObjectIF) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) ServerSynonymRestriction(net.geoprism.registry.query.ServerSynonymRestriction) IgnoreRowException(net.geoprism.registry.io.IgnoreRowException) ServerGeoObjectQuery(net.geoprism.registry.query.ServerGeoObjectQuery) JSONObject(org.json.JSONObject) AmbiguousParentException(net.geoprism.registry.io.AmbiguousParentException) ParentCodeException(net.geoprism.registry.io.ParentCodeException) GeoObject(org.commongeoregistry.adapter.dataaccess.GeoObject) JSONObject(org.json.JSONObject) BusinessObject(net.geoprism.registry.model.BusinessObject) VertexObject(com.runwaysdk.business.graph.VertexObject) ParentMatchStrategy(net.geoprism.registry.io.ParentMatchStrategy) ParentReferenceProblem(net.geoprism.registry.etl.ParentReferenceProblem) ServerCodeRestriction(net.geoprism.registry.query.ServerCodeRestriction) ServerExternalIdRestriction(net.geoprism.registry.query.ServerExternalIdRestriction) Location(net.geoprism.registry.io.Location)

Example 2 with Location

use of net.geoprism.registry.io.Location 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;
}
Also used : ServerGeoObjectIF(net.geoprism.registry.model.ServerGeoObjectIF) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) ServerSynonymRestriction(net.geoprism.registry.query.ServerSynonymRestriction) IgnoreRowException(net.geoprism.registry.io.IgnoreRowException) ServerGeoObjectQuery(net.geoprism.registry.query.ServerGeoObjectQuery) JSONObject(org.json.JSONObject) AmbiguousParentException(net.geoprism.registry.io.AmbiguousParentException) ParentCodeException(net.geoprism.registry.io.ParentCodeException) GeoObject(org.commongeoregistry.adapter.dataaccess.GeoObject) JSONObject(org.json.JSONObject) VertexServerGeoObject(net.geoprism.registry.model.graph.VertexServerGeoObject) VertexObject(com.runwaysdk.business.graph.VertexObject) GeoObject(org.commongeoregistry.adapter.dataaccess.GeoObject) VertexServerGeoObject(net.geoprism.registry.model.graph.VertexServerGeoObject) ParentMatchStrategy(net.geoprism.registry.io.ParentMatchStrategy) ParentReferenceProblem(net.geoprism.registry.etl.ParentReferenceProblem) ServerCodeRestriction(net.geoprism.registry.query.ServerCodeRestriction) ServerExternalIdRestriction(net.geoprism.registry.query.ServerExternalIdRestriction) Location(net.geoprism.registry.io.Location)

Example 3 with Location

use of net.geoprism.registry.io.Location in project geoprism-registry by terraframe.

the class GeoObjectImporter method parsePostalCode.

private ServerGeoObjectIF parsePostalCode(FeatureRow feature) {
    LocationBuilder builder = PostalCodeFactory.get(this.configuration.getType());
    Location location = builder.build(this.configuration.getFunction(GeoObject.CODE));
    ShapefileFunction function = location.getFunction();
    String code = (String) function.getValue(feature);
    if (code != null) {
        // Search
        ServerGeoObjectQuery query = new ServerGeoObjectService().createQuery(location.getType(), this.configuration.getStartDate());
        query.setRestriction(new ServerCodeRestriction(code));
        // Assert.assertNull(query.getSingleResult());
        ServerGeoObjectIF result = query.getSingleResult();
        if (result != null) {
            return result;
        } else {
            PostalCodeLocationException e = new PostalCodeLocationException();
            e.setCode(code);
            e.setTypeLabel(location.getType().getLabel().getValue());
            throw e;
        }
    }
    return null;
}
Also used : ServerGeoObjectService(net.geoprism.registry.geoobject.ServerGeoObjectService) LocationBuilder(net.geoprism.registry.io.LocationBuilder) ServerGeoObjectIF(net.geoprism.registry.model.ServerGeoObjectIF) PostalCodeLocationException(net.geoprism.registry.io.PostalCodeLocationException) ShapefileFunction(net.geoprism.data.importer.ShapefileFunction) ServerGeoObjectQuery(net.geoprism.registry.query.ServerGeoObjectQuery) ServerCodeRestriction(net.geoprism.registry.query.ServerCodeRestriction) Location(net.geoprism.registry.io.Location)

Example 4 with Location

use of net.geoprism.registry.io.Location in project geoprism-registry by terraframe.

the class ShapefileServiceTest method testGetAttributeInformationPostalCode.

@Test
@Request
public void testGetAttributeInformationPostalCode() {
    InputStream istream = this.getClass().getResourceAsStream("/cb_2017_us_state_500k.zip.test");
    ServerGeoObjectType type = USATestData.STATE.getServerObject();
    PostalCodeFactory.addPostalCode(type, new LocationBuilder() {

        @Override
        public Location build(ShapefileFunction function) {
            return null;
        }
    });
    Assert.assertNotNull(istream);
    ShapefileService service = new ShapefileService();
    JSONObject result = service.getShapefileConfiguration(testData.clientRequest.getSessionId(), USATestData.STATE.getCode(), null, null, "cb_2017_us_state_500k.zip", istream, ImportStrategy.NEW_AND_UPDATE, false);
    Assert.assertTrue(result.getBoolean(GeoObjectImportConfiguration.HAS_POSTAL_CODE));
}
Also used : JSONObject(org.json.JSONObject) LocationBuilder(net.geoprism.registry.io.LocationBuilder) InputStream(java.io.InputStream) ServerGeoObjectType(net.geoprism.registry.model.ServerGeoObjectType) ShapefileFunction(net.geoprism.data.importer.ShapefileFunction) ConstantShapefileFunction(net.geoprism.registry.io.ConstantShapefileFunction) ShapefileService(net.geoprism.registry.service.ShapefileService) Location(net.geoprism.registry.io.Location) Test(org.junit.Test) Request(com.runwaysdk.session.Request)

Example 5 with Location

use of net.geoprism.registry.io.Location in project geoprism-registry by terraframe.

the class ShapefileServiceTest method testImportShapefileWithBadParentCode.

@Test
@Request
public void testImportShapefileWithBadParentCode() throws Throwable {
    GeoObject geoObj = ServiceFactory.getRegistryService().newGeoObjectInstance(testData.clientRequest.getSessionId(), USATestData.COUNTRY.getCode());
    geoObj.setCode("00");
    geoObj.setDisplayLabel(LocalizedValue.DEFAULT_LOCALE, "Test Label");
    geoObj.setUid(ServiceFactory.getIdService().getUids(1)[0]);
    ServerGeoObjectIF serverGO = new ServerGeoObjectService(new AllowAllGeoObjectPermissionService()).apply(geoObj, TestDataSet.DEFAULT_OVER_TIME_DATE, TestDataSet.DEFAULT_END_TIME_DATE, true, false);
    geoObj = RegistryService.getInstance().getGeoObjectByCode(Session.getCurrentSession().getOid(), serverGO.getCode(), serverGO.getType().getCode(), TestDataSet.DEFAULT_OVER_TIME_DATE);
    InputStream istream = this.getClass().getResourceAsStream("/cb_2017_us_state_500k.zip.test");
    Assert.assertNotNull(istream);
    ShapefileService service = new ShapefileService();
    GeoObjectImportConfiguration config = this.getTestConfiguration(istream, service, null, ImportStrategy.NEW_AND_UPDATE);
    ServerHierarchyType hierarchyType = ServerHierarchyType.get(USATestData.HIER_ADMIN.getCode());
    config.setHierarchy(hierarchyType);
    config.addParent(new Location(USATestData.COUNTRY.getServerObject(), hierarchyType, new BasicColumnFunction("GEOID"), ParentMatchStrategy.CODE));
    ImportHistory hist = mockImport(config);
    Assert.assertTrue(hist.getStatus().get(0).equals(AllJobStatus.FEEDBACK));
    hist = ImportHistory.get(hist.getOid());
    Assert.assertEquals(new Long(56), hist.getWorkTotal());
    Assert.assertEquals(new Long(56), hist.getWorkProgress());
    Assert.assertEquals(new Long(0), hist.getImportedRecords());
    Assert.assertEquals(ImportStage.VALIDATION_RESOLVE, hist.getStage().get(0));
    final GeoObjectImportConfiguration test = new GeoObjectImportConfiguration();
    test.fromJSON(hist.getConfigJson(), false);
    // TODO
    // Assert.assertEquals(config.getParentLookupType(),
    // test.getParentLookupType());
    // JSONArray errors = new JSONArray(hist.getErrorJson());
    // 
    // Assert.assertEquals(0, errors.length());
    // Ensure the geo objects were not created
    ServerGeoObjectQuery query = new ServerGeoObjectService().createQuery(USATestData.STATE.getServerObject(), config.getStartDate());
    query.setRestriction(new ServerCodeRestriction("01"));
    Assert.assertNull(query.getSingleResult());
}
Also used : ServerGeoObjectService(net.geoprism.registry.geoobject.ServerGeoObjectService) ServerHierarchyType(net.geoprism.registry.model.ServerHierarchyType) GeoObjectImportConfiguration(net.geoprism.registry.io.GeoObjectImportConfiguration) ServerGeoObjectIF(net.geoprism.registry.model.ServerGeoObjectIF) InputStream(java.io.InputStream) BasicColumnFunction(net.geoprism.data.importer.BasicColumnFunction) ShapefileService(net.geoprism.registry.service.ShapefileService) ServerGeoObjectQuery(net.geoprism.registry.query.ServerGeoObjectQuery) GeoObject(org.commongeoregistry.adapter.dataaccess.GeoObject) ServerCodeRestriction(net.geoprism.registry.query.ServerCodeRestriction) AllowAllGeoObjectPermissionService(net.geoprism.registry.permission.AllowAllGeoObjectPermissionService) Location(net.geoprism.registry.io.Location) Test(org.junit.Test) Request(com.runwaysdk.session.Request)

Aggregations

Location (net.geoprism.registry.io.Location)8 JSONObject (org.json.JSONObject)6 Request (com.runwaysdk.session.Request)5 ServerGeoObjectIF (net.geoprism.registry.model.ServerGeoObjectIF)5 ServerCodeRestriction (net.geoprism.registry.query.ServerCodeRestriction)5 ServerGeoObjectQuery (net.geoprism.registry.query.ServerGeoObjectQuery)5 JSONArray (org.json.JSONArray)5 ServerGeoObjectService (net.geoprism.registry.geoobject.ServerGeoObjectService)4 GeoObject (org.commongeoregistry.adapter.dataaccess.GeoObject)4 InputStream (java.io.InputStream)3 BasicColumnFunction (net.geoprism.data.importer.BasicColumnFunction)3 ShapefileFunction (net.geoprism.data.importer.ShapefileFunction)3 ConstantShapefileFunction (net.geoprism.registry.io.ConstantShapefileFunction)3 ParentMatchStrategy (net.geoprism.registry.io.ParentMatchStrategy)3 ServerHierarchyType (net.geoprism.registry.model.ServerHierarchyType)3 ShapefileService (net.geoprism.registry.service.ShapefileService)3 Test (org.junit.Test)3 VertexObject (com.runwaysdk.business.graph.VertexObject)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ArrayList (java.util.ArrayList)2