use of com.serotonin.m2m2.web.dwr.util.DwrPermission in project ma-modules-public by infiniteautomation.
the class ReportsDwr method deleteReportInstance.
@DwrPermission(custom = ReportPermissionDefinition.PERMISSION)
public List<ReportInstance> deleteReportInstance(int instanceId) {
User user = Common.getUser();
ReportDao reportDao = ReportDao.instance;
reportDao.deleteReportInstance(instanceId, user.getId());
return getReportInstances(user);
}
use of com.serotonin.m2m2.web.dwr.util.DwrPermission in project ma-modules-public by infiniteautomation.
the class M2MReportImportDwr method generateJson.
@DwrPermission(admin = true)
public ProcessResult generateJson(String driverClassname, String connectionUrl, String username, String password) {
ProcessResult result = new ProcessResult();
// Validate the connection
try {
DriverManager.registerDriver((Driver) Class.forName(driverClassname).newInstance());
Connection connection = DriverManager.getConnection(connectionUrl, username, password);
connection.setAutoCommit(false);
// Test the connection.
DatabaseMetaData md = connection.getMetaData();
String productName = md.getDatabaseProductName();
DatabaseType type = DatabaseType.DERBY;
if (productName.equalsIgnoreCase("mysql")) {
type = DatabaseType.MYSQL;
} else if (productName.equalsIgnoreCase("Apache Derby")) {
type = DatabaseType.DERBY;
} else if (productName.contains("Microsoft")) {
type = DatabaseType.MSSQL;
} else if (productName.equalsIgnoreCase("h2")) {
type = DatabaseType.H2;
} else if (productName.equalsIgnoreCase("postgressql")) {
type = DatabaseType.MYSQL;
}
// Get the reports
M2MReportDao dao = new M2MReportDao(connection, type);
List<M2MReportVO> legacyReports = dao.getReports();
StringWriter stringWriter = new StringWriter();
JsonWriter jsonWriter = new JsonWriter(Common.JSON_CONTEXT, stringWriter);
jsonWriter.setPrettyIndent(3);
jsonWriter.setPrettyOutput(true);
int cnt = 0;
jsonWriter.append("{");
jsonWriter.indent();
jsonWriter.quote("reports");
jsonWriter.append(": [");
// Convert the reports to our VOs
for (M2MReportVO legacyReport : legacyReports) {
legacyReport.jsonWrite(jsonWriter, dao);
cnt++;
if (cnt < legacyReports.size())
jsonWriter.append(",");
}
jsonWriter.append(']');
jsonWriter.append("}");
jsonWriter.flush();
result.addData("reports", stringWriter.toString());
} catch (Exception e) {
result.addContextualMessage("connectionUrl", "common.default", e.getMessage());
return result;
}
return result;
}
use of com.serotonin.m2m2.web.dwr.util.DwrPermission in project ma-modules-public by infiniteautomation.
the class VirtualEditDwr method saveVirtualDataSource.
@DwrPermission(user = true)
public ProcessResult saveVirtualDataSource(BasicDataSourceVO basic, int updatePeriods, int updatePeriodType, boolean polling) {
VirtualDataSourceVO ds = (VirtualDataSourceVO) Common.getUser().getEditDataSource();
setBasicProps(ds, basic);
ds.setUpdatePeriods(updatePeriods);
ds.setUpdatePeriodType(updatePeriodType);
ds.setPolling(polling);
return tryDataSourceSave(ds);
}
use of com.serotonin.m2m2.web.dwr.util.DwrPermission in project ma-modules-public by infiniteautomation.
the class ScheduledEventsDwr method saveScheduledEvent.
@DwrPermission(user = true)
public ProcessResult saveScheduledEvent(int id, String xid, String alias, int alarmLevel, int scheduleType, boolean returnToNormal, boolean disabled, int activeYear, int activeMonth, int activeDay, int activeHour, int activeMinute, int activeSecond, String activeCron, int inactiveYear, int inactiveMonth, int inactiveDay, int inactiveHour, int inactiveMinute, int inactiveSecond, String inactiveCron) {
Permissions.ensureDataSourcePermission(Common.getUser());
// Validate the given information. If there is a problem, return an appropriate error message.
ScheduledEventVO se = new ScheduledEventVO();
se.setId(id);
se.setXid(xid);
se.setAlias(alias);
se.setAlarmLevel(alarmLevel);
se.setScheduleType(scheduleType);
se.setReturnToNormal(returnToNormal);
se.setDisabled(disabled);
se.setActiveYear(activeYear);
se.setActiveMonth(activeMonth);
se.setActiveDay(activeDay);
se.setActiveHour(activeHour);
se.setActiveMinute(activeMinute);
se.setActiveSecond(activeSecond);
se.setActiveCron(activeCron);
se.setInactiveYear(inactiveYear);
se.setInactiveMonth(inactiveMonth);
se.setInactiveDay(inactiveDay);
se.setInactiveHour(inactiveHour);
se.setInactiveMinute(inactiveMinute);
se.setInactiveSecond(inactiveSecond);
se.setInactiveCron(inactiveCron);
ProcessResult response = new ProcessResult();
ScheduledEventDao scheduledEventDao = ScheduledEventDao.instance;
if (StringUtils.isBlank(xid))
response.addContextualMessage("xid", "validate.required");
else if (!scheduledEventDao.isXidUnique(xid, id))
response.addContextualMessage("xid", "validate.xidUsed");
se.validate(response);
// Save the scheduled event
if (!response.getHasMessages())
RTMDefinition.instance.saveScheduledEvent(se);
response.addData("seId", se.getId());
return response;
}
use of com.serotonin.m2m2.web.dwr.util.DwrPermission in project ma-modules-public by infiniteautomation.
the class PointLinksDwr method validateScript.
@DwrPermission(user = true)
public ProcessResult validateScript(String script, int sourcePointId, int targetPointId, ScriptPermissions permissions, int logLevel) {
ProcessResult response = new ProcessResult();
TranslatableMessage message;
DataPointRT source = Common.runtimeManager.getDataPoint(sourcePointId);
if (source == null) {
DataPointVO sourceVo = DataPointDao.instance.getDataPoint(sourcePointId, false);
if (sourceVo == null) {
message = new TranslatableMessage("pointLinks.validate.sourceRequired");
response.addMessage("script", message);
return response;
}
if (sourceVo.getDefaultCacheSize() == 0)
sourceVo.setDefaultCacheSize(1);
source = new DataPointRT(sourceVo, sourceVo.getPointLocator().createRuntime(), DataSourceDao.instance.getDataSource(sourceVo.getDataSourceId()), null);
source.resetValues();
}
DataPointRT target = Common.runtimeManager.getDataPoint(targetPointId);
if (target == null) {
DataPointVO targetVo = DataPointDao.instance.getDataPoint(targetPointId, false);
if (targetVo == null) {
message = new TranslatableMessage("pointLinks.validate.targetRequired");
response.addMessage("script", message);
return response;
}
if (targetVo.getDefaultCacheSize() == 0)
targetVo.setDefaultCacheSize(1);
target = new DataPointRT(targetVo, targetVo.getPointLocator().createRuntime(), DataSourceDao.instance.getDataSource(targetVo.getDataSourceId()), null);
target.resetValues();
}
Map<String, IDataPointValueSource> context = new HashMap<String, IDataPointValueSource>();
context.put(PointLinkRT.CONTEXT_SOURCE_VAR_NAME, source);
context.put(PointLinkRT.CONTEXT_TARGET_VAR_NAME, target);
int targetDataType = target.getDataTypeId();
final StringWriter scriptOut = new StringWriter();
final PrintWriter scriptWriter = new PrintWriter(scriptOut);
ScriptLog scriptLog = new ScriptLog(scriptWriter, logLevel);
final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/YYY HH:mm:ss");
ScriptPointValueSetter loggingSetter = new ScriptPointValueSetter(permissions) {
@Override
public void set(IDataPointValueSource point, Object value, long timestamp, String annotation) {
DataPointRT dprt = (DataPointRT) point;
if (!dprt.getVO().getPointLocator().isSettable()) {
scriptOut.append("Point " + dprt.getVO().getExtendedName() + " not settable.");
return;
}
if (!Permissions.hasPermission(dprt.getVO().getSetPermission(), permissions.getDataPointSetPermissions())) {
scriptOut.write(new TranslatableMessage("pointLinks.setTest.permissionDenied", dprt.getVO().getXid()).translate(Common.getTranslations()));
return;
}
scriptOut.append("Setting point " + dprt.getVO().getName() + " to " + value + " @" + sdf.format(new Date(timestamp)) + "\r\n");
}
@Override
protected void setImpl(IDataPointValueSource point, Object value, long timestamp, String annotation) {
// not really setting
}
};
try {
CompiledScript compiledScript = CompiledScriptExecutor.compile(script);
PointValueTime pvt = CompiledScriptExecutor.execute(compiledScript, context, null, System.currentTimeMillis(), targetDataType, -1, permissions, scriptWriter, scriptLog, loggingSetter, null, true);
if (pvt.getValue() == null)
message = new TranslatableMessage("event.pointLink.nullResult");
else if (pvt.getValue() == CompiledScriptExecutor.UNCHANGED)
message = new TranslatableMessage("pointLinks.validate.successNoValue");
else if (pvt.getTime() == -1)
message = new TranslatableMessage("pointLinks.validate.success", pvt.getValue());
else
message = new TranslatableMessage("pointLinks.validate.successTs", pvt.getValue(), Functions.getTime(pvt.getTime()));
// Add the script logging output
response.addData("out", scriptOut.toString().replaceAll("\n", "<br/>"));
} catch (ScriptException e) {
message = new TranslatableMessage("pointLinks.validate.scriptError", e.getMessage());
} catch (ResultTypeException e) {
message = e.getTranslatableMessage();
}
response.addMessage("script", message);
return response;
}
Aggregations