Search in sources :

Example 6 with ShapefileFunction

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

the class LocalizedValueFunction method getValue.

@Override
public Object getValue(FeatureRow feature) {
    Map<String, String> localeValues = new HashMap<>();
    Set<Entry<String, ShapefileFunction>> entries = map.entrySet();
    for (Entry<String, ShapefileFunction> entry : entries) {
        String locale = entry.getKey();
        ShapefileFunction function = entry.getValue();
        Object value = function.getValue(feature);
        if (value != null) {
            localeValues.put(locale, value.toString());
        }
    }
    return new LocalizedValue("", localeValues);
}
Also used : Entry(java.util.Map.Entry) LocalizedValue(org.commongeoregistry.adapter.dataaccess.LocalizedValue) HashMap(java.util.HashMap) ShapefileFunction(net.geoprism.data.importer.ShapefileFunction)

Example 7 with ShapefileFunction

use of net.geoprism.data.importer.ShapefileFunction 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 8 with ShapefileFunction

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

the class BusinessObjectImportConfiguration method toJSON.

@Request
@Override
public JSONObject toJSON() {
    JSONObject config = new JSONObject();
    super.toJSON(config);
    SimpleDateFormat format = new SimpleDateFormat(BusinessObjectImportConfiguration.DATE_FORMAT);
    format.setTimeZone(GeoRegistryUtil.SYSTEM_TIMEZONE);
    JSONObject type = new JSONObject(this.type.toJSON(true).toString());
    JSONArray attributes = type.getJSONArray(GeoObjectType.JSON_ATTRIBUTES);
    for (int i = 0; i < attributes.length(); i++) {
        JSONObject attribute = attributes.getJSONObject(i);
        String attributeName = attribute.getString(AttributeType.JSON_CODE);
        if (this.functions.containsKey(attributeName)) {
            ShapefileFunction function = this.functions.get(attributeName);
            if (function instanceof LocalizedValueFunction) {
                String locale = attribute.getString("locale");
                ShapefileFunction localeFunction = ((LocalizedValueFunction) function).getFunction(locale);
                if (localeFunction != null) {
                    attribute.put(TARGET, localeFunction.toJson());
                }
            } else {
                attribute.put(TARGET, function.toJson());
            }
        }
    }
    JSONArray locations = new JSONArray();
    for (Location location : this.locations) {
        locations.put(location.toJSON());
    }
    config.put(BusinessObjectImportConfiguration.TYPE, type);
    config.put(BusinessObjectImportConfiguration.LOCATIONS, locations);
    if (this.getDate() != null) {
        config.put(BusinessObjectImportConfiguration.DATE, format.format(this.getDate()));
    }
    if (this.hierarchy != null) {
        config.put(BusinessObjectImportConfiguration.HIERARCHY, this.getHierarchy().getCode());
    }
    if (this.exclusions.size() > 0) {
        JSONArray exclusions = new JSONArray();
        this.exclusions.forEach((key, set) -> {
            set.forEach(value -> {
                JSONObject object = new JSONObject();
                object.put(AttributeType.JSON_CODE, key);
                object.put(VALUE, value);
                exclusions.put(object);
            });
        });
        config.put(EXCLUSIONS, exclusions);
    }
    return config;
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ShapefileFunction(net.geoprism.data.importer.ShapefileFunction) ConstantShapefileFunction(net.geoprism.registry.io.ConstantShapefileFunction) SimpleDateFormat(java.text.SimpleDateFormat) LocalizedValueFunction(net.geoprism.registry.io.LocalizedValueFunction) Location(net.geoprism.registry.io.Location) Request(com.runwaysdk.session.Request)

Example 9 with ShapefileFunction

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

the class BusinessObjectImporter method getCode.

/**
 * @param feature
 *          Shapefile feature
 *
 * @return The geoId as defined by the 'oid' attribute on the feature. If the
 *         geoId is null then a blank geoId is returned.
 */
protected String getCode(FeatureRow row) {
    ShapefileFunction function = this.configuration.getFunction(BusinessObject.CODE);
    if (function == null) {
        RequiredMappingException ex = new RequiredMappingException();
        ex.setAttributeLabel(this.configuration.getType().getAttribute(BusinessObject.CODE).getLabel().getValue());
        throw ex;
    }
    Object code = function.getValue(row);
    if (code != null) {
        return code.toString();
    }
    return null;
}
Also used : 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)

Example 10 with ShapefileFunction

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

ShapefileFunction (net.geoprism.data.importer.ShapefileFunction)14 JSONObject (org.json.JSONObject)10 VertexObject (com.runwaysdk.business.graph.VertexObject)7 GeoObject (org.commongeoregistry.adapter.dataaccess.GeoObject)7 RequiredMappingException (net.geoprism.registry.io.RequiredMappingException)6 Entry (java.util.Map.Entry)5 Transaction (com.runwaysdk.dataaccess.transaction.Transaction)4 Request (com.runwaysdk.session.Request)4 IgnoreRowException (net.geoprism.registry.io.IgnoreRowException)4 ServerGeoObjectIF (net.geoprism.registry.model.ServerGeoObjectIF)4 VertexServerGeoObject (net.geoprism.registry.model.graph.VertexServerGeoObject)4 LocalizedValue (org.commongeoregistry.adapter.dataaccess.LocalizedValue)4 AttributeType (org.commongeoregistry.adapter.metadata.AttributeType)4 Location (net.geoprism.registry.io.Location)3 BusinessObject (net.geoprism.registry.model.BusinessObject)3 ProblemException (com.runwaysdk.ProblemException)2 ProblemIF (com.runwaysdk.ProblemIF)2 Geometry (com.vividsolutions.jts.geom.Geometry)2 SimpleDateFormat (java.text.SimpleDateFormat)2 RowValidationProblem (net.geoprism.registry.etl.RowValidationProblem)2