use of com.serotonin.m2m2.i18n.ProcessResult in project ma-core-public by infiniteautomation.
the class VirtualSerialPortImporter method importImpl.
@Override
protected void importImpl() {
String xid = json.getString("xid");
boolean isNew = false;
if (StringUtils.isBlank(xid)) {
xid = VirtualSerialPortConfigDao.instance.generateUniqueXid();
isNew = true;
}
VirtualSerialPortConfig vo = VirtualSerialPortConfigDao.instance.getByXid(xid);
if (vo == null) {
isNew = true;
String typeStr = json.getString("type");
if (StringUtils.isBlank(typeStr))
addFailureMessage("emport.virtualserialport.missingType", xid, VirtualSerialPortConfig.PORT_TYPE_CODES);
else {
try {
Class<?> virtualPortClass = VirtualSerialPortConfigResolver.findClass(typeStr);
vo = (VirtualSerialPortConfig) virtualPortClass.newInstance();
vo.setXid(xid);
} catch (TranslatableJsonException ex) {
addFailureMessage("emport.virtualserialport.prefix", xid, ex.getMsg());
} catch (InstantiationException e) {
addFailureMessage("emport.virtualserialport.prefix", xid, e.getMessage());
} catch (IllegalAccessException e) {
addFailureMessage("emport.virtualserialport.prefix", xid, e.getMessage());
}
}
}
if (vo != null) {
try {
// The VO was found or successfully created. Finish reading it in.
ctx.getReader().readInto(vo, json);
// Now validate it. Use a new response object so we can distinguish errors in this vo from
// other errors.
ProcessResult voResponse = new ProcessResult();
vo.validate(voResponse);
if (voResponse.getHasMessages())
setValidationMessages(voResponse, "emport.virtualserialport.prefix", xid);
else {
// Sweet. Save it.
VirtualSerialPortConfigDao.instance.save(vo);
addSuccessMessage(isNew, "emport.virtualserialport.prefix", xid);
}
} catch (TranslatableJsonException e) {
addFailureMessage("emport.virtualserialport.prefix", xid, e.getMsg());
} catch (JsonException e) {
addFailureMessage("emport.virtualserialport.prefix", xid, getJsonExceptionMessage(e));
}
}
}
use of com.serotonin.m2m2.i18n.ProcessResult 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");
}
use of com.serotonin.m2m2.i18n.ProcessResult in project ma-core-public by infiniteautomation.
the class DeltamationCommon method validatePoint.
public static DataPointVO validatePoint(int pointId, String name, ProcessResult response, Integer dataType, boolean requireSettable) {
DataPointDao points = DataPointDao.instance;
DataPointVO point = points.getDataPoint(pointId);
if (point == null) {
response.addContextualMessage(name, "validate.noPoint");
return null;
}
if (requireSettable && !point.getPointLocator().isSettable()) {
response.addContextualMessage(name, "validate.pointNotSettable", point.getName());
}
if (dataType != null && point.getPointLocator().getDataTypeId() != dataType) {
response.addContextualMessage(name, "validate.pointWrongType", point.getName());
}
return point;
}
use of com.serotonin.m2m2.i18n.ProcessResult in project ma-modules-public by infiniteautomation.
the class AsciiFileEditDwr method saveFileDataSource.
@DwrPermission(user = true)
public ProcessResult saveFileDataSource(BasicDataSourceVO basic, int updatePeriods, int updatePeriodType, String filePath) {
AsciiFileDataSourceVO ds = (AsciiFileDataSourceVO) Common.getHttpUser().getEditDataSource();
setBasicProps(ds, basic);
ds.setUpdatePeriods(updatePeriods);
ds.setUpdatePeriodType(updatePeriodType);
try {
ds.setFilePath(new File(filePath).getCanonicalPath());
} catch (IOException e) {
ProcessResult pr = new ProcessResult();
pr.addContextualMessage("filePath", "dsEdit.file.ioexceptionCanonical", filePath);
return pr;
}
return tryDataSourceSave(ds);
}
use of com.serotonin.m2m2.i18n.ProcessResult in project ma-modules-public by infiniteautomation.
the class GraphicalView method validate.
public void validate(ProcessResult response) {
if (StringUtils.isBlank(name))
response.addMessage("name", new TranslatableMessage("validate.required"));
else if (StringValidation.isLengthGreaterThan(name, 100))
response.addMessage("name", new TranslatableMessage("validate.notLongerThan", 100));
if (StringUtils.isBlank(xid))
response.addMessage("xid", new TranslatableMessage("validate.required"));
else if (StringValidation.isLengthGreaterThan(xid, 50))
response.addMessage("xid", new TranslatableMessage("validate.notLongerThan", 50));
else if (!new GraphicalViewDao().isXidUnique(xid, id))
response.addMessage("xid", new TranslatableMessage("validate.xidUsed"));
for (ViewComponent vc : viewComponents) vc.validate(response);
// Validate the permissions
User user = Common.getUser();
GraphicalView existingView = null;
if (this.id != Common.NEW_ID) {
existingView = new GraphicalViewDao().getView(id);
}
if (existingView == null) {
Permissions.validateAddedPermissions(this.readPermission, user, response, "readPermission");
Permissions.validateAddedPermissions(this.setPermission, user, response, "setPermission");
Permissions.validateAddedPermissions(this.editPermission, user, response, "editPermission");
} else {
// We are updating a view so only validate the new permissions, allow existing ones to remain and don't let
// the user remove permissions they do not have
this.readPermission = trimPermission(this.readPermission);
validateUpdatedPermissions(existingView.readPermission, this.readPermission, user, response, "readPermission");
this.setPermission = trimPermission(this.setPermission);
validateUpdatedPermissions(existingView.setPermission, this.setPermission, user, response, "setPermission");
this.editPermission = trimPermission(this.editPermission);
validateUpdatedPermissions(existingView.editPermission, this.editPermission, user, response, "editPermission");
}
}
Aggregations