Search in sources :

Example 1 with LocalizableMessage

use of com.serotonin.web.i18n.LocalizableMessage in project ma-core-public by infiniteautomation.

the class CompoundEventDetectorVO method validate.

public static void validate(String condition, DwrResponseI18n response) {
    try {
        User user = Common.getUser();
        final boolean admin = Permissions.hasAdmin(user);
        if (!admin)
            Permissions.ensureDataSourcePermission(user);
        LogicalOperator l = CompoundEventDetectorRT.parseConditionStatement(condition);
        List<String> keys = l.getDetectorKeys();
        // Get all of the point event detectors.
        List<DataPointVO> dataPoints = DataPointDao.instance.getDataPoints(null, true);
        // Create a lookup of data sources.
        Map<Integer, DataSourceVO<?>> dss = new HashMap<>();
        for (DataSourceVO<?> ds : DataSourceDao.instance.getAll()) dss.put(ds.getId(), ds);
        for (String key : keys) {
            if (!key.startsWith(SimpleEventDetectorVO.POINT_EVENT_DETECTOR_PREFIX))
                continue;
            boolean found = false;
            for (DataPointVO dp : dataPoints) {
                if (!admin && !Permissions.hasDataSourcePermission(user, dss.get(dp.getDataSourceId())))
                    continue;
                if (found)
                    break;
            }
            if (!found)
                throw new ConditionParseException(new LocalizableMessage("compoundDetectors.validation.invalidKey"));
        }
    } catch (ConditionParseException e) {
        response.addMessage("condition", e.getLocalizableMessage());
        if (e.isRange()) {
            response.addData("range", true);
            response.addData("from", e.getFrom());
            response.addData("to", e.getTo());
        }
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataSourceVO(com.serotonin.m2m2.vo.dataSource.DataSourceVO) User(com.serotonin.m2m2.vo.User) HashMap(java.util.HashMap) LogicalOperator(com.serotonin.m2m2.rt.event.compound.LogicalOperator) ConditionParseException(com.serotonin.m2m2.rt.event.compound.ConditionParseException) LocalizableMessage(com.serotonin.web.i18n.LocalizableMessage)

Example 2 with LocalizableMessage

use of com.serotonin.web.i18n.LocalizableMessage in project ma-core-public by infiniteautomation.

the class CompoundEventDetectorRT method parseConditionStatement.

public static LogicalOperator parseConditionStatement(String condition) throws ConditionParseException {
    if (StringUtils.isBlank(condition))
        throw new ConditionParseException(new LocalizableMessage("compoundDetectors.validation.notDefined"));
    // Do bracket validation and character checking.
    int parenCount = 0;
    String allowedCharacters = "0123456789SP&|!()";
    for (int i = 0; i < condition.length(); i++) {
        char c = condition.charAt(i);
        if (allowedCharacters.indexOf(c) == -1 && !Character.isWhitespace(c))
            throw new ConditionParseException(new LocalizableMessage("compoundDetectors.validation.illegalChar"), i, i + 1);
        if (c == '(')
            parenCount++;
        else if (c == ')')
            parenCount--;
        if (parenCount < 0)
            throw new ConditionParseException(new LocalizableMessage("compoundDetectors.validation.closeParen"), i, i + 1);
    }
    if (parenCount != 0) {
        int paren = condition.indexOf('(');
        throw new ConditionParseException(new LocalizableMessage("compoundDetectors.validation.openParen"), paren, paren + 1);
    }
    // Maintain the original indices of the characters
    List<ConditionStatementCharacter> charList = new ArrayList<ConditionStatementCharacter>(condition.length());
    for (int i = 0; i < condition.length(); i++) charList.add(new ConditionStatementCharacter(condition.charAt(i), i));
    // Remove whitespace
    for (int i = condition.length() - 1; i >= 0; i--) {
        if (Character.isWhitespace(charList.get(i).c))
            charList.remove(i);
    }
    // Convert to array.
    ConditionStatementCharacter[] chars = new ConditionStatementCharacter[charList.size()];
    charList.toArray(chars);
    // Parse into tokens
    return parseTokens(chars, 0, chars.length);
}
Also used : ArrayList(java.util.ArrayList) LocalizableMessage(com.serotonin.web.i18n.LocalizableMessage)

Example 3 with LocalizableMessage

use of com.serotonin.web.i18n.LocalizableMessage in project ma-core-public by infiniteautomation.

the class CompoundEventDetectorRT method parseTokens.

private static LogicalOperator parseTokens(ConditionStatementCharacter[] chars, int from, int to) throws ConditionParseException {
    if (from >= chars.length)
        throw new ConditionParseException(new LocalizableMessage("compoundDetectors.validation.syntax"), chars[chars.length - 1].originalIndex, chars[chars.length - 1].originalIndex + 1);
    if (to - from < 0)
        throw new ConditionParseException(new LocalizableMessage("compoundDetectors.validation.syntax"), chars[from].originalIndex, chars[from].originalIndex + 1);
    if (to - from == 0)
        throw new ConditionParseException(new LocalizableMessage("compoundDetectors.validation.syntax"), chars[from].originalIndex, chars[to].originalIndex + 1);
    // Find or conditions not in parens.
    int start = indexOfLevel0(TOKEN_OR, chars, from, to);
    if (start != -1)
        // Found an or condition.
        return new OrOperator(parseTokens(chars, from, start), parseTokens(chars, start + TOKEN_OR.length, to));
    // Find and conditions not in parens.
    start = indexOfLevel0(TOKEN_AND, chars, from, to);
    if (start != -1)
        // Found an and condition.
        return new AndOperator(parseTokens(chars, from, start), parseTokens(chars, start + TOKEN_AND.length, to));
    // Check for not
    if (chars[from].c == '!')
        return new NotOperator(parseTokens(chars, from + 1, to));
    // Check for parentheses.
    if (chars[from].c == '(' && chars[to - 1].c == ')')
        return new Parenthesis(parseTokens(chars, from + 1, to - 1));
    // Must be a detector reference. Check the syntax.
    if (to - from > 1 && Character.isLetter(chars[from].c)) {
        // The rest must be digits.
        for (int i = from + 1; i < to; i++) {
            if (!Character.isDigit(chars[i].c))
                throw new ConditionParseException(new LocalizableMessage("compoundDetectors.validation.reference"), chars[from].originalIndex, chars[to - 1].originalIndex + 1);
        }
        return new EventDetectorWrapper(toString(chars, from, to));
    }
    // Not good
    throw new ConditionParseException(new LocalizableMessage("compoundDetectors.validation.reference"), chars[from].originalIndex, chars[to - 1].originalIndex + 1);
}
Also used : LocalizableMessage(com.serotonin.web.i18n.LocalizableMessage)

Example 4 with LocalizableMessage

use of com.serotonin.web.i18n.LocalizableMessage in project ma-core-public by infiniteautomation.

the class LocalizableMessageConverter method convertOutbound.

@Override
public OutboundVariable convertOutbound(Object data, OutboundContext outctx) throws MarshallException {
    WebContext webctx = WebContextFactory.get();
    LocalizableMessage lm = (LocalizableMessage) data;
    String s = lm.getLocalizedMessage(I18NUtils.getBundle(webctx.getHttpServletRequest()));
    return super.convertOutbound(s, outctx);
}
Also used : WebContext(org.directwebremoting.WebContext) LocalizableMessage(com.serotonin.web.i18n.LocalizableMessage)

Aggregations

LocalizableMessage (com.serotonin.web.i18n.LocalizableMessage)4 ConditionParseException (com.serotonin.m2m2.rt.event.compound.ConditionParseException)1 LogicalOperator (com.serotonin.m2m2.rt.event.compound.LogicalOperator)1 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)1 User (com.serotonin.m2m2.vo.User)1 DataSourceVO (com.serotonin.m2m2.vo.dataSource.DataSourceVO)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 WebContext (org.directwebremoting.WebContext)1