Search in sources :

Example 1 with BusinessObject

use of net.geoprism.registry.model.BusinessObject in project geoprism-registry by terraframe.

the class BusinessObjectImporter 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 + BusinessObjectImporter.refreshSessionRecordCount) < curWorkProgress) {
            SessionFacade.renewSession(Session.getCurrentSession().getOid());
            this.lastValidateSessionRefresh = curWorkProgress;
        }
        try {
            if (this.configuration.getHierarchy() != null && this.configuration.getLocations().size() > 0) {
                this.getGeoObject(row);
            }
            /*
         * 2. Check for serialization and term problems
         */
            String code = this.getCode(row);
            if (code == null || code.length() <= 0) {
                RequiredMappingException ex = new RequiredMappingException();
                ex.setAttributeLabel(GeoObjectTypeMetadata.getAttributeDisplayLabel(DefaultAttribute.CODE.getName()));
                throw ex;
            }
            BusinessObject entity;
            entity = BusinessObject.newInstance(this.configuration.getType());
            Map<String, AttributeType> attributes = this.getConfiguration().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);
                        }
                    }
                }
            }
        } 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();
    }
}
Also used : IgnoreRowException(net.geoprism.registry.io.IgnoreRowException) BusinessObject(net.geoprism.registry.model.BusinessObject) Entry(java.util.Map.Entry) AttributeType(org.commongeoregistry.adapter.metadata.AttributeType) RequiredMappingException(net.geoprism.registry.io.RequiredMappingException) ShapefileFunction(net.geoprism.data.importer.ShapefileFunction) GeoObject(org.commongeoregistry.adapter.dataaccess.GeoObject) JSONObject(org.json.JSONObject) BusinessObject(net.geoprism.registry.model.BusinessObject) VertexObject(com.runwaysdk.business.graph.VertexObject) RowValidationProblem(net.geoprism.registry.etl.RowValidationProblem) Transaction(com.runwaysdk.dataaccess.transaction.Transaction)

Example 2 with BusinessObject

use of net.geoprism.registry.model.BusinessObject 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 3 with BusinessObject

use of net.geoprism.registry.model.BusinessObject in project geoprism-registry by terraframe.

the class BusinessObjectTest method testSetGetValue.

@Test
@Request
public void testSetGetValue() {
    BusinessObject object = BusinessObject.newInstance(type);
    object.setValue(attribute.getName(), "Test Text");
    object.setCode(TEST_CODE);
    object.apply();
    try {
        Assert.assertEquals("Test Text", object.getObjectValue(attribute.getName()));
    } finally {
        object.delete();
    }
}
Also used : BusinessObject(net.geoprism.registry.model.BusinessObject) Test(org.junit.Test) Request(com.runwaysdk.session.Request)

Example 4 with BusinessObject

use of net.geoprism.registry.model.BusinessObject 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)

Example 5 with BusinessObject

use of net.geoprism.registry.model.BusinessObject in project geoprism-registry by terraframe.

the class BusinessObjectImporter 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 + BusinessObjectImporter.refreshSessionRecordCount) < curWorkProgress) {
        SessionFacade.renewSession(Session.getCurrentSession().getOid());
        this.lastImportSessionRefresh = curWorkProgress;
    }
    BusinessObject businessObject = null;
    ServerGeoObjectIF geoObject = null;
    boolean isNew = false;
    GeoObjectErrorBuilder builder = new GeoObjectErrorBuilder();
    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)) {
            businessObject = BusinessObject.getByCode(this.configuration.getType(), code);
        }
        if (businessObject == 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;
            businessObject = BusinessObject.newInstance(this.configuration.getType());
            businessObject.setCode(code);
        }
        builder.setObject(businessObject);
        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();
            ShapefileFunction function = this.configuration.getFunction(attributeName);
            if (function != null) {
                Object value = function.getValue(row);
                AttributeType attributeType = entry.getValue();
                if (value != null && !this.isEmptyString(value)) {
                    this.setValue(businessObject, attributeType, attributeName, value);
                } else if (this.configuration.getCopyBlank()) {
                    this.setValue(businessObject, attributeType, attributeName, null);
                }
            }
            /*
         * 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.getHierarchy() != null && this.configuration.getLocations().size() > 0) {
                geoObject = this.getGeoObject(row);
            }
            builder.setGeoObject(geoObject);
            if (this.progressListener.hasValidationProblems()) {
                throw new RuntimeException("Did not expect to encounter validation problems during import.");
            }
            data.setGoJson(businessObject.toJSON().toString());
            data.setNew(isNew);
            data.setParentBuilder(builder);
            businessObject.setGeoObject(geoObject);
            businessObject.apply();
        }
        // 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> existingProblems = RequestState.getProblemsInCurrentRequest();
        if (existingProblems.size() != 0) {
            throw new ProblemException(null, new LinkedList<ProblemIF>(existingProblems));
        }
        this.progressListener.setImportedRecords(this.progressListener.getImportedRecords() + 1);
    } catch (IgnoreRowException e) {
    // Do nothing
    } catch (Throwable t) {
        buildRecordException(businessObject.toJSON().toString(), isNew, builder, t);
    }
    this.progressListener.setWorkProgress(curWorkProgress + 1);
}
Also used : ProblemException(com.runwaysdk.ProblemException) ProblemIF(com.runwaysdk.ProblemIF) ServerGeoObjectIF(net.geoprism.registry.model.ServerGeoObjectIF) IgnoreRowException(net.geoprism.registry.io.IgnoreRowException) BusinessObject(net.geoprism.registry.model.BusinessObject) Entry(java.util.Map.Entry) AttributeType(org.commongeoregistry.adapter.metadata.AttributeType) ShapefileFunction(net.geoprism.data.importer.ShapefileFunction) RequiredMappingException(net.geoprism.registry.io.RequiredMappingException) GeoObject(org.commongeoregistry.adapter.dataaccess.GeoObject) JSONObject(org.json.JSONObject) BusinessObject(net.geoprism.registry.model.BusinessObject) VertexObject(com.runwaysdk.business.graph.VertexObject) Transaction(com.runwaysdk.dataaccess.transaction.Transaction)

Aggregations

BusinessObject (net.geoprism.registry.model.BusinessObject)9 Request (com.runwaysdk.session.Request)7 Test (org.junit.Test)7 JSONObject (org.json.JSONObject)5 JsonObject (com.google.gson.JsonObject)3 HashMap (java.util.HashMap)3 BasicColumnFunction (net.geoprism.data.importer.BasicColumnFunction)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 VertexServerGeoObject (net.geoprism.registry.model.graph.VertexServerGeoObject)3 VertexObject (com.runwaysdk.business.graph.VertexObject)2 Transaction (com.runwaysdk.dataaccess.transaction.Transaction)2 Entry (java.util.Map.Entry)2 ShapefileFunction (net.geoprism.data.importer.ShapefileFunction)2 IgnoreRowException (net.geoprism.registry.io.IgnoreRowException)2 RequiredMappingException (net.geoprism.registry.io.RequiredMappingException)2 GeoObject (org.commongeoregistry.adapter.dataaccess.GeoObject)2 AttributeType (org.commongeoregistry.adapter.metadata.AttributeType)2