use of net.geoprism.dhis2.dhis2adapter.exception.BadServerUriException in project geoprism-registry by terraframe.
the class HTTPConnector method httpGet.
public DHIS2Response httpGet(String url, List<NameValuePair> params) throws InvalidLoginException, HTTPException, BadServerUriException {
try {
if (!isInitialized()) {
initialize();
}
HttpGet get = new HttpGet(this.buildUri(url, params));
get.addHeader("Accept", "application/json");
try (CloseableHttpResponse response = client.execute(get, getContext())) {
return this.convertResponse(response);
}
} catch (URISyntaxException | IOException e) {
throw new HTTPException(e);
}
}
use of net.geoprism.dhis2.dhis2adapter.exception.BadServerUriException 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;
}
use of net.geoprism.dhis2.dhis2adapter.exception.BadServerUriException in project geoprism-registry by terraframe.
the class DHIS2FeatureService method getDHIS2Attributes.
private List<Attribute> getDHIS2Attributes(DHIS2TransportServiceIF dhis2) {
try {
MetadataGetResponse<Attribute> resp = dhis2.<Attribute>metadataGet(Attribute.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<Attribute> attrs = resp.getObjects();
attrs.addAll(buildDefaultDhis2OrgUnitAttributes());
return attrs;
} catch (InvalidLoginException e) {
LoginException cgrlogin = new LoginException(e);
throw cgrlogin;
} catch (HTTPException | BadServerUriException e) {
HttpError cgrhttp = new HttpError(e);
throw cgrhttp;
}
}
use of net.geoprism.dhis2.dhis2adapter.exception.BadServerUriException in project geoprism-registry by terraframe.
the class DHIS2ServiceFactory method instanceGetDhis2Service.
public synchronized DHIS2TransportServiceIF instanceGetDhis2Service(DHIS2ExternalSystem system) throws UnexpectedResponseException, InvalidLoginException, HTTPException, BadServerUriException {
if (this.dhis2 == null) {
HTTPConnector connector = new HTTPConnector();
connector.setServerUrl(system.getUrl());
connector.setCredentials(system.getUsername(), system.getPassword());
DHIS2TransportService dhis2 = new DHIS2TransportService(connector);
try {
dhis2.initialize();
if (dhis2.getVersionRemoteServerApi() > DHIS2FeatureService.LAST_TESTED_DHIS2_API_VERSION) {
Integer compatLayerVersion = dhis2.getVersionRemoteServerApi() - 2;
if (compatLayerVersion < DHIS2FeatureService.LAST_TESTED_DHIS2_API_VERSION) {
compatLayerVersion = DHIS2FeatureService.LAST_TESTED_DHIS2_API_VERSION;
}
dhis2.setVersionApiCompat(compatLayerVersion);
}
} catch (IncompatibleServerVersionException e) {
throw new ProgrammingErrorException(e);
}
return dhis2;
}
return this.dhis2;
}
use of net.geoprism.dhis2.dhis2adapter.exception.BadServerUriException in project geoprism-registry by terraframe.
the class DHIS2SynchronizationManager method synchronize.
public void synchronize() {
this.init();
final ExternalSystem es = dhis2Config.getSystem();
long rowIndex = 0;
long total = 0;
long exportCount = 0;
SortedSet<DHIS2SyncLevel> levels = dhis2Config.getLevels();
Boolean includeTranslations = LocalizationFacade.getInstalledLocales().size() > 0;
// First calculate the total number of records
HashMap<Integer, Long> countAtLevel = new HashMap<Integer, Long>();
int expectedLevel = 0;
for (DHIS2SyncLevel level : levels) {
if (level.getLevel() != expectedLevel) {
throw new ProgrammingErrorException("Unexpected level number [" + level.getLevel() + "].");
}
if (level.getSyncType() != null && !DHIS2SyncLevel.Type.NONE.equals(level.getSyncType())) {
long count = this.getCount(level.getGeoObjectType());
total += count;
countAtLevel.put(level.getLevel(), count);
}
expectedLevel++;
}
history.appLock();
history.setWorkTotal(total);
history.apply();
// Now do the work
for (DHIS2SyncLevel level : levels) {
if (level.getSyncType() != null && !DHIS2SyncLevel.Type.NONE.equals(level.getSyncType())) {
long skip = 0;
long pageSize = 1000;
long count = countAtLevel.get(level.getLevel());
while (skip < count) {
List<VertexServerGeoObject> objects = this.query(level.getGeoObjectType(), skip, pageSize);
for (VertexServerGeoObject go : objects) {
try {
this.exportGeoObject(dhis2Config, level, levels, rowIndex, go, includeTranslations);
exportCount++;
history.appLock();
history.setWorkProgress(rowIndex);
history.setExportedRecords(exportCount);
history.apply();
if (level.getOrgUnitGroupId() != null && level.getOrgUnitGroupId().length() > 0) {
final String externalId = go.getExternalId(es);
level.getOrCreateOrgUnitGroupIdSet(level.getOrgUnitGroupId()).add(externalId);
}
} catch (DHIS2SyncError ee) {
recordExportError(ee, history);
}
rowIndex++;
}
;
// Export OrgUnitGroup changes
if (level.getOrgUnitGroupIdSet().size() > 0) {
try {
Map<String, Set<String>> orgUnitGroupIdSet = level.getOrgUnitGroupIdSet();
// Fetch and populate all the org unit groups with the ids of org units that we will be exporting
MetadataGetResponse<OrganisationUnitGroup> resp = dhis2.metadataGet(OrganisationUnitGroup.class);
this.service.validateDhis2Response(resp);
List<OrganisationUnitGroup> orgUnitGroups = resp.getObjects();
if (orgUnitGroups != null) {
Iterator<? extends OrganisationUnitGroup> it = orgUnitGroups.iterator();
while (it.hasNext()) {
OrganisationUnitGroup group = it.next();
if (orgUnitGroupIdSet.containsKey(group.getId())) {
orgUnitGroupIdSet.get(group.getId()).addAll(group.getOrgUnitIds());
group.setOrgUnitIds(orgUnitGroupIdSet.get(group.getId()));
orgUnitGroupIdSet.remove(group.getId());
} else {
it.remove();
}
}
if (orgUnitGroups.size() > 0) {
JsonObject payload = new JsonObject();
JsonArray jaOrgUnitGroups = new JsonArray();
for (OrganisationUnitGroup group : orgUnitGroups) {
GsonBuilder builder = new GsonBuilder();
JsonObject joOrgUnitGroup = builder.create().toJsonTree(group, group.getClass()).getAsJsonObject();
joOrgUnitGroup.remove("created");
joOrgUnitGroup.remove("lastUpdated");
joOrgUnitGroup.remove("symbol");
joOrgUnitGroup.remove("publicAccess");
joOrgUnitGroup.remove("user");
joOrgUnitGroup.remove("userGroupAccesses");
joOrgUnitGroup.remove("attributeValues");
joOrgUnitGroup.remove("translations");
joOrgUnitGroup.remove("userAccesses");
jaOrgUnitGroups.add(joOrgUnitGroup);
}
payload.add(DHIS2Objects.ORGANISATION_UNIT_GROUPS, jaOrgUnitGroups);
List<NameValuePair> params = new ArrayList<NameValuePair>();
MetadataImportResponse resp2 = dhis2.metadataPost(params, new StringEntity(payload.toString(), Charset.forName("UTF-8")));
this.service.validateDhis2Response(resp2);
}
}
} catch (InvalidLoginException e) {
LoginException cgrlogin = new LoginException(e);
throw cgrlogin;
} catch (HTTPException | BadServerUriException e) {
HttpError cgrhttp = new HttpError(e);
throw cgrhttp;
}
}
skip += pageSize;
NotificationFacade.queue(new GlobalNotificationMessage(MessageType.DATA_EXPORT_JOB_CHANGE, null));
}
}
}
history.appLock();
history.setWorkProgress(rowIndex);
history.setExportedRecords(exportCount);
history.clearStage();
history.addStage(ExportStage.COMPLETE);
history.apply();
NotificationFacade.queue(new GlobalNotificationMessage(MessageType.DATA_EXPORT_JOB_CHANGE, null));
handleExportErrors();
}
Aggregations