Search in sources :

Example 91 with ProcessResult

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

the class DefaultDataPointPropertiesTemplateFactory method saveTemplate.

protected void saveTemplate(DataPointPropertiesTemplateVO template) {
    ProcessResult response = new ProcessResult();
    template.validate(response);
    if (!response.getHasMessages()) {
        TemplateDao.instance.save(template);
    } else {
        String output = new String();
        List<ProcessMessage> messages = response.getMessages();
        for (ProcessMessage message : messages) {
            output += message.toString(Common.getTranslations());
            output += "\n";
        }
        throw new ShouldNeverHappenException(output);
    }
}
Also used : ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) ProcessMessage(com.serotonin.m2m2.i18n.ProcessMessage)

Example 92 with ProcessResult

use of com.serotonin.m2m2.i18n.ProcessResult 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);
        }
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) PublisherRT(com.serotonin.m2m2.rt.publish.PublisherRT) HashSet(java.util.HashSet)

Example 93 with ProcessResult

use of com.serotonin.m2m2.i18n.ProcessResult 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));
        }
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) DataPointSummary(com.serotonin.m2m2.vo.DataPointSummary) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) LicenseViolatedException(com.serotonin.m2m2.LicenseViolatedException) DataPointPropertiesTemplateVO(com.serotonin.m2m2.vo.template.DataPointPropertiesTemplateVO) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) ProcessMessage(com.serotonin.m2m2.i18n.ProcessMessage)

Example 94 with ProcessResult

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

the class EventHandlerImporter method importImpl.

@Override
protected void importImpl() {
    String xid = json.getString("xid");
    if (StringUtils.isBlank(xid))
        xid = ctx.getEventHandlerDao().generateUniqueXid();
    AbstractEventHandlerVO<?> handler = ctx.getEventHandlerDao().getEventHandler(xid);
    if (handler == null) {
        String typeStr = json.getString("handlerType");
        if (StringUtils.isBlank(typeStr))
            addFailureMessage("emport.eventHandler.missingType", xid, ModuleRegistry.getEventHandlerDefinitionTypes());
        else {
            EventHandlerDefinition<?> def = ModuleRegistry.getEventHandlerDefinition(typeStr);
            if (def == null)
                addFailureMessage("emport.eventHandler.invalidType", xid, typeStr, ModuleRegistry.getEventHandlerDefinitionTypes());
            else {
                handler = def.baseCreateEventHandlerVO();
                handler.setXid(xid);
            }
        }
    }
    JsonObject et = json.getJsonObject("eventType");
    JsonArray ets = json.getJsonArray("eventTypes");
    EventType eventType = null;
    List<EventType> eventTypes = null;
    try {
        // Find the event type.
        if (et != null)
            eventType = ctx.getReader().read(EventType.class, et);
        else if (ets != null) {
            eventTypes = new ArrayList<EventType>(ets.size());
            Iterator<JsonValue> iter = ets.iterator();
            while (iter.hasNext()) eventTypes.add(ctx.getReader().read(EventType.class, iter.next()));
        }
        ctx.getReader().readInto(handler, json);
        // Now validate it. Use a new response object so we can distinguish errors in this vo from other errors.
        ProcessResult voResponse = new ProcessResult();
        handler.validate(voResponse);
        if (voResponse.getHasMessages())
            setValidationMessages(voResponse, "emport.eventHandler.prefix", xid);
        else {
            // Sweet.
            boolean isnew = handler.getId() == Common.NEW_ID;
            // Save it.
            if (et != null)
                ctx.getEventHandlerDao().saveEventHandler(eventType, handler);
            else
                ctx.getEventHandlerDao().saveEventHandler(handler);
            if (eventTypes != null)
                for (EventType type : eventTypes) ctx.getEventHandlerDao().addEventHandlerMappingIfMissing(handler.getId(), type);
            addSuccessMessage(isnew, "emport.eventHandler.prefix", xid);
        }
    } catch (TranslatableJsonException e) {
        addFailureMessage("emport.eventHandler.prefix", xid, e.getMsg());
    } catch (JsonException e) {
        addFailureMessage("emport.eventHandler.prefix", xid, getJsonExceptionMessage(e));
    }
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) EventType(com.serotonin.m2m2.rt.event.type.EventType) ArrayList(java.util.ArrayList) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) JsonObject(com.serotonin.json.type.JsonObject) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonArray(com.serotonin.json.type.JsonArray) Iterator(java.util.Iterator)

Example 95 with ProcessResult

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

the class TemplateImporter method importImpl.

@Override
protected void importImpl() {
    String xid = json.getString("xid");
    if (StringUtils.isBlank(xid))
        xid = TemplateDao.instance.generateUniqueXid();
    BaseTemplateVO<?> vo = TemplateDao.instance.getByXid(xid);
    if (vo == null) {
        String typeStr = json.getString("templateType");
        if (StringUtils.isBlank(typeStr))
            addFailureMessage("emport.template.missingType", xid, ModuleRegistry.getTemplateDefinitionTypes());
        else {
            TemplateDefinition def = ModuleRegistry.getTemplateDefinition(typeStr);
            if (def == null)
                addFailureMessage("emport.template.invalidType", xid, typeStr, ModuleRegistry.getTemplateDefinitionTypes());
            else {
                vo = def.baseCreateTemplateVO();
                vo.setXid(xid);
            }
        }
    }
    if (vo != null) {
        try {
            // The VO was found or successfully created. Finish reading it in.
            ctx.getReader().readInto(vo, json);
            // 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.template.prefix", xid);
            else {
                // Sweet. Save it.
                boolean isnew = vo.isNew();
                TemplateDao.instance.save(vo);
                addSuccessMessage(isnew, "emport.template.prefix", xid);
            }
        } catch (TranslatableJsonException e) {
            addFailureMessage("emport.template.prefix", xid, e.getMsg());
        } catch (JsonException e) {
            addFailureMessage("emport.template.prefix", xid, getJsonExceptionMessage(e));
        }
    }
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) TemplateDefinition(com.serotonin.m2m2.module.TemplateDefinition)

Aggregations

ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)165 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)132 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)31 User (com.serotonin.m2m2.vo.User)31 ArrayList (java.util.ArrayList)28 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)27 JsonException (com.serotonin.json.JsonException)21 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)20 JsonObject (com.serotonin.json.type.JsonObject)13 HashMap (java.util.HashMap)12 DuplicateKeyException (org.springframework.dao.DuplicateKeyException)11 StringStringPair (com.serotonin.db.pair.StringStringPair)9 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)9 IOException (java.io.IOException)9 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)8 ProcessMessage (com.serotonin.m2m2.i18n.ProcessMessage)8 AbstractVO (com.serotonin.m2m2.vo.AbstractVO)8 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)8 SystemSettingsDao (com.serotonin.m2m2.db.dao.SystemSettingsDao)7 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)7