Search in sources :

Example 71 with TranslatableMessage

use of com.serotonin.m2m2.i18n.TranslatableMessage in project ma-core-public by infiniteautomation.

the class DataSourceDwr method get.

@DwrPermission(user = true)
@Override
public ProcessResult get(int id) {
    ProcessResult response;
    try {
        if (id > 0) {
            response = super.get(id);
            // Kludge for modules to be able to use a default edit point for some of their tools (Bacnet for example needs this for adding lots of points)
            // This is an issue for opening AllDataPoints Point because it opens the Datasource too.
            // TODO to fix this we need to fix DataSourceEditDwr to not save the editing DataPoint state in the User, this will propogate into existing modules...
            DataSourceVO<?> vo = (DataSourceVO<?>) response.getData().get("vo");
            // Quick fix to ensure we don't keep the edit point around if we have switched data sources
            if ((Common.getUser().getEditPoint() == null) || (Common.getUser().getEditPoint().getDataSourceId() != vo.getId()) || (Common.getUser().getEditPoint().getDataSourceTypeName() != vo.getDefinition().getDataSourceTypeName())) {
                DataPointVO dp = new DataPointVO();
                dp.setXid(DataPointDao.instance.generateUniqueXid());
                dp.setDataSourceId(vo.getId());
                dp.setDataSourceTypeName(vo.getDefinition().getDataSourceTypeName());
                dp.setDeviceName(vo.getName());
                dp.setEventDetectors(new ArrayList<AbstractPointEventDetectorVO<?>>(0));
                dp.defaultTextRenderer();
                dp.setXid(DataPointDao.instance.generateUniqueXid());
                dp.setPointLocator(vo.createPointLocator());
                Common.getUser().setEditPoint(dp);
            }
        } else {
            throw new ShouldNeverHappenException("Unable to get a new DataSource.");
        }
        // Setup the page info
        response.addData("editPagePath", ((DataSourceVO<?>) response.getData().get("vo")).getDefinition().getModule().getWebPath() + "/" + ((DataSourceVO<?>) response.getData().get("vo")).getDefinition().getEditPagePath());
        response.addData("statusPagePath", ((DataSourceVO<?>) response.getData().get("vo")).getDefinition().getModule().getWebPath() + "/" + ((DataSourceVO<?>) response.getData().get("vo")).getDefinition().getStatusPagePath());
    } catch (Exception e) {
        LOG.error(e.getMessage());
        response = new ProcessResult();
        response.addMessage(new TranslatableMessage("table.error.dwr", e.getMessage()));
    }
    return response;
}
Also used : DataSourceVO(com.serotonin.m2m2.vo.dataSource.DataSourceVO) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 72 with TranslatableMessage

use of com.serotonin.m2m2.i18n.TranslatableMessage in project ma-core-public by infiniteautomation.

the class DataSourceEditDwr method validatePoint.

protected ProcessResult validatePoint(int id, String xid, String name, PointLocatorVO<?> locator, DataPointDefaulter defaulter, boolean includePointList) {
    ProcessResult response = new ProcessResult();
    // This saving of the point into the User is a bad idea, need to rework to
    // pass the point back and forth to page.
    DataPointVO dp = getPoint(id, defaulter);
    dp.setXid(xid);
    dp.setName(name);
    dp.setPointLocator(locator);
    // Confirm that we are assinging a point to the correct data source
    DataSourceVO<?> ds = DataSourceDao.instance.get(dp.getDataSourceId());
    PointLocatorVO<?> plvo = ds.createPointLocator();
    if (plvo.getClass() != locator.getClass()) {
        response.addGenericMessage("validate.invalidType");
        return response;
    }
    // If we are a new point then only validate the basics
    if (id == Common.NEW_ID) {
        if (StringUtils.isBlank(xid))
            response.addContextualMessage("xid", "validate.required");
        else if (StringValidation.isLengthGreaterThan(xid, 50))
            response.addMessage("xid", new TranslatableMessage("validate.notLongerThan", 50));
        else if (!DataPointDao.instance.isXidUnique(xid, id))
            response.addContextualMessage("xid", "validate.xidUsed");
        if (StringUtils.isBlank(name))
            response.addContextualMessage("name", "validate.required");
        // Load in the default Template
        DataPointPropertiesTemplateVO template = TemplateDao.instance.getDefaultDataPointTemplate(locator.getDataTypeId());
        if (template != null) {
            template.updateDataPointVO(dp);
        }
        // Should really be done elsewhere
        dp.setEventDetectors(new ArrayList<AbstractPointEventDetectorVO<?>>());
    } else if (id == DataPointDwr.COPY_ID) {
        dp.setId(Common.NEW_ID);
        if (StringUtils.isBlank(xid))
            response.addContextualMessage("xid", "validate.required");
        else if (StringValidation.isLengthGreaterThan(xid, 50))
            response.addMessage("xid", new TranslatableMessage("validate.notLongerThan", 50));
        else if (!DataPointDao.instance.isXidUnique(xid, id))
            response.addContextualMessage("xid", "validate.xidUsed");
        if (StringUtils.isBlank(name))
            response.addContextualMessage("name", "validate.required");
    } else {
        // New validation on save for all settings on existing points
        dp.validate(response);
        if (dp.getChartRenderer() != null)
            dp.getChartRenderer().validate(response);
        if (dp.getTextRenderer() != null)
            dp.getTextRenderer().validate(response);
    }
    // Validate Locator
    locator.validate(response, dp);
    if (!response.getHasMessages()) {
        try {
            Common.runtimeManager.saveDataPoint(dp);
        } catch (DuplicateKeyException e) {
            response.addGenericMessage("pointEdit.detectors.duplicateXid");
            return response;
        } catch (LicenseViolatedException e) {
            response.addMessage(e.getErrorMessage());
            return response;
        }
        if (defaulter != null)
            defaulter.postSave(dp);
        response.addData("id", dp.getId());
        response.addData("vo", dp);
        if (includePointList)
            response.addData("points", getPoints());
        // Set the User Point
        Common.getUser().setEditPoint(dp);
    }
    return response;
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) LicenseViolatedException(com.serotonin.m2m2.LicenseViolatedException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) DataPointPropertiesTemplateVO(com.serotonin.m2m2.vo.template.DataPointPropertiesTemplateVO) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) DuplicateKeyException(org.springframework.dao.DuplicateKeyException)

Example 73 with TranslatableMessage

use of com.serotonin.m2m2.i18n.TranslatableMessage in project ma-core-public by infiniteautomation.

the class DataSourceEditDwr method getGeneralStatusMessages.

@DwrPermission(user = true)
public final ProcessResult getGeneralStatusMessages() {
    ProcessResult result = new ProcessResult();
    DataSourceVO<?> vo = Common.getUser().getEditDataSource();
    DataSourceRT<?> rt = Common.runtimeManager.getRunningDataSource(vo.getId());
    List<TranslatableMessage> messages = new ArrayList<>();
    result.addData("messages", messages);
    if (rt == null)
        messages.add(new TranslatableMessage("dsEdit.notEnabled"));
    else {
        rt.addStatusMessages(messages);
        if (messages.isEmpty())
            messages.add(new TranslatableMessage("dsEdit.noStatus"));
    }
    return result;
}
Also used : ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) ArrayList(java.util.ArrayList) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 74 with TranslatableMessage

use of com.serotonin.m2m2.i18n.TranslatableMessage in project ma-core-public by infiniteautomation.

the class EventHandlersDwr method validateScript.

@DwrPermission(user = true)
public ProcessResult validateScript(String script, Integer targetPointId, int type, List<IntStringPair> additionalContext, ScriptPermissions scriptPermissions) {
    ProcessResult response = new ProcessResult();
    TranslatableMessage message;
    Map<String, IDataPointValueSource> context = new HashMap<String, IDataPointValueSource>();
    int targetDataType;
    if (type == SetPointEventHandlerDefinition.ACTIVE_SCRIPT_TYPE || type == SetPointEventHandlerDefinition.INACTIVE_SCRIPT_TYPE) {
        DataPointRT target = targetPointId == null ? null : Common.runtimeManager.getDataPoint(targetPointId.intValue());
        if (target == null) {
            DataPointVO targetVo = targetPointId == null ? null : DataPointDao.instance.getDataPoint(targetPointId.intValue(), false);
            if (targetVo == null) {
                if (// These are passed in the validateScript of eventHandlers.jsp
                type == SetPointEventHandlerDefinition.ACTIVE_SCRIPT_TYPE)
                    response.addMessage("activeScript", new TranslatableMessage("eventHandlers.noTargetPoint"));
                else if (type == SetPointEventHandlerDefinition.INACTIVE_SCRIPT_TYPE)
                    response.addMessage("inactiveScript", new TranslatableMessage("eventHandlers.noTargetPoint"));
                return response;
            }
            if (targetVo.getDefaultCacheSize() == 0)
                targetVo.setDefaultCacheSize(1);
            target = new DataPointRT(targetVo, targetVo.getPointLocator().createRuntime(), DataSourceDao.instance.getDataSource(targetVo.getDataSourceId()), null);
            target.resetValues();
            context.put(SetPointEventHandlerVO.TARGET_CONTEXT_KEY, target);
        }
        targetDataType = target.getDataTypeId();
    } else {
        targetDataType = DataTypes.ALPHANUMERIC;
    }
    for (IntStringPair cxt : additionalContext) {
        DataPointRT dprt = Common.runtimeManager.getDataPoint(cxt.getKey());
        if (dprt == null) {
            DataPointVO dpvo = DataPointDao.instance.getDataPoint(cxt.getKey(), false);
            if (dpvo == null) {
                if (type == SetPointEventHandlerDefinition.ACTIVE_SCRIPT_TYPE)
                    response.addMessage("activeScript", new TranslatableMessage("event.script.contextPointMissing", cxt.getValue(), cxt.getKey()));
                else if (type == SetPointEventHandlerDefinition.INACTIVE_SCRIPT_TYPE)
                    response.addMessage("inactiveScript", new TranslatableMessage("event.script.contextPointMissing", cxt.getValue(), cxt.getKey()));
                else if (type == EmailEventHandlerDefinition.EMAIL_SCRIPT_TYPE)
                    response.addMessage("emailScript", new TranslatableMessage("event.script.contextPointMissing", cxt.getValue(), cxt.getKey()));
                return response;
            }
            if (dpvo.getDefaultCacheSize() == 0)
                dpvo.setDefaultCacheSize(1);
            dprt = new DataPointRT(dpvo, dpvo.getPointLocator().createRuntime(), DataSourceDao.instance.getDataSource(dpvo.getDataSourceId()), null);
            dprt.resetValues();
        }
        context.put(cxt.getValue(), dprt);
    }
    Map<String, Object> otherContext = new HashMap<String, Object>();
    otherContext.put(SetPointEventHandlerVO.EVENT_CONTEXT_KEY, getTestEvent());
    if (type == EmailEventHandlerDefinition.EMAIL_SCRIPT_TYPE)
        otherContext.put("model", new HashMap<String, Object>());
    final StringWriter scriptOut = new StringWriter();
    final PrintWriter scriptWriter = new PrintWriter(scriptOut);
    final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/YYY HH:mm:ss");
    ScriptPointValueSetter loggingSetter = new ScriptPointValueSetter(scriptPermissions) {

        @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, otherContext, System.currentTimeMillis(), targetDataType, System.currentTimeMillis(), scriptPermissions, scriptWriter, new ScriptLog(SetPointHandlerRT.NULL_WRITER, LogLevel.FATAL), loggingSetter, null, true);
        if (pvt.getValue() == null)
            message = new TranslatableMessage("eventHandlers.script.nullResult");
        else if (CompiledScriptExecutor.UNCHANGED == pvt.getValue())
            message = new TranslatableMessage("eventHandlers.script.successUnchanged");
        else
            message = new TranslatableMessage("eventHandlers.script.success", pvt.getValue());
        // Add the script logging output
        response.addData("out", scriptOut.toString().replaceAll("\n", "<br/>"));
    } catch (ScriptPermissionsException e) {
        message = e.getTranslatableMessage();
    } catch (ScriptException e) {
        message = new TranslatableMessage("eventHandlers.script.failure", e.getMessage());
    } catch (ResultTypeException e) {
        message = e.getTranslatableMessage();
    }
    if (type == SetPointEventHandlerDefinition.ACTIVE_SCRIPT_TYPE)
        response.addMessage("activeScript", message);
    else if (type == SetPointEventHandlerDefinition.INACTIVE_SCRIPT_TYPE)
        response.addMessage("inactiveScript", message);
    else if (type == EmailEventHandlerDefinition.EMAIL_SCRIPT_TYPE)
        response.addMessage("emailScript", 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) LinkedHashMap(java.util.LinkedHashMap) IntStringPair(com.serotonin.db.pair.IntStringPair) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) Date(java.util.Date) ScriptLog(com.serotonin.m2m2.rt.script.ScriptLog) ScriptException(javax.script.ScriptException) ResultTypeException(com.serotonin.m2m2.rt.script.ResultTypeException) StringWriter(java.io.StringWriter) ScriptPermissionsException(com.serotonin.m2m2.rt.script.ScriptPermissionsException) IDataPointValueSource(com.serotonin.m2m2.rt.dataImage.IDataPointValueSource) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) 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)

Example 75 with TranslatableMessage

use of com.serotonin.m2m2.i18n.TranslatableMessage in project ma-core-public by infiniteautomation.

the class AbstractRTDwr method save.

/**
 * Save the Process
 * @return
 */
@DwrPermission(admin = true)
@Override
public ProcessResult save(VO vo) {
    ProcessResult response = new ProcessResult();
    vo.validate(response);
    if (!response.getHasMessages()) {
        // Save it
        try {
            runtimeManager.save(vo);
        } catch (Exception e) {
            // Handle the exceptions.
            // TODO Clean up and generify these messages to some central place
            LOG.error(e);
            if (e instanceof DuplicateKeyException)
                response.addMessage(this.keyName + "Errors", new TranslatableMessage("dsEdit.alreadyExists"));
            else
                response.addMessage(this.keyName + "Errors", new TranslatableMessage("dsEdit.unableToSave"));
        }
    }
    response.addData("vo", vo);
    // In case there are errors
    response.addData("id", vo.getId());
    return response;
}
Also used : ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Aggregations

TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)180 User (com.serotonin.m2m2.vo.User)53 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)52 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)52 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)33 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)33 IOException (java.io.IOException)28 HashMap (java.util.HashMap)27 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)24 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)22 ArrayList (java.util.ArrayList)22 DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)20 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)20 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)19 BadRequestException (com.infiniteautomation.mango.rest.v2.exception.BadRequestException)18 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)17 File (java.io.File)16 URI (java.net.URI)16 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)12 ResponseEntity (org.springframework.http.ResponseEntity)11