Search in sources :

Example 1 with ConditionSortLimitWithTagKeys

use of com.infiniteautomation.mango.db.query.ConditionSortLimitWithTagKeys in project ma-modules-public by infiniteautomation.

the class DataPointRestController method doQuery.

private static StreamedArrayWithTotal doQuery(ASTNode rql, User user) {
    if (user.isAdmin()) {
        return new StreamedVOQueryWithTotal<>(DataPointDao.instance, rql, item -> {
            DataPointDao.instance.loadPartialRelationalData(item);
            return new DataPointModel(item);
        });
    } else {
        // Add some conditions to restrict based on user permissions
        ConditionSortLimitWithTagKeys conditions = DataPointDao.instance.rqlToCondition(rql);
        conditions.addCondition(DataPointDao.instance.userHasPermission(user));
        DataPointFilter dataPointFilter = new DataPointFilter(user);
        return new StreamedVOQueryWithTotal<>(DataPointDao.instance, conditions, item -> {
            boolean oldFilterMatches = dataPointFilter.hasDataPointReadPermission(item);
            // this is just a double check, permissions should be accounted for via SQL restrictions added by DataPointDao.userHasPermission()
            if (!oldFilterMatches) {
                throw new RuntimeException("Data point does not match old permission filter");
            }
            return true;
        }, item -> {
            DataPointDao.instance.loadPartialRelationalData(item);
            return new DataPointModel(item);
        });
    }
}
Also used : StreamedVOQueryWithTotal(com.infiniteautomation.mango.rest.v2.model.StreamedVOQueryWithTotal) DataPointModel(com.infiniteautomation.mango.rest.v2.model.dataPoint.DataPointModel) ConditionSortLimitWithTagKeys(com.infiniteautomation.mango.db.query.ConditionSortLimitWithTagKeys) DataPointFilter(com.serotonin.m2m2.web.mvc.rest.v1.model.dataPoint.DataPointFilter)

Example 2 with ConditionSortLimitWithTagKeys

use of com.infiniteautomation.mango.db.query.ConditionSortLimitWithTagKeys in project ma-core-public by infiniteautomation.

the class JsonEmportScriptUtility method dataPointQuery.

public String dataPointQuery(String query, int prettyIndent) {
    Map<String, Object> data = new LinkedHashMap<>();
    if (admin) {
        ASTNode root = parser.parse(query);
        List<DataPointVO> dataPoints = new ArrayList<>();
        ConditionSortLimitWithTagKeys conditions = DataPointDao.instance.rqlToCondition(root);
        DataPointDao.instance.customizedQuery(conditions, new MappedRowCallback<DataPointVO>() {

            @Override
            public void row(DataPointVO item, int index) {
                dataPoints.add(item);
            }
        });
        data.put(ConfigurationExportData.DATA_POINTS, dataPoints);
    }
    return EmportDwr.export(data, prettyIndent);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) ASTNode(net.jazdw.rql.parser.ASTNode) ArrayList(java.util.ArrayList) JsonObject(com.serotonin.json.type.JsonObject) ConditionSortLimitWithTagKeys(com.infiniteautomation.mango.db.query.ConditionSortLimitWithTagKeys) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with ConditionSortLimitWithTagKeys

use of com.infiniteautomation.mango.db.query.ConditionSortLimitWithTagKeys in project ma-core-public by infiniteautomation.

the class DataPointTagsDao method getTagValuesForKey.

public Set<String> getTagValuesForKey(String tagKey, ASTNode restrictions, User user) {
    RQLToConditionWithTagKeys visitor = new RQLToConditionWithTagKeys();
    Name tagKeyColumn = visitor.columnNameForTagKey(tagKey);
    List<Condition> conditionList = new ArrayList<>();
    ConditionSortLimitWithTagKeys conditions = visitor.visit(restrictions);
    if (conditions.getCondition() != null) {
        conditionList.add(conditions.getCondition());
    }
    Map<String, Name> tagKeyToColumn = conditions.getTagKeyToColumn();
    Table<Record> from = createTagPivotSql(tagKeyToColumn).asTable().as(DATA_POINT_TAGS_PIVOT_ALIAS);
    if (!user.isAdmin()) {
        from = joinPointPermissions(from, PIVOT_ALIAS_DATA_POINT_ID, user);
    }
    SelectJoinStep<Record1<String>> query = this.create.selectDistinct(DSL.field(DATA_POINT_TAGS_PIVOT_ALIAS.append(tagKeyColumn), String.class)).from(from);
    Select<Record1<String>> result = query;
    if (!conditionList.isEmpty()) {
        result = query.where(DSL.and(conditionList));
    }
    try (Stream<Record1<String>> stream = result.stream()) {
        return stream.map(r -> r.value1()).collect(Collectors.toSet());
    }
}
Also used : Condition(org.jooq.Condition) SelectJoinStep(org.jooq.SelectJoinStep) DSL(org.jooq.impl.DSL) Table(org.jooq.Table) TableOnConditionStep(org.jooq.TableOnConditionStep) HashMap(java.util.HashMap) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) Condition(org.jooq.Condition) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Record2(org.jooq.Record2) Record1(org.jooq.Record1) Map(java.util.Map) ConditionSortLimitWithTagKeys(com.infiniteautomation.mango.db.query.ConditionSortLimitWithTagKeys) Select(org.jooq.Select) Record(org.jooq.Record) Common(com.serotonin.m2m2.Common) Name(org.jooq.Name) Set(java.util.Set) Field(org.jooq.Field) Collectors(java.util.stream.Collectors) List(java.util.List) Stream(java.util.stream.Stream) ASTNode(net.jazdw.rql.parser.ASTNode) SQLDataType(org.jooq.impl.SQLDataType) Entry(java.util.Map.Entry) Log(org.apache.commons.logging.Log) BatchBindStep(org.jooq.BatchBindStep) LogFactory(org.apache.commons.logging.LogFactory) RQLToConditionWithTagKeys(com.infiniteautomation.mango.db.query.RQLToConditionWithTagKeys) User(com.serotonin.m2m2.vo.User) ArrayList(java.util.ArrayList) ConditionSortLimitWithTagKeys(com.infiniteautomation.mango.db.query.ConditionSortLimitWithTagKeys) RQLToConditionWithTagKeys(com.infiniteautomation.mango.db.query.RQLToConditionWithTagKeys) Name(org.jooq.Name) Record(org.jooq.Record) Record1(org.jooq.Record1)

Example 4 with ConditionSortLimitWithTagKeys

use of com.infiniteautomation.mango.db.query.ConditionSortLimitWithTagKeys in project ma-core-public by infiniteautomation.

the class DataPointQuery method query.

/**
 * Perform a query on the data points table
 * @param query
 * @return
 */
public List<DataPointWrapper> query(String query) {
    ASTNode root = parser.parse(query);
    List<DataPointVO> dataPoints = new ArrayList<>();
    ConditionSortLimitWithTagKeys conditions = DataPointDao.instance.rqlToCondition(root);
    DataPointDao.instance.customizedQuery(conditions, new MappedRowCallback<DataPointVO>() {

        @Override
        public void row(DataPointVO item, int index) {
            dataPoints.add(item);
        }
    });
    List<DataPointWrapper> results = new ArrayList<DataPointWrapper>();
    // Filter on permissions
    DataPointRT rt = null;
    AbstractPointWrapper wrapper = null;
    for (DataPointVO dp : dataPoints) {
        // Can we read or write to this point?
        if (Permissions.hasDataPointSetPermission(permissions.getDataPointSetPermissions(), dp) || Permissions.hasDataPointReadPermission(permissions.getDataPointReadPermissions(), dp)) {
            rt = Common.runtimeManager.getDataPoint(dp.getId());
            if (rt != null)
                wrapper = ScriptUtils.wrapPoint(engine, rt, setter);
            else
                wrapper = null;
            results.add(new DataPointWrapper(dp, wrapper));
        }
    }
    return results;
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) ASTNode(net.jazdw.rql.parser.ASTNode) ArrayList(java.util.ArrayList) ConditionSortLimitWithTagKeys(com.infiniteautomation.mango.db.query.ConditionSortLimitWithTagKeys)

Aggregations

ConditionSortLimitWithTagKeys (com.infiniteautomation.mango.db.query.ConditionSortLimitWithTagKeys)4 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)3 ArrayList (java.util.ArrayList)3 ASTNode (net.jazdw.rql.parser.ASTNode)3 DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)2 RQLToConditionWithTagKeys (com.infiniteautomation.mango.db.query.RQLToConditionWithTagKeys)1 StreamedVOQueryWithTotal (com.infiniteautomation.mango.rest.v2.model.StreamedVOQueryWithTotal)1 DataPointModel (com.infiniteautomation.mango.rest.v2.model.dataPoint.DataPointModel)1 JsonObject (com.serotonin.json.type.JsonObject)1 Common (com.serotonin.m2m2.Common)1 User (com.serotonin.m2m2.vo.User)1 DataPointFilter (com.serotonin.m2m2.web.mvc.rest.v1.model.dataPoint.DataPointFilter)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1