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);
}
Aggregations