Search in sources :

Example 1 with BaseAssetQuery

use of org.openremote.model.asset.BaseAssetQuery in project openremote by openremote.

the class AssetStorageService method buildWhereClause.

protected String buildWhereClause(BaseAssetQuery query, int level, List<ParameterBinder> binders) {
    // level = 1 is main query
    // level = 2 is union
    // level = 3 is CTE
    StringBuilder sb = new StringBuilder();
    boolean recursive = query.select.recursive;
    sb.append(" where true");
    if (level == 2) {
        return sb.toString();
    }
    if (level == 1 && query.id != null) {
        sb.append(" and A.ID = ?");
        final int pos = binders.size() + 1;
        binders.add(st -> st.setString(pos, query.id));
    }
    if (level == 1 && query.name != null) {
        sb.append(query.name.caseSensitive ? " and A.NAME " : " and upper(A.NAME)");
        switch(query.name.match) {
            case EXACT:
                sb.append(" = ? ");
                break;
            case NOT_EXACT:
                sb.append(" <> ? ");
                break;
            case BEGIN:
            case END:
            case CONTAINS:
                sb.append(" like ? ");
                break;
        }
        final int pos = binders.size() + 1;
        binders.add(st -> st.setString(pos, query.name.prepareValue()));
    }
    if (level == 1 && query.location != null) {
        if (query.location instanceof RadialLocationPredicate) {
            RadialLocationPredicate location = (RadialLocationPredicate) query.location;
            sb.append(" and ST_Distance_Sphere(A.LOCATION, ST_MakePoint(");
            sb.append(location.lng);
            sb.append(",");
            sb.append(location.lat);
            sb.append(location.negated ? ")) > " : ")) <= ");
            sb.append(location.radius);
        } else if (query.location instanceof RectangularLocationPredicate) {
            RectangularLocationPredicate location = (RectangularLocationPredicate) query.location;
            sb.append(location.negated ? " and NOT" : " and");
            sb.append(" ST_Within(A.LOCATION,");
            sb.append("ST_MakeEnvelope(");
            sb.append(location.lngMin);
            sb.append(",");
            sb.append(location.latMin);
            sb.append(",");
            sb.append(location.lngMax);
            sb.append(",");
            sb.append(location.latMax);
            sb.append("))");
        }
    }
    if (query.parent != null) {
        // Can only restrict recursive query parent by asset type
        if (level == 1 && query.parent.id != null) {
            sb.append(" and p.ID = a.PARENT_ID");
            sb.append(" and A.PARENT_ID = ?");
            final int pos = binders.size() + 1;
            binders.add(st -> st.setString(pos, query.parent.id));
        } else if (query.parent.type != null) {
            sb.append(" and p.ID = a.PARENT_ID");
            sb.append(" and P.ASSET_TYPE = ?");
            final int pos = binders.size() + 1;
            binders.add(st -> st.setString(pos, query.parent.type));
        } else if (level == 1 && query.parent.noParent) {
            sb.append(" and A.PARENT_ID is null");
        }
    }
    if (level == 1 && query.path != null && query.path.hasPath()) {
        sb.append(" and ? <@ get_asset_tree_path(A.ID)");
        final int pos = binders.size() + 1;
        binders.add(st -> st.setArray(pos, st.getConnection().createArrayOf("text", query.path.path)));
    }
    if (!recursive || level == 3) {
        if (query.tenant != null && query.tenant.realmId != null) {
            sb.append(" and R.ID = ?");
            final int pos = binders.size() + 1;
            binders.add(st -> st.setString(pos, query.tenant.realmId));
        } else if (query.tenant != null && query.tenant.realm != null) {
            sb.append(" and R.NAME = ?");
            final int pos = binders.size() + 1;
            binders.add(st -> st.setString(pos, query.tenant.realm));
        }
        if (query.userId != null) {
            sb.append(" and ua.ASSET_ID = a.ID and ua.USER_ID = ?");
            final int pos = binders.size() + 1;
            binders.add(st -> st.setString(pos, query.userId));
        }
        if (level == 1 && query.select.access == Access.PUBLIC_READ) {
            sb.append(" and A.ACCESS_PUBLIC_READ is true");
        }
        if (query.type != null) {
            sb.append(query.type.caseSensitive ? " and A.ASSET_TYPE" : " and upper(A.ASSET_TYPE)");
            switch(query.type.match) {
                case EXACT:
                    sb.append(" = ? ");
                    break;
                case NOT_EXACT:
                    sb.append(" <> ? ");
                    break;
                case BEGIN:
                case END:
                case CONTAINS:
                    sb.append(" like ? ");
                    break;
            }
            final int pos = binders.size() + 1;
            binders.add(st -> st.setString(pos, query.type.prepareValue()));
        }
        if (query.attributeMeta != null) {
            for (AttributeMetaPredicate attributeMetaPredicate : query.attributeMeta) {
                String attributeMetaFilter = buildAttributeMetaFilter(attributeMetaPredicate, binders);
                if (attributeMetaFilter.length() > 0) {
                    sb.append(" and A.ID in (select A.ID from");
                    sb.append(" jsonb_each(A.ATTRIBUTES) as AX,");
                    sb.append(" jsonb_array_elements(AX.VALUE #> '{meta}') as AM");
                    sb.append(" where true");
                    sb.append(attributeMetaFilter);
                    sb.append(")");
                }
            }
        }
        if (query.attribute != null) {
            for (AssetQuery.AttributePredicate attributePredicate : query.attribute) {
                StringBuilder attributeFilterBuilder = new StringBuilder();
                attributeFilterBuilder.append(buildAttributeFilter(attributePredicate, binders));
                if (attributeFilterBuilder.length() > 0) {
                    sb.append(" and A.ID in (select A.ID from");
                    sb.append(" jsonb_each(A.ATTRIBUTES) as AX");
                    sb.append(" where true");
                    sb.append(attributeFilterBuilder.toString());
                    sb.append(")");
                }
            }
        }
    }
    return sb.toString();
}
Also used : ClientRole(org.openremote.model.security.ClientRole) DateTime(net.fortuna.ical4j.model.DateTime) AuthContext(org.openremote.container.security.AuthContext) Date(java.util.Date) NoResultException(javax.persistence.NoResultException) RRule(net.fortuna.ical4j.model.property.RRule) Period(net.fortuna.ical4j.model.Period) ObjectValue(org.openremote.model.value.ObjectValue) UserConfiguration(org.openremote.manager.security.UserConfiguration) Dur(net.fortuna.ical4j.model.Dur) PersistenceEvent.isPersistenceEventForEntityType(org.openremote.container.persistence.PersistenceEvent.isPersistenceEventForEntityType) AttributeEvent(org.openremote.model.attribute.AttributeEvent) TextUtil(org.openremote.model.util.TextUtil) PERSISTENCE_TOPIC(org.openremote.container.persistence.PersistenceEvent.PERSISTENCE_TOPIC) Constants(org.openremote.model.Constants) Logger(java.util.logging.Logger) MessageBrokerSetupService(org.openremote.container.message.MessageBrokerSetupService) Value(org.openremote.model.value.Value) RouteBuilder(org.apache.camel.builder.RouteBuilder) Recur(net.fortuna.ical4j.model.Recur) TextUtil.isNullOrEmpty(org.openremote.model.util.TextUtil.isNullOrEmpty) ALL(org.openremote.model.asset.BaseAssetQuery.Include.ALL) PersistenceEvent(org.openremote.container.persistence.PersistenceEvent) java.sql(java.sql) CalendarEvent(org.openremote.model.calendar.CalendarEvent) java.util(java.util) BaseAssetQuery(org.openremote.model.asset.BaseAssetQuery) WebService(org.openremote.container.web.WebService) CLIENT_EVENT_TOPIC(org.openremote.manager.event.ClientEventService.CLIENT_EVENT_TOPIC) Session(org.hibernate.Session) ValidationFailure(org.openremote.model.ValidationFailure) RESTRICTED_READ(org.openremote.model.asset.BaseAssetQuery.Access.RESTRICTED_READ) Point(com.vividsolutions.jts.geom.Point) AbstractReturningWork(org.hibernate.jdbc.AbstractReturningWork) PGobject(org.postgresql.util.PGobject) Container(org.openremote.container.Container) TenantFilter(org.openremote.model.event.shared.TenantFilter) ContainerService(org.openremote.container.ContainerService) PeriodRule(net.fortuna.ical4j.filter.PeriodRule) PersistenceService(org.openremote.container.persistence.PersistenceService) ClientEventService.getSessionKey(org.openremote.manager.event.ClientEventService.getSessionKey) org.openremote.model.asset(org.openremote.model.asset) VEvent(net.fortuna.ical4j.model.component.VEvent) MessageBrokerService(org.openremote.container.message.MessageBrokerService) User(org.openremote.model.security.User) ALL_EXCEPT_PATH_AND_ATTRIBUTES(org.openremote.model.asset.BaseAssetQuery.Include.ALL_EXCEPT_PATH_AND_ATTRIBUTES) ManagerIdentityService(org.openremote.manager.security.ManagerIdentityService) PRIVATE_READ(org.openremote.model.asset.BaseAssetQuery.Access.PRIVATE_READ) RecurrenceRule(org.openremote.model.calendar.RecurrenceRule) EntityManager(javax.persistence.EntityManager) Consumer(java.util.function.Consumer) ClientEventService(org.openremote.manager.event.ClientEventService) TimerService(org.openremote.container.timer.TimerService) Values(org.openremote.model.value.Values) BaseAssetQuery(org.openremote.model.asset.BaseAssetQuery) Point(com.vividsolutions.jts.geom.Point)

Aggregations

Point (com.vividsolutions.jts.geom.Point)1 java.sql (java.sql)1 java.util (java.util)1 Date (java.util.Date)1 Consumer (java.util.function.Consumer)1 Logger (java.util.logging.Logger)1 EntityManager (javax.persistence.EntityManager)1 NoResultException (javax.persistence.NoResultException)1 PeriodRule (net.fortuna.ical4j.filter.PeriodRule)1 DateTime (net.fortuna.ical4j.model.DateTime)1 Dur (net.fortuna.ical4j.model.Dur)1 Period (net.fortuna.ical4j.model.Period)1 Recur (net.fortuna.ical4j.model.Recur)1 VEvent (net.fortuna.ical4j.model.component.VEvent)1 RRule (net.fortuna.ical4j.model.property.RRule)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 Session (org.hibernate.Session)1 AbstractReturningWork (org.hibernate.jdbc.AbstractReturningWork)1 Container (org.openremote.container.Container)1 ContainerService (org.openremote.container.ContainerService)1