use of com.serotonin.m2m2.vo.dataSource.DataSourceVO in project ma-core-public by infiniteautomation.
the class DataSourceDwr method getCopy.
@DwrPermission(user = true)
@Override
public ProcessResult getCopy(int id) {
// Get a Full Copy
DataSourceVO<?> vo = dao.getFull(id);
ProcessResult response = new ProcessResult();
String name = StringUtils.abbreviate(TranslatableMessage.translate(getTranslations(), "common.copyPrefix", vo.getName()), 40);
// Setup the Copy
DataSourceVO<?> copy = vo.copy();
copy.setId(Common.NEW_ID);
copy.setName(name);
copy.setXid(dao.generateUniqueXid());
response.addData("vo", copy);
return response;
}
use of com.serotonin.m2m2.vo.dataSource.DataSourceVO 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.dataSource.DataSourceVO 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.dataSource.DataSourceVO in project ma-core-public by infiniteautomation.
the class DataPurge method purgePoint.
private void purgePoint(DataPointVO dataPoint, boolean countPointValues) {
if (dataPoint.getLoggingType() == DataPointVO.LoggingTypes.NONE) {
// If there is no logging, then there should be no data, unless logging was just changed to none. In either
// case, it's ok to delete everything.
log.info("Purging all data for data point with id " + dataPoint.getId() + " because it is set to logging type NONE.");
if (Common.runtimeManager.getState() == RuntimeManager.RUNNING) {
if (countPointValues)
deletedSamples += Common.runtimeManager.purgeDataPointValues(dataPoint.getId());
else {
if (Common.runtimeManager.purgeDataPointValuesWithoutCount(dataPoint.getId()))
anyDeletedSamples = true;
}
}
} else {
// Determine the purging properties to use.
int purgeType;
int purgePeriod;
if (dataPoint.isPurgeOverride()) {
purgeType = dataPoint.getPurgeType();
purgePeriod = dataPoint.getPurgePeriod();
} else {
// Check the data source level.
DataSourceVO<?> ds = DataSourceDao.instance.getDataSource(dataPoint.getDataSourceId());
if (ds.isPurgeOverride()) {
purgeType = ds.getPurgeType();
purgePeriod = ds.getPurgePeriod();
} else {
// Use the system settings.
purgeType = SystemSettingsDao.getIntValue(SystemSettingsDao.POINT_DATA_PURGE_PERIOD_TYPE);
purgePeriod = SystemSettingsDao.getIntValue(SystemSettingsDao.POINT_DATA_PURGE_PERIODS);
}
}
List<PurgeFilter> purgeFilters = new ArrayList<PurgeFilter>();
for (PurgeFilterDefinition pfd : ModuleRegistry.getDefinitions(PurgeFilterDefinition.class)) purgeFilters.add(pfd.getPurgeFilter());
// No matter when this purge actually runs, we want it to act like it's midnight.
DateTime cutoff = new DateTime(runtime);
cutoff = DateUtils.truncateDateTime(cutoff, Common.TimePeriods.DAYS);
cutoff = DateUtils.minus(cutoff, purgeType, purgePeriod);
if (Common.runtimeManager.getState() == RuntimeManager.RUNNING) {
long millis = cutoff.getMillis();
for (PurgeFilter pf : purgeFilters) millis = pf.adjustPurgeTime(dataPoint, millis);
if (countPointValues)
deletedSamples += Common.runtimeManager.purgeDataPointValues(dataPoint.getId(), cutoff.getMillis());
else {
if (Common.runtimeManager.purgeDataPointValuesWithoutCount(dataPoint.getId(), cutoff.getMillis()))
anyDeletedSamples = true;
}
}
// If this is an image data type, get the point value ids.
if (dataPoint.getPointLocator().getDataTypeId() == DataTypes.IMAGE)
fileIds.addAll(pointValueDao.getFiledataIds(dataPoint.getId()));
}
}
use of com.serotonin.m2m2.vo.dataSource.DataSourceVO in project ma-core-public by infiniteautomation.
the class DataSourceQuery method query.
public List<DataSourceWrapper> query(String query) {
ASTNode root = parser.parse(query);
BaseSqlQuery<DataSourceVO<?>> sqlQuery = DataSourceDao.instance.createQuery(root, true);
List<DataSourceVO<?>> dataSources = sqlQuery.immediateQuery();
List<DataSourceWrapper> results = new ArrayList<DataSourceWrapper>();
// Filter on permissions
for (DataSourceVO<?> ds : dataSources) {
if (Permissions.hasDataSourcePermission(permissions.getDataSourcePermissions(), ds)) {
List<DataPointWrapper> points = getPointsForSource(ds);
results.add(new DataSourceWrapper(ds, points));
}
}
return results;
}
Aggregations