Search in sources :

Example 11 with ServerGeoObjectIF

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

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

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

the class GeoObjectImporter 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 + GeoObjectImporter.refreshSessionRecordCount) < curWorkProgress) {
            SessionFacade.renewSession(Session.getCurrentSession().getOid());
            this.lastValidateSessionRefresh = curWorkProgress;
        }
        try {
            /*
         * 1. Check for location problems
         */
            if (this.configuration.isPostalCode() && PostalCodeFactory.isAvailable(this.configuration.getType())) {
            // Skip location synonym check
            } else if (this.configuration.getHierarchy() != null && this.configuration.getLocations().size() > 0) {
                this.getParent(row);
            }
            /*
         * 2. Check for serialization and term problems
         */
            String code = this.getCode(row);
            ServerGeoObjectIF entity;
            if (code == null || code.length() <= 0) {
                RequiredMappingException ex = new RequiredMappingException();
                ex.setAttributeLabel(GeoObjectTypeMetadata.getAttributeDisplayLabel(DefaultAttribute.CODE.getName()));
                throw ex;
            }
            entity = service.newInstance(this.configuration.getType());
            entity.setCode(code);
            entity.setInvalid(false);
            try {
                LocalizedValue entityName = this.getName(row);
                if (entityName != null && this.hasValue(entityName)) {
                    entity.setDisplayLabel(entityName, this.configuration.getStartDate(), this.configuration.getEndDate());
                }
                Geometry geometry = (Geometry) this.getFormatSpecificImporter().getGeometry(row);
                if (geometry != null) {
                    // geometry.getSRID().
                    if (geometry.isValid()) {
                        entity.setGeometry(geometry, this.configuration.getStartDate(), this.configuration.getEndDate());
                    } else {
                        InvalidGeometryException geomEx = new InvalidGeometryException();
                        throw geomEx;
                    }
                }
                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();
                    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);
                            }
                        }
                    }
                }
                GeoObjectOverTime go = entity.toGeoObjectOverTime(false);
                go.toJSON().toString();
                if (this.configuration.isExternalImport()) {
                    ShapefileFunction function = this.configuration.getExternalIdFunction();
                    Object value = function.getValue(row);
                    if (value == null || !(value instanceof String || value instanceof Integer || value instanceof Long) || (value instanceof String && ((String) value).length() == 0)) {
                        throw new InvalidExternalIdException();
                    }
                }
            } finally {
                entity.unlock();
            }
        } 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 : InvalidExternalIdException(net.geoprism.registry.etl.InvalidExternalIdException) ServerGeoObjectIF(net.geoprism.registry.model.ServerGeoObjectIF) GeoObjectOverTime(org.commongeoregistry.adapter.dataaccess.GeoObjectOverTime) IgnoreRowException(net.geoprism.registry.io.IgnoreRowException) Geometry(com.vividsolutions.jts.geom.Geometry) Entry(java.util.Map.Entry) LocalizedValue(org.commongeoregistry.adapter.dataaccess.LocalizedValue) 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) VertexServerGeoObject(net.geoprism.registry.model.graph.VertexServerGeoObject) VertexObject(com.runwaysdk.business.graph.VertexObject) RowValidationProblem(net.geoprism.registry.etl.RowValidationProblem) InvalidGeometryException(net.geoprism.registry.io.InvalidGeometryException) Transaction(com.runwaysdk.dataaccess.transaction.Transaction)

Example 14 with ServerGeoObjectIF

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

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

the class GeoObjectExcelExporter method createWorkbook.

public Workbook createWorkbook() throws IOException {
    Workbook workbook = new XSSFWorkbook();
    Sheet sheet = workbook.createSheet(WorkbookUtil.createSafeSheetName(this.type.getLabel().getValue()));
    CreationHelper createHelper = workbook.getCreationHelper();
    Font font = workbook.createFont();
    font.setBold(true);
    CellStyle boldStyle = workbook.createCellStyle();
    boldStyle.setFont(font);
    CellStyle dateStyle = workbook.createCellStyle();
    dateStyle.setDataFormat(createHelper.createDataFormat().getFormat(BuiltinFormats.getBuiltinFormat(14)));
    Row header = sheet.createRow(0);
    boolean includeCoordinates = this.type.getGeometryType().equals(GeometryType.POINT) || this.type.getGeometryType().equals(GeometryType.MIXED);
    Collection<AttributeType> attributes = new ImportAttributeSerializer(Session.getCurrentLocale(), includeCoordinates, true, locales).attributes(this.type.getType());
    // Get the ancestors of the type
    List<GeoObjectType> dtoAncestors = this.type.getTypeAncestors(this.hierarchy, true);
    List<ServerGeoObjectType> ancestors = new LinkedList<ServerGeoObjectType>();
    for (GeoObjectType ancestor : dtoAncestors) {
        ancestors.add(ServerGeoObjectType.get(ancestor));
    }
    this.writeHeader(boldStyle, header, attributes, ancestors);
    for (int i = 0; i < this.objects.size(); i++) {
        ServerGeoObjectIF object = this.objects.get(i);
        Row row = sheet.createRow(i + 1);
        this.writeRow(row, object, attributes, ancestors, dateStyle);
    }
    return workbook;
}
Also used : ImportAttributeSerializer(net.geoprism.registry.io.ImportAttributeSerializer) ServerGeoObjectIF(net.geoprism.registry.model.ServerGeoObjectIF) ServerGeoObjectType(net.geoprism.registry.model.ServerGeoObjectType) CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) Font(org.apache.poi.ss.usermodel.Font) LinkedList(java.util.LinkedList) Point(com.vividsolutions.jts.geom.Point) ServerGeoObjectType(net.geoprism.registry.model.ServerGeoObjectType) GeoObjectType(org.commongeoregistry.adapter.metadata.GeoObjectType) AttributeType(org.commongeoregistry.adapter.metadata.AttributeType) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) CellStyle(org.apache.poi.ss.usermodel.CellStyle) Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet)

Aggregations

ServerGeoObjectIF (net.geoprism.registry.model.ServerGeoObjectIF)106 Request (com.runwaysdk.session.Request)68 Test (org.junit.Test)43 ServerGeoObjectType (net.geoprism.registry.model.ServerGeoObjectType)28 ServerGeoObjectService (net.geoprism.registry.geoobject.ServerGeoObjectService)23 ValueOverTime (com.runwaysdk.dataaccess.graph.attributes.ValueOverTime)18 ValueOverTimeCollection (com.runwaysdk.dataaccess.graph.attributes.ValueOverTimeCollection)18 ServerHierarchyType (net.geoprism.registry.model.ServerHierarchyType)16 Date (java.util.Date)15 GeoObject (org.commongeoregistry.adapter.dataaccess.GeoObject)15 Transaction (com.runwaysdk.dataaccess.transaction.Transaction)13 LinkedList (java.util.LinkedList)11 OAuthClientRequest (org.apache.oltu.oauth2.client.request.OAuthClientRequest)10 LocalizedValue (org.commongeoregistry.adapter.dataaccess.LocalizedValue)10 AttributeType (org.commongeoregistry.adapter.metadata.AttributeType)10 JsonObject (com.google.gson.JsonObject)9 VertexObject (com.runwaysdk.business.graph.VertexObject)9 ChangeRequest (net.geoprism.registry.action.ChangeRequest)9 ServerParentTreeNode (net.geoprism.registry.model.ServerParentTreeNode)9 VertexServerGeoObject (net.geoprism.registry.model.graph.VertexServerGeoObject)9