use of com.infiniteautomation.mango.util.exception.ValidationException in project ma-core-public by infiniteautomation.
the class MangoTestBase method createMockPublishedPoint.
/**
* Create a published point
*/
public MockPublishedPointVO createMockPublishedPoint(MockPublisherVO publisher, IDataPoint dataPoint, boolean enabled) {
MockPublishedPointVO pp = publisher.getDefinition().createPublishedPointVO(publisher, dataPoint);
pp.setName(dataPoint.getName());
pp.setEnabled(enabled);
PublishedPointService publishedPointService = Common.getBean(PublishedPointService.class);
try {
publishedPointService.insert(pp);
} catch (ValidationException e) {
fail(e.getValidationErrorMessage(Common.getTranslations()));
return null;
}
return pp;
}
use of com.infiniteautomation.mango.util.exception.ValidationException in project ma-core-public by infiniteautomation.
the class PublisherVO method setAlarmLevel.
/**
* Set an alarm level based on the sub-type of the publisher event type
* which MUST (and already is) one of the codes in getEventCodes()
*/
public void setAlarmLevel(String subType, AlarmLevels level) throws ValidationException {
ExportCodes codes = getEventCodes();
int eventId = codes.getId(subType);
if (eventId == -1) {
ProcessResult result = new ProcessResult();
result.addContextualMessage("alarmLevel", "emport.error.eventCode", subType, codes.getCodeList());
throw new ValidationException(result);
}
alarmLevels.put(eventId, level);
}
use of com.infiniteautomation.mango.util.exception.ValidationException in project ma-core-public by infiniteautomation.
the class DataPointService method insert.
@Override
public DataPointVO insert(DataPointVO vo) throws PermissionException, ValidationException {
PermissionHolder user = Common.getUser();
// Ensure they can create
ensureCreatePermission(user, vo);
// Ensure id is not set
if (vo.getId() != Common.NEW_ID) {
ProcessResult result = new ProcessResult();
result.addContextualMessage("id", "validate.invalidValue");
throw new ValidationException(result);
}
// Generate an Xid if necessary
if (StringUtils.isEmpty(vo.getXid()))
vo.setXid(dao.generateUniqueXid());
for (DataPointChangeDefinition def : changeDefinitions) {
def.preInsert(vo);
}
ensureValid(vo);
dao.insert(vo);
List<AbstractPointEventDetectorVO> detectors = new ArrayList<>();
for (DataPointChangeDefinition def : changeDefinitions) {
for (var detector : def.postInsert(vo)) {
if (detector.isNew()) {
log.warn("Detector added via postInsert hook was not saved");
} else if (detector.getDataPoint().getId() != vo.getId()) {
log.warn("Detector added via postInsert hook was for a different data point");
} else {
detectors.add(detector);
}
}
}
if (vo.isEnabled()) {
// the data point cannot have detectors if it was just inserted, don't query for detectors
getRuntimeManager().startDataPoint(new DataPointWithEventDetectors(vo, detectors));
}
return vo;
}
use of com.infiniteautomation.mango.util.exception.ValidationException in project ma-core-public by infiniteautomation.
the class DataSourceService method copy.
/**
* Copy a data source and optionally its points
*/
public DataSourceVO copy(String xid, String copyXid, String copyName, String copyDeviceName, boolean enabled, boolean copyPoints) throws PermissionException, NotFoundException {
DataSourceVO existing = get(xid);
PermissionHolder user = Common.getUser();
ensureCreatePermission(user, existing);
// Determine the new name
String newName;
if (StringUtils.isEmpty(copyName)) {
newName = StringUtils.abbreviate(TranslatableMessage.translate(Common.getTranslations(), "common.copyPrefix", existing.getName()), 40);
} else {
newName = copyName;
}
// Determine the new xid
String newXid;
if (StringUtils.isEmpty(copyXid)) {
newXid = DataSourceDao.getInstance().generateUniqueXid();
} else {
newXid = copyXid;
}
String newDeviceName;
if (StringUtils.isEmpty(copyDeviceName)) {
newDeviceName = existing.getName();
} else {
newDeviceName = copyDeviceName;
}
// Ensure device name is valid
if (StringValidation.isLengthGreaterThan(newDeviceName, 255)) {
ProcessResult result = new ProcessResult();
result.addMessage("deviceName", new TranslatableMessage("validate.notLongerThan", 255));
throw new ValidationException(result);
}
DataSourceVO copy = (DataSourceVO) existing.copy();
copy.setId(Common.NEW_ID);
copy.setName(newName);
copy.setXid(newXid);
copy.setEnabled(enabled);
ensureValid(copy);
// Save it
insert(copy);
if (copyPoints) {
// Copy the points from this data source
dataPointService.copyDataSourcePoints(existing.getId(), copy.getId(), newDeviceName);
}
return get(newXid);
}
use of com.infiniteautomation.mango.util.exception.ValidationException in project ma-core-public by infiniteautomation.
the class SystemPermissionService method update.
/**
*/
public void update(MangoPermission permission, PermissionDefinition def) throws ValidationException {
Objects.requireNonNull(def, "Permission definition cannot be null");
permissionService.ensureAdminRole(Common.getUser());
ProcessResult validation = new ProcessResult();
if (permission == null) {
validation.addContextualMessage("permission", "validate.required");
throw new ValidationException(validation);
}
permission.getRoles().stream().flatMap(Collection::stream).forEach(role -> {
try {
roleService.get(role.getXid());
} catch (NotFoundException e) {
validation.addGenericMessage("validate.role.notFound", role.getXid());
}
});
if (validation.getHasMessages()) {
throw new ValidationException(validation);
}
// Execute in transaction as def.setPermission may make database calls
dao.doInTransaction(tx -> {
dao.update(def.getPermissionTypeName(), def.getPermission(), permission);
def.setPermission(permission);
});
this.eventPublisher.publishEvent(new SystemPermissionUpdated(def));
}
Aggregations