Search in sources :

Example 1 with LocationDef

use of org.opennms.upgrade.implementations.monitoringLocations16.LocationDef in project opennms by OpenNMS.

the class MonitoringLocationsMigratorOffline method execute.

@Override
public void execute() throws OnmsUpgradeException {
    if (monitoringLocationsConfig == null)
        return;
    log("Moving monitoring locations into the database...\n");
    long count = 0;
    try {
        Connection connection = null;
        final DBUtils dbUtils = new DBUtils(getClass());
        try {
            connection = DataSourceFactory.getInstance().getConnection();
            dbUtils.watch(connection);
            PreparedStatement insertLocation = connection.prepareStatement("INSERT INTO monitoringlocations (id, monitoringarea, geolocation, latitude, longitude, priority) VALUES (?,?,?,?,?,?)");
            PreparedStatement insertPollingPackage = connection.prepareStatement("INSERT INTO monitoringlocationspollingpackages (monitoringlocationid, packagename) VALUES (?,?)");
            PreparedStatement insertCollectionPackage = connection.prepareStatement("INSERT INTO monitoringlocationscollectionpackages (monitoringlocationid, packagename) VALUES (?,?)");
            PreparedStatement insertTag = connection.prepareStatement("INSERT INTO monitoringlocationstags (monitoringlocationid, tag) VALUES (?,?)");
            dbUtils.watch(insertLocation);
            dbUtils.watch(insertPollingPackage);
            dbUtils.watch(insertCollectionPackage);
            dbUtils.watch(insertTag);
            for (LocationDef location : monitoringLocationsConfig.getLocations()) {
                // id
                insertLocation.setString(1, location.getLocationName());
                // monitoringarea
                insertLocation.setString(2, location.getMonitoringArea());
                if (location.getGeolocation() != null && !"".equals(location.getGeolocation().trim())) {
                    // geolocation
                    insertLocation.setString(3, location.getGeolocation());
                } else {
                    insertLocation.setNull(3, Types.VARCHAR);
                }
                if (location.getCoordinates() != null && !"".equals(location.getCoordinates())) {
                    String[] latLong = location.getCoordinates().split(",");
                    if (latLong.length == 2) {
                        // latitude
                        insertLocation.setDouble(4, Double.valueOf(latLong[0]));
                        // longitude
                        insertLocation.setDouble(5, Double.valueOf(latLong[1]));
                    } else {
                        insertLocation.setNull(4, Types.DOUBLE);
                        insertLocation.setNull(5, Types.DOUBLE);
                    }
                } else {
                    insertLocation.setNull(4, Types.DOUBLE);
                    insertLocation.setNull(5, Types.DOUBLE);
                }
                if (location.getPriority() == null) {
                    // priority
                    insertLocation.setNull(6, Types.INTEGER);
                } else {
                    // priority
                    insertLocation.setLong(6, location.getPriority());
                }
                insertLocation.execute();
                count++;
                if (location.getPollingPackageName() != null && !"".equals(location.getPollingPackageName())) {
                    // monitoringlocationid
                    insertPollingPackage.setString(1, location.getLocationName());
                    // packagename
                    insertPollingPackage.setString(2, location.getPollingPackageName());
                    insertPollingPackage.execute();
                }
                if (location.getCollectionPackageName() != null && !"".equals(location.getCollectionPackageName())) {
                    // monitoringlocationid
                    insertCollectionPackage.setString(1, location.getLocationName());
                    // packagename
                    insertCollectionPackage.setString(2, location.getCollectionPackageName());
                    insertCollectionPackage.execute();
                }
                for (Tag tag : location.getTags()) {
                    if (tag.getName() != null && !"".equals(tag.getName().trim())) {
                        // monitoringlocationid
                        insertTag.setString(1, location.getLocationName());
                        // tag
                        insertTag.setString(2, tag.getName());
                        insertTag.execute();
                    }
                }
            }
        } finally {
            dbUtils.cleanUp();
        }
    } catch (Throwable e) {
        throw new OnmsUpgradeException("Can't fix services configuration because " + e.getMessage(), e);
    }
    log("Moved %d monitoring locations into the database\n", count);
}
Also used : LocationDef(org.opennms.upgrade.implementations.monitoringLocations16.LocationDef) Connection(java.sql.Connection) DBUtils(org.opennms.core.utils.DBUtils) PreparedStatement(java.sql.PreparedStatement) Tag(org.opennms.upgrade.implementations.monitoringLocations16.Tag) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException)

Aggregations

Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 DBUtils (org.opennms.core.utils.DBUtils)1 OnmsUpgradeException (org.opennms.upgrade.api.OnmsUpgradeException)1 LocationDef (org.opennms.upgrade.implementations.monitoringLocations16.LocationDef)1 Tag (org.opennms.upgrade.implementations.monitoringLocations16.Tag)1