Search in sources :

Example 1 with LocalizedValueFunction

use of net.geoprism.registry.io.LocalizedValueFunction 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;
}
Also used : ServerHierarchyType(net.geoprism.registry.model.ServerHierarchyType) ConstantShapefileFunction(net.geoprism.registry.io.ConstantShapefileFunction) BasicColumnFunction(net.geoprism.data.importer.BasicColumnFunction) ServerGeoObjectType(net.geoprism.registry.model.ServerGeoObjectType) JSONArray(org.json.JSONArray) BusinessType(net.geoprism.registry.BusinessType) LocalizedValueFunction(net.geoprism.registry.io.LocalizedValueFunction) ProgrammingErrorException(com.runwaysdk.dataaccess.ProgrammingErrorException) JSONObject(org.json.JSONObject) ParseException(java.text.ParseException) ParentMatchStrategy(net.geoprism.registry.io.ParentMatchStrategy) SimpleDateFormat(java.text.SimpleDateFormat) Location(net.geoprism.registry.io.Location) Request(com.runwaysdk.session.Request)

Example 2 with LocalizedValueFunction

use of net.geoprism.registry.io.LocalizedValueFunction 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)

Aggregations

Request (com.runwaysdk.session.Request)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ConstantShapefileFunction (net.geoprism.registry.io.ConstantShapefileFunction)2 LocalizedValueFunction (net.geoprism.registry.io.LocalizedValueFunction)2 Location (net.geoprism.registry.io.Location)2 JSONArray (org.json.JSONArray)2 JSONObject (org.json.JSONObject)2 ProgrammingErrorException (com.runwaysdk.dataaccess.ProgrammingErrorException)1 ParseException (java.text.ParseException)1 BasicColumnFunction (net.geoprism.data.importer.BasicColumnFunction)1 ShapefileFunction (net.geoprism.data.importer.ShapefileFunction)1 BusinessType (net.geoprism.registry.BusinessType)1 ParentMatchStrategy (net.geoprism.registry.io.ParentMatchStrategy)1 ServerGeoObjectType (net.geoprism.registry.model.ServerGeoObjectType)1 ServerHierarchyType (net.geoprism.registry.model.ServerHierarchyType)1