use of com.serotonin.m2m2.vo.DataPointVO in project ma-core-public by infiniteautomation.
the class PublisherVO method validate.
public void validate(ProcessResult response) {
if (StringUtils.isBlank(name))
response.addContextualMessage("name", "validate.required");
if (StringValidation.isLengthGreaterThan(name, 40))
response.addContextualMessage("name", "validate.nameTooLong");
if (StringUtils.isBlank(xid))
response.addContextualMessage("xid", "validate.required");
else if (!PublisherDao.instance.isXidUnique(xid, id))
response.addContextualMessage("xid", "validate.xidUsed");
else if (StringValidation.isLengthGreaterThan(xid, 50))
response.addContextualMessage("xid", "validate.notLongerThan", 50);
if (sendSnapshot) {
if (snapshotSendPeriods <= 0)
response.addContextualMessage("snapshotSendPeriods", "validate.greaterThanZero");
if (!Common.TIME_PERIOD_CODES.isValidId(snapshotSendPeriodType, Common.TimePeriods.MILLISECONDS, Common.TimePeriods.DAYS, Common.TimePeriods.WEEKS, Common.TimePeriods.MONTHS, Common.TimePeriods.YEARS))
response.addContextualMessage("snapshotSendPeriodType", "validate.invalidValue");
}
if (cacheWarningSize < 1)
response.addContextualMessage("cacheWarningSize", "validate.greaterThanZero");
if (cacheDiscardSize <= cacheWarningSize)
response.addContextualMessage("cacheDiscardSize", "validate.publisher.cacheDiscardSize");
Set<Integer> set = new HashSet<>();
ListIterator<T> it = points.listIterator();
while (it.hasNext()) {
T point = it.next();
int pointId = point.getDataPointId();
if (set.contains(pointId)) {
DataPointVO vo = DataPointDao.instance.getDataPoint(pointId, false);
response.addGenericMessage("validate.publisher.duplicatePoint", vo.getExtendedName(), vo.getXid());
} else {
String dpXid = DataPointDao.instance.getXidById(pointId);
if (dpXid == null)
it.remove();
else
set.add(pointId);
}
}
}
use of com.serotonin.m2m2.vo.DataPointVO in project ma-core-public by infiniteautomation.
the class ImportTask method processUpdatedDetectors.
private void processUpdatedDetectors(Map<String, DataPointVO> eventDetectorMap) {
for (DataPointVO dpvo : eventDetectorMap.values()) {
// We can't really guarantee that the import didnt' also ocntain event detectors on the point or that the
// Data Point VO's we retrieved are still correct, so mix in the detectors to the point out of the database.
boolean isNew = true;
DataPointVO saved = DataPointDao.instance.getDataPoint(dpvo.getId(), false);
DataPointDao.instance.setEventDetectors(saved);
for (AbstractPointEventDetectorVO<?> eventDetector : dpvo.getEventDetectors()) {
Iterator<AbstractPointEventDetectorVO<?>> iter = saved.getEventDetectors().iterator();
while (iter.hasNext()) {
AbstractPointEventDetectorVO<?> next = iter.next();
if (eventDetector.getXid().equals(next.getXid())) {
// Same detector, replace it
eventDetector.setId(next.getId());
isNew = false;
iter.remove();
break;
}
}
// Having removed the old versions, add the new.
saved.getEventDetectors().add(eventDetector);
}
// Save the data point
Common.runtimeManager.saveDataPoint(saved);
for (AbstractPointEventDetectorVO<?> modified : dpvo.getEventDetectors()) importContext.addSuccessMessage(isNew, "emport.eventDetector.prefix", modified.getXid());
}
}
use of com.serotonin.m2m2.vo.DataPointVO in project ma-core-public by infiniteautomation.
the class DataPointImporter method importImpl.
@Override
protected void importImpl() {
String xid = json.getString("xid");
DataPointVO vo = null;
DataSourceVO<?> dsvo = null;
if (StringUtils.isBlank(xid))
xid = ctx.getDataPointDao().generateUniqueXid();
else
vo = ctx.getDataPointDao().getDataPoint(xid);
if (vo == null) {
// Locate the data source for the point.
String dsxid = json.getString("dataSourceXid");
dsvo = ctx.getDataSourceDao().getDataSource(dsxid);
if (dsvo == null)
addFailureMessage("emport.dataPoint.badReference", xid);
else {
vo = new DataPointVO();
vo.setXid(xid);
vo.setDataSourceId(dsvo.getId());
vo.setDataSourceXid(dsxid);
vo.setPointLocator(dsvo.createPointLocator());
vo.setEventDetectors(new ArrayList<AbstractPointEventDetectorVO<?>>(0));
// Not needed as it will be set via the template or JSON or it exists in the DB already: vo.setTextRenderer(new PlainRenderer());
}
}
if (vo != null) {
try {
DataPointPropertiesTemplateVO template = null;
if (json.containsKey("templateXid")) {
String templateXid = json.getString("templateXid");
if (!StringUtils.isEmpty(templateXid))
template = (DataPointPropertiesTemplateVO) TemplateDao.instance.getByXid(templateXid);
}
// Read into the VO to get all properties
ctx.getReader().readInto(vo, json);
// Override the settings if we need to
if (template != null) {
template.updateDataPointVO(vo);
}
// If the name is not provided, default to the XID
if (StringUtils.isBlank(vo.getName()))
vo.setName(xid);
// If the chart colour is null provide default of '' to handle legacy code that sets colour to null
if (vo.getChartColour() == null)
vo.setChartColour("");
// 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.dataPoint.prefix", xid);
else {
// We will always override the DS Info with the one from the XID Lookup
dsvo = ctx.getDataSourceDao().getDataSource(vo.getDataSourceXid());
if (dsvo == null)
addFailureMessage("emport.dataPoint.badReference", xid);
else {
// Compare this point to the existing point in DB to ensure
// that we aren't moving a point to a different type of Data Source
DataPointVO oldPoint = ctx.getDataPointDao().getDataPoint(vo.getId(), false);
// Does the old point have a different data source?
if (oldPoint != null && (oldPoint.getDataSourceId() != dsvo.getId())) {
vo.setDataSourceId(dsvo.getId());
vo.setDataSourceName(dsvo.getName());
}
}
boolean isNew = vo.isNew();
try {
if (Common.runtimeManager.getState() == RuntimeManager.RUNNING) {
Common.runtimeManager.saveDataPoint(vo);
if (hierarchyList != null && json.containsKey(PATH)) {
String path = json.getString(PATH);
if (StringUtils.isNotEmpty(path))
hierarchyList.add(new DataPointSummaryPathPair(new DataPointSummary(vo), path));
}
addSuccessMessage(isNew, "emport.dataPoint.prefix", xid);
} else {
addFailureMessage(new ProcessMessage("Runtime Manager not running point with xid: " + xid + " not saved."));
}
} catch (LicenseViolatedException e) {
addFailureMessage(new ProcessMessage(e.getErrorMessage()));
}
}
} catch (TranslatableJsonException e) {
addFailureMessage("emport.dataPoint.prefix", xid, e.getMsg());
} catch (JsonException e) {
addFailureMessage("emport.dataPoint.prefix", xid, getJsonExceptionMessage(e));
}
}
}
use of com.serotonin.m2m2.vo.DataPointVO in project ma-core-public by infiniteautomation.
the class BaseDataSourceController method handleRequestInternal.
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
DataSourceVO<?> dataSourceVO = null;
User user = Common.getUser(request);
final boolean admin = Permissions.hasAdmin(user);
// Create the model.
Map<String, Object> model = new HashMap<>();
// Get the id.
int id = Common.NEW_ID;
String idStr = request.getParameter("dsid");
// Check for a data point id
String pidStr = request.getParameter("dpid");
// Get the Type
String type = request.getParameter("typeId");
// For legacy use of PID from the point details page
if (request.getParameter("pid") != null)
pidStr = request.getParameter("pid");
// If type and dsid and pid are null then don't perform any actions here
if ((idStr == null) && (pidStr == null) && (type == null)) {
return new ModelAndView(getViewName(), model);
}
// Is this a new datasource?
if (type != null) {
if (StringUtils.isBlank(type)) {
model.put("key", "dsEdit.error.noTypeProvided");
return new ModelAndView(new RedirectView(errorViewName), model);
}
// Prepare the DS
if (!admin)
Permissions.ensureDataSourcePermission(user);
// A new data source
DataSourceDefinition def = ModuleRegistry.getDataSourceDefinition(type);
if (def == null)
return new ModelAndView(new RedirectView(errorViewName));
dataSourceVO = def.baseCreateDataSourceVO();
dataSourceVO.setId(Common.NEW_ID);
dataSourceVO.setXid(DataSourceDao.instance.generateUniqueXid());
}
// Are we going to be making a copy?
String copyStr = request.getParameter("copy");
// Are we editing a point?
if (pidStr != null) {
int pid = Integer.parseInt(pidStr);
DataPointVO dp = DataPointDao.instance.getDataPoint(pid);
if (dp == null) {
// The requested data point doesn't exist. Return to the list page.
model.put("key", "dsEdit.error.pointDNE");
model.put("params", pid);
return new ModelAndView(new RedirectView(errorViewName), model);
}
id = dp.getDataSourceId();
if (copyStr != null) {
Boolean copy = Boolean.parseBoolean(copyStr);
model.put("copy", copy);
if (copy) {
String name = StringUtils.abbreviate(TranslatableMessage.translate(Common.getTranslations(), "common.copyPrefix", dp.getName()), 40);
// Setup the Copy
DataPointVO copyDp = dp.copy();
copyDp.setId(Common.NEW_ID);
copyDp.setName(name);
copyDp.setXid(DataPointDao.instance.generateUniqueXid());
model.put("dataPoint", copyDp);
}
} else
model.put("dataPoint", dp);
}
if (idStr != null) {
// An existing configuration or copy
id = Integer.parseInt(idStr);
}
if (id != Common.NEW_ID) {
dataSourceVO = Common.runtimeManager.getDataSource(id);
if (copyStr != null) {
Boolean copy = Boolean.parseBoolean(copyStr);
model.put("copy", copy);
if (copy) {
String name = StringUtils.abbreviate(TranslatableMessage.translate(Common.getTranslations(), "common.copyPrefix", dataSourceVO.getName()), 40);
// Setup the Copy
dataSourceVO = dataSourceVO.copy();
dataSourceVO.setId(Common.NEW_ID);
dataSourceVO.setName(name);
dataSourceVO.setXid(DataSourceDao.instance.generateUniqueXid());
}
}
if (dataSourceVO == null) {
// The requested data source doesn't exist. Return to the list page.
model.put("key", "dsEdit.error.dataSourceDNE");
model.put("params", id);
return new ModelAndView(new RedirectView(errorViewName), model);
}
if (!admin)
Permissions.ensureDataSourcePermission(user, dataSourceVO);
}
// Set the id of the data source in the user object for the DWR.
user.setEditDataSource(dataSourceVO);
// The data source
if (dataSourceVO != null) {
model.put("dataSource", dataSourceVO);
model.put("modulePath", dataSourceVO.getDefinition().getModule().getWebPath());
dataSourceVO.addEditContext(model);
}
// Reference data
try {
model.put("commPorts", Common.serialPortManager.getFreeCommPorts());
} catch (SerialPortConfigException e) {
model.put("commPortError", e.getMessage());
}
List<DataPointVO> allPoints = DataPointDao.instance.getDataPoints(DataPointExtendedNameComparator.instance, false);
List<DataPointVO> userPoints = new LinkedList<>();
List<DataPointVO> analogPoints = new LinkedList<>();
for (DataPointVO dp : allPoints) {
if (admin || Permissions.hasDataPointReadPermission(user, dp)) {
userPoints.add(dp);
if (dp.getPointLocator().getDataTypeId() == DataTypes.NUMERIC)
analogPoints.add(dp);
}
}
model.put("userPoints", userPoints);
model.put("analogPoints", analogPoints);
return new ModelAndView(getViewName(), model);
}
use of com.serotonin.m2m2.vo.DataPointVO in project ma-core-public by infiniteautomation.
the class DataPointDetailsController method handleRequest.
@Override
public View handleRequest(HttpServletRequest request, HttpServletResponse response, Map<String, Object> model) throws Exception {
User user = Common.getHttpUser();
int id = -1;
if (user.getEditPoint() != null)
id = user.getEditPoint().getId();
DataPointDao dataPointDao = DataPointDao.instance;
String idStr = request.getParameter("dpid");
DataPointVO point = null;
if (StringUtils.equals(idStr, "exception"))
throw new IOException("testing");
else if (StringUtils.equals(idStr, "permission-exception"))
throw new PermissionException(new TranslatableMessage("common.default", "Testing"), user);
if (StringUtils.isBlank(idStr)) {
// Check for pedid (point event detector id)
String pedStr = request.getParameter("pedid");
if (StringUtils.isBlank(pedStr)) {
// Check if an XID was provided.
String xid = request.getParameter("dpxid");
if (!StringUtils.isBlank(xid)) {
model.put("currentXid", xid);
point = dataPointDao.getDataPoint(xid);
id = point == null ? -2 : point.getId();
}
} else {
int pedid = Integer.parseInt(pedStr);
id = EventDetectorDao.instance.getSourceId(pedid, EventType.EventTypeNames.DATA_POINT);
}
} else
id = Integer.parseInt(idStr);
// Find accessible points for the goto list
List<DataPointSummary> userPoints = ControllerUtils.addPointListDataToModel(user, id, model);
// Get the point.
if (point == null && id != -1)
point = dataPointDao.getDataPoint(id);
if (point == null && id != -2 && /* -2 means an explicit XID was provided but not found */
!userPoints.isEmpty()) {
// Load at least 1 point, there may be many points but some might not actually load if thier data source DNE anymore
for (DataPointSummary userPoint : userPoints) {
point = dataPointDao.getDataPoint(userPoint.getId());
if (point != null)
break;
}
}
if (point != null) {
// Check permissions
Permissions.ensureDataPointReadPermission(user, point);
// Put the point in the model.
model.put("point", point);
// Get the users that have access to this point.
List<User> allUsers = UserDao.instance.getUsers();
List<Map<String, Object>> users = new LinkedList<>();
Map<String, Object> userData;
int accessType;
for (User mangoUser : allUsers) {
accessType = Permissions.getDataPointAccessType(mangoUser, point);
if (accessType != Permissions.DataPointAccessTypes.NONE) {
userData = new HashMap<>();
userData.put("user", mangoUser);
userData.put("accessType", accessType);
users.add(userData);
}
}
model.put("users", users);
// Determine whether the link to edit the point should be displayed
model.put("pointEditor", Permissions.hasDataSourcePermission(user, point.getDataSourceId()));
// Put the events in the model.
model.put("events", EventDao.instance.getEventsForDataPoint(id, user.getId()));
// Put the default history table count into the model. Default to 10.
int historyLimit = 10;
if (point.getChartRenderer() instanceof TableChartRenderer)
historyLimit = ((TableChartRenderer) point.getChartRenderer()).getLimit();
else if (point.getChartRenderer() instanceof ImageFlipbookRenderer)
historyLimit = ((ImageFlipbookRenderer) point.getChartRenderer()).getLimit();
model.put("historyLimit", historyLimit);
// Determine our image chart rendering capabilities.
if (ImageChartRenderer.getDefinition().supports(point.getPointLocator().getDataTypeId())) {
// This point can render an image chart. Carry on...
int periodType = Common.TimePeriods.DAYS;
int periodCount = 1;
if (point.getChartRenderer() instanceof ImageChartRenderer) {
ImageChartRenderer r = (ImageChartRenderer) point.getChartRenderer();
periodType = r.getTimePeriod();
periodCount = r.getNumberOfPeriods();
}
model.put("periodType", periodType);
model.put("periodCount", periodCount);
}
// Determine out flipbook rendering capabilities
if (ImageFlipbookRenderer.getDefinition().supports(point.getPointLocator().getDataTypeId()))
model.put("flipbookLimit", 10);
// Set the point in the session for the dwr.
user.setEditPoint(point);
model.put("currentXid", point.getXid());
model.put("hierPath", CollectionUtils.implode(dataPointDao.getPointHierarchy(true).getPath(id), " » "));
}
return null;
}
Aggregations