Search in sources :

Example 6 with DwrPermission

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);
}
Also used : User(com.serotonin.m2m2.vo.User) ReportDao(com.serotonin.m2m2.reports.ReportDao) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 7 with DwrPermission

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;
}
Also used : DatabaseType(com.serotonin.m2m2.db.DatabaseProxy.DatabaseType) StringWriter(java.io.StringWriter) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) Connection(java.sql.Connection) DatabaseMetaData(java.sql.DatabaseMetaData) JsonWriter(com.serotonin.json.JsonWriter) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 8 with DwrPermission

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);
}
Also used : VirtualDataSourceVO(com.serotonin.m2m2.virtual.vo.VirtualDataSourceVO) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 9 with DwrPermission

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;
}
Also used : ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 10 with DwrPermission

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;
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) CompiledScript(javax.script.CompiledScript) ScriptPointValueSetter(com.serotonin.m2m2.rt.script.ScriptPointValueSetter) HashMap(java.util.HashMap) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) ScriptLog(com.serotonin.m2m2.rt.script.ScriptLog) Date(java.util.Date) ScriptException(javax.script.ScriptException) ResultTypeException(com.serotonin.m2m2.rt.script.ResultTypeException) StringWriter(java.io.StringWriter) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) IDataPointValueSource(com.serotonin.m2m2.rt.dataImage.IDataPointValueSource) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) SimpleDateFormat(java.text.SimpleDateFormat) PrintWriter(java.io.PrintWriter) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Aggregations

DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)220 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)126 User (com.serotonin.m2m2.vo.User)56 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)47 ArrayList (java.util.ArrayList)35 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)24 HashMap (java.util.HashMap)21 StringStringPair (com.serotonin.db.pair.StringStringPair)11 SystemSettingsDao (com.serotonin.m2m2.db.dao.SystemSettingsDao)11 DuplicateKeyException (org.springframework.dao.DuplicateKeyException)10 IOException (java.io.IOException)9 DateTime (org.joda.time.DateTime)9 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)9 AbstractVO (com.serotonin.m2m2.vo.AbstractVO)8 AnonymousUser (com.serotonin.m2m2.vo.AnonymousUser)8 LinkedHashMap (java.util.LinkedHashMap)8 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)7 ShareUser (com.serotonin.m2m2.view.ShareUser)7 ResultsWithTotal (com.serotonin.m2m2.db.dao.ResultsWithTotal)6 ReportVO (com.serotonin.m2m2.reports.vo.ReportVO)6