use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectReader in project dhis2-core by dhis2.
the class DefaultCsvEventService method readEvents.
@Override
public List<Event> readEvents(InputStream inputStream, boolean skipFirst) throws IOException, ParseException {
List<Event> events = Lists.newArrayList();
ObjectReader reader = CSV_MAPPER.readerFor(CsvEventDataValue.class).with(CSV_SCHEMA.withSkipFirstDataRow(skipFirst));
MappingIterator<CsvEventDataValue> iterator = reader.readValues(inputStream);
Event event = new Event();
event.setEvent("not_valid");
while (iterator.hasNext()) {
CsvEventDataValue dataValue = iterator.next();
if (!event.getEvent().equals(dataValue.getEvent())) {
event = new Event();
event.setEvent(dataValue.getEvent());
event.setStatus(StringUtils.isEmpty(dataValue.getStatus()) ? EventStatus.ACTIVE : Enum.valueOf(EventStatus.class, dataValue.getStatus()));
event.setProgram(dataValue.getProgram());
event.setProgramStage(dataValue.getProgramStage());
event.setEnrollment(dataValue.getEnrollment());
event.setOrgUnit(dataValue.getOrgUnit());
event.setEventDate(dataValue.getEventDate());
event.setDueDate(dataValue.getDueDate());
event.setCompletedDate(dataValue.getCompletedDate());
event.setCompletedBy(dataValue.getCompletedBy());
if (dataValue.getGeometry() != null) {
event.setGeometry(new WKTReader().read(dataValue.getGeometry()));
} else if (dataValue.getLongitude() != null && dataValue.getLatitude() != null) {
event.setGeometry(new WKTReader().read("Point(" + dataValue.getLongitude() + " " + dataValue.getLatitude() + ")"));
}
events.add(event);
}
DataValue value = new DataValue(dataValue.getDataElement(), dataValue.getValue());
value.setStoredBy(dataValue.getStoredBy());
value.setProvidedElsewhere(dataValue.getProvidedElsewhere());
event.getDataValues().add(value);
}
return events;
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectReader in project dhis2-core by dhis2.
the class V2_33_5__Update_job_parameters_with_system_setting_values method migrate.
@Override
public void migrate(final Context context) throws Exception {
// 1. Fetch data from SystemSettings if present
int trackerPageSize = 0;
int eventPageSize = 0;
int dataValuesPageSize = 0;
String sql = "SELECT value FROM systemsetting WHERE name = '" + TRACKER_PROGRAM_SYNC_PAGE_SIZE + "';";
try (Statement stmt = context.getConnection().createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
if (rs.next()) {
trackerPageSize = (Integer) SerializationUtils.deserialize(rs.getBytes("value"));
}
}
sql = "SELECT value FROM systemsetting WHERE name = '" + EVENT_PROGRAM_SYNC_PAGE_SIZE + "';";
try (Statement stmt = context.getConnection().createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
if (rs.next()) {
eventPageSize = (Integer) SerializationUtils.deserialize(rs.getBytes("value"));
}
}
sql = "SELECT value FROM systemsetting WHERE name = '" + DATA_VALUES_SYNC_PAGE_SIZE + "';";
try (Statement stmt = context.getConnection().createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
if (rs.next()) {
dataValuesPageSize = (Integer) SerializationUtils.deserialize(rs.getBytes("value"));
}
}
log.info("Following values were found in SystemSettings: trackerPageSize: " + trackerPageSize + ", eventPageSize: " + eventPageSize + ", dataValuePageSize: " + dataValuesPageSize);
// 2. Update jobParameters with data from SystemSettings
if (trackerPageSize > 0 || eventPageSize > 0 || dataValuesPageSize > 0) {
Map<Integer, JobParameters> updatedJobParameters = new HashMap<>();
ObjectMapper mapper = new ObjectMapper();
mapper.activateDefaultTyping(BasicPolymorphicTypeValidator.builder().allowIfBaseType(JobParameters.class).build());
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
JavaType resultingJavaType = mapper.getTypeFactory().constructType(JobParameters.class);
ObjectReader reader = mapper.readerFor(resultingJavaType);
ObjectWriter writer = mapper.writerFor(resultingJavaType);
if (trackerPageSize > 0) {
sql = "SELECT jobconfigurationid, jsonbjobparameters FROM jobconfiguration " + "WHERE jobtype = '" + JobType.TRACKER_PROGRAMS_DATA_SYNC.name() + "';";
try (Statement stmt = context.getConnection().createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
TrackerProgramsDataSynchronizationJobParameters jobparams = reader.readValue(rs.getString("jsonbjobparameters"));
jobparams.setPageSize(trackerPageSize);
updatedJobParameters.put(rs.getInt("jobconfigurationid"), jobparams);
}
}
}
if (eventPageSize > 0) {
sql = "SELECT jobconfigurationid, jsonbjobparameters FROM jobconfiguration " + "WHERE jobtype = '" + JobType.EVENT_PROGRAMS_DATA_SYNC.name() + "';";
try (Statement stmt = context.getConnection().createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
EventProgramsDataSynchronizationJobParameters jobparams = reader.readValue(rs.getString("jsonbjobparameters"));
jobparams.setPageSize(eventPageSize);
updatedJobParameters.put(rs.getInt("jobconfigurationid"), jobparams);
}
}
}
sql = "SELECT jobconfigurationid, jsonbjobparameters FROM jobconfiguration " + "WHERE jobtype = '" + JobType.META_DATA_SYNC.name() + "';";
try (Statement stmt = context.getConnection().createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
MetadataSyncJobParameters jobparams = reader.readValue(rs.getString("jsonbjobparameters"));
if (trackerPageSize > 0) {
jobparams.setTrackerProgramPageSize(trackerPageSize);
}
if (eventPageSize > 0) {
jobparams.setEventProgramPageSize(eventPageSize);
}
if (dataValuesPageSize > 0) {
jobparams.setDataValuesPageSize(dataValuesPageSize);
}
updatedJobParameters.put(rs.getInt("jobconfigurationid"), jobparams);
}
}
for (Map.Entry<Integer, JobParameters> jobParams : updatedJobParameters.entrySet()) {
try (PreparedStatement ps = context.getConnection().prepareStatement("UPDATE jobconfiguration SET jsonbjobparameters = ? where jobconfigurationid = ?;")) {
PGobject pg = new PGobject();
pg.setType("jsonb");
pg.setValue(writer.writeValueAsString(jobParams.getValue()));
ps.setObject(1, pg);
ps.setInt(2, jobParams.getKey());
ps.execute();
}
}
}
// 3. Remove entries from systemsetting table
try (Statement stmt = context.getConnection().createStatement()) {
stmt.executeUpdate("DELETE FROM systemsetting WHERE name IN ('" + TRACKER_PROGRAM_SYNC_PAGE_SIZE + "', '" + EVENT_PROGRAM_SYNC_PAGE_SIZE + "', '" + DATA_VALUES_SYNC_PAGE_SIZE + "');");
}
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectReader in project uPortal by Jasig.
the class JacksonColumnMapper method fromNonNullValue.
@Override
public final Object fromNonNullValue(String s) {
try {
final JsonWrapper jsonWrapper = objectReader.readValue(s);
final ObjectReader typeReader = typedObjectReaders.getUnchecked(jsonWrapper.getType());
return typeReader.readValue(jsonWrapper.getValue());
} catch (JsonProcessingException e) {
throw new IllegalArgumentException("Could not read from JSON: " + s, e);
} catch (IOException e) {
throw new IllegalArgumentException("Could not read from JSON: " + s, e);
}
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectReader in project uPortal by Jasig.
the class JpaStatisticalSummaryTest method testStorelessUnivariateStatistic.
public void testStorelessUnivariateStatistic(StorelessUnivariateStatistic sus, double expected) throws Exception {
assertEquals(expected, sus.getResult(), 0.1);
final ObjectMapper mapper = new ObjectMapper();
mapper.findAndRegisterModules();
// Configure Jackson to just use fields
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
mapper.setVisibility(PropertyAccessor.GETTER, Visibility.NONE);
mapper.setVisibility(PropertyAccessor.IS_GETTER, Visibility.NONE);
mapper.setVisibility(PropertyAccessor.SETTER, Visibility.NONE);
mapper.setVisibility(PropertyAccessor.CREATOR, Visibility.NONE);
mapper.addMixInAnnotations(Object.class, IgnoreTypeMixIn.class);
final FilterProvider filters = new SimpleFilterProvider().addFilter("storedDataFilter", SimpleBeanPropertyFilter.serializeAllExcept("storedData"));
final ObjectWriter ssWriter = mapper.writer(filters);
final ObjectReader ssReader = mapper.reader(sus.getClass());
final String susString = ssWriter.writeValueAsString(sus);
System.out.println(susString);
final StorelessUnivariateStatistic newSus = ssReader.readValue(susString);
assertEquals(expected, newSus.getResult(), 0.1);
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectReader in project scoold by Erudika.
the class AdminController method restore.
@PostMapping("/import")
public String restore(@RequestParam("file") MultipartFile file, @RequestParam(required = false, defaultValue = "false") Boolean isso, HttpServletRequest req, HttpServletResponse res) {
Profile authUser = utils.getAuthUser(req);
if (!utils.isAdmin(authUser)) {
res.setStatus(403);
return null;
}
ObjectReader reader = ParaObjectUtils.getJsonMapper().readerFor(new TypeReference<List<Map<String, Object>>>() {
});
Map<String, String> comments2authors = new LinkedHashMap<>();
int count = 0;
int importBatchSize = Config.getConfigInt("import_batch_size", 100);
String filename = file.getOriginalFilename();
Sysprop s = new Sysprop();
s.setType("scooldimport");
try (InputStream inputStream = file.getInputStream()) {
if (StringUtils.endsWithIgnoreCase(filename, ".zip")) {
try (ZipInputStream zipIn = new ZipInputStream(inputStream)) {
ZipEntry zipEntry;
List<ParaObject> toCreate = new LinkedList<ParaObject>();
while ((zipEntry = zipIn.getNextEntry()) != null) {
if (isso) {
count += importFromSOArchive(zipIn, zipEntry, reader, comments2authors).size();
} else if (zipEntry.getName().endsWith(".json")) {
List<Map<String, Object>> objects = reader.readValue(new FilterInputStream(zipIn) {
public void close() throws IOException {
zipIn.closeEntry();
}
});
objects.forEach(o -> toCreate.add(ParaObjectUtils.setAnnotatedFields(o)));
if (toCreate.size() >= importBatchSize) {
pc.createAll(toCreate);
toCreate.clear();
}
count += objects.size();
} else {
logger.error("Expected JSON but found unknown file type to import: {}", zipEntry.getName());
}
}
if (!toCreate.isEmpty()) {
pc.createAll(toCreate);
}
if (isso) {
updateSOCommentAuthors(comments2authors);
}
}
} else if (StringUtils.endsWithIgnoreCase(filename, ".json")) {
List<Map<String, Object>> objects = reader.readValue(inputStream);
List<ParaObject> toCreate = new LinkedList<ParaObject>();
objects.forEach(o -> toCreate.add(ParaObjectUtils.setAnnotatedFields(o)));
count = objects.size();
pc.createAll(toCreate);
}
s.setCreatorid(authUser.getCreatorid());
s.setName(authUser.getName());
s.addProperty("count", count);
s.addProperty("file", filename);
logger.info("Imported {} objects to {}. Executed by {}", count, Config.getConfigParam("access_key", "scoold"), authUser.getCreatorid() + " " + authUser.getName());
if (count > 0) {
pc.create(s);
}
} catch (Exception e) {
logger.error("Failed to import " + filename, e);
return "redirect:" + ADMINLINK + "?error=true&imported=" + count;
}
return "redirect:" + ADMINLINK + "?success=true&imported=" + count;
}
Aggregations