use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.UPDATED in project dhis2-core by dhis2.
the class KeyJsonValueController method updateKeyJsonValue.
/**
* Update a key in the given namespace.
*/
@RequestMapping(value = "/{namespace}/{key}", method = RequestMethod.PUT, produces = "application/json", consumes = "application/json")
public void updateKeyJsonValue(@PathVariable String namespace, @PathVariable String key, @RequestBody String body, HttpServletRequest request, HttpServletResponse response) throws WebMessageException, IOException {
if (!hasAccess(namespace)) {
throw new WebMessageException(WebMessageUtils.forbidden("The namespace '" + namespace + "' is protected, and you don't have the right authority to access it."));
}
KeyJsonValue keyJsonValue = keyJsonValueService.getKeyJsonValue(namespace, key);
if (keyJsonValue == null) {
throw new WebMessageException(WebMessageUtils.notFound("The key '" + key + "' was not found in the namespace '" + namespace + "'."));
}
if (!renderService.isValidJson(body)) {
throw new WebMessageException(WebMessageUtils.badRequest("The data is not valid JSON."));
}
keyJsonValue.setValue(body);
keyJsonValueService.updateKeyJsonValue(keyJsonValue);
response.setStatus(HttpServletResponse.SC_OK);
messageService.sendJson(WebMessageUtils.ok("Key '" + key + "' updated."), response);
}
use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.UPDATED in project dhis2-core by dhis2.
the class DefaultSynchronizationManager method executeDataPush.
/**
* Executes a push of data values to the given remote instance.
*
* @param instance the remote system instance.
* @return an ImportSummary.
*/
private ImportSummary executeDataPush(SystemInstance instance) throws WebMessageParseException {
// ---------------------------------------------------------------------
// Set time for last success to start of process to make data saved
// subsequently part of next synch process without being ignored
// ---------------------------------------------------------------------
final Date startTime = new Date();
final Date lastSuccessTime = getLastDataSynchSuccessFallback();
final int lastUpdatedCount = dataValueService.getDataValueCountLastUpdatedAfter(lastSuccessTime, true);
log.info("Values: " + lastUpdatedCount + " since last synch success: " + lastSuccessTime);
if (lastUpdatedCount == 0) {
log.debug("Skipping synch, no new or updated data values");
return null;
}
log.info("Values: " + lastUpdatedCount + " since last synch success: " + lastSuccessTime);
log.info("Remote server POST URL: " + instance.getUrl());
final RequestCallback requestCallback = request -> {
request.getHeaders().setContentType(MediaType.APPLICATION_JSON);
request.getHeaders().add(HEADER_AUTHORIZATION, CodecUtils.getBasicAuthString(instance.getUsername(), instance.getPassword()));
dataValueSetService.writeDataValueSetJson(lastSuccessTime, request.getBody(), new IdSchemes());
};
ResponseExtractor<ImportSummary> responseExtractor = new ImportSummaryResponseExtractor();
ImportSummary summary = null;
try {
summary = restTemplate.execute(instance.getUrl(), HttpMethod.POST, requestCallback, responseExtractor);
} catch (HttpClientErrorException ex) {
String responseBody = ex.getResponseBodyAsString();
summary = WebMessageParseUtils.fromWebMessageResponse(responseBody, ImportSummary.class);
} catch (HttpServerErrorException ex) {
String responseBody = ex.getResponseBodyAsString();
log.error("Internal error happened during event data push: " + responseBody, ex);
throw ex;
} catch (ResourceAccessException ex) {
log.error("Exception during event data push: " + ex.getMessage(), ex);
throw ex;
}
log.info("Synch summary: " + summary);
if (summary != null && ImportStatus.SUCCESS.equals(summary.getStatus())) {
setLastDataSynchSuccess(startTime);
log.info("Synch successful, setting last success time: " + startTime);
} else {
log.warn("Sync failed: " + summary);
}
return summary;
}
use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.UPDATED in project dhis2-core by dhis2.
the class DefaultSynchronizationManager method executeEventPush.
@Override
public ImportSummaries executeEventPush() throws WebMessageParseException {
AvailabilityStatus availability = isRemoteServerAvailable();
if (!availability.isAvailable()) {
log.info("Aborting synch, server not available");
return null;
}
// ---------------------------------------------------------------------
// Set time for last success to start of process to make data saved
// subsequently part of next synch process without being ignored
// ---------------------------------------------------------------------
final Date startTime = new Date();
final Date lastSuccessTime = getLastEventSynchSuccessFallback();
int lastUpdatedEventsCount = eventService.getAnonymousEventValuesCountLastUpdatedAfter(lastSuccessTime);
log.info("Events: " + lastUpdatedEventsCount + " since last synch success: " + lastSuccessTime);
if (lastUpdatedEventsCount == 0) {
log.info("Skipping synch, no new or updated data values for events");
return null;
}
String url = systemSettingManager.getSystemSetting(SettingKey.REMOTE_INSTANCE_URL) + "/api/events";
log.info("Remote server events POST URL: " + url);
final String username = (String) systemSettingManager.getSystemSetting(SettingKey.REMOTE_INSTANCE_USERNAME);
final String password = (String) systemSettingManager.getSystemSetting(SettingKey.REMOTE_INSTANCE_PASSWORD);
final RequestCallback requestCallback = new RequestCallback() {
@Override
public void doWithRequest(ClientHttpRequest request) throws IOException {
request.getHeaders().setContentType(MediaType.APPLICATION_JSON);
request.getHeaders().add(HEADER_AUTHORIZATION, CodecUtils.getBasicAuthString(username, password));
Events result = eventService.getAnonymousEventValuesLastUpdatedAfter(lastSuccessTime);
renderService.toJson(request.getBody(), result);
}
};
ResponseExtractor<ImportSummaries> responseExtractor = new ImportSummariesResponseExtractor();
ImportSummaries summaries = null;
try {
summaries = restTemplate.execute(url, HttpMethod.POST, requestCallback, responseExtractor);
} catch (HttpClientErrorException ex) {
String responseBody = ex.getResponseBodyAsString();
summaries = WebMessageParseUtils.fromWebMessageResponse(responseBody, ImportSummaries.class);
} catch (HttpServerErrorException ex) {
String responseBody = ex.getResponseBodyAsString();
log.error("Internal error happened during event data push: " + responseBody, ex);
throw ex;
} catch (ResourceAccessException ex) {
log.error("Exception during event data push: " + ex.getMessage(), ex);
throw ex;
}
log.info("Event synch summary: " + summaries);
boolean isError = false;
if (summaries != null) {
for (ImportSummary summary : summaries.getImportSummaries()) {
if (ImportStatus.ERROR.equals(summary.getStatus()) || ImportStatus.WARNING.equals(summary.getStatus())) {
isError = true;
log.debug("Sync failed: " + summaries);
break;
}
}
}
if (!isError) {
setLastEventSynchSuccess(startTime);
log.info("Synch successful, setting last success time: " + startTime);
}
return summaries;
}
use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.UPDATED in project dhis2-core by dhis2.
the class AnalyticsUtils method getDataValueSetFromGrid.
/**
* Generates a data value set based on the given grid with aggregated data.
* Sets the created and last updated fields to the current date.
*
* @param params the data query parameters.
* @param grid the grid.
* @return a data value set.
*/
public static DataValueSet getDataValueSetFromGrid(DataQueryParams params, Grid grid) {
int dxInx = grid.getIndexOfHeader(DATA_X_DIM_ID);
int peInx = grid.getIndexOfHeader(PERIOD_DIM_ID);
int ouInx = grid.getIndexOfHeader(ORGUNIT_DIM_ID);
int coInx = grid.getIndexOfHeader(CATEGORYOPTIONCOMBO_DIM_ID);
int aoInx = grid.getIndexOfHeader(ATTRIBUTEOPTIONCOMBO_DIM_ID);
int vlInx = grid.getWidth() - 1;
Assert.isTrue(dxInx >= 0, "Data dimension index must be greater than or equal to zero");
Assert.isTrue(peInx >= 0, "Period dimension index must be greater than or equal to zero");
Assert.isTrue(ouInx >= 0, "Org unit dimension index must be greater than or equal to zero");
Assert.isTrue(coInx >= 0, "Category option combo dimension index must be greater than or equal to zero");
Assert.isTrue(aoInx >= 0, "Attribute option combo dimension index must be greater than or equal to zero");
Assert.isTrue(vlInx >= 0, "Value index must be greater than or equal to zero");
String created = DateUtils.getMediumDateString();
DataValueSet dvs = new DataValueSet();
Set<String> primaryKeys = Sets.newHashSet();
for (List<Object> row : grid.getRows()) {
DataValue dv = new DataValue();
Object coc = row.get(coInx);
Object aoc = row.get(aoInx);
dv.setDataElement(String.valueOf(row.get(dxInx)));
dv.setPeriod(String.valueOf(row.get(peInx)));
dv.setOrgUnit(String.valueOf(row.get(ouInx)));
dv.setCategoryOptionCombo(coc != null ? String.valueOf(coc) : null);
dv.setAttributeOptionCombo(aoc != null ? String.valueOf(aoc) : null);
dv.setValue(String.valueOf(row.get(vlInx)));
dv.setComment(KEY_AGG_VALUE);
dv.setStoredBy(KEY_AGG_VALUE);
dv.setCreated(created);
dv.setLastUpdated(created);
if (!params.isDuplicatesOnly() || !primaryKeys.add(dv.getPrimaryKey())) {
dvs.getDataValues().add(dv);
}
}
return dvs;
}
use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.UPDATED in project dhis2-core by dhis2.
the class DefaultCompleteDataSetRegistrationExchangeService method saveCompleteDataSetRegistrations.
private ImportSummary saveCompleteDataSetRegistrations(ImportOptions importOptions, TaskId id, CompleteDataSetRegistrations completeRegistrations) {
Clock clock = new Clock(log).startClock().logTime("Starting complete data set registration import, options: " + importOptions);
notifier.clear(id).notify(id, "Process started");
// Start here so we can access any outer attributes for the configuration
completeRegistrations.open();
ImportSummary importSummary = new ImportSummary();
// ---------------------------------------------------------------------
// Set up import configuration
// ---------------------------------------------------------------------
importOptions = importOptions != null ? importOptions : ImportOptions.getDefaultImportOptions();
log.info("Import options: " + importOptions);
ImportConfig cfg = new ImportConfig(completeRegistrations, importOptions);
// ---------------------------------------------------------------------
// Set up meta-data
// ---------------------------------------------------------------------
MetaDataCaches caches = new MetaDataCaches();
MetaDataCallables metaDataCallables = new MetaDataCallables(cfg);
if (importOptions.isPreheatCacheDefaultFalse()) {
caches.preheat(idObjManager, cfg);
}
// ---------------------------------------------------------------------
// Perform import
// ---------------------------------------------------------------------
notifier.notify(id, "Importing complete data set registrations");
int totalCount = batchImport(completeRegistrations, cfg, importSummary, metaDataCallables, caches);
notifier.notify(id, NotificationLevel.INFO, "Import done", true).addTaskSummary(id, importSummary);
ImportCount count = importSummary.getImportCount();
clock.logTime(String.format("Complete data set registration import done, total: %d, imported: %d, updated: %d, deleted: %d", totalCount, count.getImported(), count.getUpdated(), count.getDeleted()));
completeRegistrations.close();
return importSummary;
}
Aggregations