use of com.serotonin.m2m2.module.SystemSettingsDefinition in project ma-core-public by infiniteautomation.
the class SystemSettingsDao method validate.
/**
* Validate the system settings passed in, only validating settings that exist in the map.
*
* @param settings
* @param voResponse
*/
public void validate(Map<String, Object> settings, ProcessResult response) {
Object setting = null;
try {
setting = settings.get(EMAIL_CONTENT_TYPE);
if (setting != null) {
if (setting instanceof Number) {
int emailContentType = ((Number) setting).intValue();
switch(emailContentType) {
case MangoEmailContent.CONTENT_TYPE_BOTH:
case MangoEmailContent.CONTENT_TYPE_HTML:
case MangoEmailContent.CONTENT_TYPE_TEXT:
break;
default:
response.addContextualMessage(EMAIL_CONTENT_TYPE, "validate.invalideValue");
}
} else {
// String Code
if (MangoEmailContent.CONTENT_TYPE_CODES.getId((String) setting) < 0)
response.addContextualMessage(EMAIL_CONTENT_TYPE, "emport.error.invalid", EMAIL_CONTENT_TYPE, (String) setting, MangoEmailContent.CONTENT_TYPE_CODES.getCodeList());
}
}
} catch (NumberFormatException e) {
response.addContextualMessage(EMAIL_CONTENT_TYPE, "validate.illegalValue");
}
validatePeriodType(POINT_DATA_PURGE_PERIOD_TYPE, settings, response);
validatePeriodType(DATA_POINT_EVENT_PURGE_PERIOD_TYPE, settings, response);
validatePeriodType(DATA_SOURCE_EVENT_PURGE_PERIOD_TYPE, settings, response);
validatePeriodType(SYSTEM_EVENT_PURGE_PERIOD_TYPE, settings, response);
validatePeriodType(PUBLISHER_EVENT_PURGE_PERIOD_TYPE, settings, response);
validatePeriodType(AUDIT_EVENT_PURGE_PERIOD_TYPE, settings, response);
validatePeriodType(NONE_ALARM_PURGE_PERIOD_TYPE, settings, response);
validatePeriodType(INFORMATION_ALARM_PURGE_PERIOD_TYPE, settings, response);
validatePeriodType(IMPORTANT_ALARM_PURGE_PERIOD_TYPE, settings, response);
validatePeriodType(WARNING_ALARM_PURGE_PERIOD_TYPE, settings, response);
validatePeriodType(URGENT_ALARM_PURGE_PERIOD_TYPE, settings, response);
validatePeriodType(CRITICAL_ALARM_PURGE_PERIOD_TYPE, settings, response);
validatePeriodType(LIFE_SAFETY_ALARM_PURGE_PERIOD_TYPE, settings, response);
validatePeriodType(EVENT_PURGE_PERIOD_TYPE, settings, response);
validatePeriodType(FUTURE_DATE_LIMIT_PERIOD_TYPE, settings, response);
try {
setting = settings.get(CHART_BACKGROUND_COLOUR);
if (setting != null)
ColorUtils.toColor((String) setting);
} catch (InvalidArgumentException e) {
response.addContextualMessage(CHART_BACKGROUND_COLOUR, "systemSettings.validation.invalidColour");
}
try {
setting = settings.get(PLOT_BACKGROUND_COLOUR);
if (setting != null)
ColorUtils.toColor((String) setting);
} catch (InvalidArgumentException e) {
response.addContextualMessage(PLOT_BACKGROUND_COLOUR, "systemSettings.validation.invalidColour");
}
try {
setting = settings.get(PLOT_GRIDLINE_COLOUR);
if (setting != null)
ColorUtils.toColor((String) setting);
} catch (InvalidArgumentException e) {
response.addContextualMessage(PLOT_GRIDLINE_COLOUR, "systemSettings.validation.invalidColour");
}
setting = settings.get(BACKUP_FILE_LOCATION);
if (setting != null) {
File tmp = new File((String) setting);
if (!tmp.exists()) {
// Doesn't exist, push up message
response.addContextualMessage(BACKUP_FILE_LOCATION, "systemSettings.validation.backupLocationNotExists");
}
if (!tmp.canWrite()) {
response.addContextualMessage(BACKUP_FILE_LOCATION, "systemSettings.validation.cannotWriteToBackupFileLocation");
}
}
// Validate the Hour and Minute
Integer backupHour = getIntValue(BACKUP_HOUR, settings);
if (backupHour != null)
if ((backupHour > 23) || (backupHour < 0)) {
response.addContextualMessage(BACKUP_HOUR, "systemSettings.validation.backupHourInvalid");
}
Integer backupMinute = getIntValue(BACKUP_MINUTE, settings);
if (backupMinute != null)
if ((backupMinute > 59) || (backupMinute < 0)) {
response.addContextualMessage(BACKUP_MINUTE, "systemSettings.validation.backupMinuteInvalid");
}
validatePeriodType(BACKUP_PERIOD_TYPE, settings, response);
// Validate the number of backups to keep
Integer backupFileCount = getIntValue(BACKUP_FILE_COUNT, settings);
if (backupFileCount != null)
if (backupFileCount < 1) {
response.addContextualMessage(BACKUP_FILE_COUNT, "systemSettings.validation.backupFileCountInvalid");
}
// Validate
setting = settings.get(DATABASE_BACKUP_FILE_LOCATION);
if (setting != null) {
File tmp = new File((String) setting);
if (!tmp.exists()) {
// Doesn't exist, push up message
response.addContextualMessage(DATABASE_BACKUP_FILE_LOCATION, "systemSettings.validation.databaseBackupLocationNotExists");
}
if (!tmp.canWrite()) {
response.addContextualMessage(DATABASE_BACKUP_FILE_LOCATION, "systemSettings.validation.cannotWriteToDatabaseBackupFileLocation");
}
}
// Validate the Hour and Minute
Integer databaseBackupHour = getIntValue(DATABASE_BACKUP_HOUR, settings);
if (databaseBackupHour != null)
if ((databaseBackupHour > 23) || (databaseBackupHour < 0)) {
response.addContextualMessage(DATABASE_BACKUP_HOUR, "systemSettings.validation.databaseBackupHourInvalid");
}
Integer databaseBackupMinute = getIntValue(DATABASE_BACKUP_MINUTE, settings);
if (databaseBackupMinute != null)
if ((databaseBackupMinute > 59) || (databaseBackupMinute < 0)) {
response.addContextualMessage(DATABASE_BACKUP_MINUTE, "systemSettings.validation.databaseBackupMinuteInvalid");
}
validatePeriodType(DATABASE_BACKUP_PERIOD_TYPE, settings, response);
// Validate the number of backups to keep
Integer databaseBackupFileCount = getIntValue(DATABASE_BACKUP_FILE_COUNT, settings);
if (databaseBackupFileCount != null)
if (databaseBackupFileCount < 1) {
response.addContextualMessage(DATABASE_BACKUP_FILE_COUNT, "systemSettings.validation.databaseBackupFileCountInvalid");
}
// Thread Pool Sizes
Integer corePoolSize = getIntValue(HIGH_PRI_CORE_POOL_SIZE, settings);
Integer maxPoolSize = getIntValue(HIGH_PRI_MAX_POOL_SIZE, settings);
if ((corePoolSize != null) && (corePoolSize < 0)) {
response.addContextualMessage(HIGH_PRI_CORE_POOL_SIZE, "validate.greaterThanOrEqualTo", 0);
}
if ((maxPoolSize != null) && (maxPoolSize < BackgroundProcessing.HIGH_PRI_MAX_POOL_SIZE_MIN)) {
response.addContextualMessage(HIGH_PRI_MAX_POOL_SIZE, "validate.greaterThanOrEqualTo", BackgroundProcessing.HIGH_PRI_MAX_POOL_SIZE_MIN);
}
if ((maxPoolSize != null) && (maxPoolSize < corePoolSize)) {
response.addContextualMessage(HIGH_PRI_MAX_POOL_SIZE, "systemSettings.threadPools.validate.maxPoolMustBeGreaterThanCorePool");
}
// For Medium and Low the Max has no effect because they use a LinkedBlockingQueue and will just block until a
// core pool thread is available
corePoolSize = getIntValue(MED_PRI_CORE_POOL_SIZE, settings);
if (corePoolSize < BackgroundProcessing.MED_PRI_MAX_POOL_SIZE_MIN) {
response.addContextualMessage(MED_PRI_CORE_POOL_SIZE, "validate.greaterThanOrEqualTo", BackgroundProcessing.MED_PRI_MAX_POOL_SIZE_MIN);
}
corePoolSize = getIntValue(LOW_PRI_CORE_POOL_SIZE, settings);
if (corePoolSize < BackgroundProcessing.LOW_PRI_MAX_POOL_SIZE_MIN) {
response.addContextualMessage(LOW_PRI_CORE_POOL_SIZE, "validate.greaterThanOrEqualTo", BackgroundProcessing.LOW_PRI_MAX_POOL_SIZE_MIN);
}
// Validate Upgrade Version State
setting = settings.get(UPGRADE_VERSION_STATE);
if (setting != null) {
if (setting instanceof Number) {
// Legacy Integer Value
try {
int value = ((Number) setting).intValue();
switch(value) {
case UpgradeVersionState.DEVELOPMENT:
case UpgradeVersionState.ALPHA:
case UpgradeVersionState.BETA:
case UpgradeVersionState.RELEASE_CANDIDATE:
case UpgradeVersionState.PRODUCTION:
break;
default:
response.addContextualMessage(UPGRADE_VERSION_STATE, "validate.invalidValue");
break;
}
} catch (NumberFormatException e) {
response.addContextualMessage(UPGRADE_VERSION_STATE, "validate.illegalValue");
}
} else {
// Must be a code
if (Common.VERSION_STATE_CODES.getId((String) setting) < 0)
response.addContextualMessage(UPGRADE_VERSION_STATE, "emport.error.invalid", UPGRADE_VERSION_STATE, (String) setting, Common.VERSION_STATE_CODES.getCodeList());
}
}
// Validate point hierarchy settings
setting = settings.get(HIERARCHY_PATH_SEPARATOR);
if (setting != null) {
if (StringUtils.isEmpty((String) setting))
response.addContextualMessage(HIERARCHY_PATH_SEPARATOR, "validate.cannotContainEmptyString");
}
// Validate the Module Settings
for (SystemSettingsDefinition def : ModuleRegistry.getSystemSettingsDefinitions()) def.validateSettings(settings, response);
// Ensure no one can change the superadmin permission
if (settings.get(SuperadminPermissionDefinition.PERMISSION) != null)
response.addContextualMessage(SuperadminPermissionDefinition.PERMISSION, "validate.readOnly");
}
Aggregations