Search in sources :

Example 1 with HttpError

use of net.geoprism.registry.etl.export.HttpError in project geoprism-registry by terraframe.

the class DHIS2SynchronizationManager method init.

// https://play.dhis2.org/2.35.1/api/metadata.xml?organisationUnits=true&filter=level:eq:1
private void init() {
    if (this.ouLevel1 != null) {
        return;
    }
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("filter", "level:eq:1"));
    try {
        MetadataGetResponse<OrganisationUnit> resp = this.dhis2.metadataGet(OrganisationUnit.class, params);
        this.service.validateDhis2Response(resp);
        List<OrganisationUnit> orgUnits = resp.getObjects();
        this.ouLevel1 = orgUnits;
    } catch (InvalidLoginException e) {
        LoginException cgrlogin = new LoginException(e);
        throw cgrlogin;
    } catch (HTTPException | BadServerUriException e) {
        HttpError cgrhttp = new HttpError(e);
        throw cgrhttp;
    }
}
Also used : NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) OrganisationUnit(net.geoprism.dhis2.dhis2adapter.response.model.OrganisationUnit) HTTPException(net.geoprism.dhis2.dhis2adapter.exception.HTTPException) ArrayList(java.util.ArrayList) BadServerUriException(net.geoprism.dhis2.dhis2adapter.exception.BadServerUriException) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) InvalidLoginException(net.geoprism.dhis2.dhis2adapter.exception.InvalidLoginException) LoginException(net.geoprism.registry.etl.export.LoginException) InvalidLoginException(net.geoprism.dhis2.dhis2adapter.exception.InvalidLoginException) HttpError(net.geoprism.registry.etl.export.HttpError)

Example 2 with HttpError

use of net.geoprism.registry.etl.export.HttpError in project geoprism-registry by terraframe.

the class DHIS2OptionCache method init.

private void init() {
    try {
        MetadataGetResponse<OptionSet> resp = dhis2.<OptionSet>metadataGet(OptionSet.class);
        if (!resp.isSuccess()) {
            // if (resp.hasMessage())
            // {
            // ExportRemoteException ere = new ExportRemoteException();
            // ere.setRemoteError(resp.getMessage());
            // throw ere;
            // }
            // else
            // {
            UnexpectedRemoteResponse re = new UnexpectedRemoteResponse();
            throw re;
        // }
        }
        List<OptionSet> objects = resp.getObjects();
        this.optionSets = new HashMap<String, IntegratedOptionSet>(objects.size());
        for (OptionSet os : objects) {
            this.optionSets.put(os.getId(), new IntegratedOptionSet(os));
        }
        if (this.optionSets.size() > 0) {
            MetadataGetResponse<Option> resp2 = dhis2.<Option>metadataGet(Option.class);
            if (!resp2.isSuccess()) {
                // if (resp.hasMessage())
                // {
                // ExportRemoteException ere = new ExportRemoteException();
                // ere.setRemoteError(resp.getMessage());
                // throw ere;
                // }
                // else
                // {
                UnexpectedRemoteResponse re = new UnexpectedRemoteResponse();
                throw re;
            // }
            }
            List<Option> options = resp2.getObjects();
            for (Option option : options) {
                if (option.getOptionSetId() != null && this.optionSets.containsKey(option.getOptionSetId())) {
                    this.optionSets.get(option.getOptionSetId()).addOption(option);
                }
            }
        }
    } catch (InvalidLoginException e) {
        LoginException cgrlogin = new LoginException(e);
        throw cgrlogin;
    } catch (HTTPException e) {
        HttpError cgrhttp = new HttpError(e);
        throw cgrhttp;
    } catch (BadServerUriException e) {
        HttpError cgrhttp = new HttpError(e);
        throw cgrhttp;
    }
}
Also used : HTTPException(net.geoprism.dhis2.dhis2adapter.exception.HTTPException) BadServerUriException(net.geoprism.dhis2.dhis2adapter.exception.BadServerUriException) UnexpectedRemoteResponse(net.geoprism.registry.etl.export.UnexpectedRemoteResponse) InvalidLoginException(net.geoprism.dhis2.dhis2adapter.exception.InvalidLoginException) LoginException(net.geoprism.registry.etl.export.LoginException) InvalidLoginException(net.geoprism.dhis2.dhis2adapter.exception.InvalidLoginException) Option(net.geoprism.dhis2.dhis2adapter.response.model.Option) HttpError(net.geoprism.registry.etl.export.HttpError) OptionSet(net.geoprism.dhis2.dhis2adapter.response.model.OptionSet)

Example 3 with HttpError

use of net.geoprism.registry.etl.export.HttpError in project geoprism-registry by terraframe.

the class FhirExportSynchronizationManager method synchronize.

public void synchronize() {
    final FhirExternalSystem system = (FhirExternalSystem) this.config.getSystem();
    try (FhirConnection connection = FhirConnectionFactory.get(system)) {
        SortedSet<FhirSyncLevel> levels = this.config.getLevels();
        int expectedLevel = 0;
        long exportCount = 0;
        for (FhirSyncLevel level : levels) {
            if (level.getLevel() != expectedLevel) {
                throw new ProgrammingErrorException("Unexpected level number [" + level.getLevel() + "].");
            }
            history.appLock();
            history.setWorkProgress((long) expectedLevel);
            history.setExportedRecords(exportCount);
            history.apply();
            ListTypeVersion version = ListTypeVersion.get(level.getVersionId());
            FhirDataPopulator populator = FhirFactory.getPopulator(level.getImplementation());
            ListTypeFhirExporter exporter = new ListTypeFhirExporter(version, connection, populator, true);
            long results = exporter.export();
            exportCount += results;
            expectedLevel++;
        }
        history.appLock();
        history.setWorkTotal((long) expectedLevel);
        history.setWorkProgress((long) expectedLevel);
        history.setExportedRecords(exportCount);
        history.clearStage();
        history.addStage(ExportStage.COMPLETE);
        history.apply();
        NotificationFacade.queue(new GlobalNotificationMessage(MessageType.DATA_EXPORT_JOB_CHANGE, null));
        handleExportErrors();
    } catch (Exception e) {
        throw new HttpError(e);
    }
}
Also used : FhirSyncLevel(net.geoprism.registry.etl.FhirSyncLevel) ListTypeVersion(net.geoprism.registry.ListTypeVersion) FhirExternalSystem(net.geoprism.registry.graph.FhirExternalSystem) ProgrammingErrorException(com.runwaysdk.dataaccess.ProgrammingErrorException) ProgrammingErrorException(com.runwaysdk.dataaccess.ProgrammingErrorException) DataFormatException(ca.uhn.fhir.parser.DataFormatException) IOException(java.io.IOException) HttpError(net.geoprism.registry.etl.export.HttpError) GlobalNotificationMessage(net.geoprism.registry.ws.GlobalNotificationMessage)

Example 4 with HttpError

use of net.geoprism.registry.etl.export.HttpError in project geoprism-registry by terraframe.

the class FhirExportSynchronizationManager method writeToFile.

public File writeToFile() throws IOException {
    final FhirExternalSystem system = (FhirExternalSystem) this.config.getSystem();
    try (FhirConnection connection = FhirConnectionFactory.get(system)) {
        String name = SessionPredicate.generateId();
        File root = new File(new File(VaultProperties.getPath("vault.default"), "files"), name);
        root.mkdirs();
        Bundle bundle = this.generateBundle(connection);
        FhirContext ctx = FhirContext.forR4();
        IParser parser = ctx.newJsonParser();
        try {
            parser.encodeResourceToWriter(bundle, new FileWriter(new File(root, "bundle.json")));
        } catch (DataFormatException | IOException e) {
            throw new ProgrammingErrorException(e);
        }
        return root;
    } catch (Exception e) {
        throw new HttpError(e);
    }
}
Also used : FhirContext(ca.uhn.fhir.context.FhirContext) Bundle(org.hl7.fhir.r4.model.Bundle) FileWriter(java.io.FileWriter) IOException(java.io.IOException) FhirExternalSystem(net.geoprism.registry.graph.FhirExternalSystem) ProgrammingErrorException(com.runwaysdk.dataaccess.ProgrammingErrorException) ProgrammingErrorException(com.runwaysdk.dataaccess.ProgrammingErrorException) DataFormatException(ca.uhn.fhir.parser.DataFormatException) IOException(java.io.IOException) DataFormatException(ca.uhn.fhir.parser.DataFormatException) HttpError(net.geoprism.registry.etl.export.HttpError) File(java.io.File) IParser(ca.uhn.fhir.parser.IParser)

Example 5 with HttpError

use of net.geoprism.registry.etl.export.HttpError in project geoprism-registry by terraframe.

the class DHIS2FeatureService method getDHIS2AttributeConfiguration.

@Request(RequestType.SESSION)
public JsonArray getDHIS2AttributeConfiguration(String sessionId, String dhis2SystemOid, String geoObjectTypeCode) {
    DHIS2ExternalSystem system = DHIS2ExternalSystem.get(dhis2SystemOid);
    JsonArray response = new JsonArray();
    ServerGeoObjectType got = ServerGeoObjectType.get(geoObjectTypeCode);
    Map<String, AttributeType> cgrAttrs = got.getAttributeMap();
    DHIS2TransportServiceIF dhis2;
    try {
        dhis2 = DHIS2ServiceFactory.buildDhis2TransportService(system);
    } catch (InvalidLoginException e) {
        LoginException cgrlogin = new LoginException(e);
        throw cgrlogin;
    } catch (HTTPException | UnexpectedResponseException | BadServerUriException | IllegalArgumentException e) {
        HttpError cgrhttp = new HttpError(e);
        throw cgrhttp;
    }
    final DHIS2OptionCache optionCache = new DHIS2OptionCache(dhis2);
    List<Attribute> dhis2Attrs = getDHIS2Attributes(dhis2);
    final String[] skipAttrs = new String[] { DefaultAttribute.GEOMETRY.getName(), DefaultAttribute.SEQUENCE.getName(), DefaultAttribute.TYPE.getName() };
    for (AttributeType cgrAttr : cgrAttrs.values()) {
        if (!ArrayUtils.contains(skipAttrs, cgrAttr.getName())) {
            JsonObject joAttr = new JsonObject();
            JsonObject joCgrAttr = new JsonObject();
            joCgrAttr.addProperty("name", cgrAttr.getName());
            joCgrAttr.addProperty("label", cgrAttr.getLabel().getValue());
            joCgrAttr.addProperty("type", cgrAttr.getType());
            joCgrAttr.addProperty("typeLabel", AttributeTypeMetadata.get().getTypeEnumDisplayLabel(cgrAttr.getType()));
            joAttr.add("cgrAttr", joCgrAttr);
            JsonArray jaStrategies = new JsonArray();
            List<DHIS2AttributeMapping> strategies = this.getMappingStrategies(cgrAttr);
            for (DHIS2AttributeMapping strategy : strategies) {
                jaStrategies.add(strategy.getClass().getName());
            }
            joAttr.add("attributeMappingStrategies", jaStrategies);
            JsonArray jaDhis2Attrs = new JsonArray();
            for (Attribute dhis2Attr : dhis2Attrs) {
                if (!dhis2Attr.getOrganisationUnitAttribute()) {
                    continue;
                }
                boolean valid = false;
                JsonObject jo = new JsonObject();
                if (cgrAttr instanceof AttributeBooleanType && dhis2Attr.getOptionSetId() == null && (dhis2Attr.getValueType().equals(ValueType.BOOLEAN) || dhis2Attr.getValueType().equals(ValueType.TRUE_ONLY))) {
                    valid = true;
                } else if (cgrAttr instanceof AttributeIntegerType && dhis2Attr.getOptionSetId() == null && (dhis2Attr.getValueType().equals(ValueType.INTEGER) || dhis2Attr.getValueType().equals(ValueType.INTEGER_POSITIVE) || dhis2Attr.getValueType().equals(ValueType.INTEGER_NEGATIVE) || dhis2Attr.getValueType().equals(ValueType.INTEGER_ZERO_OR_POSITIVE))) {
                    valid = true;
                } else if (cgrAttr instanceof AttributeFloatType && dhis2Attr.getOptionSetId() == null && (dhis2Attr.getValueType().equals(ValueType.NUMBER) || dhis2Attr.getValueType().equals(ValueType.UNIT_INTERVAL) || dhis2Attr.getValueType().equals(ValueType.PERCENTAGE))) {
                    valid = true;
                } else if (cgrAttr instanceof AttributeDateType && dhis2Attr.getOptionSetId() == null && (dhis2Attr.getValueType().equals(ValueType.DATE) || dhis2Attr.getValueType().equals(ValueType.DATETIME) || dhis2Attr.getValueType().equals(ValueType.TIME) || dhis2Attr.getValueType().equals(ValueType.AGE))) {
                    valid = true;
                } else if (cgrAttr instanceof AttributeTermType && dhis2Attr.getOptionSetId() != null) {
                    valid = true;
                    JsonArray jaDhis2Options = new JsonArray();
                    IntegratedOptionSet set = optionCache.getOptionSet(dhis2Attr.getOptionSetId());
                    SortedSet<Option> options = set.getOptions();
                    for (Option option : options) {
                        JsonObject joDhis2Option = new JsonObject();
                        joDhis2Option.addProperty("code", option.getCode());
                        joDhis2Option.addProperty("name", option.getName());
                        joDhis2Option.addProperty("id", option.getName());
                        jaDhis2Options.add(joDhis2Option);
                    }
                    jo.add("options", jaDhis2Options);
                } else if ((cgrAttr instanceof AttributeCharacterType || cgrAttr instanceof AttributeLocalType) && dhis2Attr.getOptionSetId() == null && (dhis2Attr.getValueType().equals(ValueType.TEXT) || dhis2Attr.getValueType().equals(ValueType.LONG_TEXT) || dhis2Attr.getValueType().equals(ValueType.LETTER) || dhis2Attr.getValueType().equals(ValueType.PHONE_NUMBER) || dhis2Attr.getValueType().equals(ValueType.EMAIL) || dhis2Attr.getValueType().equals(ValueType.USERNAME) || dhis2Attr.getValueType().equals(ValueType.URL))) {
                    valid = true;
                }
                if (valid) {
                    jo.addProperty("dhis2Id", dhis2Attr.getId());
                    jo.addProperty("code", dhis2Attr.getCode());
                    jo.addProperty("name", dhis2Attr.getName());
                    jaDhis2Attrs.add(jo);
                }
            }
            joAttr.add("dhis2Attrs", jaDhis2Attrs);
            if (cgrAttr instanceof AttributeTermType) {
                JsonArray terms = new JsonArray();
                List<Term> children = ((AttributeTermType) cgrAttr).getTerms();
                for (Term child : children) {
                    JsonObject joTerm = new JsonObject();
                    joTerm.addProperty("label", child.getLabel().getValue());
                    joTerm.addProperty("code", child.getCode());
                    terms.add(joTerm);
                }
                joAttr.add("terms", terms);
            }
            response.add(joAttr);
        }
    }
    return response;
}
Also used : HTTPException(net.geoprism.dhis2.dhis2adapter.exception.HTTPException) AttributeIntegerType(org.commongeoregistry.adapter.metadata.AttributeIntegerType) Attribute(net.geoprism.dhis2.dhis2adapter.response.model.Attribute) DefaultAttribute(org.commongeoregistry.adapter.constants.DefaultAttribute) JsonObject(com.google.gson.JsonObject) DHIS2AttributeMapping(net.geoprism.registry.etl.DHIS2AttributeMapping) AttributeFloatType(org.commongeoregistry.adapter.metadata.AttributeFloatType) AttributeDateType(org.commongeoregistry.adapter.metadata.AttributeDateType) AttributeType(org.commongeoregistry.adapter.metadata.AttributeType) AttributeBooleanType(org.commongeoregistry.adapter.metadata.AttributeBooleanType) DHIS2TransportServiceIF(net.geoprism.registry.etl.export.dhis2.DHIS2TransportServiceIF) DHIS2ExternalSystem(net.geoprism.registry.graph.DHIS2ExternalSystem) ServerGeoObjectType(net.geoprism.registry.model.ServerGeoObjectType) Term(org.commongeoregistry.adapter.Term) BadServerUriException(net.geoprism.dhis2.dhis2adapter.exception.BadServerUriException) IntegratedOptionSet(net.geoprism.registry.etl.export.dhis2.DHIS2OptionCache.IntegratedOptionSet) JsonArray(com.google.gson.JsonArray) DHIS2OptionCache(net.geoprism.registry.etl.export.dhis2.DHIS2OptionCache) AttributeLocalType(org.commongeoregistry.adapter.metadata.AttributeLocalType) InvalidLoginException(net.geoprism.dhis2.dhis2adapter.exception.InvalidLoginException) LoginException(net.geoprism.registry.etl.export.LoginException) InvalidLoginException(net.geoprism.dhis2.dhis2adapter.exception.InvalidLoginException) UnexpectedResponseException(net.geoprism.dhis2.dhis2adapter.exception.UnexpectedResponseException) Option(net.geoprism.dhis2.dhis2adapter.response.model.Option) HttpError(net.geoprism.registry.etl.export.HttpError) AttributeTermType(org.commongeoregistry.adapter.metadata.AttributeTermType) AttributeCharacterType(org.commongeoregistry.adapter.metadata.AttributeCharacterType) Request(com.runwaysdk.session.Request)

Aggregations

HttpError (net.geoprism.registry.etl.export.HttpError)11 HTTPException (net.geoprism.dhis2.dhis2adapter.exception.HTTPException)7 InvalidLoginException (net.geoprism.dhis2.dhis2adapter.exception.InvalidLoginException)7 LoginException (net.geoprism.registry.etl.export.LoginException)7 BadServerUriException (net.geoprism.dhis2.dhis2adapter.exception.BadServerUriException)6 JsonArray (com.google.gson.JsonArray)4 JsonObject (com.google.gson.JsonObject)4 ProgrammingErrorException (com.runwaysdk.dataaccess.ProgrammingErrorException)4 ArrayList (java.util.ArrayList)4 NameValuePair (org.apache.http.NameValuePair)4 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)4 IOException (java.io.IOException)3 GlobalNotificationMessage (net.geoprism.registry.ws.GlobalNotificationMessage)3 DataFormatException (ca.uhn.fhir.parser.DataFormatException)2 GsonBuilder (com.google.gson.GsonBuilder)2 Request (com.runwaysdk.session.Request)2 Date (java.util.Date)2 UnexpectedResponseException (net.geoprism.dhis2.dhis2adapter.exception.UnexpectedResponseException)2 Attribute (net.geoprism.dhis2.dhis2adapter.response.model.Attribute)2 Option (net.geoprism.dhis2.dhis2adapter.response.model.Option)2