Search in sources :

Example 1 with BasicColumnFunction

use of net.geoprism.data.importer.BasicColumnFunction in project geoprism-registry by terraframe.

the class ImportConfiguration method fromJSON.

public void fromJSON(String json) {
    JSONObject jo = new JSONObject(json);
    this.objectType = jo.getString(OBJECT_TYPE);
    this.formatType = jo.getString(FORMAT_TYPE);
    if (jo.has(HISTORY_ID)) {
        this.historyId = jo.getString(HISTORY_ID);
    }
    if (jo.has(JOB_ID)) {
        this.jobId = jo.getString(JOB_ID);
    }
    this.vaultFileId = jo.getString(VAULT_FILE_ID);
    this.importStrategy = ImportStrategy.valueOf(jo.getString(IMPORT_STRATEGY));
    this.fileName = jo.getString(FILE_NAME);
    if (jo.has(EXTERNAL_SYSTEM_ID)) {
        this.externalSystemId = jo.getString(EXTERNAL_SYSTEM_ID);
    }
    if (jo.has(IS_EXTERNAL)) {
        this.isExternal = jo.getBoolean(IS_EXTERNAL);
    }
    if (jo.has(COPY_BLANK)) {
        this.copyBlank = jo.getBoolean(COPY_BLANK);
    }
    if (jo.has(EXTERNAL_ID_ATTRIBUTE_TARGET)) {
        this.externalIdFunction = new BasicColumnFunction(jo.getString(EXTERNAL_ID_ATTRIBUTE_TARGET));
    }
}
Also used : JSONObject(org.json.JSONObject) BasicColumnFunction(net.geoprism.data.importer.BasicColumnFunction)

Example 2 with BasicColumnFunction

use of net.geoprism.data.importer.BasicColumnFunction in project geoprism-registry by terraframe.

the class GeoObjectImportConfiguration method fromJSON.

@Request
public GeoObjectImportConfiguration fromJSON(String json, boolean includeCoordinates) {
    super.fromJSON(json);
    SimpleDateFormat format = new SimpleDateFormat(GeoObjectImportConfiguration.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);
    ServerGeoObjectType got = ServerGeoObjectType.get(code);
    this.setType(got);
    this.setIncludeCoordinates(includeCoordinates);
    this.setPostalCode(config.has(POSTAL_CODE) && config.getBoolean(POSTAL_CODE));
    if (config.has(REVEAL_GEOMETRY_COLUMN)) {
        this.setRevealGeometryColumn(config.getString(REVEAL_GEOMETRY_COLUMN));
    }
    try {
        if (config.has(GeoObjectImportConfiguration.START_DATE)) {
            this.setStartDate(format.parse(config.getString(GeoObjectImportConfiguration.START_DATE)));
        }
        if (config.has(GeoObjectImportConfiguration.END_DATE)) {
            this.setEndDate(format.parse(config.getString(GeoObjectImportConfiguration.END_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);
            List<GeoObjectType> ancestors = got.getTypeAncestors(hierarchyType, true);
            this.setHierarchy(hierarchyType);
            if (ancestors.size() > 0) {
                this.setRoot(null);
            }
        }
    }
    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);
            ServerHierarchyType pHierarchy = got.findHierarchy(this.hierarchy, pType);
            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.addParent(new Location(pType, pHierarchy, new ConstantShapefileFunction(target), matchStrategy));
            } else {
                this.addParent(new Location(pType, pHierarchy, new BasicColumnFunction(target), matchStrategy));
            }
        }
    }
    // If the hierarchy is inherited, we need to resolve the hierarchy
    // inheritance chain and set them properly on the Location objects
    // To do this, we must start from the bottom and resolve upwards
    ServerHierarchyType ht = this.hierarchy;
    for (int i = this.locations.size() - 1; i >= 0; --i) {
        Location loc = this.locations.get(i);
        ht = got.findHierarchy(ht, loc.getType());
        loc.setHierarchy(ht);
    }
    return this;
}
Also used : ServerHierarchyType(net.geoprism.registry.model.ServerHierarchyType) ServerGeoObjectType(net.geoprism.registry.model.ServerGeoObjectType) BasicColumnFunction(net.geoprism.data.importer.BasicColumnFunction) JSONArray(org.json.JSONArray) ProgrammingErrorException(com.runwaysdk.dataaccess.ProgrammingErrorException) JSONObject(org.json.JSONObject) ServerGeoObjectType(net.geoprism.registry.model.ServerGeoObjectType) GeoObjectType(org.commongeoregistry.adapter.metadata.GeoObjectType) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) Request(com.runwaysdk.session.Request)

Example 3 with BasicColumnFunction

use of net.geoprism.data.importer.BasicColumnFunction in project geoprism-registry by terraframe.

the class BusinessObjectImporterTest method testUpdateValue.

@Test
@Request
public void testUpdateValue() throws InterruptedException {
    BusinessObject object = BusinessObject.newInstance(type);
    object.setCode(TEST_CODE);
    object.apply();
    try {
        String value = "Test Text";
        String rowAttribute = "Bad";
        HashMap<String, Object> row = new HashMap<String, Object>();
        row.put(rowAttribute, value);
        row.put(BusinessObject.CODE, TEST_CODE);
        BusinessObjectImportConfiguration configuration = new BusinessObjectImportConfiguration();
        configuration.setImportStrategy(ImportStrategy.UPDATE_ONLY);
        configuration.setType(type);
        configuration.setDate(FastTestDataset.DEFAULT_END_TIME_DATE);
        configuration.setCopyBlank(false);
        configuration.setFunction(attributeType.getName(), new BasicColumnFunction(rowAttribute));
        configuration.setFunction(BusinessObject.CODE, new BasicColumnFunction(BusinessObject.CODE));
        BusinessObjectImporter importer = new BusinessObjectImporter(configuration, new NullImportProgressListener());
        importer.importRow(new MapFeatureRow(row));
        Assert.assertFalse(configuration.hasExceptions());
        BusinessObject result = BusinessObject.getByCode(type, TEST_CODE);
        Assert.assertEquals(result.getObjectValue(attributeType.getName()), value);
    } finally {
        if (object != null) {
            object.delete();
        }
    }
}
Also used : HashMap(java.util.HashMap) BasicColumnFunction(net.geoprism.data.importer.BasicColumnFunction) BusinessObjectImportConfiguration(net.geoprism.registry.etl.upload.BusinessObjectImportConfiguration) NullImportProgressListener(net.geoprism.registry.etl.NullImportProgressListener) JsonObject(com.google.gson.JsonObject) JSONObject(org.json.JSONObject) VertexServerGeoObject(net.geoprism.registry.model.graph.VertexServerGeoObject) BusinessObject(net.geoprism.registry.model.BusinessObject) BusinessObject(net.geoprism.registry.model.BusinessObject) BusinessObjectImporter(net.geoprism.registry.etl.upload.BusinessObjectImporter) MapFeatureRow(net.geoprism.registry.excel.MapFeatureRow) Test(org.junit.Test) Request(com.runwaysdk.session.Request)

Example 4 with BasicColumnFunction

use of net.geoprism.data.importer.BasicColumnFunction 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)

Example 5 with BasicColumnFunction

use of net.geoprism.data.importer.BasicColumnFunction in project geoprism-registry by terraframe.

the class BusinessObjectImporterTest method testImportValueExistingNewOnly.

@Test
@Request
public void testImportValueExistingNewOnly() throws InterruptedException {
    BusinessObject object = BusinessObject.newInstance(type);
    object.setCode(TEST_CODE);
    object.apply();
    try {
        String value = "Test Text";
        String rowAttribute = "Bad";
        HashMap<String, Object> row = new HashMap<String, Object>();
        row.put(rowAttribute, value);
        row.put(BusinessObject.CODE, TEST_CODE);
        BusinessObjectImportConfiguration configuration = new BusinessObjectImportConfiguration();
        configuration.setImportStrategy(ImportStrategy.NEW_ONLY);
        configuration.setType(type);
        configuration.setDate(FastTestDataset.DEFAULT_END_TIME_DATE);
        configuration.setCopyBlank(false);
        configuration.setFunction(attributeType.getName(), new BasicColumnFunction(rowAttribute));
        configuration.setFunction(BusinessObject.CODE, new BasicColumnFunction(BusinessObject.CODE));
        BusinessObjectImporter importer = new BusinessObjectImporter(configuration, new NullImportProgressListener());
        importer.importRow(new MapFeatureRow(row));
        Assert.assertTrue(configuration.hasExceptions());
        LinkedList<BusinessObjectRecordedErrorException> exceptions = configuration.getExceptions();
        Assert.assertEquals(1, exceptions.size());
        BusinessObjectRecordedErrorException exception = exceptions.get(0);
        Assert.assertTrue(exception.getError() instanceof DuplicateDataException);
    } finally {
        if (object != null) {
            object.delete();
        }
    }
}
Also used : HashMap(java.util.HashMap) BasicColumnFunction(net.geoprism.data.importer.BasicColumnFunction) BusinessObjectImportConfiguration(net.geoprism.registry.etl.upload.BusinessObjectImportConfiguration) BusinessObject(net.geoprism.registry.model.BusinessObject) BusinessObjectImporter(net.geoprism.registry.etl.upload.BusinessObjectImporter) DuplicateDataException(com.runwaysdk.dataaccess.DuplicateDataException) BusinessObjectRecordedErrorException(net.geoprism.registry.etl.upload.BusinessObjectRecordedErrorException) NullImportProgressListener(net.geoprism.registry.etl.NullImportProgressListener) JsonObject(com.google.gson.JsonObject) JSONObject(org.json.JSONObject) VertexServerGeoObject(net.geoprism.registry.model.graph.VertexServerGeoObject) BusinessObject(net.geoprism.registry.model.BusinessObject) MapFeatureRow(net.geoprism.registry.excel.MapFeatureRow) Test(org.junit.Test) Request(com.runwaysdk.session.Request)

Aggregations

BasicColumnFunction (net.geoprism.data.importer.BasicColumnFunction)10 Request (com.runwaysdk.session.Request)9 JSONObject (org.json.JSONObject)7 Test (org.junit.Test)6 ServerHierarchyType (net.geoprism.registry.model.ServerHierarchyType)5 JsonObject (com.google.gson.JsonObject)3 InputStream (java.io.InputStream)3 HashMap (java.util.HashMap)3 NullImportProgressListener (net.geoprism.registry.etl.NullImportProgressListener)3 BusinessObjectImportConfiguration (net.geoprism.registry.etl.upload.BusinessObjectImportConfiguration)3 BusinessObjectImporter (net.geoprism.registry.etl.upload.BusinessObjectImporter)3 MapFeatureRow (net.geoprism.registry.excel.MapFeatureRow)3 GeoObjectImportConfiguration (net.geoprism.registry.io.GeoObjectImportConfiguration)3 Location (net.geoprism.registry.io.Location)3 BusinessObject (net.geoprism.registry.model.BusinessObject)3 VertexServerGeoObject (net.geoprism.registry.model.graph.VertexServerGeoObject)3 ShapefileService (net.geoprism.registry.service.ShapefileService)3 GeoObject (org.commongeoregistry.adapter.dataaccess.GeoObject)3 JSONArray (org.json.JSONArray)3 ProgrammingErrorException (com.runwaysdk.dataaccess.ProgrammingErrorException)2