Search in sources :

Example 1 with JobParameters

use of org.hisp.dhis.scheduling.JobParameters 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 + "');");
    }
}
Also used : HashMap(java.util.HashMap) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) PreparedStatement(java.sql.PreparedStatement) EventProgramsDataSynchronizationJobParameters(org.hisp.dhis.scheduling.parameters.EventProgramsDataSynchronizationJobParameters) PGobject(org.postgresql.util.PGobject) JavaType(com.fasterxml.jackson.databind.JavaType) TrackerProgramsDataSynchronizationJobParameters(org.hisp.dhis.scheduling.parameters.TrackerProgramsDataSynchronizationJobParameters) ResultSet(java.sql.ResultSet) MetadataSyncJobParameters(org.hisp.dhis.scheduling.parameters.MetadataSyncJobParameters) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) JobParameters(org.hisp.dhis.scheduling.JobParameters) EventProgramsDataSynchronizationJobParameters(org.hisp.dhis.scheduling.parameters.EventProgramsDataSynchronizationJobParameters) MetadataSyncJobParameters(org.hisp.dhis.scheduling.parameters.MetadataSyncJobParameters) TrackerProgramsDataSynchronizationJobParameters(org.hisp.dhis.scheduling.parameters.TrackerProgramsDataSynchronizationJobParameters) HashMap(java.util.HashMap) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with JobParameters

use of org.hisp.dhis.scheduling.JobParameters in project dhis2-core by dhis2.

the class V2_35_2__Update_data_sync_job_parameters_with_system_setting_value method updateJobParamaters.

private void updateJobParamaters(final Context context, int dataValuesPageSize) throws JsonProcessingException, SQLException {
    if (dataValuesPageSize > 0) {
        Map<Integer, JobParameters> updatedJobParameters = new HashMap<>();
        String sql = "SELECT jobconfigurationid, jsonbjobparameters FROM jobconfiguration " + "WHERE jobtype = '" + JobType.DATA_SYNC.name() + "';";
        try (Statement stmt = context.getConnection().createStatement();
            ResultSet rs = stmt.executeQuery(sql)) {
            while (rs.next()) {
                DataSynchronizationJobParameters jobparams = reader.readValue(rs.getString("jsonbjobparameters"));
                jobparams.setPageSize(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();
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) JobParameters(org.hisp.dhis.scheduling.JobParameters) MetadataSyncJobParameters(org.hisp.dhis.scheduling.parameters.MetadataSyncJobParameters) DataSynchronizationJobParameters(org.hisp.dhis.scheduling.parameters.DataSynchronizationJobParameters) HashMap(java.util.HashMap) Map(java.util.Map) DataSynchronizationJobParameters(org.hisp.dhis.scheduling.parameters.DataSynchronizationJobParameters) PGobject(org.postgresql.util.PGobject)

Example 3 with JobParameters

use of org.hisp.dhis.scheduling.JobParameters in project dhis2-core by dhis2.

the class V2_31_2__Job_configuration_param_to_jsonb method migrate.

public void migrate(Context context) throws Exception {
    ObjectMapper MAPPER = new ObjectMapper();
    MAPPER.enableDefaultTyping();
    MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    writer = MAPPER.writerFor(JobParameters.class);
    // 1. Create new jsonb column for jobparameters in jobconfiguration
    try (Statement stmt = context.getConnection().createStatement()) {
        stmt.executeUpdate("ALTER TABLE jobconfiguration ADD COLUMN IF NOT EXISTS jsonbjobparameters jsonb");
    }
    // 2. Move existing jobparameters from bytearray column into jsonb
    // column
    Map<Integer, byte[]> jobParamByteMap = new HashMap<>();
    Map<Integer, byte[]> jobTypeByteMap = new HashMap<>();
    try (Statement stmt = context.getConnection().createStatement()) {
        try (ResultSet rows = stmt.executeQuery("select jobconfigurationid,jobparameters,jobtype from jobconfiguration where jobparameters is not null")) {
            while (rows.next()) {
                jobParamByteMap.put(rows.getInt(1), rows.getBytes(2));
                jobTypeByteMap.put(rows.getInt(1), rows.getBytes(3));
            }
        }
    }
    jobParamByteMap.forEach((id, jobParamByteArray) -> {
        Object jParaB = null;
        JobType jobType = null;
        try {
            jParaB = toObject(jobParamByteArray);
            jobType = (JobType) toObject(jobTypeByteMap.get(id));
        } catch (IOException | ClassNotFoundException e) {
            log.error("Flyway java migration error:", e);
            throw new FlywayException(e);
        }
        try (PreparedStatement ps = context.getConnection().prepareStatement("Update jobconfiguration set jsonbjobparameters =? where  jobconfigurationid = ?")) {
            PGobject pg = new PGobject();
            pg.setType("jsonb");
            pg.setValue(convertObjectToJson(jobType.getJobParameters().cast(jParaB)));
            ps.setObject(1, pg);
            ps.setInt(2, id);
            ps.execute();
        } catch (SQLException e) {
            log.error("Flyway java migration error:", e);
            throw new FlywayException(e);
        }
    });
    // 3. Delete old byte array column for jobparameters in jobconfiguration
    try (Statement stmt = context.getConnection().createStatement()) {
        stmt.executeUpdate("ALTER TABLE jobconfiguration DROP COLUMN IF EXISTS jobparameters");
    }
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) PGobject(org.postgresql.util.PGobject) JobType(org.hisp.dhis.scheduling.JobType) ResultSet(java.sql.ResultSet) JobParameters(org.hisp.dhis.scheduling.JobParameters) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 Statement (java.sql.Statement)3 HashMap (java.util.HashMap)3 JobParameters (org.hisp.dhis.scheduling.JobParameters)3 PGobject (org.postgresql.util.PGobject)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Map (java.util.Map)2 MetadataSyncJobParameters (org.hisp.dhis.scheduling.parameters.MetadataSyncJobParameters)2 JavaType (com.fasterxml.jackson.databind.JavaType)1 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)1 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 FlywayException (org.flywaydb.core.api.FlywayException)1 JobType (org.hisp.dhis.scheduling.JobType)1 DataSynchronizationJobParameters (org.hisp.dhis.scheduling.parameters.DataSynchronizationJobParameters)1 EventProgramsDataSynchronizationJobParameters (org.hisp.dhis.scheduling.parameters.EventProgramsDataSynchronizationJobParameters)1 TrackerProgramsDataSynchronizationJobParameters (org.hisp.dhis.scheduling.parameters.TrackerProgramsDataSynchronizationJobParameters)1